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

This data measurement is implemented using Adafruit Feather M0 WiFi micro-controller.

Include the libraries

include libraries
#include <SPI.h>
#include <WiFi101.h>
  • SPI.h provides the mechanism for handling the communication between a master device and one or more peripheral devices. 
  • Wifi101.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
  • the Adafruit pins are configured for Wifi communication

     WiFi.setPins(8, 7, 4, 2); // Configure pins for Adafruit ATWINC1500 Feather
  • 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);

resetWifi()

  • this method does a Wifi reset by taking the A0 pin low and then high after 10 milliseconds
  • the Adafruit then attempts to reconnect to the Wifi network by calling WiFi.begin(ssid, pass)

    void resetWifi() {
       // shutdown WiFi stack, reset Feather M0, then restart WiFi101 software stack
      WiFi.end();
      digitalWrite(A0, LOW);
      delay(10);
      digitalWrite(A0, HIGH);
      WiFi.begin(ssid, pass);
    }

printWifiStatus()

  • this method is called in setupWifi() method
  • it also prints the following information: Wifi SSID, Wifi shield IP address, Wifi firmware version 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");
    • Wifi firmware version

      Serial.print("WiFi firmware version: ");
      Serial.println(WiFi.firmwareVersion());

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-Feather-M0-Wifi");
        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();
      }
  • reset the Wifi connection using resetWifi() method if connection to server fails

    else {
        // if you couldn't make a connection
        Serial.println("connection failed");
        resetWifi();
      }

An example of how to send measured data using Adafruit Feather M0 WiFi can be found here.

  • No labels