Hi
I am building an indoor air quality monitoring project so I have a code,can someone help me to edit the code and send data to influx db then grafana for creating dashboard
I am just a student and building a college project ,so pls if someone know how to edit the code to update the sensor data to influx db pls help meโ:pray:
Components i am using
0.96 OLED display
Bme280
C03 sensor
ESP32 board
Pls help me
Anaisdg
September 21, 2022, 6:20pm
2
Hello @Thakur_Ashish ,
Welcome!
Have you seen the following resources:
And hereโs another example:
#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>
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>
#define WIFI_SSID "xxx"
#define WIFI_PASSWORD "xxx"
#define INFLUXDB_URL "https://us-west-2-1.aws.cloud2.influxdata.com"
#define INFLUXDB_TOKEN "UWixzECaEBqW1QU8r-ChF-SvVknccF39qCeMg7jzIogZyV9ZjgMBCnZ-K-LgJdoH77fRkthOmE7lbHQRfNahIA=="
#define INFLUXDB_ORG "0437f6d51b579000"
#define INFLUXDB_BUCKET "06021398b2fa0000"
#define TZ_INFO "GMT+0BST-1,M3.5.0/01:00:00,M10.5.0/02:00:00"
#define DHTPIN 12 // D7 on node mcu. Other pins get set HIGH/LOW during boot and seem to stop the sensor working until replugged.
#define DHTTYPE DHT22
InfluxDBClient client(INFLUXDB_URL, INFLUXDB_ORG, INFLUXDB_BUCKET, INFLUXDB_TOKEN, InfluxDbCloud2CACert);
Point influx_sensor("dht22"); // Set up Influx data point for sensor
DHT_Unified dht(DHTPIN, DHTTYPE); // create dht temperature sensor
void setup() {
Serial.begin(115200);
while(!Serial) { };
// Setup wifi
WiFi.mode(WIFI_STA);
wifiMulti.addAP(WIFI_SSID, WIFI_PASSWORD);
Serial.print("\n\nConnecting to wifi");
while (wifiMulti.run() != WL_CONNECTED) {
Serial.print(".");
delay(250);
}
Serial.println(F("Wifi Started"));
Serial.print(F("IP Address: "));
Serial.println(WiFi.localIP());
Serial.println(F("-------------------------------------"));
timeSync(TZ_INFO, "pool.ntp.org", "time.nis.gov");
//client.validateConnection(); // test to see if it fires an exception
// https://github.com/tobiasschuerg/InfluxDB-Client-for-Arduino/issues/86
// Check server connection // This was throwing exceptions all over the place for me
// if (client.validateConnection()) {
// Serial.print("Connected to InfluxDB: ");
// Serial.println(client.getServerUrl());
// } else {
// Serial.print("InfluxDB connection failed: ");
// Serial.println(client.getLastErrorMessage());
// }
dht.begin();
sensor_t temp_sensor;
dht.temperature().getSensor(&temp_sensor);
Serial.println(F("------------------------------------"));
Serial.println(F("Temperature Sensor"));
Serial.print (F("Sensor Type: ")); Serial.println(temp_sensor.name);
Serial.print (F("Driver Ver: ")); Serial.println(temp_sensor.version);
Serial.print (F("Unique ID: ")); Serial.println(temp_sensor.sensor_id);
Serial.print (F("Max Value: ")); Serial.print(temp_sensor.max_value); Serial.println(F("ยฐC"));
Serial.print (F("Min Value: ")); Serial.print(temp_sensor.min_value); Serial.println(F("ยฐC"));
Serial.print (F("Resolution: ")); Serial.print(temp_sensor.resolution); Serial.println(F("ยฐC"));
Serial.println(F("------------------------------------"));
dht.humidity().getSensor(&temp_sensor);
Serial.println(F("Humidity Sensor"));
Serial.print (F("Sensor Type: ")); Serial.println(temp_sensor.name);
Serial.print (F("Driver Ver: ")); Serial.println(temp_sensor.version);
Serial.print (F("Unique ID: ")); Serial.println(temp_sensor.sensor_id);
Serial.print (F("Max Value: ")); Serial.print(temp_sensor.max_value); Serial.println(F("%"));
Serial.print (F("Min Value: ")); Serial.print(temp_sensor.min_value); Serial.println(F("%"));
Serial.print (F("Resolution: ")); Serial.print(temp_sensor.resolution); Serial.println(F("%"));
Serial.println(F("------------------------------------"));
influx_sensor.addTag("user", "cool_plant_lady");
}
void loop() {
Serial.println("Wait 10s");
delay(10000);
sensors_event_t event; // DHT temperature sensor
dht.temperature().getEvent(&event);
if(isnan(event.temperature)) {
Serial.println(F("Error reading temperature!"));
} else {
Serial.print(F("Temperature: "));
Serial.print(event.temperature);
Serial.println(F("ยฐC"));
influx_sensor.addField("temperature", event.temperature); // I dont like using "temp" for temperature
}
dht.humidity().getEvent(&event);
if (isnan(event.relative_humidity)) {
Serial.println(F("Error reading humidity!"));
}
else {
Serial.print(F("Humidity: "));
Serial.print(event.relative_humidity);
Serial.println(F("%"));
influx_sensor.addField("humidity", event.temperature);
}
// Print what are we exactly writing
Serial.print("Writing: ");
Serial.println(influx_sensor.toLineProtocol());
// If no Wifi signal, try to reconnect it
if ((WiFi.RSSI() == 0) && (wifiMulti.run() != WL_CONNECTED)) {
Serial.println("Wifi connection lost");
}
// Write point
Serial.println("XX");
if (!client.writePoint(influx_sensor)) {
Serial.print("InfluxDB write failed: ");
Serial.println(client.getLastErrorMessage());
}
influx_sensor.clearFields();
}