From 4115f2a723f6d3d981cb7aaf42d52bd725417560 Mon Sep 17 00:00:00 2001 From: Martin Korbel Date: Mon, 20 Jan 2020 21:04:59 +0100 Subject: [PATCH] Add support for weather stations (#532) * Add support for weather stations * Add configuration for ESP8266 --- main/User_config.h | 1 + main/ZgatewayWeatherStation.ino | 159 ++++++++++++++++++++++++++++++++ main/config_WeatherStation.h | 52 +++++++++++ main/main.ino | 12 +++ platformio.ini | 27 ++++++ 5 files changed, 251 insertions(+) create mode 100644 main/ZgatewayWeatherStation.ino create mode 100644 main/config_WeatherStation.h diff --git a/main/User_config.h b/main/User_config.h index 27668a4..6a86c40 100644 --- a/main/User_config.h +++ b/main/User_config.h @@ -115,6 +115,7 @@ char gateway_name[parameters_size * 2] = Gateway_Name; //#define ZgatewayIR "IR" //ESP8266, Arduino, Sonoff RF Bridge //#define ZgatewayLORA "LORA" //ESP8266, Arduino, ESP32 //#define ZgatewayPilight "Pilight" //ESP8266, Arduino, ESP32 +//#define ZgatewayWeatherStation "WeatherStation" //ESP8266, Arduino, ESP32 //#define ZgatewayBT "BT" //ESP8266, ESP32 //#define ZgatewayRF2 "RF2" //ESP8266, Arduino, ESP32 //#define ZgatewaySRFB "SRFB" // Sonoff RF Bridge diff --git a/main/ZgatewayWeatherStation.ino b/main/ZgatewayWeatherStation.ino new file mode 100644 index 0000000..e75a480 --- /dev/null +++ b/main/ZgatewayWeatherStation.ino @@ -0,0 +1,159 @@ +/* + OpenMQTTGateway - ESP8266 or Arduino program for home automation + + Act as a wifi or ethernet gateway between your 433mhz/infrared IR signal and a MQTT broker + Send and receiving command by MQTT + + This gateway enables to: + - publish MQTT data, which are received from RF433Mhz signal (based on WeatherStationDataRx) + + Library for read weather data from Venus W174/W132, Auriol H13726, Hama EWS 1500, Meteoscan W155/W160 + + This file is part of OpenMQTTGateway. + + OpenMQTTGateway is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + OpenMQTTGateway is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ +#include "User_config.h" + +#ifdef ZgatewayWeatherStation +#include +WeatherStationDataRx wsdr(RF_WS_RECEIVER_PIN, true); + +void PairedDeviceAdded(byte newID) +{ +#if defined(ESP8266) || defined(ESP32) + Serial.printf("ZgatewayWeatherStation: New device paired %d\r\n", newID); +#else + Serial.print("ZgatewayWeatherStation: New device paired "); + Serial.println(newID, DEC); +#endif + const int JSON_MSG_CALC_BUFFER = JSON_OBJECT_SIZE(2); + StaticJsonBuffer jsonBuffer; + JsonObject &RFdata = jsonBuffer.createObject(); + RFdata.set("sensor", newID); + RFdata.set("action", "paried"); + pub(subjectRFtoMQTT, RFdata); + wsdr.pair(NULL, PairedDeviceAdded); +} + +void setupWeatherStation() +{ + trc(F("RF_WS_RECEIVER_PIN ")); + trc(String(RF_WS_RECEIVER_PIN)); + wsdr.begin(); + wsdr.pair(NULL, PairedDeviceAdded); + trc(F("ZgatewayWeatherStation setup done ")); +} + +void sendWindSpeedData(byte id, float wind_speed, byte battery_status) +{ + unsigned long MQTTvalue = 10000 + round(wind_speed); + if (!isAduplicate(MQTTvalue)) + { // conditions to avoid duplications of RF -->MQTT + const int JSON_MSG_CALC_BUFFER = JSON_OBJECT_SIZE(3); + StaticJsonBuffer jsonBuffer; + JsonObject &RFdata = jsonBuffer.createObject(); + RFdata.set("sensor", id); + RFdata.set("wind_speed", wind_speed); + RFdata.set("battery", bitRead(battery_status, 0) == 0 ? "OK" : "Low"); + pub(subjectRFtoMQTT, RFdata); + trc(F("Store wind speed val")); + storeValue(MQTTvalue); + } +} + +void sendRainData(byte id, float rain_volume, byte battery_status) +{ + unsigned long MQTTvalue = 11000 + round(rain_volume * 10.0); + if (!isAduplicate(MQTTvalue)) + { // conditions to avoid duplications of RF -->MQTT + const int JSON_MSG_CALC_BUFFER = JSON_OBJECT_SIZE(3); + StaticJsonBuffer jsonBuffer; + JsonObject &RFdata = jsonBuffer.createObject(); + RFdata.set("sensor", id); + RFdata.set("rain_volume", rain_volume); + RFdata.set("battery", bitRead(battery_status, 1) == 0 ? "OK" : "Low"); + pub(subjectRFtoMQTT, RFdata); + trc(F("Store rain_volume")); + storeValue(MQTTvalue); + } +} + +void sendWindData(byte id, int wind_direction, float wind_gust, byte battery_status) +{ + unsigned long MQTTvalue = 20000 + round(wind_gust * 10.0) + wind_direction; + if (!isAduplicate(MQTTvalue)) + { // conditions to avoid duplications of RF -->MQTT + const int JSON_MSG_CALC_BUFFER = JSON_OBJECT_SIZE(4); + StaticJsonBuffer jsonBuffer; + JsonObject &RFdata = jsonBuffer.createObject(); + RFdata.set("sensor", id); + RFdata.set("wind_direction", wind_direction); + RFdata.set("wind_gust", wind_gust); + RFdata.set("battery", bitRead(battery_status, 0) == 0 ? "OK" : "Low"); + pub(subjectRFtoMQTT, RFdata); + trc(F("Store wind data val")); + storeValue(MQTTvalue); + } +} + +void sendTemperatureData(byte id, float temperature, int humidity, byte battery_status) +{ + unsigned long MQTTvalue = 40000 + abs(round(temperature * 100.0)) + humidity; + if (!isAduplicate(MQTTvalue)) + { // conditions to avoid duplications of RF -->MQTT + const int JSON_MSG_CALC_BUFFER = JSON_OBJECT_SIZE(4); + StaticJsonBuffer jsonBuffer; + JsonObject &RFdata = jsonBuffer.createObject(); + RFdata.set("sensor", id); + RFdata.set("temperature", temperature); + RFdata.set("humidity", humidity); + RFdata.set("battery", bitRead(battery_status, 0) == 0 ? "OK" : "Low"); + pub(subjectRFtoMQTT, RFdata); + trc(F("Store temperature val")); + storeValue(MQTTvalue); + } +} + +void ZgatewayWeatherStationtoMQTT() +{ + char newData = wsdr.readData(); + switch (newData) + { + case 'T': + trc(F("Temperature")); + sendTemperatureData(wsdr.sensorID(), wsdr.readTemperature(), wsdr.readHumidity(), wsdr.batteryStatus()); + break; + + case 'S': + trc(F("Wind speed")); + sendWindSpeedData(wsdr.sensorID(), wsdr.readWindSpeed(), wsdr.batteryStatus()); + break; + + case 'G': + trc(F("Wind direction")); + sendWindData(wsdr.sensorID(), wsdr.readWindDirection(), wsdr.readWindGust(), wsdr.batteryStatus()); + break; + + case 'R': + trc(F("Rain volume")); + sendRainData(wsdr.sensorID(), wsdr.readRainVolume(), wsdr.batteryStatus()); + break; + + default: + break; + } + +} + +#endif diff --git a/main/config_WeatherStation.h b/main/config_WeatherStation.h new file mode 100644 index 0000000..2b133f3 --- /dev/null +++ b/main/config_WeatherStation.h @@ -0,0 +1,52 @@ +/* + OpenMQTTGateway - ESP8266 or Arduino program for home automation + + Act as a wifi or ethernet gateway between your 433mhz/infrared IR signal and a MQTT broker + Send and receiving command by MQTT + + This files enables to set your parameter for the DHT11/22 sensor + + Copyright: (c)Florian ROBERT + + This file is part of OpenMQTTGateway. + + OpenMQTTGateway is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + OpenMQTTGateway is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ +#ifndef config_WeatherStation_h +#define config_WeatherStation_h + +#include +#include + +extern void setupWeatherStation(); +extern void ZgatewayWeatherStationtoMQTT(); +/*----------------------------USER PARAMETERS-----------------------------*/ +/*-------------DEFINE YOUR MQTT PARAMETERS BELOW----------------*/ +#define subjectRFtoMQTT "/433toMQTT" + + +/*-------------------PIN DEFINITIONS----------------------*/ +#ifndef RF_WS_RECEIVER_PIN + #ifdef ESP8266 + #define RF_WS_RECEIVER_PIN 0 // D3 on nodemcu // put 4 with rf bridge direct mod + #elif ESP32 + #define RF_WS_RECEIVER_PIN 27 // D27 on DOIT ESP32 + #elif __AVR_ATmega2560__ + #define RF_WS_RECEIVER_PIN 1 //1 = D3 on mega + #else + #define RF_WS_RECEIVER_PIN 1 //1 = D3 on arduino + #endif +#endif + +#endif \ No newline at end of file diff --git a/main/main.ino b/main/main.ino index 6c25f52..cc34634 100644 --- a/main/main.ino +++ b/main/main.ino @@ -45,6 +45,9 @@ unsigned long ReceivedSignal[array_size][2] = {{0, 0}, {0, 0}, {0, 0}, {0, 0}}; #if defined(ZgatewayRF) || defined(ZgatewayRF2) || defined(ZgatewayPilight) #include "config_RF.h" #endif +#ifdef ZgatewayWeatherStation +#include "config_WeatherStation.h" +#endif #ifdef ZgatewayLORA #include "config_LORA.h" #endif @@ -734,6 +737,9 @@ void setup() #ifdef ZgatewayPilight setupPilight(); #endif + #ifdef ZgatewayWeatherStation + setupWeatherStation(); + #endif #ifdef ZgatewaySRFB setupSRFB(); #endif @@ -1156,6 +1162,9 @@ void loop() #ifdef ZgatewayRF2 RF2toMQTT(); #endif + #ifdef ZgatewayWeatherStation + ZgatewayWeatherStationtoMQTT(); + #endif #ifdef ZgatewayPilight PilighttoMQTT(); #endif @@ -1286,6 +1295,9 @@ void stateMeasures() #ifdef ZgatewayRF2 modules = modules + ZgatewayRF2; #endif + #ifdef ZgatewayWeatherStation + modules = modules + ZgatewayWeatherStation; + #endif #ifdef ZgatewayPilight modules = modules + ZgatewayPilight; #endif diff --git a/platformio.ini b/platformio.ini index 803b4dc..d26eeb3 100644 --- a/platformio.ini +++ b/platformio.ini @@ -33,6 +33,7 @@ extra_configs = ;default_envs = esp32dev-all ;default_envs = esp32dev-rf ;default_envs = esp32dev-pilight +;default_envs = esp32dev-weatherstation ;default_envs = esp32dev-ir ;default_envs = esp32dev-ble ;default_envs = esp32-m5stick-ble @@ -43,6 +44,7 @@ extra_configs = ;default_envs = rf-wifi-gateway ;default_envs = nodemcuv2-rf2 ;default_envs = nodemcuv2-pilight +;default_envs = nodemcuv2-weatherstation ;default_envs = nodemcuv2-ir ;default_envs = nodemcuv2-ble ;default_envs = nodemcuv2-all @@ -67,6 +69,7 @@ irremoteesp = IRremoteESP8266@2.7.0 irremote = IRremote lora = sandeepmistry/arduino-LoRa#f4a1d27 esppilight = ESPiLight@0.16.2 +rfWeatherStation = WeatherStationDataRx@0.3.0 rfm69 = RFM69@4a6f77ad47 dht = DHT sensor library@1.3.0 unifiedsensor = Adafruit Unified Sensor@1.0.2 @@ -209,6 +212,17 @@ build_flags = '-DZgatewayPilight="Pilight"' '-DGateway_Name="OpenMQTTGateway_ESP32_RF"' +[env:esp32dev-weatherstation] +platform = ${com.esp32_platform} +board = esp32dev +lib_deps = + ${com-esp.lib_deps} + ${libraries.rfWeatherStation} +build_flags = + ${com-esp.build_flags} + '-DZgatewayWeatherStation="WeatherStation"' + '-DGateway_Name="OpenMQTTGateway_ESP32_WeatherStation"' + [env:esp32dev-ir] platform = ${com.esp32_platform} board = esp32dev @@ -433,6 +447,19 @@ build_flags = '-DGateway_Name="OpenMQTTGateway_ESP8266_Pilight"' board_build.flash_mode = dout +[env:nodemcuv2-weatherstation] +platform = ${com.esp8266_platform} +board = nodemcuv2 +lib_deps = + ${com-esp.lib_deps} + ${libraries.rfWeatherStation} + ${libraries.esp8266_mdns} +build_flags = + ${com-esp.build_flags} + '-DZgatewayWeatherStation="WeatherStationDataRx"' + '-DGateway_Name="OpenMQTTGateway_ESP8266_RF3"' +board_build.flash_mode = dout + [env:sonoff-basic] platform = ${com.esp8266_platform} board = esp8285