Hello!
Recently I bought esp8266 (NodeMcu) – it is a low cost device with wifi chip, that can be programmed by Arduino IDE or Lua script language.
My first idea for this device is to draw graphs of temperature readings from ds18b20 sensor and signal strength on wifi connection for each SSID with which the device can connect. For logging I have raspberry pi configured with graphite server and Grafana. To log something on graphite server the esp8266 have to send UDP packet with string containg: metric name, metric value, timestamp. Bellow expample of such strings:
1 2 |
esp.temp 21.50 1482075500 esp.rssi.JAN -73 1482075500 |
Charts in Grafana:
And the code doing this looks as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
#include <ESP8266WiFi.h> #include <WiFiUdp.h> #include <time.h> #include <OneWire.h> #include <DallasTemperature.h> extern "C" { #include "user_interface.h" } #define ONE_WIRE_BUS 2 // DS18B20 pin OneWire oneWire(ONE_WIRE_BUS); DallasTemperature DS18B20(&oneWire); WiFiUDP Udp; IPAddress remoteIP(10,10,10,10); //graphite server unsigned int remotePort = 2003; //graphite server port unsigned int localPort = 2003; const char* ssid = "wifi_network_ssid"; const char* password = "wifi_network_password"; void setup() { Serial.begin(115200); delay(200); pinMode(LED_BUILTIN, OUTPUT); WiFi.setSleepMode(WIFI_LIGHT_SLEEP); WiFi.mode(WIFI_STA); WiFi.begin(ssid, password); Serial.println(); Serial.println("Wait for WiFi... "); while(WiFi.status() != WL_CONNECTED) { Serial.print("."); delay(500); } Serial.println(""); Serial.println("WiFi connected"); Serial.println("IP address: "); Serial.println(WiFi.localIP()); //void configTime(int timezone, int daylightOffset_sec, const char* server1, const char* server2, const char* server3) configTime(0, 0, "0.europe.pool.ntp.org"); //checking if time is synced properly, if not - retrying Serial.println("Waiting for time"); while(time(nullptr) <= 100000) { Serial.print("."); delay(100); } Serial.println("NTP synced"); //blinking led if wifi is connected and time is synced for (int i=1;i<10;i++){ digitalWrite(LED_BUILTIN, LOW); delay(50); digitalWrite(LED_BUILTIN, HIGH); delay(50); } } void loop() { send_temperature_to_graphite(); send_wifi_rssi_to_graphite(); Serial.println("Delay for 50 seconds"); delay(50000); } void send_temperature_to_graphite() { time_t now = time(nullptr); // temperature section char graphite_temperature[100]; float temp; DS18B20.requestTemperatures(); delay(5000); temp = DS18B20.getTempCByIndex(0); /* example of graphite_temperature_string: * esp.temp 23.87 1540228668 */ String graphite_temperature_string = "esp.temp " + String(temp) + " " + now; graphite_temperature_string.toCharArray(graphite_temperature, 100); Udp.beginPacket(remoteIP, remotePort); Udp.write(graphite_temperature); Udp.endPacket(); } void send_wifi_rssi_to_graphite() { time_t now = time(nullptr); // wifi rssi section char graphite_rssi[100]; int rssi = WiFi.RSSI(); String ssid = WiFi.SSID(); /* example of graphite_rssi_string: * esp.rssi.JAN -52 1540228502 */ String graphite_rssi_string = "esp.rssi." + ssid + " " + rssi + " " + now; graphite_rssi_string.toCharArray(graphite_rssi, 100); Udp.beginPacket(remoteIP, remotePort); Udp.write(graphite_rssi); Udp.endPacket(); } |
Libraries used in this code (DallasTemperature and OneWire):
libraries.zip
You can download this through library manager from Arduino IDE.