#include <Adafruit_Sensor.h>
#include <DHT_U.h>
#include <WiFiMulti.h>
WiFiMulti wifiMulti;
#define DEVICE "ESP32 KITCHEN"
#include <InfluxDbClient.h>
#include <InfluxDbCloud.h>
// WiFi AP SSID
#define WIFI_SSID "Wokwi-GUEST"
// WiFi password
#define WIFI_PASSWORD ""
#define INFLUXDB_URL "https://us-east-1-1.aws.cloud2.influxdata.com"
#define INFLUXDB_TOKEN "zqWno8y6-rjuqfG9m2vId3AVrMuRrYl3DnwvtMMPz1aFIGf7ZGOSW1HJTvvIBamtoqYilNSu53Jff8noeAk2RA=="
#define INFLUXDB_ORG "b0522690979acb52"
#define INFLUXDB_BUCKET "House data"
// Defining Pins
#define DHTPIN 12
#define LDR 4
#define motionsensor 14
// DHT parameters
#define DHTTYPE DHT22 // DHT 11
DHT_Unified dht(DHTPIN, DHTTYPE);
uint32_t delayMS;
// 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 dbdata("Kitchen_data");
void setup() {
Serial.begin(115200);
// 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();
// Initialize device.
dht.begin();
// Get temperature sensor details.
sensor_t sensor;
dht.temperature().getSensor(&sensor);
dht.humidity().getSensor(&sensor);
pinMode(LDR, INPUT);
pinMode(motionsensor,INPUT);
// Accurate time is necessary for certificate validation and writing in batches
// We use the NTP servers in your area as provided by: https://www.pool.ntp.org/zone/
// Syncing progress and the time will be printed to Serial.
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());
}
dbdata.addTag("device", DEVICE);
dbdata.addTag("SSID", WiFi.SSID());
}
void loop() {
// Clear fields for reusing the point. Tags will remain the same as set above.
dbdata.clearFields();
// Store measured value into point
// Report RSSI of currently connected network
sensors_event_t event;
dht.temperature().getEvent(&event);
if (!isnan(event.temperature) && !isnan(event.relative_humidity)) {
dbdata.addField("rssi", WiFi.RSSI());
dbdata.addField("temperature",event.temperature);
dbdata.addField("humidity",event.relative_humidity);
dbdata.addField("motion_sensor",digitalRead(motionsensor));
dbdata.addField("light",analogRead(LDR));
// Print what are we exactly writing
Serial.print("Writing: ");
Serial.println(dbdata.toLineProtocol());
}
// Check WiFi connection and reconnect if needed
if (wifiMulti.run() != WL_CONNECTED) {
Serial.println("Wifi connection lost");
}
// Write point
if (!client.writePoint(dbdata)) {
Serial.print("InfluxDB write failed: ");
Serial.println(client.getLastErrorMessage());
}
Serial.println("Waiting 1 second");
delay(1000);
}
// #include <WiFi.h>
// #include <PubSubClient.h>
// #include <ArduinoJson.h> // Include the ArduinoJson library for JSON handling
// // MQTT Credentials
// const char* ssid = "Wokwi-GUEST"; // Setting your AP SSID
// const char* password = ""; // Setting your AP PSK
// const char* mqttServer = "broker.hivemq.com";
// // const char* mqttUserName = "";
// // const char* mqttPwd = "";
// const char* clientID = "kitchen-mqtt"; // Client ID username+0001
// const char* topic = "/house/kitchen-001"; // Publish topic
// // Parameters for using non-blocking delay
// unsigned long previousMillis = 0;
// const long interval = 10000;
// String msgStr = ""; // MQTT message buffer
// float temp, hum;
// // Setting up WiFi and MQTT client
// WiFiClient espClient;
// PubSubClient client(espClient);
// void setup_wifi() {
// delay(10);
// WiFi.begin(ssid, password);
// while (WiFi.status() != WL_CONNECTED) {
// delay(500);
// Serial.print(".");
// }
// Serial.println("");
// Serial.println("WiFi connected");
// Serial.println("IP address: ");
// Serial.println(WiFi.localIP());
// }
// void reconnect() {
// while (!client.connected()) {
// if (client.connect(clientID)) {
// // if (client.connect(clientID,mqttUserName, mqttPwd)) {
// Serial.println("MQTT connected");
// // client.subscribe("lights");
// // client.subscribe("servo"); // Subscribe to servo topic
// // Serial.println("Topic Subscribed");
// }
// else {
// Serial.print("failed, rc=");
// Serial.print(client.state());
// Serial.println(" try again in 5 seconds");
// delay(5000); // wait 5sec and retry
// }
// }
// }
// // // Subscribe callback
// // void callback(char* topic, byte* payload, unsigned int length) {
// // Serial.print("Message arrived in topic: ");
// // Serial.println(topic);
// // Serial.print("Message: ");
// // String data = "";
// // for (int i = 0; i < length; i++) {
// // Serial.print((char)payload[i]);
// // data += (char)payload[i];
// // }
// // Serial.println();
// // Serial.print("Message size: ");
// // Serial.println(length);
// // Serial.println();
// // Serial.println("-----------------------");
// // Serial.println(data);
// // // if (String(topic) == "lights") {
// // // if (data == "ON") {
// // // Serial.println("LED");
// // // digitalWrite(LED, HIGH);
// // // }
// // // else {
// // // digitalWrite(LED, LOW);
// // // }
// // // }
// // // else if (String(topic) == "servo") {
// // // int degree = data.toInt(); // Convert the received data to an integer
// // // Serial.print("Moving servo to degree: ");
// // // Serial.println(degree);
// // // servo.write(degree); // Move the servo to the specified degree
// // // }
// // }
// void setup() {
// Serial.begin(115200);
// setup_wifi();
// client.setServer(mqttServer, 1883); // Setting MQTT server
// // client.setCallback(callback); // Define function which will be called when a message is received.
// }
// StaticJsonDocument<256> jsonDoc;
// void loop() {
// if (!client.connected()) { // If the client is not connected
// reconnect(); // Try to reconnect
// }
// client.loop();
// unsigned long currentMillis = millis(); // Read current time
// if (currentMillis - previousMillis >= interval) { // If current time - last time > 5 sec
// previousMillis = currentMillis;
// // Read temperature and humidity
// String jsonStr;
// serializeJson(jsonDoc, jsonStr); // Serialize JSON to a string
// Serial.print("PUBLISH JSON DATA: ");
// Serial.println(jsonStr);
// client.publish(topic, jsonStr.c_str()); // Publish JSON data
// } else {
// Serial.println("Error reading temperature or humidity!");
// }
// delay(1);
// }
// }