IoT devices can collect a lot of data depending on the use case. This data cannot be stored on the device itself (storage capacity limitations). Therefore connecting to a remote platform is required to offload data for further use (decision making, analytics, …).

Network Connectivity

The Feather HUZZAH ESP8266 can connect to a wireless network. Once the connection to the wireless network has been established, it can make web request like any other device.

A simple program which connects to the WiFi and sends a single request to https://tmb.gr/ would look like this

  #include <ESP8266WiFi.h>

const char *ssid = "MY_SSID"; // TODO
const char *password = "MY_PASSWORD"; // TODO

void setup() {
  Serial.begin(115200);
  Serial.print("\nConnecting to network ");
  Serial.println(ssid);
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
  }
  Serial.print("Connected to network, local IP = "); 
  Serial.println(WiFi.localIP());

  const char *host = "tmb.gr";
  const char *path = "/hello.json";
  const int port = 80;

  // connect to remote host
  WiFiClient client;
  if (client.connect(host, port)) {

    // send HTTP request
    client.print("GET ");
    client.print(path);
    client.print(" HTTP/1.1\r\n");
    client.print("Host: ");
    client.print(host);
    client.print("\r\n");
    client.print("Connection: close\r\n\r\n");

    // read HTTP response
    while (client.connected() || client.available()) {
      int ch;
      while ((ch = client.read())) >= 0) {
          Serial.print((char) ch);
      }
    }
  }
}

void loop() {}
  

HTTPS/TLS

The example above uses an unsafe HTTP connection to make a request to the remote host. Even small embedded devices are capable of making secure connections using HTTPS and verify the remotes fingerprint or certificate.

Network Time Protocol

MCUs do not know what the current time is. The millis() method for example will only return the milliseconds ellapsed since the MCU has been started. If the actual date and time are required the time of the MCU must be synchronized. There are two variants for this:

  • use NTP (network time protocol) to set the internal clock
  • use the timestamp in an HTTP response to set the internal clock

Sync with NTP

  #include <ESP8266WiFi.h>
#include <time.h>

const char *ssid = "MY_SSID"; // TODO
const char *password = "MY_PASSWORD"; // TODO

void setup() {
  Serial.begin(115200);
  Serial.print("\nConnecting to network ");
  Serial.println(ssid);
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
  }
  Serial.print("Connected to network, local IP = "); 
  Serial.println(WiFi.localIP());

  const int timezone = 0;
  const int dst_off = 0;
  configTime(timezone * 3600, dst_off, "ntp11.metas.ch", "ntp12.metas.ch", "ntp13.metas.ch");
  // wait for time() being adjusted, as a side effect of configTime
  while (time(NULL) < 28800 * 2) {
    delay(500);
  }
}

void loop() {
  // number of seconds elapsed since start of epoche
  time_t now = time(NULL);
  Serial.print(ctime(&now));
  const char *format = "%Y-%m-%dT%H:%M:%SZ"; // ISO 8601 UTC
  const struct tm *t = gmtime(&now);
  const size_t maxLen = 21;
  char s[maxLen];
  size_t len = strftime(s, maxLen, format, t);
  Serial.println(s);
  delay(1000);
}
  

IoT Platform

IoT platforms are used to upload data from devices to a central storage location. They usually provide better or more powerful tools for management, analytics and visualization.

Cool and free platforms to use are:

These platforms usually provide an API which can be used to send and save data on the remote servers. This is either done via plain HTTP requests from the device or the usage of libraries (e.g. ThingSpeak library for ESP8266)