Showing posts with label IoT. Show all posts
Showing posts with label IoT. Show all posts

Friday, February 8, 2019

Zerynth, middleware for IoT, you can program 32-bit microcontrollers using C/Python

With Zerynth you can program in Python or hybrid C/Python language the most popular 32-bit microcontrollers including ESP32, and connect them to the top Cloud infrastructures.

Read more: https://www.zerynth.com/



Python on ESP32 DevKitC using Zerynth Studio - Hello World

Thursday, November 15, 2018

Wireless Control of Devices with the Arduino MKR1010

Arduino LiveCast demo how to control an Arduino MKR1010 from a cell phone, via WiFi.




Introducing the Arduino MKR WiFi 1010


The MKR WiFi 1010, which offers low power consumption and has been designed not only to speed up and simplify the prototyping of WiFi-based IoT applications, but also to be embedded in production IoT applications that require WiFi connectivity. The board is an evolution of the existing Arduino MKR1000, but now comes equipped with an ESP32-based module manufactured by u-blox. This key element delivers 2.4GHz WiFi and Bluetooth communications capability, along with leading RF and power performance: the ESP32 is a highly flexible device that provides adjustable power output, enabling optimal trade-offs between communication range, data rate and power consumption.

Fully compatible with the Arduino IT cloud, the MKR1010 also offers simple migration from other Arduino boards, and uses open-source WiFi firmware, which makes it easy to reprogram for upgrading or to repair any security flaws. A significant feature is the MKR1010’s two standalone programmable processors – the first based on ARM processor core technology, the second based on a dual-core Espressif IC – making the board a high-performance solution that can distribute its workload across its dual-processor system. Another major feature is the integration of a secure authentication module – Microchip’s ECC508 – which uses crypto authentication to secure TLS network communications and connections.

Learn more: Arduino Blog > The MKR family gets bigger with two new IoT boards!

Sunday, May 22, 2016

“IoT: The Struggle for Meaning” by Massimo Banzi

Massimo Banzi explores the Internet of Things during an Arduino Day 2016 talk in Berkeley, CA.


Monday, April 4, 2016

NodeMCU act as WiFi client to update dweet.io



dweet.io is simple publishing and subscribing for machines, sensors, devices, robots, and gadgets (we just call them things). We call published messages ‘dweets’. It’s helpful to think of dweet.io as a Twitter for things, in fact.

I have a old example to show how to "Arduino Uno + Ethernet Shield send data to dweet.io and freeboard.io". It's a NodeMCU (with ESP8266 core for Arduino) version to update dweet.io.

You can visit following link on web browser to update dweet.io manually to test your url:
www.dweet.io/dweet/for/test_NodeMCU?A0=123

And check your thing at:
http://dweet.io/follow/test_NodeMCU

where testNodeMCU is the id of your thing in dweet.io, 123 is the value to update.

