MAIN CODE FOR ARDUINO ESP8266
#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 "DHT.h"
#define DHTPIN 4 // Digital pin connected to the DHT sensor
#define DHTTYPE DHT11 // DHT 11
// Set up your WiFi connection here
// WiFi AP SSID
#define WIFI_SSID ""
// WiFi password
#define WIFI_PASSWORD ""
// Set up your InfluxDB details here
// InfluxDB v2 server url (Use: InfluxDB UI -> Load Data -> Client Libraries)
#define INFLUXDB_URL ""
// InfluxDB v2 server or cloud API authentication token (Use: InfluxDB UI -> Data -> Tokens -> <select token>)
#define INFLUXDB_TOKEN ""
// InfluxDB v2 organization id (Use: InfluxDB UI -> User -> About -> Common Ids )
#define INFLUXDB_ORG ""
// InfluxDB v2 bucket name (Use: InfluxDB UI -> Data -> Buckets)
#define INFLUXDB_BUCKET ""
// Set timezone string according to https://www.gnu.org/software/libc/manual/html_node/TZ-Variable.html
#define TZ_INFO "CET-1CEST,M3.5.0,M10.5.0/3"
// InfluxDB client instance with preconfigured InfluxCloud certificate
InfluxDBClient client(INFLUXDB_URL, INFLUXDB_ORG, INFLUXDB_BUCKET, INFLUXDB_TOKEN, InfluxDbCloud2CACert);
// Create your Data Point here
Point sensor("climate");
Point wifi("wifi_status");
// Set up the DHT connection
DHT dht(DHTPIN, DHTTYPE);
void setup() {
// Start Serial for monitoring
Serial.begin(115200);
// Init the DHT sensor
dht.begin();
// 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();
// Add tags. Here we will track which device our data is coming from
sensor.addTag("device", DEVICE);
wifi.addTag("device", DEVICE);
wifi.addTag("SSID", WiFi.SSID());
// Accurate time is necessary for certificate validation and writing in batches
// For the fastest time sync find NTP servers in your area: 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());
}
}
void loop() {
// Clear fields for reusing the point. Tags will remain untouched
sensor.clearFields();
wifi.clearFields();
// Read the temperature and humidity from the sensor, and add them to your data point, along with a calculated heat index
float h = dht.readHumidity();
float t = dht.readTemperature();
sensor.addField("humidity", h);
sensor.addField("temperature", t);
sensor.addField("heat_index", dht.computeHeatIndex(t, h, false));
wifi.addField("rssi", WiFi.RSSI());
// Print what are we exactly writing
Serial.print("Writing: ");
Serial.println(sensor.toLineProtocol());
Serial.print("Writing: ");
Serial.println(wifi.toLineProtocol());
// If no Wifi signal, try to reconnect it
if ((WiFi.RSSI() == 0) && (wifiMulti.run() != WL_CONNECTED)) {
Serial.println("Wifi connection lost");
}
// Write point
if (!client.writePoint(sensor)) {
Serial.print("InfluxDB write failed: ");
Serial.println(client.getLastErrorMessage());
//Wait 10s
Serial.println("Wait 10s");
delay(10000);
}}
CODE FOR RAIN SENSOR
#define DIGITAL_PIN 3
#define ANALOG_PIN 0
#define SENSOR_POWER 2
uint16_t rainVal;
boolean isRaining = false;
String raining;
void setup() {
Serial.begin(9600);
pinMode(DIGITAL_PIN, INPUT);
pinMode(SENSOR_POWER, OUTPUT);
digitalWrite(SENSOR_POWER, LOW);
}
void loop() {
digitalWrite(SENSOR_POWER, HIGH);
delay(10);
rainVal = analogRead(ANALOG_PIN);
isRaining = digitalRead(DIGITAL_PIN);
digitalWrite(SENSOR_POWER, LOW);
if (isRaining) {
raining = "No";
}
else {
raining = "Yes";
}
rainVal = map(rainVal, 0, 1023, 100, 0);
Serial.print("Raining: ");
Serial.println(raining);
Serial.print("Moisture: ");
Serial.print(rainVal);
Serial.println("%\n");
delay(1000);
}```
THIS IS WHAT I DID BUT THE RAIN SENSOR DOESNT WORK
#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 "DHT.h"
#define DHTPIN 4 // Digital pin connected to the DHT sensor
#define DHTTYPE DHT11 // DHT 11
#define DIGITAL_PIN 3
#define ANALOG_PIN 0
#define SENSOR_POWER 2
uint16_t rainVal;
boolean isRaining = false;
String raining;
// Set up your WiFi connection here
// WiFi AP SSID
#define WIFI_SSID ""
// WiFi password
#define WIFI_PASSWORD ""
// Set up your InfluxDB details here
// InfluxDB v2 server url (Use: InfluxDB UI -> Load Data -> Client Libraries)
#define INFLUXDB_URL ""
// InfluxDB v2 server or cloud API authentication token (Use: InfluxDB UI -> Data -> Tokens -> <select token>)
#define INFLUXDB_TOKEN ""
// InfluxDB v2 organization id (Use: InfluxDB UI -> User -> About -> Common Ids )
#define INFLUXDB_ORG ""
// InfluxDB v2 bucket name (Use: InfluxDB UI -> Data -> Buckets)
#define INFLUXDB_BUCKET ""
// Set timezone string according to https://www.gnu.org/software/libc/manual/html_node/TZ-Variable.html
#define TZ_INFO "CET-1CEST,M3.5.0,M10.5.0/3"
// InfluxDB client instance with preconfigured InfluxCloud certificate
InfluxDBClient client(INFLUXDB_URL, INFLUXDB_ORG, INFLUXDB_BUCKET, INFLUXDB_TOKEN, InfluxDbCloud2CACert);
// Create your Data Point here
Point sensor("climate");
Point rain("rain");
// Set up the DHT connection
DHT dht(DHTPIN, DHTTYPE);
void setup() {
// Start Serial for monitoring
Serial.begin(115200);
// Init the DHT sensor
dht.begin();
// 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();
// Add tags. Here we will track which device our data is coming from
sensor.addTag("device", DEVICE);
rain.addTag("device", DEVICE);
// Accurate time is necessary for certificate validation and writing in batches
// For the fastest time sync find NTP servers in your area: 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());
}
}
void loop() {
// Clear fields for reusing the point. Tags will remain untouched
sensor.clearFields();
rain.clearFields();
digitalWrite(SENSOR_POWER, HIGH);
delay(10);
rainVal = analogRead(ANALOG_PIN);
isRaining = digitalRead(DIGITAL_PIN);
digitalWrite(SENSOR_POWER, LOW);
if (isRaining) {
raining = "No";
}
else {
raining = "Yes";
}
// Read the temperature and humidity from the sensor, and add them to your data point, along with a calculated heat index
float h = dht.readHumidity();
float t = dht.readTemperature();
float r = rainVal;
float rr = isRaining;
sensor.addField("humidity", h);
sensor.addField("temperature", t);
sensor.addField("heat_index", dht.computeHeatIndex(t, h, false));
rain.addField("rain", r);
rain.addField("raining", rr);
// Print what are we exactly writing
Serial.print("Writing: ");
Serial.println(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
if (!client.writePoint(sensor)) {
Serial.print("InfluxDB write failed: ");
Serial.println(client.getLastErrorMessage());
if (!client.writePoint(rain)) {
Serial.print("InfluxDB write failed: ");
Serial.println(client.getLastErrorMessage());
}
}
//Wait 10s
Serial.println("Wait 10s");
delay(10000);
} ,,,