User Tools

Site Tools


proiecte:influxdb

InfluxDB as a backend server. ESP8266 client implementation

Volmer Mihai-Cristian - AAC

Introduction

A backend server for an IoT project is a server that collects data from sensors. It can also process the data and display it on a web page or provide ways to access the data.

I have studied some of the available backend solutions for IoT projects. There are many options to choose from. However, each comes with its advantages and flaws.

Some of the available backend solutions are: AdafruitIO, DeviceHub, ThingSpeak, Sense IoT etc.

InfluxDB

InfluxDB is a timeseries database with an SQL-like syntax. This means that the entries are indexed by a timestamp so they are easier to process.

Unlike other backend solutions, this provides only the database with HTTP and UDP endpoints that allow remote devices to insert or query data.

InfluxDB is free and open-source. This means that it can be deployed on any user-owned machine running Linux or Mac OSX. This is a great advantage because it provides greater flexibility. It can also run on multiple machines in a cluster-like infrastructure.

We have chosen InfluxDB in our project because we were able to deploy it on our machines and configure it how we wanted. Also, we were able to try different configurations without affecting other users. Another important reason is that the InfluxDB community is very active at this moment.

Configuration and deployment

Deploying an InfluxDB is very straight forward. The official website has extensive documentation regarding deployment, configuration and usage.

For Ubuntu 15.04+ or Debian 8+ systems:

sudo apt-get update && sudo apt-get install influxdb
sudo systemctl start influxdb

InfluxDB is highly configurable. This is a great feature because it makes it flexible for a wide range of usages. The configuration is made by modifying the /etc/influxdb/influxdb.conf file.

We have temporarily disabled the Meta Service option since we did not use the server in a cluster-like infrastructure. We also enabled the Continuous query service.

For connectivity, we have enabled the HTTP endpoint and one UDP listener.

[http]
  enabled = true
  bind-address = ":8086"
  auth-enabled = true
  log-enabled = true
  write-tracing = false
  pprof-enabled = false
  https-enabled = false
  https-certificate = "/etc/ssl/influxdb.pem"

[[udp]]
  enabled = true
  bind-address = ":8089"
  database = "test_db"

[continuous_queries]
  log-enabled = true
  enabled = true

Usage

Here are some examples of commands used to insert and query data to/from the InfluxDB server.

We assume the server is locally hosted, the database is called “mydb”, the data series is called “cpu” and the values are identified with “temp”. The entries also contain tags which help us process the data (e.g.: for join operations).

The commands are written for bash. However, the queries can also be done manually (sending the whole query through a socket) or through a library.

# Insert an entry in the database with the tags host='lenovo' and region='ro' and value temp=$TEMP, which is a bash variable
curl -XPOST 'http://localhost:8086/write?db=mydb' -d "cpu,host=lenovo,region=ro temp=$TEMP"

# To list every entry from the cpu series
curl -G 'http://localhost:8086/query?pretty=true' --data-urlencode "db=mydb" --data-urlencode "q=SELECT * FROM cpu"

# List mean values for each minute from the selected timeframe. Process only entries that have the tag host='esp8266'
curl -G 'http://localhost:8086/query?pretty=true' --data-urlencode "db=mydb" --data-urlencode "q=SELECT MEAN("temp") FROM "cpu" WHERE host='esp8266' AND time >= '2017-01-17T22:00:00Z' AND time <= '2017-01-18T00:00:00Z' GROUP BY time(1m)"

ESP8266

ESP8266 is a very versatile device. It has a 80 MHz processor, 4MB of flash memory and WiFi connectivity, all in a finger-sized device. It is also very cheap.

It can use several firmwares, such as NodeMCU (lua interpreter) or micropython. However, these are slow and buggy. The device can also use Arduino firmware. This is preferable since there are many libraries already implemented.

To connect an ESP8266 to the WiFi as a station:

void setup() {
  // Insert other code here, such as pin initializations
  // .....................

  const char* ssid = "";
  const char* password = "";

  // Connect tp Wifi
  Serial.printf("Connecting to %s\n", ssid);
  WiFi.mode(WIFI_STA);
  if (String(WiFi.SSID()) != String(ssid)) {
    WiFi.begin(ssid, password);
  }
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.print("Connected! IP address: ");
  Serial.println(WiFi.localIP());

  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
  Serial.println(WiFi.macAddress());
}

We made two implementations that send data from an LDR sensor to the server.

const int LDR = A0;
void loop() {
   int ldr = analogRead(LDR);
   Serial.printf("LDR value = %d\n", ldr);

   send_value(ldr);

   delay(500);
}

Sendint to HTTP endpoint (TCP)

void setup() {
   // Insert other code, such as wifi connection
   // ................

   if (!client.connect(host, port)) {
     Serial.println("connection failed");
     return;
   }
}
   
void send_value(int value) {
   String content = "cpu,host=esp8266 ldr=" + String(value);
   
   client.print("POST /write?db=mydb HTTP/1.1\r\n");
   client.print("User-Agent: esp8266/0.1\r\n");
   client.print("Host: localhost:8086\r\n");
   client.print("Accept: */*\r\n");
   client.print("Content-Length: " + String(content.length()) + "\r\n");
   client.print("Content-Type: application/x-www-form-urlencoded\r\n");
   client.print("\r\n");
   client.print(content + "\r\n");

   client.flush();
}

Sendint to UDP endpoint

InfluxDB has a very simple line protocol when using UDP. However, this has the disadvantage of sending the data in clear text. It also lets anyone who knows the server address to insert data without authentication.

void send_value(int ldr) {
   String content = "cpu,host=esp8266 ldr=" + String(ldr);
   udp.beginPacket(host, port);
   udp.print(content);
   udp.endPacket();
}

Results

We have used a t2.micro (1 core of 2.4 GHz, 512MB RAM) AWS machine and a Lenovo Thinkpad W540 laptop (8 cores * 2.8GHz, 16GB RAM) to host our InfluxDB instances.

Resources

proiecte/influxdb.txt · Last modified: 2017/02/16 00:46 by mihai.volmer