User Tools

Site Tools


proiecte:influxdb

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
proiecte:influxdb [2017/02/15 23:14]
mihai.volmer [InfluxDB]
proiecte:influxdb [2017/02/16 00:46] (current)
mihai.volmer [ESP8266]
Line 19: Line 19:
 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. 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.+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 ==== ==== Configuration and deployment ====
 +Deploying an InfluxDB is very straight forward. The [[https://docs.influxdata.com/influxdb/v1.2/introduction/installation/ | official website]] has extensive documentation regarding deployment, configuration and usage.
 +
 +For Ubuntu 15.04+ or Debian 8+ systems:
 +<code>
 +sudo apt-get update && sudo apt-get install influxdb
 +sudo systemctl start influxdb
 +</code>
 +
 InfluxDB is highly configurable. This is a great feature because it makes it flexible for a wide range 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. of usages. The configuration is made by modifying the /etc/influxdb/influxdb.conf file.
Line 28: Line 36:
  
 For connectivity, we have enabled the HTTP endpoint and one UDP listener. For connectivity, we have enabled the HTTP endpoint and one UDP listener.
 +
 +<code>
 +[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
 +</code>
 +
 +==== 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.
 +
 +<code>
 +# 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)"
 +</code>
  
 ===== ESP8266 ===== ===== 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.
  
-===== Results =====+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:
 +<code>
 +void setup() {
 +  // Insert other code here, such as pin initializations
 +  // .....................
  
-===== Resources =====+  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());
 +}
 +</code>
 +
 +We made two implementations that send data from an LDR sensor to the server.
 +
 +<code>
 +const int LDR = A0;
 +void loop() {
 +   int ldr = analogRead(LDR);
 +   Serial.printf("LDR value = %d\n", ldr);
 +
 +   send_value(ldr);
 +
 +   delay(500);
 +}
 +</code>
 +
 +==== Sendint to HTTP endpoint (TCP) ====
 +
 +<code>
 +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();
 +}
 +</code>
 +
 +==== 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.
 +
 +<code>
 +void send_value(int ldr) {
 +   String content = "cpu,host=esp8266 ldr=" + String(ldr);
 +   udp.beginPacket(host, port);
 +   udp.print(content);
 +   udp.endPacket();
 +}
 +</code>
 +===== 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 =====
 +[[https://docs.influxdata.com/influxdb/v1.2/introduction/installation/ | InfluxDB official website]]
  
proiecte/influxdb.1487193263.txt.gz · Last modified: 2017/02/15 23:14 by mihai.volmer