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

Tutorial overview

This tutorial demonstrates the use case how to develop an application for UCW Platform. The DHT Logger is an example how to implement a data logger for DHTx sensors (source code).

Requirements 

Download tutorial sources

To download tutorial sources and test their functionality, do as follows:

  1. Make sure you have downloaded all the requirements.
  2. Open a terminal/console and navigate to the directory where you want to work on the tutorial.
  3. Download the sources using the following command:

    git clone https://github.com/unitycloudware/dht-logger-firmware.git dht-logger-firmware
  4. Open dht-logger.ino or dht-logger-ucw.ino file in Arduino IDE

Settings

Before you start to use the firmware you need to do following steps to set-up connection to the UCW server

Step 1 - UCW Host

You need to configure your UCW host. For example when you are running the plugin DHT Logger for UCW Portal on your machine, then use the IP address of your machine. In case you are running a firewall, please make sure port 9601 is allowed on your firewall.

dht-logger.ino
#define UCW_API_HOST          "192.168.0.104"
#define UCW_API_PORT          9601

When using the UCW Arduino Library, device authentication and connectivity information are presented in the config.h file. It has a struct, UCWConfig, which handles device authentication where is provided with a device access token. It holds the server connection information such as host address (here, an IP address) and port number, and if network connection uses secured communication over SSL / TLS (variable isSecuredConnection). Also, if the connection to the network is via WiFi, useMQTT variable indicates if the connection method is MQTT or REST API.

Edit config.h file: (dht-logger-ucw.ino)
/************************ UCW Platform Config *******************************/

// Configuration of the connection string to the UCW Platform.
static UCWConfig cfg = {
  .host = "192.168.0.104",
  .port = 9601,
  .isSecuredConnection = false,
  .useMqtt = false,
  .mqttUser = UCW_MQTT_USER,
  .mqttPassword = UCW_MQTT_PASS,
  .token = "your_token"
}

Step 2 - Device Access Token

You need a device access token for communication with the UCW portal over REST API. The token can be found in section Access Tokens of the UCW project (see below). The scope of the device token has to be READ_WRITE.

dht-logger.ino
#define UCW_API_HOST "192.168.0.104"
#define UCW_API_PORT 9601

#define UCW_API_DEVICE_TOKEN  "e4ek0h1fujtoa1rnadb8t5fkbl6v2sn7"

The device access token obtained from the UCW platform is provided in the token variable of the UCWConfig struct.

Edit config.h file: (dht-logger-ucw.ino)
/************************ UCW Platform Config *******************************/

// Configuration of the connection string to the UCW Platform.
static UCWConfig cfg = {
     .host = 192.168.0.104,
     .port = 9601,
     .isSecuredConnection = false,
     .useMqtt = false,
     .token = "e4ek0h1fujtoa1rnadb8t5fkbl6v2sn7"
};

Step 3 - Device ID

You need a device ID that identifies your device for particular UCW project, and it allows to store data in the data stream ucw-dhtlogger.

dht-logger.ino
#define UCW_API_HOST "192.168.0.104"
#define UCW_API_PORT 9601

#define UCW_API_DEVICE_TOKEN  "e4ek0h1fujtoa1rnadb8t5fkbl6v2sn7"
#define UCW_API_DEVICE_ID     "e2e7c676-fcb8-444f-997c-d80b5a2982ee"
dht-logger-ucw.ino
#define DEVICE_ID     "e2e7c676-fcb8-444f-997c-d80b5a2982ee"
#define DATA_STREAM   "ucw-dhtlogger"

Step 4 - DHT library and settings

Include the DHT library and set the DHT pin and type. In this example, the sensor data pin is connected to the micro-controller pin 6. Details of connection between Adafruit Feather M0 Wifi and DHT22 sensor with diagramatic representation can be seen here.

dht settings
#include "DHT.h"

#define DHTPIN 6      // set DHT pin
#define DHTTYPE DHT22 // indicate the DHT type

DHT dht(DHTPIN, DHTTYPE); //create DHT object

Step 5 - WiFi credentials

The SSID and password of WiFi network are provided here

WiFi SSID and password (dht-logger.ino)
char ssid[] = "your_ssid";     // your network SSID (name)
char pass[] = "your_password"; // your network password (use for WPA, or use as key for WEP)

As stated in Step 1, the config.h file handles network connectivity information. Since the micro-controller connects to the UCW server via a WiFi network in this example, the WiFi credentials are provided in the WiFi section of the config file.

WiFi SSID and password (config.h)
/******************************* 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);

DHT Logger Firmware - REST API

Step 6 - WiFi set-up, connection and data collection

The procedure for connecting the micro-controller to the WiFi network, sensor data collection and sending same to the UCW Platform can be seen in a tutorial for Adafruit Feather M0 WiFi and Adafruit Huzzah ESP8266.

Step 7 - Read data

This method reads the temperature, humidity and heat index data measured by the sensor. It is called in the readData() method.

Reading temperature, humidity and heat index data from the DHT22 sensor
String readData() {
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float h = dht.readHumidity();

  // Read temperature as Celsius (the default)
  float t = dht.readTemperature();

  // Read temperature as Fahrenheit (isFahrenheit = true)
  float f = dht.readTemperature(true);

  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t) || isnan(f)) {
    Serial.println("Failed to read from DHT sensor!");
    return "";
  }

  // Compute heat index in Fahrenheit (the default)
  float hif = dht.computeHeatIndex(f, h);

  // Compute heat index in Celsius (isFahreheit = false)
  float hic = dht.computeHeatIndex(t, h, false);

  String data = "{\"temperature\": %temperatureC, \"humidity\": %humidity, \"heatIndex\": %heatIndexC}";
  data.replace("%humidity", String(h));
  data.replace("%temperatureC", String(t));
  data.replace("%heatIndexC", String(hic));

  return data;
}

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

DHT Logger Firmware - UCW Arduino Library

Step 8 - WiFi set-up, connection and data collection

The procedure for connecting the micro-controller to the WiFi network, sensor data collection and sending same to the UCW Platform can be seen in the tutorial Data Measurement with DHT sensor.

A detailed example of DHT logger firmware using UCW Arduino Library can be viewed here.


  • No labels