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

This library explains the steps taken to send data, using WiFi, to UCW platform using any of these micro-controllers: Adafruit Feather M0 WiFi, Adafruit ESP32, and Adafruit Huzzah ESP8266.



The config.h file

The steps below explain the procedure to create this header file

  • Create a header file on the next tab, called "config.h"
  • In this file, include "UCW_System.h
  • Connect to the UCW platform by providing authentication details as a UCWConfig object 
  • Select the desired connection mode: uncomment the WiFi section and comment the rest (Ethernet, LoRa, BLE and GSM)
  • Each of these connection modes includes the appropriate header files/libraries and creates object "ucw"
  • Details of this file can be found in each example 

    config.h for WiFi
    #include <UCW_System.h>
    
    /************************ UCW Platform Config *******************************/
    
    // Configuration of the connection string to the UCW Platform.
    //for WiFi/GSM/Ethernet connections only
    static UCWConfig cfg = {
      .host = UCW_API_HOST,
      .port = UCW_API_PORT,
      .isSecuredConnection = false,
      .useMqtt = false,
      .mqttUser = UCW_MQTT_USER,
      .mqttPassword = UCW_MQTT_PASS,
      .token = "your_token"
    };
    
    /******************************* WiFi **************************************/
    
    /*
      The UCW_WiFi client will work with the following boards:
        - Feather M0 WiFi -> https://www.adafruit.com/products/3010
        - Feather HUZZAH ESP8266 -> https://www.adafruit.com/products/2821
        - HUZZAH ESP8266 Breakout -> https://www.adafruit.com/products/2471
        - Feather WICED -> https://www.adafruit.com/products/3056
    */
    
    #define WIFI_SSID       "your_ssid"
    #define WIFI_PASS       "your_pass"
    
    #include "UCW_WiFi.h"
    UCW_WiFi ucw(&cfg, WIFI_SSID, WIFI_PASS);

The UCW_Config.h file

This configuration file contains the switches.  Depending on the connection interface, the user is expected to un-comment the appropriate switch.

UCW_Config
//switches
//#define UCW_BLE_DEVICE       //BLE switch
//#define UCW_ETHERNET_DEVICE  //Ethernet switch
//#define UCW_GSM_DEVICE       //GSM switch
//#define UCW_LORA_DEVICE      //LORA switch
#define UCW_WIFI_DEVICE        //WiFi switch

The Library

Include the config.h library

Include library
#include "config.h"

Connecting to UCW IoT Cloud

These steps are taken in the "void set-up" section to connect to the UCW cloud:

connect()

  • this method initiates the connection between micro-controller and WiFi network

connect method
ucw.connect();

printNetworkInfo()

  • details of connected network are displayed on the serial monitor
print network details
ucw.printNetworkInfo(); 

Data collection and transmission to cloud

The following steps are taken in the "void loop" section to collect data via sensors and transmit to cloud using already establised connection

sys()

  • this method verifies the connection to the UCW cloud/server is still valid/established

confirm connection to cloud
ucw.sys();

Read data

  • The user decides on the approach of reading and collecting sensor data.
  • The sensor or device used to collect the data influences the collection method
  • The data is stored as a JSON string 

Read data
double temperature = 22.00;
int humidity = 43;
  
String data = "{\"temperature\": %temperature, \"humidity\": %humidity}";
data.replace("%temperature", String(temperature));
data.replace("%humidity", String(humidity));

Sending the data

  • The data is sent to the server using the sendData() method
  • the synthax of this method depends on the connection medium

Example using sendData method
#define DEVICE_ID   "your_device_id"
#define DATA_STREAM "data-test"
ucw.sendData(DEVICE_ID, DATA_STREAM ,data);

This method takes the following string parameters:

    • DEVICE_ID: the user provides the ID of the monitoring device. the ID used here is simple "your_device_id"
    • DATA_STREAM: the sensor data is named for identification. In this example the data is named as "data_test"
    • payload/data: this is simply the JSON string data variable used to store the data collected from the sensor. 

The full code can be found here.

  • No labels