diff --git a/esp-receiver/src/main.cpp b/esp-receiver/src/main.cpp index 5adbb62..e5496fb 100644 --- a/esp-receiver/src/main.cpp +++ b/esp-receiver/src/main.cpp @@ -13,7 +13,7 @@ * Baud Rate for the 433 Mhz connection. * Caution: Must be the same for sender & receiver! **/ -#define RH_SPEED 1000 +#define RH_SPEED 300 /** * Pin for the receiver @@ -24,7 +24,7 @@ #define RH_RX_PIN D8 #endif -#define RH_BUF_LEN 5 +#define RH_BUF_LEN 11 uint8_t rh_buf[RH_BUF_LEN]; #define RH_ASK_MAX_MESSAGE_LEN RH_BUF_LEN @@ -32,6 +32,8 @@ uint8_t rh_buf[RH_BUF_LEN]; const int AirValue = 800; //replace the value with value when placed in air const int WaterValue = 345; //replace the value with value when placed in water +volatile boolean new_value = false; + const char *MQTT_BROKER = "192.168.178.74"; const char *MQTT_SENSOR_TOPIC = "living/sensor2"; const char *MQTT_CLIENT_ID = "living_mobile_sensor"; @@ -46,10 +48,8 @@ RH_ASK driver(RH_SPEED, RH_RX_PIN); * Message identifier constants: * */ #define MSG_START 0x00 -#define MSG_TEMP 0x01 -#define MSG_HUMID 0x02 #define MSG_BAT 0x03 -#define MSG_SOIL 0x04 +#define MSG_HEADER 0x05 #define MSG_ERR 0xee char buff[20]; @@ -79,6 +79,8 @@ void configModeCallback(WiFiManager *myWiFiManager) void setup() { + + pinMode(LED_BUILTIN, OUTPUT); Serial.begin(9600); if (!driver.init()) { @@ -120,6 +122,9 @@ void setup() server.begin(); Serial.println("HTTP server started"); + + // switch build in led OFF (with HIGH); + digitalWrite(LED_BUILTIN, HIGH); } void loop() @@ -131,41 +136,40 @@ void loop() if (driver.recv(rh_buf, &len)) { + Serial.println("Message received."); switch (rh_buf[0]) { case MSG_START: // start message Serial.println("0x00 start byte"); break; - case MSG_TEMP: + case MSG_HEADER: // DHT Temperature - Serial.println("0x01 DHT Temp"); + digitalWrite(LED_BUILTIN, LOW); + Serial.println("DHT Temp"); memcpy(&Temperature, &rh_buf[1], 4); Serial.print(Temperature); Serial.println(" °C"); - break; - case MSG_HUMID: // DHT Humidity: - Serial.println("0x02 DHT Humidity"); - memcpy(&Humidity, &rh_buf[1], 4); + Serial.println("DHT Humidity"); + memcpy(&Humidity, &rh_buf[5], 4); Serial.print(Humidity); Serial.println(" %"); - break; - case MSG_SOIL: - // Soil Humidity: - Serial.println("0x04 Soil Humidity"); - - memcpy(&soil, &rh_buf[1], 2); + Serial.println("Soil Humidity"); + memcpy(&soil, &rh_buf[9], 2); Serial.println(soil); SoilHumidity = (float)(map(soil, AirValue, WaterValue, 0, 100)); Serial.println(SoilHumidity); + new_value = true; break; case MSG_BAT: // battery data - Serial.println("0x03 Battery"); + digitalWrite(LED_BUILTIN, LOW); + Serial.println("Battery"); memcpy(&battery, &rh_buf[1], 4); Serial.print(battery); Serial.println(" mV"); + new_value = true; break; case MSG_ERR: // error @@ -183,13 +187,17 @@ void loop() reconnect(); } - if (ms - lastMillis > 5000 && Temperature != 0.0 && Humidity != 0.0 && SoilHumidity != 0.0) + if (ms - lastMillis > 5000 && new_value) { publishData(Temperature, Humidity, SoilHumidity, battery); lastMillis = ms; + new_value = false; } mqttClient.loop(); + + digitalWrite(LED_BUILTIN, HIGH); + } void reconnect() diff --git a/sensor433/src/main.cpp b/sensor433/src/main.cpp index ede8810..e09c319 100644 --- a/sensor433/src/main.cpp +++ b/sensor433/src/main.cpp @@ -23,7 +23,7 @@ #define RH_OWN_ADDR 0xca // 202 // RadioHead bitrate in bit/s -#define RH_SPEED 1000 +#define RH_SPEED 300 /* define when to wake up @@ -33,7 +33,7 @@ #define WATCHDOG_WAKEUP 9 // Sleep this many again before waging up. Sleep time in s = WATCHDOG_WAKEUP s * WATCHDOG_SLEEP_FURTHER, e.g. 8*8s = 64s -#define WATCHDOG_SLEEP_FURTHER 8 +#define WATCHDOG_SLEEP_FURTHER 2 // pins for the radio hardware #define RH_RX_PIN 6 // not used, set to a non-existent pin @@ -59,8 +59,8 @@ #include "dht22.h" dht22 dht; -#define RH_BUF_LEN 5 -uint8_t rh_buf[RH_BUF_LEN] = {0x00, 0x00, 0x00, 0x00, 0x00}; +#define RH_BUF_LEN 11 +uint8_t rh_buf[RH_BUF_LEN] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; // reduce the RadioHead max message length to save memory #define RH_ASK_MAX_MESSAGE_LEN RH_BUF_LEN @@ -74,6 +74,7 @@ uint8_t rh_buf[RH_BUF_LEN] = {0x00, 0x00, 0x00, 0x00, 0x00}; #define MSG_HUMID 0x02 #define MSG_BAT 0x03 #define MSG_SOIL 0x04 +#define MSG_HEADER 0x05 #define MSG_ERR 0xee RH_ASK rh_driver(RH_SPEED, RH_RX_PIN, RH_TX_PIN, RH_PTT_PIN); @@ -151,22 +152,12 @@ void loop() } else if (dht_read_data(&dht, &t, &h) == 1) { - if (counter % 3 == 0) - { - rh_buf[0] = MSG_TEMP; + rh_buf[0] = MSG_HEADER; memcpy(&rh_buf[1], &t, 4); - } - else if (counter % 3 == 1) - { - rh_buf[0] = MSG_HUMID; - memcpy(&rh_buf[1], &h, 4); - } - else if (counter % 3 == 2) - { - rh_buf[0] = MSG_SOIL; + memcpy(&rh_buf[5], &h, 4); soil = analogRead(SOIL_PIN); - memcpy(&rh_buf[1], &soil, 2); - } + memcpy(&rh_buf[9], &soil, 2); + rh_send(RH_BUF_LEN); } else