Page tree
Skip to end of metadata
Go to start of metadata

This data measurement is implemented using Adafruit Huzzah ESP8266 micro-controller.

Include the libraries

include libraries
#include <SPI.h>
#include <ESP8266WiFi.h>
  • SPI.h provides the mechanism for handling the communication between a master device and one or more peripheral devices.
  • ESP8266WiFi.h library manages connection to an existing WiFi network.

Provide Wifi details

Wifi SSID and password
char ssid[] = "your_ssid";     // your network SSID (name)
char pass[] = "your_password"; // your network password (use for WPA, or use as key for WEP)
int keyIndex = 0;              // your network key Index number (needed only for WEP)
byte mac[6];
int status = WL_IDLE_STATUS;
  • provide Wifi SSID and password
  • declare variable mac to hold wifi MAC address
    Note:
    • the SSID and password are case sensitive
    • they should be provided between quotation marks (" ")

Provide token and device ID

define token and device ID
#define UCW_API_HOST          "cloud.unitycloudware.com"
#define UCW_API_PORT          80
#define UCW_API_DEVICE_TOKEN  "your_token"
#define UCW_API_DEVICE_ID     "your_device_id"
#define UCW_API_DATA_STREAM   "data-monitoring"
  • provide token and ID of device (sensor) being monitored
  • also update the API host and port if necessary

Creating objects for WiFiClient and IPAddress classes from WiFi101.h library

Wifi and IP address objects
IPAddress server;
WiFiClient client;

setupSerialPorts()

setupSerialPorts()
void setupSerialPorts() {
  //Initialize serial and wait for port to open:
  Serial.begin(9600);
  
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
}
  • this method initializes the serial monitor by calling Serial.begin(9600);

    Note:

    • while the default baud rate is 9600, other rates are available for use
    • same baud rate used here must be chosen at the serial monitor

setupWifi()

  • this method connects to the Wifi network using provided SSID and password

  • It checks for the Wifi shield presence. If it doesn't detect Wifi shield, it prints "WiFi shield not present" on the serial monitor.

    if (WiFi.status() == WL_NO_SHIELD) {
        Serial.println("WiFi shield not present");
        // don't continue:
        while (true);
      }
  • then connects to the Wifi network using provided SSID and password
  • the printWifiStatus() and WiFi.hostByName() methods are called here 

    status = WiFi.begin(ssid, pass);  // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
    printWifiStatus();
    WiFi.hostByName(UCW_API_HOST, server);

printWifiStatus()

  • this method is called in setupWifi() method
  • it also prints the following information: Wifi SSID, Wifi shield IP address, and the strength of the received signal on the serial monitor
    • Wifi SSID

        // print the SSID of the network you're attached to:
        Serial.print("SSID: ");
        Serial.println(WiFi.SSID());
    • Wifi shield IP address

       // print your WiFi shield's IP address:
        IPAddress ip = WiFi.localIP();
        Serial.print("IP Address: ");
        Serial.println(ip);
    • Wifi MAC address

        WiFi.macAddress(mac);
        Serial.print("MAC Address: ");
        Serial.print(mac[5],HEX);
        Serial.print(":");
        Serial.print(mac[4],HEX);
        Serial.print(":");
        Serial.print(mac[3],HEX);
        Serial.print(":");
        Serial.print(mac[2],HEX);
        Serial.print(":");
        Serial.print(mac[1],HEX);
        Serial.print(":");
        Serial.println(mac[0],HEX);
    • RSSI

      // print the received signal strength:
        long rssi = WiFi.RSSI();
        Serial.print("signal strength (RSSI):");
        Serial.print(rssi);
        Serial.println(" dBm");

collectData()

  • this method calls the readData() method to receive data from the sensor
  • the received data is saved as a string. In this example, it is saved in string variable data
  • the data variable is tested to check if it has received any measured data from the sensor, and calls the sendData() method to send the data to UCW platform.
void collectData() {
  if ((millis() - lastConnectionTime) > postingInterval) {
    String data = "";

    Serial.print("Data = ");

    data = readData();
    Serial.print(data);

    Serial.println("");

    if (data.length() > 0) {
      sendData(data);      
    }
  }
}

readData()

readData()
String readData() {
  double temperature = 22.00;
  int humidity = 43;
  
  String data = "{\"temperature\": %temperature, \"humidity\": %humidity}";
  data.replace("%temperature", String(temperature));
  data.replace("%humidity", String(humidity));
  
  return data;
}
  • this method is called in the collectData() method
  • It takes the measured data and saves it as a JSON string
  • In this example, the temperature and humidity variables are of type double and int respectively. They are saved as JSON string

sendData(String payload)

  • this method sends the sensor data to the UCW platform
  • the payload is checked if data has been received from sensor

     if (payload.length() < 1) {
        Serial.println("No data to send!");
        return;
      }
  • connection is made to the server and payload, device ID information are transferred using REST API

    if (client.connect(server, UCW_API_PORT)) {
        Serial.println();
        Serial.println("Request:");
        Serial.println();
        Serial.println("Sending payload: " + payload);
        Serial.print("Payload length: ");
        Serial.println(payload.length());
    
        String apiUri = "POST /api/ucw/v1/data-streams/%dataStream/messages/%deviceId HTTP/1.1";
        apiUri.replace("%dataStream", UCW_API_DATA_STREAM);
    	apiUri.replace("%deviceId", UCW_API_DEVICE_ID);
    	
        Serial.print("API URI: ");
        Serial.println(apiUri);
        
        client.println(apiUri);
        client.print("Host: ");
        client.println(UCW_API_HOST);
        client.println("User-Agent: Adafruit-HUZZAH-ESP8266");
        client.println("Connection: close");
        client.println("Content-Type: application/json");
        client.print("Content-Length: ");
        client.println(payload.length());
        client.print("Authorization: Bearer ");
        client.println(UCW_API_DEVICE_TOKEN);
        client.println();
        client.println(payload);
    
        Serial.println();
        Serial.println("Response:");
        Serial.println();
    
        while (client.connected()) {
          while (client.available()) {
            char c = client.read();
            Serial.write(c);
          }
        }
     // This will free the socket on the WiFi shield
        client.stop();
      }

An example of how to send measured data using Adafruit Huzzah ESP8266 can be found here.

  • No labels