All values in one message
This commit is contained in:
parent
09c96271c8
commit
fd9aad5e2f
|
|
@ -13,7 +13,7 @@
|
||||||
* Baud Rate for the 433 Mhz connection.
|
* Baud Rate for the 433 Mhz connection.
|
||||||
* Caution: Must be the same for sender & receiver!
|
* Caution: Must be the same for sender & receiver!
|
||||||
**/
|
**/
|
||||||
#define RH_SPEED 1000
|
#define RH_SPEED 300
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Pin for the receiver
|
* Pin for the receiver
|
||||||
|
|
@ -24,7 +24,7 @@
|
||||||
#define RH_RX_PIN D8
|
#define RH_RX_PIN D8
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#define RH_BUF_LEN 5
|
#define RH_BUF_LEN 11
|
||||||
uint8_t rh_buf[RH_BUF_LEN];
|
uint8_t rh_buf[RH_BUF_LEN];
|
||||||
|
|
||||||
#define RH_ASK_MAX_MESSAGE_LEN 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 AirValue = 800; //replace the value with value when placed in air
|
||||||
const int WaterValue = 345; //replace the value with value when placed in water
|
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_BROKER = "192.168.178.74";
|
||||||
const char *MQTT_SENSOR_TOPIC = "living/sensor2";
|
const char *MQTT_SENSOR_TOPIC = "living/sensor2";
|
||||||
const char *MQTT_CLIENT_ID = "living_mobile_sensor";
|
const char *MQTT_CLIENT_ID = "living_mobile_sensor";
|
||||||
|
|
@ -46,10 +48,8 @@ RH_ASK driver(RH_SPEED, RH_RX_PIN);
|
||||||
* Message identifier constants:
|
* Message identifier constants:
|
||||||
* */
|
* */
|
||||||
#define MSG_START 0x00
|
#define MSG_START 0x00
|
||||||
#define MSG_TEMP 0x01
|
|
||||||
#define MSG_HUMID 0x02
|
|
||||||
#define MSG_BAT 0x03
|
#define MSG_BAT 0x03
|
||||||
#define MSG_SOIL 0x04
|
#define MSG_HEADER 0x05
|
||||||
#define MSG_ERR 0xee
|
#define MSG_ERR 0xee
|
||||||
|
|
||||||
char buff[20];
|
char buff[20];
|
||||||
|
|
@ -79,6 +79,8 @@ void configModeCallback(WiFiManager *myWiFiManager)
|
||||||
|
|
||||||
void setup()
|
void setup()
|
||||||
{
|
{
|
||||||
|
|
||||||
|
pinMode(LED_BUILTIN, OUTPUT);
|
||||||
Serial.begin(9600);
|
Serial.begin(9600);
|
||||||
if (!driver.init())
|
if (!driver.init())
|
||||||
{
|
{
|
||||||
|
|
@ -120,6 +122,9 @@ void setup()
|
||||||
|
|
||||||
server.begin();
|
server.begin();
|
||||||
Serial.println("HTTP server started");
|
Serial.println("HTTP server started");
|
||||||
|
|
||||||
|
// switch build in led OFF (with HIGH);
|
||||||
|
digitalWrite(LED_BUILTIN, HIGH);
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop()
|
void loop()
|
||||||
|
|
@ -131,41 +136,40 @@ void loop()
|
||||||
|
|
||||||
if (driver.recv(rh_buf, &len))
|
if (driver.recv(rh_buf, &len))
|
||||||
{
|
{
|
||||||
|
Serial.println("Message received.");
|
||||||
switch (rh_buf[0])
|
switch (rh_buf[0])
|
||||||
{
|
{
|
||||||
case MSG_START:
|
case MSG_START:
|
||||||
// start message
|
// start message
|
||||||
Serial.println("0x00 start byte");
|
Serial.println("0x00 start byte");
|
||||||
break;
|
break;
|
||||||
case MSG_TEMP:
|
case MSG_HEADER:
|
||||||
// DHT Temperature
|
// DHT Temperature
|
||||||
Serial.println("0x01 DHT Temp");
|
digitalWrite(LED_BUILTIN, LOW);
|
||||||
|
Serial.println("DHT Temp");
|
||||||
memcpy(&Temperature, &rh_buf[1], 4);
|
memcpy(&Temperature, &rh_buf[1], 4);
|
||||||
Serial.print(Temperature);
|
Serial.print(Temperature);
|
||||||
Serial.println(" °C");
|
Serial.println(" °C");
|
||||||
break;
|
|
||||||
case MSG_HUMID:
|
|
||||||
// DHT Humidity:
|
// DHT Humidity:
|
||||||
Serial.println("0x02 DHT Humidity");
|
Serial.println("DHT Humidity");
|
||||||
memcpy(&Humidity, &rh_buf[1], 4);
|
memcpy(&Humidity, &rh_buf[5], 4);
|
||||||
Serial.print(Humidity);
|
Serial.print(Humidity);
|
||||||
Serial.println(" %");
|
Serial.println(" %");
|
||||||
break;
|
Serial.println("Soil Humidity");
|
||||||
case MSG_SOIL:
|
memcpy(&soil, &rh_buf[9], 2);
|
||||||
// Soil Humidity:
|
|
||||||
Serial.println("0x04 Soil Humidity");
|
|
||||||
|
|
||||||
memcpy(&soil, &rh_buf[1], 2);
|
|
||||||
Serial.println(soil);
|
Serial.println(soil);
|
||||||
SoilHumidity = (float)(map(soil, AirValue, WaterValue, 0, 100));
|
SoilHumidity = (float)(map(soil, AirValue, WaterValue, 0, 100));
|
||||||
Serial.println(SoilHumidity);
|
Serial.println(SoilHumidity);
|
||||||
|
new_value = true;
|
||||||
break;
|
break;
|
||||||
case MSG_BAT:
|
case MSG_BAT:
|
||||||
// battery data
|
// battery data
|
||||||
Serial.println("0x03 Battery");
|
digitalWrite(LED_BUILTIN, LOW);
|
||||||
|
Serial.println("Battery");
|
||||||
memcpy(&battery, &rh_buf[1], 4);
|
memcpy(&battery, &rh_buf[1], 4);
|
||||||
Serial.print(battery);
|
Serial.print(battery);
|
||||||
Serial.println(" mV");
|
Serial.println(" mV");
|
||||||
|
new_value = true;
|
||||||
break;
|
break;
|
||||||
case MSG_ERR:
|
case MSG_ERR:
|
||||||
// error
|
// error
|
||||||
|
|
@ -183,13 +187,17 @@ void loop()
|
||||||
reconnect();
|
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);
|
publishData(Temperature, Humidity, SoilHumidity, battery);
|
||||||
lastMillis = ms;
|
lastMillis = ms;
|
||||||
|
new_value = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
mqttClient.loop();
|
mqttClient.loop();
|
||||||
|
|
||||||
|
digitalWrite(LED_BUILTIN, HIGH);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void reconnect()
|
void reconnect()
|
||||||
|
|
|
||||||
|
|
@ -23,7 +23,7 @@
|
||||||
#define RH_OWN_ADDR 0xca // 202
|
#define RH_OWN_ADDR 0xca // 202
|
||||||
|
|
||||||
// RadioHead bitrate in bit/s
|
// RadioHead bitrate in bit/s
|
||||||
#define RH_SPEED 1000
|
#define RH_SPEED 300
|
||||||
|
|
||||||
/*
|
/*
|
||||||
define when to wake up
|
define when to wake up
|
||||||
|
|
@ -33,7 +33,7 @@
|
||||||
#define WATCHDOG_WAKEUP 9
|
#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
|
// 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
|
// pins for the radio hardware
|
||||||
#define RH_RX_PIN 6 // not used, set to a non-existent pin
|
#define RH_RX_PIN 6 // not used, set to a non-existent pin
|
||||||
|
|
@ -59,8 +59,8 @@
|
||||||
#include "dht22.h"
|
#include "dht22.h"
|
||||||
dht22 dht;
|
dht22 dht;
|
||||||
|
|
||||||
#define RH_BUF_LEN 5
|
#define RH_BUF_LEN 11
|
||||||
uint8_t rh_buf[RH_BUF_LEN] = {0x00, 0x00, 0x00, 0x00, 0x00};
|
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
|
// reduce the RadioHead max message length to save memory
|
||||||
#define RH_ASK_MAX_MESSAGE_LEN RH_BUF_LEN
|
#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_HUMID 0x02
|
||||||
#define MSG_BAT 0x03
|
#define MSG_BAT 0x03
|
||||||
#define MSG_SOIL 0x04
|
#define MSG_SOIL 0x04
|
||||||
|
#define MSG_HEADER 0x05
|
||||||
#define MSG_ERR 0xee
|
#define MSG_ERR 0xee
|
||||||
|
|
||||||
RH_ASK rh_driver(RH_SPEED, RH_RX_PIN, RH_TX_PIN, RH_PTT_PIN);
|
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)
|
else if (dht_read_data(&dht, &t, &h) == 1)
|
||||||
{
|
{
|
||||||
if (counter % 3 == 0)
|
rh_buf[0] = MSG_HEADER;
|
||||||
{
|
|
||||||
rh_buf[0] = MSG_TEMP;
|
|
||||||
memcpy(&rh_buf[1], &t, 4);
|
memcpy(&rh_buf[1], &t, 4);
|
||||||
}
|
memcpy(&rh_buf[5], &h, 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;
|
|
||||||
soil = analogRead(SOIL_PIN);
|
soil = analogRead(SOIL_PIN);
|
||||||
memcpy(&rh_buf[1], &soil, 2);
|
memcpy(&rh_buf[9], &soil, 2);
|
||||||
}
|
|
||||||
rh_send(RH_BUF_LEN);
|
rh_send(RH_BUF_LEN);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue