From 26d0aa5a3d6ca68c288311beb17017d6e0631da7 Mon Sep 17 00:00:00 2001 From: Emmanuel Lacour Date: Wed, 3 Jan 2024 12:29:20 +0100 Subject: [PATCH] Add "grande chambre" (using esp8266 nodemcu) --- grande-chambre/grande-chambre-config.h.in | 13 +++++ grande-chambre/grande-chambre.ino | 83 +++++++++++++++++++++++++++++++ 2 files changed, 96 insertions(+) create mode 100644 grande-chambre/grande-chambre-config.h.in create mode 100644 grande-chambre/grande-chambre.ino diff --git a/grande-chambre/grande-chambre-config.h.in b/grande-chambre/grande-chambre-config.h.in new file mode 100644 index 0000000..4a1402d --- /dev/null +++ b/grande-chambre/grande-chambre-config.h.in @@ -0,0 +1,13 @@ +// Enable debug on serial port +#define DEBUG 1 +// Wifi +#define WIFI_SSID "XXXXXX" +#define WIFI_PASS "xxxxxx" +// InfluxDB +#define INFLUXDB_IP {w,x,y,z} +#define INFLUXDB_PORT xxxx +// Polling interval (ms) +#define POLL_INT 10000 +// Location informations +#define CITY "xxxxx" +#define LOCATION "xxxxx" diff --git a/grande-chambre/grande-chambre.ino b/grande-chambre/grande-chambre.ino new file mode 100644 index 0000000..0d36ca5 --- /dev/null +++ b/grande-chambre/grande-chambre.ino @@ -0,0 +1,83 @@ +// Board ESP8266 - NodeMCU 1.0 (ESP-12E module) +#include +#include +#include +#include +#include +#include + +#include "grande-chambre-config.h" + +const char* wifi_ssid = WIFI_SSID; +const char* wifi_pass = WIFI_PASS; + +// Grafana +IPAddress influxdb_ip(INFLUXDB_IP); +long influxdb_port = INFLUXDB_PORT; +unsigned int localPort = 2390; +// MilliSeconds +long unsigned poll_int = POLL_INT; +String city = CITY; +String location = LOCATION; +WiFiUDP Udp; + +// Temp +// 5 = D1 on esp8266 +#define DHTPIN 5 +#define DHTTYPE DHT22 +DHT_Unified dht(DHTPIN, DHTTYPE); + +void setup() { + + Serial.begin(9600); + // Not for esp8266 WiFi.mode(WIFI_STA); + WiFi.begin(WIFI_SSID, WIFI_PASS); + while (WiFi.status() != WL_CONNECTED) { + delay(500); + } + + dht.begin(); + sensor_t sensor; + dht.temperature().getSensor(&sensor); + dht.humidity().getSensor(&sensor); + +} + +void loop() { + delay(POLL_INT); + // Check wifi connexion + if ( WiFi.status() != WL_CONNECTED ) { + int retry = 0; + // Not for esp8266 WiFi.mode(WIFI_STA); + WiFi.begin(WIFI_SSID, WIFI_PASS); + while (retry < 10 || WiFi.status() != WL_CONNECTED) { + retry++; + delay(500); + } + } + + if ( WiFi.status() == WL_CONNECTED ) { + sensors_event_t event; + + dht.temperature().getEvent(&event); + if (isnan(event.temperature)) { + // Error + } else { + sendToInfluxDB("temperature,city="+city+",location="+location, "value", String(event.temperature)); + } + dht.humidity().getEvent(&event); + if (isnan(event.relative_humidity)) { + // Error + } else { + sendToInfluxDB("humidity,city="+city+",location="+location, "value", String(event.relative_humidity)); + } + } +} + +void sendToInfluxDB(String measure, String key, String value) { + String line = measure+" "+key+"="+value; + Udp.begin(localPort); + Udp.beginPacket(influxdb_ip, influxdb_port); + Udp.print(line); + Udp.endPacket(); +} -- 2.11.0