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:
- ESP8266 for car monitoring and counting.
- Grafana for real-time data visualization.
- InfluxDB as the database for storing information.
- SNMP traps for special event notifications that are sent to the manager.
Small demo:
The code is:
#include <Arduino.h>
//OLED
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
//SNMP
#include <WiFiUdp.h>
#include <SNMP_Agent.h>
const char* rocommunity = "public";
const char* rwcommunity = "private";
WiFiUDP udp;
SNMPAgent snmp = SNMPAgent(rocommunity, rwcommunity);
// Callback timestamp
TimestampType uptimeCallback;
//Influx
#if defined(ESP32)
#include <WiFiMulti.h>
WiFiMulti wifiMulti;
#define DEVICE "ESP32"
#elif defined(ESP8266)
#include <ESP8266WiFiMulti.h>
ESP8266WiFiMulti wifiMulti;
#define DEVICE "ESP8266"
#endif
#include <InfluxDbClient.h>
#include <InfluxDbCloud.h>
// WiFi AP SSID
#define WIFI_SSID "wifissid"
// WiFi password
#define WIFI_PASSWORD "pass"
#define INFLUXDB_URL "http://IP:8086"
#define INFLUXDB_TOKEN "----------"
#define INFLUXDB_ORG "------"
#define INFLUXDB_BUCKET "cars"
// Time zone info
#define TZ_INFO "UTC1"
// Declare InfluxDB client instance with preconfigured InfluxCloud certificate
InfluxDBClient client(INFLUXDB_URL, INFLUXDB_ORG, INFLUXDB_BUCKET, INFLUXDB_TOKEN, InfluxDbCloud2CACert);
// Declare Data point
Point cars("garage_one");
int cars_count=0;
// Buttons
const int pinIncrease = 12; // +
const int pinDecrease = 14; // -
void setup() {
Serial.begin(115200);
// buttons input
pinMode(pinIncrease, INPUT_PULLUP);
pinMode(pinDecrease, INPUT_PULLUP);
// Setup wifi
WiFi.mode(WIFI_STA);
wifiMulti.addAP(WIFI_SSID, WIFI_PASSWORD);
Serial.print("Connecting to wifi");
while (wifiMulti.run() != WL_CONNECTED) {
Serial.print(".");
delay(100);
}
Serial.println();
//timeSync(TZ_INFO, "pool.ntp.org", "time.nis.gov");
// Check server connection
if (client.validateConnection()) {
Serial.print("Connected to InfluxDB: ");
Serial.println(client.getServerUrl());
} else {
Serial.print("InfluxDB connection failed: ");
Serial.println(client.getLastErrorMessage());
}
// Add tags to the data point
cars.addTag("device", "floor1");
//OLED
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
for (;;)
;
}
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(1, 5);
display.display();
//SNMP
snmp.setUDP(&udp);
snmp.begin();
}
void loop() {
// Clear fields
cars.clearFields();
snmp.loop();
if (digitalRead(pinIncrease) == LOW) {
cars_count++;
if(cars_count>10){cars_count=10;}
delay(200);
}
else if (digitalRead(pinDecrease) == LOW) {
cars_count--;
if(cars_count<0){cars_count=0;}
delay(200);
}
//TRAP
if(cars_count==10){
SNMPTrap* buttonTrap = new SNMPTrap("public", SNMP_VERSION_1);
buttonTrap->setUDP(&udp);
buttonTrap->setTrapOID(new OIDType(".1.3.6.1.2.1.33")); // OID for the trap
buttonTrap->setSpecificTrap(1);
if (snmp.sendTrapTo(buttonTrap, IPAddress(192, x, x, x), true, 2, 5000) != INVALID_SNMP_REQUEST_ID) {
Serial.println("Sent SNMP Trap");
} else {
Serial.println("Couldn't send SNMP Trap");
}
}
//OLED
display.clearDisplay();
display.setCursor(1, 5);
display.print("Cars: ");
display.print(cars_count);
cars.addField("cars_count", cars_count);
Serial.print("Writing: ");
Serial.println(cars.toLineProtocol());
// Check WiFi connection
if (wifiMulti.run() != WL_CONNECTED) {
Serial.println("Wifi connection lost");
}
// Write point
if (!client.writePoint(cars)) {
Serial.print("InfluxDB write failed: ");
Serial.println(client.getLastErrorMessage());
}
Serial.println("Waiting 1 second");
display.display();
delay(1000);
}
This small project integrate IoT devices, databases, and visualization tools.
The references are the documentation for the libraries and the code provided by influxdbv2 when we select this type of data dump.