{"id":216,"date":"2024-01-24T14:43:30","date_gmt":"2024-01-24T14:43:30","guid":{"rendered":"https:\/\/cascajolabs.es\/?p=216"},"modified":"2024-08-11T10:26:53","modified_gmt":"2024-08-11T10:26:53","slug":"grafana-dashboard-influxdb-snmp-and-esp8266","status":"publish","type":"post","link":"https:\/\/cascajolabs.es\/?p=216","title":{"rendered":"Grafana Dashboard, Influxdb, SNMP and ESP8266"},"content":{"rendered":"\n<p>In this project, I developed a simulated parking monitoring system using two ESP8266 as the hosts devices. The ESP8266 is responsible for counting the number of cars in a parking lot and transmitting this information to an InfluxDB database and the use of Grafana for real-time visualization.<\/p>\n\n\n\n<p>Technologies:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>ESP8266 for car monitoring and counting.<\/li>\n\n\n\n<li>Grafana for real-time data visualization.<\/li>\n\n\n\n<li>InfluxDB as the database for storing information.<\/li>\n\n\n\n<li>SNMP traps for special event notifications that are sent to the manager.<\/li>\n<\/ul>\n\n\n\n<p>Small demo:<\/p>\n\n\n\n<iframe loading=\"lazy\" width=\"378\" height=\"673\" src=\"https:\/\/www.youtube.com\/embed\/a8Lof6kGGWY\" title=\"InfluxDB, Grafana, ESP8266 | IOT project\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" allowfullscreen><\/iframe>\n\n\n\n<p>The code is:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;Arduino.h>\n\/\/OLED\n#include &lt;Wire.h>\n#include &lt;Adafruit_GFX.h>\n#include &lt;Adafruit_SSD1306.h>\n#define SCREEN_WIDTH 128\n#define SCREEN_HEIGHT 32\nAdafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &amp;Wire, -1);\n\/\/SNMP\n#include &lt;WiFiUdp.h>\n#include &lt;SNMP_Agent.h>\nconst char* rocommunity = \"public\";\nconst char* rwcommunity = \"private\";\nWiFiUDP udp;\nSNMPAgent snmp = SNMPAgent(rocommunity, rwcommunity);\n\/\/ Callback timestamp\nTimestampType uptimeCallback;\n\n\/\/Influx\n#if defined(ESP32)\n  #include &lt;WiFiMulti.h>\n  WiFiMulti wifiMulti;\n  #define DEVICE \"ESP32\"\n  #elif defined(ESP8266)\n  #include &lt;ESP8266WiFiMulti.h>\n  ESP8266WiFiMulti wifiMulti;\n  #define DEVICE \"ESP8266\"\n  #endif\n  \n  #include &lt;InfluxDbClient.h>\n  #include &lt;InfluxDbCloud.h>\n  \n  \/\/ WiFi AP SSID\n  #define WIFI_SSID \"wifissid\"\n  \/\/ WiFi password\n  #define WIFI_PASSWORD \"pass\"\n  \n  #define INFLUXDB_URL \"http:\/\/IP:8086\"\n  #define INFLUXDB_TOKEN \"----------\"\n  #define INFLUXDB_ORG \"------\"\n  #define INFLUXDB_BUCKET \"cars\"\n  \/\/ Time zone info\n  #define TZ_INFO \"UTC1\"\n  \n  \/\/ Declare InfluxDB client instance with preconfigured InfluxCloud certificate\n  InfluxDBClient client(INFLUXDB_URL, INFLUXDB_ORG, INFLUXDB_BUCKET, INFLUXDB_TOKEN, InfluxDbCloud2CACert);\n  \n  \/\/ Declare Data point\n  Point cars(\"garage_one\");\n  int cars_count=0;\n  \/\/ Buttons\n  const int pinIncrease = 12; \/\/ +\n  const int pinDecrease = 14; \/\/ -\n\n  void setup() {\n    Serial.begin(115200);\n    \/\/ buttons input\n    pinMode(pinIncrease, INPUT_PULLUP);\n    pinMode(pinDecrease, INPUT_PULLUP);\n    \/\/ Setup wifi\n    WiFi.mode(WIFI_STA);\n    wifiMulti.addAP(WIFI_SSID, WIFI_PASSWORD);\n  \n    Serial.print(\"Connecting to wifi\");\n    while (wifiMulti.run() != WL_CONNECTED) {\n      Serial.print(\".\");\n      delay(100);\n    }\n    Serial.println();\n    \/\/timeSync(TZ_INFO, \"pool.ntp.org\", \"time.nis.gov\");\n  \n    \/\/ Check server connection\n    if (client.validateConnection()) {\n      Serial.print(\"Connected to InfluxDB: \");\n      Serial.println(client.getServerUrl());\n    } else {\n      Serial.print(\"InfluxDB connection failed: \");\n      Serial.println(client.getLastErrorMessage());\n    }\n        \/\/ Add tags to the data point\n    cars.addTag(\"device\", \"floor1\");\n    \/\/OLED\n      if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {\n        for (;;)\n      ;\n    }\n    display.setTextSize(2);\n    display.setTextColor(WHITE);\n    display.setCursor(1, 5);\n    display.display();\n    \/\/SNMP\n    snmp.setUDP(&amp;udp);\n    snmp.begin();\n\n  }\nvoid loop() {\n    \/\/ Clear fields\n    cars.clearFields();\n    snmp.loop();\n    if (digitalRead(pinIncrease) == LOW) {\n      cars_count++;\n        if(cars_count>10){cars_count=10;}\n         \n      delay(200);\n    }\n    else if (digitalRead(pinDecrease) == LOW) {\n   \n      cars_count--;\n        if(cars_count&lt;0){cars_count=0;}\n\n      delay(200);\n    }\n    \/\/TRAP\n    if(cars_count==10){\n    SNMPTrap* buttonTrap = new SNMPTrap(\"public\", SNMP_VERSION_1);\n    buttonTrap->setUDP(&amp;udp);\n    buttonTrap->setTrapOID(new OIDType(\".1.3.6.1.2.1.33\"));  \/\/ OID for the trap\n    buttonTrap->setSpecificTrap(1);\n      if (snmp.sendTrapTo(buttonTrap, IPAddress(192, x, x, x), true, 2, 5000) != INVALID_SNMP_REQUEST_ID) {\n        Serial.println(\"Sent SNMP Trap\");\n      } else {\n        Serial.println(\"Couldn't send SNMP Trap\");\n      }\n    }\n    \/\/OLED\n    display.clearDisplay();\n    display.setCursor(1, 5);\n    display.print(\"Cars: \");\n    display.print(cars_count);\n\n    cars.addField(\"cars_count\", cars_count);\n  \n     Serial.print(\"Writing: \");\n    Serial.println(cars.toLineProtocol());\n  \n    \/\/ Check WiFi connection\n    if (wifiMulti.run() != WL_CONNECTED) {\n      Serial.println(\"Wifi connection lost\");\n    }\n  \n    \/\/ Write point\n    if (!client.writePoint(cars)) {\n      Serial.print(\"InfluxDB write failed: \");\n      Serial.println(client.getLastErrorMessage());\n    }\n  \n    Serial.println(\"Waiting 1 second\");\n    display.display();\n    delay(1000);\n    }<\/code><\/pre>\n\n\n\n<p>This small project integrate IoT devices, databases, and visualization tools.<\/p>\n\n\n\n<p>The references are the documentation for the libraries and the code provided by influxdbv2 when we select this type of data dump.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this project, I developed a simulated parking monitoring system using two ESP8266 as the hosts devices. The ESP8266 is responsible for counting the number of cars in a parking lot and transmitting this information to an InfluxDB database and the use of Grafana for real-time visualization. Technologies: Small demo: [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":224,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[10,54,56,48,50,52],"class_list":["post-216","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blog","tag-esp8266","tag-grafana","tag-influxdb","tag-iot","tag-oled","tag-snmp"],"_links":{"self":[{"href":"https:\/\/cascajolabs.es\/index.php?rest_route=\/wp\/v2\/posts\/216","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cascajolabs.es\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cascajolabs.es\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cascajolabs.es\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cascajolabs.es\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=216"}],"version-history":[{"count":4,"href":"https:\/\/cascajolabs.es\/index.php?rest_route=\/wp\/v2\/posts\/216\/revisions"}],"predecessor-version":[{"id":244,"href":"https:\/\/cascajolabs.es\/index.php?rest_route=\/wp\/v2\/posts\/216\/revisions\/244"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/cascajolabs.es\/index.php?rest_route=\/wp\/v2\/media\/224"}],"wp:attachment":[{"href":"https:\/\/cascajolabs.es\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=216"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cascajolabs.es\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=216"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cascajolabs.es\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=216"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}