All values in one message

This commit is contained in:
2021-04-15 22:36:26 +02:00
parent 09c96271c8
commit fd9aad5e2f
2 changed files with 36 additions and 37 deletions

View File

@@ -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()