This code run on NodeMCU (suppose other standalone ESP8266 also), to connect to WiFi as client, read analog input from A0, and update dweet.io. (It's modified from Examples > ESP8266WiFi > WiFiClient,)


Connect analog input to A0, connection refer to last post "NodeMCU to read analog input, A0".

NodeMCU_WiFiClient_dweetio.ino
/*
 * reference: Examples > ESP8266WiFi > WiFiClient 
 */

#include <ESP8266WiFi.h>
const int AnalogIn  = A0;

const char* ssid     = "myssid";
const char* password = "password";

const char* host = "www.dweet.io";
const char* thing  = "test_NodeMCU";
const char* thing_content = "A0";

void setup() {
  Serial.begin(115200);
  delay(10);

  // We start by connecting to a WiFi network

  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  
  WiFi.begin(ssid, password);
  
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");  
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

int value = 0;

void loop() {
  delay(1000);
  value = analogRead(AnalogIn);

  Serial.print("connecting to ");
  Serial.println(host);
  
  // Use WiFiClient class to create TCP connections
  WiFiClient client;
  const int httpPort = 80;
  if (!client.connect(host, httpPort)) {
    Serial.println("connection failed");
    return;
  }
  
  // We now create a URI for the request
  String url = "/dweet/for/";
  url += thing;
  url += "?";
  url += thing_content;
  url += "=";
  url += value;
  
  Serial.print("Requesting URL: ");
  Serial.println(url);
  
  // This will send the request to the server
  client.print(String("GET ") + url + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" + 
               "Connection: close\r\n\r\n");
  int timeout = millis() + 5000;
  while (client.available() == 0) {
    if (timeout - millis() < 0) {
      Serial.println(">>> Client Timeout !");
      client.stop();
      return;
    }
  }
  
  // Read all the lines of the reply from server and print them to Serial
  while(client.available()){
    String line = client.readStringUntil('\r');
    Serial.print(line);
  }
  
  Serial.println();
  Serial.println("closing connection");
}


Friday, January 1, 2016

Standalone ESP8266/ESP-12 to read Adafruit IO feed

This example of Standalone ESP8266/ESP-12 show how to read Adafruit IO feed of toggle button and turn on/off on-board LED.

Adafruit IO is a IoT solution by Adafruit. It's now open beta. You can sign-up and join HERE.

Then you can create a dashboard (testOnOff) with feed of toggle button (OnOff).


Then you can program standalone ESP8266/ESP-12 to read the feed from Adafruit IO, and set on-board LED accordingly.


(As shown in the video, sometimes it cannot update!)

Adafruit MQTT Library is needed. You can install it in Arduino IDE Library Manager.


The program modified from GitGub: openhomeautomation/adafruit-io-esp8266. You have edit it with your AIO_USERNAME, AIO_KEY, and also WLAN_SSID and WLAN_PASS for your WiFi router.

The on-board LED share with Serial TX pin. So we have to end Serial and set the on-board LED as output after connected to Adafruit IO server.

AdafruitIoTestOnOff.ino
/***************************************************
 * Modified from:
 * https://github.com/openhomeautomation/adafruit-io-esp8266/tree/master/esp8266_lamp_module
 * 
 * =================================================
  Adafruit ESP8266 Lamp Controller Module
  
  Must use ESP8266 Arduino from:
    https://github.com/esp8266/Arduino
  Works great with Adafruit's Huzzah ESP board:
  ----> https://www.adafruit.com/product/2471
  Adafruit invests time and resources providing this open source code,
  please support Adafruit and open-source hardware by purchasing
  products from Adafruit!
  Written by Tony DiCola for Adafruit Industries.
  Adafruit IO example additions by Todd Treece.
  MIT license, all text above must be included in any redistribution
 ****************************************************/

// Libraries
#include <ESP8266WiFi.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"

// WiFi parameters
#define WLAN_SSID       "testAP"
#define WLAN_PASS       "12345678"

// Adafruit IO
#define AIO_SERVER      "io.adafruit.com"
#define AIO_SERVERPORT  1883
#define AIO_USERNAME    "your AIO username"
#define AIO_KEY         "your AIO key"

// Create an ESP8266 WiFiClient class to connect to the MQTT server.
WiFiClient client;

// Store the MQTT server, client ID, username, and password in flash memory.
// This is required for using the Adafruit MQTT library.
const char MQTT_SERVER[] PROGMEM    = AIO_SERVER;
// Set a unique MQTT client ID using the AIO key + the date and time the sketch
// was compiled (so this should be unique across multiple devices for a user,
// alternatively you can manually set this to a GUID or other random value).
const char MQTT_CLIENTID[] PROGMEM  = AIO_KEY __DATE__ __TIME__;
const char MQTT_USERNAME[] PROGMEM  = AIO_USERNAME;
const char MQTT_PASSWORD[] PROGMEM  = AIO_KEY;

// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
Adafruit_MQTT_Client mqtt(&client, MQTT_SERVER, AIO_SERVERPORT, MQTT_CLIENTID, MQTT_USERNAME, MQTT_PASSWORD);

/****************************** Feeds ***************************************/

// Setup a feed called 'OnOff' for subscribing to changes.
// Notice MQTT paths for AIO follow the form: <username>/feeds/<feedname>
const char ONOFF_FEED[] PROGMEM = AIO_USERNAME "/feeds/OnOff";
Adafruit_MQTT_Subscribe OnOff = Adafruit_MQTT_Subscribe(&mqtt, ONOFF_FEED);

/*************************** Sketch Code ************************************/

// connect to adafruit io via MQTT
void connect() {

  Serial.print(F("Connecting to Adafruit IO... "));

  int8_t ret;

  while ((ret = mqtt.connect()) != 0) {

    switch (ret) {
      case 1: Serial.println(F("Wrong protocol")); break;
      case 2: Serial.println(F("ID rejected")); break;
      case 3: Serial.println(F("Server unavail")); break;
      case 4: Serial.println(F("Bad user/pass")); break;
      case 5: Serial.println(F("Not authed")); break;
      case 6: Serial.println(F("Failed to subscribe")); break;
      default: Serial.println(F("Connection failed")); break;
    }

    if(ret >= 0)
      mqtt.disconnect();

    Serial.println(F("Retrying connection..."));
    delay(5000);

  }

  Serial.println(F("Adafruit IO Connected!"));

}

void setup() {

  Serial.begin(115200);

  Serial.println(F("Adafruit IO Example"));

  // Connect to WiFi access point.
  Serial.println(); Serial.println();
  delay(10);
  Serial.print(F("Connecting to "));
  Serial.println(WLAN_SSID);

  WiFi.begin(WLAN_SSID, WLAN_PASS);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(F("."));
  }
  Serial.println();

  Serial.println(F("WiFi connected"));
  Serial.println(F("IP address: "));
  Serial.println(WiFi.localIP());

  // listen for events on the OnOff feed
  mqtt.subscribe(&OnOff);

  // connect to adafruit io
  connect();

  delay(500);
  //In-order to control the on-board LED,
  //have to end Serial
  Serial.end();
  // Set On-board LED to output
  pinMode(BUILTIN_LED, OUTPUT);

}

void loop() {

  Adafruit_MQTT_Subscribe *subscription;

  // ping adafruit io a few times to make sure we remain connected
  if(! mqtt.ping(3)) {
    // reconnect to adafruit io
    if(! mqtt.connected())
      connect();
  }

  // this is our 'wait for incoming subscription packets' busy subloop
  while (subscription = mqtt.readSubscription(1000)) {

    // we only care about the OnOff events
    if (subscription == &OnOff) {

      // convert mqtt ascii payload to int
      char *value = (char *)OnOff.lastread;
      //Serial.print(F("Received: "));
      //Serial.println(value);

      // Apply message to OnOff
      String message = String(value);
      message.trim();
      if (message == "ON") {
        digitalWrite(BUILTIN_LED, LOW);
        //Serial.println("On-board LED ON");
        }
      if (message == "OFF") {
        digitalWrite(BUILTIN_LED, HIGH);
        //Serial.println("On-board LED OFF");
        }

    }

  }

}




Wednesday, May 27, 2015

Arduino Due + ESP8266 + DHT11, to update ThingSpeak

Here is a example to use Arduino Due read sensed data, humidity and temperature, from DHT11, send to ThingSpeak.com using WiFi module DSP8266.


ThingSpeak is an application platform for the Internet of Things (IoT). ThingSpeak allows you to build an application around data collected by sensors. Features of ThingSpeak include: real-time data collection, data processing, visualizations, apps, and plugins.

Before start,you have to sign-up ThingSpeak, visit: https://thingspeak.com/ to register, creat channel, get API Key. You can also try it using web browser.


Connection, refer to last post of "Arduino Due + ESP8266 + DHT11, to update dweet.io".

Arduino code, Due8266Client_ThingSpeak.ino
/*
Arduino Due + ESP 8266 WiFi Module
- As STA to Join AP
- Connect to thingspeak.com as client, 
  to update my thing of Humidity and Temperature
  (read from DHT11)

Serial (Tx/Rx) communicate to PC via USB
Serial3 (Tx3/Rx3) connect to ESP8266
Tx3 - ESP8266 Rx
Rx3 - ESP8266 Tx
ESP8266 CH_PD Connect to ESP8266 VCC

DHT11 connect to pin 2 of Arduino Due

for firmware:
"v0.9.5.2 AT Firmware"
(http://goo.gl/oRdG3s)
AT version:0.21.0.0
SDK version:0.9.5

*/
#define ESP8266 Serial3
#include "DHT.h"
#define DHTTYPE DHT11
#define DHTPIN 2  //pin 2 connect to DHT11
DHT dht(DHTPIN, DHTTYPE, 30);  //30 for Arduino Due

String SSID = "TestAP";
String PASSWORD = "12345678";

int LED = 13;

boolean FAIL_8266 = false;

#define BUFFER_SIZE 1024
char buffer[BUFFER_SIZE];

void setup() {
  pinMode(LED, OUTPUT);
  
  digitalWrite(LED, LOW);
  delay(300);
  digitalWrite(LED, HIGH);
  delay(200);
  digitalWrite(LED, LOW);
  delay(300);
  digitalWrite(LED, HIGH);
  delay(200);
  digitalWrite(LED, LOW);

  do{
    //Serial.begin(9600);
    //ESP8266.begin(9600);
    Serial.begin(115200);
    ESP8266.begin(115200);
  
    //Wait Serial Monitor to start
    while(!Serial);
    Serial.println("--- Start ---");

    ESP8266.println("AT+RST");
    delay(1000);
    if(ESP8266.find("ready"))
    {
      Serial.println("Module is ready");
      
      ESP8266.println("AT+GMR");
      delay(1000);
      clearESP8266SerialBuffer();
      
      ESP8266.println("AT+CWMODE=1");
      //ESP8266.println("AT+CWMODE=3");
      delay(2000);
      
      //Quit existing AP, for demo
      Serial.println("Quit AP");
      ESP8266.println("AT+CWQAP");
      delay(1000);
      
      clearESP8266SerialBuffer();
      if(cwJoinAP())
      {
        Serial.println("CWJAP Success");
        FAIL_8266 = false;
        
        delay(3000);
        clearESP8266SerialBuffer();
        //Get and display my IP
        sendESP8266Cmdln("AT+CIFSR", 1000);  
        //Set multi connections
        sendESP8266Cmdln("AT+CIPMUX=1", 1000);
        //sendESP8266Cmdln("AT+CIPMUX=0", 1000);

        Serial.println("Setup finish");
      }else{
        Serial.println("CWJAP Fail");
        delay(500);
        FAIL_8266 = true;
      }
    }else{
      Serial.println("Module have no response.");
      delay(500);
      FAIL_8266 = true;
    }
  }while(FAIL_8266);
  
  digitalWrite(LED, HIGH);
  
  //set timeout duration ESP8266.readBytesUntil
  ESP8266.setTimeout(1000);
  
  dht.begin();
}

void loop(){
  
  float valHumidity;
  float valTemperature;
  do{
    valHumidity = dht.readHumidity();
    valTemperature = dht.readTemperature();
  }while(isnan(valHumidity) || isnan(valTemperature));
  
  Serial.println("Humidity=" + String(valHumidity));
  Serial.println("Temperature=" + String(valTemperature));
  
  /*
  AT+CIPSTART=id,"type","addr",port
  id = 0
  type = "TCP"
  addr = "www.example.com"
  port = 80
  */
  String TARGET_ID="0";
  String TARGET_TYPE="TCP";
  String TARGET_ADDR="184.106.153.149";  //ThingSpeak IP Address
  String TARGET_PORT="80";

  String cmd="AT+CIPSTART=" + TARGET_ID;
  cmd += ",\"" + TARGET_TYPE + "\",\"" + TARGET_ADDR + "\"";
  cmd += ","+ TARGET_PORT;

  Serial.println(cmd);
  ESP8266.println(cmd);
  delay(1000);
  //Assume OK
  //display and clear buffer
  clearESP8266SerialBuffer();
  
  /*
  POST /update?key=<API KEY>&field1=xx.xx&field2=xx.xx HTTP/1.1\r\n
  Host: api.thingspeak.com\r\n\r\n
  */
  
  String ThingSpeakMethod = "POST";  //GET should also work
  String ThingSpeakHost = "/update";
  String ThingSpeakApiKey = "42WKM35W3FR0OJUG";
  String ThingSpeakField1 = "field1";
  String ThingSpeakField2 = "field2";
  String ThingSpeakHttp = "HTTP/1.1";
  
  String ThingSpeak_2ndLine = "Host: api.thingspeak.com\r\n\r\n";
  
  String HTTP_RQS = ThingSpeakMethod;
  HTTP_RQS += " " + ThingSpeakHost;
  HTTP_RQS += "?key=" + ThingSpeakApiKey;
  HTTP_RQS += "&" + ThingSpeakField1 + "=" + valHumidity;
  HTTP_RQS += "&" + ThingSpeakField2 + "=" + valTemperature;
  HTTP_RQS += " " + ThingSpeakHttp + "\r\n";
  HTTP_RQS += ThingSpeak_2ndLine;
  
  String cmdSEND_length = "AT+CIPSEND=";
  cmdSEND_length += TARGET_ID + "," + HTTP_RQS.length() +"\r\n";
  
  ESP8266.print(cmdSEND_length);
  Serial.println(cmdSEND_length);
  
  Serial.println("waiting >");
  
  if(!ESP8266.available());
  
  if(ESP8266.find(">")){
    Serial.println("> received");
    ESP8266.println(HTTP_RQS);
    Serial.println(HTTP_RQS);
    
    boolean OK_FOUND = false;
    
    //program blocked untill "SEND OK" return
    while(!OK_FOUND){
      if(ESP8266.readBytesUntil('\n', buffer, BUFFER_SIZE)>0){
        Serial.println("...");
        Serial.println(buffer);
        
        if(strncmp(buffer, "SEND OK", 7)==0){
          OK_FOUND = true;
          Serial.println("SEND OK found");
        }else{
          Serial.println("Not SEND OK...");
        }
      }
    }

    if(OK_FOUND){
      delay(1000);
      //Dummy display received data
      while (ESP8266.available() > 0) {
        char a = ESP8266.read();
        Serial.write(a);
      }
    }
    
  }else{
    Serial.println("> NOT received, something wrong!");
  }
  
  //Close connection
  String cmdCIPCLOSE = "AT+CIPCLOSE=" + TARGET_ID; 
  ESP8266.println(cmdCIPCLOSE);
  Serial.println(cmdCIPCLOSE);
  
  delay(5000);
  
}

boolean waitOKfromESP8266(int timeout)
{
  do{
    Serial.println("wait OK...");
    delay(1000);
    if(ESP8266.find("OK"))
    {
      return true;
    }

  }while((timeout--)>0);
  return false;
}

boolean cwJoinAP()
{
  String cmd="AT+CWJAP=\"" + SSID + "\",\"" + PASSWORD + "\"";
  ESP8266.println(cmd);
  return waitOKfromESP8266(10);
}

//Send command to ESP8266, assume OK, no error check
//wait some time and display respond
void sendESP8266Cmdln(String cmd, int waitTime)
{
  ESP8266.println(cmd);
  delay(waitTime);
  clearESP8266SerialBuffer();
}

//Basically same as sendESP8266Cmdln()
//But call ESP8266.print() instead of call ESP8266.println()
void sendESP8266Data(String data, int waitTime)
{
  //ESP8266.print(data);
  ESP8266.print(data);
  delay(waitTime);
  clearESP8266SerialBuffer();
}

//Clear and display Serial Buffer for ESP8266
void clearESP8266SerialBuffer()
{
  Serial.println("= clearESP8266SerialBuffer() =");
  while (ESP8266.available() > 0) {
    char a = ESP8266.read();
    Serial.write(a);
  }
  Serial.println("==============================");
}