From 2e72d8dc427af0bc451236aa82dc02f59f525c73 Mon Sep 17 00:00:00 2001 From: Stefan Ostermann Date: Sun, 2 May 2021 15:01:09 +0200 Subject: [PATCH] Testing, device-id, esp-receiver configurable --- esp-receiver/.gitignore | 1 + esp-receiver/platformio.ini | 1 + esp-receiver/src/config.h.example | 11 +++++++ esp-receiver/src/main.cpp | 34 +++++++++++++-------- sensor433/test/test_desktop/test_sensor.cpp | 4 +-- 5 files changed, 36 insertions(+), 15 deletions(-) create mode 100644 esp-receiver/src/config.h.example diff --git a/esp-receiver/.gitignore b/esp-receiver/.gitignore index 89cc49c..64727e5 100644 --- a/esp-receiver/.gitignore +++ b/esp-receiver/.gitignore @@ -3,3 +3,4 @@ .vscode/c_cpp_properties.json .vscode/launch.json .vscode/ipch +config.h \ No newline at end of file diff --git a/esp-receiver/platformio.ini b/esp-receiver/platformio.ini index 380e7a5..8a6fd70 100644 --- a/esp-receiver/platformio.ini +++ b/esp-receiver/platformio.ini @@ -19,4 +19,5 @@ lib_deps = knolleary/PubSubClient@^2.8 bblanchon/ArduinoJson@^6.17.3 build_flags = -D USE_ESPRESSIF +upload_port = /dev/ttyUSB0 diff --git a/esp-receiver/src/config.h.example b/esp-receiver/src/config.h.example new file mode 100644 index 0000000..7cd796c --- /dev/null +++ b/esp-receiver/src/config.h.example @@ -0,0 +1,11 @@ +#ifndef __CONFIG_H__ +#define __CONFIG_H__ +/** + * For project specific settings. Copy to config.h and place your settings there. + **/ + +const char *MQTT_BROKER = "192.168.178.74"; +const char *MQTT_SENSOR_TOPIC = "living/sensor2"; +const char *MQTT_CLIENT_ID = "living_mobile_sensor"; + +#endif \ No newline at end of file diff --git a/esp-receiver/src/main.cpp b/esp-receiver/src/main.cpp index ffcbcf1..e6e35c1 100644 --- a/esp-receiver/src/main.cpp +++ b/esp-receiver/src/main.cpp @@ -29,14 +29,20 @@ uint8_t rh_buf[RH_BUF_LEN]; #define RH_ASK_MAX_MESSAGE_LEN RH_BUF_LEN + +/** + * If this file does not exist create it from the config.h.example file + * Place project specific settings here. It should not be checked in. + * */ +#include "config.h" + + 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"; + ESP8266WebServer server(80); WiFiClient espClient; @@ -44,13 +50,11 @@ PubSubClient mqttClient(MQTT_BROKER, 1883, espClient); RH_ASK driver(RH_SPEED, RH_RX_PIN); -/** - * Message identifier constants: - * */ -#define MSG_START 0x00 -#define MSG_BAT 0x03 -#define MSG_HEADER 0x05 -#define MSG_ERR 0xee +// Message Headers: +#define MSG_START 0x10 +#define MSG_BAT 0x20 +#define MSG_TEMPHUM 0x30 +#define MSG_ERR 0x40 char buff[20]; @@ -137,13 +141,17 @@ void loop() if (driver.recv(rh_buf, &len)) { Serial.println("Message received."); - switch (rh_buf[0]) + uint8_t header = rh_buf[0] & 0xf0; + uint8_t device_id = rh_buf[0] &0x0f; + Serial.print("Device ID: "); + Serial.println(device_id); + switch (rh_buf[0] & 0xf0) { case MSG_START: // start message - Serial.println("0x00 start byte"); + Serial.println("0x00 start byte, first message from sensor"); break; - case MSG_HEADER: + case MSG_TEMPHUM: // DHT Temperature digitalWrite(LED_BUILTIN, LOW); Serial.println("DHT Temp"); diff --git a/sensor433/test/test_desktop/test_sensor.cpp b/sensor433/test/test_desktop/test_sensor.cpp index 7dc39f1..9570314 100644 --- a/sensor433/test/test_desktop/test_sensor.cpp +++ b/sensor433/test/test_desktop/test_sensor.cpp @@ -20,10 +20,10 @@ void test_message_header() { TEST_ASSERT_EQUAL(0x11, header); // header - TEST_ASSERT_EQUAL(0x10, header & 0x10); + TEST_ASSERT_EQUAL(0x10, header & 0xf0); // id - TEST_ASSERT_EQUAL(0x01, header & 0x01); + TEST_ASSERT_EQUAL(0x01, header & 0x0f); }