diff --git a/lib/esp32DHT/.gitignore b/lib/esp32DHT/.gitignore new file mode 100644 index 0000000..a0f3ef4 --- /dev/null +++ b/lib/esp32DHT/.gitignore @@ -0,0 +1,8 @@ +.pioenvs +.vscode +lib +.vscode/.browse.c_cpp.db* +.vscode/c_cpp_properties.json +.vscode/launch.json +platformio.ini +.piolibdeps diff --git a/lib/esp32DHT/.travis.yml b/lib/esp32DHT/.travis.yml new file mode 100644 index 0000000..0b066ee --- /dev/null +++ b/lib/esp32DHT/.travis.yml @@ -0,0 +1,25 @@ +language: python +python: + - "2.7" + +# Cache PlatformIO packages using Travis CI container-based infrastructure +sudo: false +cache: + directories: + - "~/.platformio" + +env: + - PLATFORMIO_CI_SRC=examples/DHT22/DHT22.ino + - CPPLINT=true + +install: + - pip install -U platformio + - pip install -U cpplint + +script: + - if [[ "$CPPLINT" ]]; then cpplint --repository=. --recursive --linelength=200 --filter=-build/include ./src; else platformio ci --lib="." --board=lolin32; fi + +notifications: + email: + on_success: change + on_failure: change \ No newline at end of file diff --git a/lib/esp32DHT/LICENSE b/lib/esp32DHT/LICENSE new file mode 100644 index 0000000..0163b80 --- /dev/null +++ b/lib/esp32DHT/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2018 Bert Melis + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/lib/esp32DHT/README.md b/lib/esp32DHT/README.md new file mode 100644 index 0000000..715ccb3 --- /dev/null +++ b/lib/esp32DHT/README.md @@ -0,0 +1,53 @@ +# esp32DHT + +[![Build Status](https://travis-ci.com/bertmelis/esp32DHT.svg?branch=master)](https://travis-ci.com/bertmelis/esp32DHT) + +This is a DHT11/22 library for ESP32 using the RMT peripheral, for use in the Arduino framework. +For ESP8266, please look into this repo: [DHT](https://github.com/bertmelis/DHT) + +The library is non blocking, doesn't use delay and is usable in async frameworks. The library is kept simple on purpose. You are responsible yourself to follow the sensor's constraints (like polling frequency) and logical programming errors. Supplementary functions like dew point calculation are not included. + +## Installation + +* For Arduino IDE: see [the Arduino Guide](https://www.arduino.cc/en/Guide/Libraries#toc4) +* For Platformio: see the [Platfomio guide](http://docs.platformio.org/en/latest/projectconf/section_env_library.html) + +## Usage + +```C++ +#include +#include + +DHT22 sensor; + +void setup() { + Serial.begin(112500); + sensor.setup(23); + sensor.setCallback([](int8_t result) { + if (result > 0) { + Serial.printf("Temp: %.1f°C\nHumid: %.1f%%\n", sensor.getTemperature(), sensor.getHumidity()); + } else { + Serial.printf("Sensor error: %s", sensor.getError()); + } + }); +} + +void loop() { + static uint32_t lastMillis = 0; + if (millis() - lastMillis > 30000) { + lastMillis = millis(); + sensor.read(); + Serial.print("Read DHT...\n"); + } +} +``` + +> Note: `setup(uint8_t, rmt_channel_t channel = RMT_CHANNEL_0);` taks 2 arguments: the pin connected to the DHT sensor and the RMT channel[0-7]. The library uses 2 channels and defaults to (starting) channel 0. This means that by default channel 0 and channel 1 are occupied by the DHT and you should not use channel 7. If you're also using other RMT channels (for IR devices, extra DHT sensors, Neopixels...) you have to keep this in mind. +> +> Read more about RMT in the docs: [ESP-IDF documentation](https://esp-idf.readthedocs.io/en/latest/api-reference/peripherals/rmt.html) + +## History + +Whatever can be done using hardware should not be done by software. ESP32 has a RMT peripheral device which is remarkably versatile. As the DHT sensors rely on tight timing, the RMT device is perfect to accomplish reliable communication. I didn't find any other Arduino library that uses the RMT and/or doesn't block during communication. So I created my own one. + +> This library won't exist without the examples for RMT. Credits go to the team and contributors of ESP-IDF and @nkolban! diff --git a/lib/esp32DHT/buildexamples.bat b/lib/esp32DHT/buildexamples.bat new file mode 100644 index 0000000..d0cd922 --- /dev/null +++ b/lib/esp32DHT/buildexamples.bat @@ -0,0 +1 @@ +platformio ci --lib="." --board=lolin32 examples/DHT22/DHT22.ino diff --git a/lib/esp32DHT/docs/DHT11.pdf b/lib/esp32DHT/docs/DHT11.pdf new file mode 100644 index 0000000..1246981 Binary files /dev/null and b/lib/esp32DHT/docs/DHT11.pdf differ diff --git a/lib/esp32DHT/docs/DHT22-AM2302.pdf b/lib/esp32DHT/docs/DHT22-AM2302.pdf new file mode 100644 index 0000000..a2e3d52 Binary files /dev/null and b/lib/esp32DHT/docs/DHT22-AM2302.pdf differ diff --git a/lib/esp32DHT/examples/DHT22/DHT22.ino b/lib/esp32DHT/examples/DHT22/DHT22.ino new file mode 100644 index 0000000..d572d69 --- /dev/null +++ b/lib/esp32DHT/examples/DHT22/DHT22.ino @@ -0,0 +1,52 @@ +/* + +Copyright 2018 Bert Melis + +Permission is hereby granted, free of charge, to any person obtaining a +copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be included +in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, +TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +*/ + + +#include +#include +#include + +Ticker ticker; +DHT22 sensor; +// DHT11 sensor; // DHT11 also works! + +void readDHT() { + sensor.read(); +} + +void setup() { + Serial.begin(74880); + sensor.setup(23); // pin 23 is DATA, RMT channel defaults to channel 0 and 1 + sensor.onData([](float humidity, float temperature) { + Serial.printf("Temp: %g°C\nHumid: %g%%\n", temperature,humidity); + }); + sensor.onError([](uint8_t error) { + Serial.printf("Sensor error: %s", sensor.getError()); + }); + ticker.attach(30, readDHT); +} + +void loop() { +} diff --git a/lib/esp32DHT/keywords.txt b/lib/esp32DHT/keywords.txt new file mode 100644 index 0000000..5f17284 --- /dev/null +++ b/lib/esp32DHT/keywords.txt @@ -0,0 +1,24 @@ +####################################### +# Datatypes (KEYWORD1) +####################################### + +DHT11 KEYWORD1 +DHT22 KEYWORD1 + +####################################### +# Methods and Functions (KEYWORD2) +####################################### + +setPin KEYWORD2 +setCallback KEYWORD2 +read KEYWORD2 +ready KEYWORD2 +getTemperature KEYWORD2 +getHumidity KEYWORD2 +getError KEYWORD2 + +####################################### +# Constants (LITERAL1) +####################################### + +#yourLITERAL LITERAL1 diff --git a/lib/esp32DHT/library.json b/lib/esp32DHT/library.json new file mode 100644 index 0000000..fdab61e --- /dev/null +++ b/lib/esp32DHT/library.json @@ -0,0 +1,22 @@ +{ + "name": "esp32DHT", + "version": "1.0.1", + "keywords": "DHT, DTH11, DHT22, RMT, callback, Arduino, ESP32", + "description": "DHT sensor library for ESP32 using the RMT peripheral", + "homepage": "https://github.com/bertmelis/esp32DHT", + "license": "MIT", + "authors": { + "name": "Bert Melis", + "url": "https://github.com/bertmelis", + "maintainer": true + }, + "repository": { + "type": "git", + "url": "https://github.com/bertmelis/esp32DHT.git", + "branch": "master" + }, + "frameworks": "arduino", + "platforms": [ + "espressif32" + ] +} \ No newline at end of file diff --git a/lib/esp32DHT/library.properties b/lib/esp32DHT/library.properties new file mode 100644 index 0000000..eb529de --- /dev/null +++ b/lib/esp32DHT/library.properties @@ -0,0 +1,9 @@ +name=esp32DHT +version=1.0.1 +author=Bert Melis +maintainer=Bert Melis +sentence=DHT sensor library for ESP32 using the RMT pheripheral +paragraph= +category=Sensors +url=https://github.com/bertmelis/esp32DHT +architectures=esp32 diff --git a/lib/esp32DHT/src/esp32DHT.cpp b/lib/esp32DHT/src/esp32DHT.cpp new file mode 100644 index 0000000..7eab72c --- /dev/null +++ b/lib/esp32DHT/src/esp32DHT.cpp @@ -0,0 +1,197 @@ +/* + +Copyright 2018 Bert Melis + +Permission is hereby granted, free of charge, to any person obtaining a +copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be included +in all copies or substantial portions of the Software. +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, +TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONDHTTION WITH THE +SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +*/ + +#include "esp32DHT.hpp" // NOLINT + +#define RMT_CLK_DIV 80 + +DHT::DHT() : + _status(0), + _data{0}, + _pin(0), + _channel(RMT_CHANNEL_0), + _onData(nullptr), + _onError(nullptr), + _timer(nullptr), + _task(nullptr) {} + +DHT::~DHT() { + if (_timer) { // if _timer is true, setup() has been called + // so RMT driver is loaded and the aux task is + // running + esp_timer_delete(_timer); + rmt_driver_uninstall(_channel); + vTaskDelete(_task); + } +} + +void DHT::setup(uint8_t pin, rmt_channel_t channel) { + _pin = pin; + _channel = channel; + esp_timer_create_args_t _timerConfig; + _timerConfig.arg = static_cast(this); + _timerConfig.callback = reinterpret_cast(_handleTimer); + _timerConfig.dispatch_method = ESP_TIMER_TASK; + _timerConfig.name = "esp32DHTTimer"; + esp_timer_create(&_timerConfig, &_timer); + rmt_config_t config; + config.rmt_mode = RMT_MODE_RX; + config.channel = _channel; + config.gpio_num = static_cast(_pin); + config.mem_block_num = 2; + config.rx_config.filter_en = 1; + config.rx_config.filter_ticks_thresh = 10; + config.rx_config.idle_threshold = 1000; + config.clk_div = RMT_CLK_DIV; + rmt_config(&config); + rmt_driver_install(_channel, 400, 0); // 400 words for ringbuffer containing pulse trains from DHT + rmt_get_ringbuf_handle(_channel, &_ringBuf); + xTaskCreate((TaskFunction_t)&_handleData, "esp32DHT", 2048, this, 5, &_task); + pinMode(_pin, OUTPUT); + digitalWrite(_pin, HIGH); +} + +void DHT::onData(esp32DHTInternals::OnData_CB callback) { + _onData = callback; +} + +void DHT::onError(esp32DHTInternals::OnError_CB callback) { + _onError = callback; +} + +void DHT::read() { + // _pin should be set to OUTPUT and HIGH + digitalWrite(_pin, LOW); + esp_timer_start_once(_timer, 18 * 1000); // timer is in microseconds + _data[0] = _data[1] = _data[2] = _data[3] = _data[4] = 0; + _status = 0; +} + +const char* DHT::getError() const { + if (_status == 0) { + return "OK"; + } else if (_status == 1) { + return "TO"; + } else if (_status == 2) { + return "NACK"; + } else if (_status == 3) { + return "DATA"; + } else if (_status == 4) { + return "CS"; + } else if (_status == 5) { + return "UNDERFLOW"; + } else if (_status == 6) { + return "OVERFLOW"; + } + return "UNKNOWN"; +} + +void DHT::_handleTimer(DHT* instance) { + pinMode(instance->_pin, INPUT); + rmt_rx_start(instance->_channel, 1); + rmt_set_pin(instance->_channel, RMT_MODE_RX, static_cast(instance->_pin)); // reset after using pin as output + xTaskNotifyGive(instance->_task); +} + +void DHT::_handleData(DHT* instance) { + size_t rx_size = 0; + while (1) { + ulTaskNotifyTake(pdTRUE, portMAX_DELAY); // block and wait for notification + // blocks until data is available or timeouts after 1000 + rmt_item32_t* items = static_cast(xRingbufferReceive(instance->_ringBuf, &rx_size, 1000)); + if (items) { + instance->_decode(items, rx_size/sizeof(rmt_item32_t)); + vRingbufferReturnItem(instance->_ringBuf, static_cast(items)); + rmt_rx_stop(instance->_channel); + pinMode(instance->_pin, OUTPUT); + digitalWrite(instance->_pin, HIGH); + } else { + instance->_status = 1; // timeout error + rmt_rx_stop(instance->_channel); + pinMode(instance->_pin, OUTPUT); + digitalWrite(instance->_pin, HIGH); + } + instance->_tryCallback(); + } +} + +void DHT::_decode(rmt_item32_t* data, int numItems) { + if (numItems < 42) { + _status = 5; + } else if (numItems > 42) { + _status = 6; + } else if ((data[0].duration0 + data[0].duration1) < 140 && (data[0].duration0 + data[0].duration1) > 180) { + _status = 2; + } else { + for (uint8_t i = 1; i < numItems - 1; ++i) { // don't include tail + uint8_t pulse = data[i].duration0 + data[i].duration1; + if (pulse > 55 && pulse < 145) { + _data[(i - 1) / 8] <<= 1; // shift left + if (pulse > 120) { + _data[(i - 1) / 8] |= 1; + } + } else { + _status = 3; // DATA error + return; + } + } + if (_data[4] == ((_data[0] + _data[1] + _data[2] + _data[3]) & 0xFF)) { + _status = 0; + } else { + _status = 4; // checksum error + } + } +} + +void DHT::_tryCallback() { + if (_status == 0) { + if (_onData) _onData(_getHumidity(), _getTemperature()); + } else { + if (_onError) _onError(_status); + } +} + +float DHT11::_getTemperature() { + if (_status != 0) return NAN; + return static_cast(_data[2]); +} + +float DHT11::_getHumidity() { + if (_status != 0) return NAN; + return static_cast(_data[0]); +} + +float DHT22::_getTemperature() { + if (_status != 0) return NAN; + float temp = (((_data[2] & 0x7F) << 8) | _data[3]) * 0.1; + if (_data[2] & 0x80) { // negative temperature + temp = -temp; + } + return temp; +} + +float DHT22::_getHumidity() { + if (_status != 0) return NAN; + return ((_data[0] << 8) | _data[1]) * 0.1; +} diff --git a/lib/esp32DHT/src/esp32DHT.h b/lib/esp32DHT/src/esp32DHT.h new file mode 100644 index 0000000..3e82881 --- /dev/null +++ b/lib/esp32DHT/src/esp32DHT.h @@ -0,0 +1,27 @@ +/* + +Copyright 2018 Bert Melis + +Permission is hereby granted, free of charge, to any person obtaining a +copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be included +in all copies or substantial portions of the Software. +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, +TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +*/ + +#pragma once + +#include "esp32DHT.hpp" diff --git a/lib/esp32DHT/src/esp32DHT.hpp b/lib/esp32DHT/src/esp32DHT.hpp new file mode 100644 index 0000000..e8dec4d --- /dev/null +++ b/lib/esp32DHT/src/esp32DHT.hpp @@ -0,0 +1,85 @@ +/* + +Copyright 2018 Bert Melis + +Permission is hereby granted, free of charge, to any person obtaining a +copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be included +in all copies or substantial portions of the Software. +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, +TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +*/ + +#pragma once + +extern "C" { + #include + #include + #include + #include + #include +} +#include + +namespace esp32DHTInternals { + +typedef std::function OnData_CB; +typedef std::function OnError_CB; + +} // end namespace esp32DHTInternals + +class DHT { + public: + DHT(); + ~DHT(); + void setup(uint8_t pin, rmt_channel_t channel = RMT_CHANNEL_0); // setPin does complete setup of DHT lib + void onData(esp32DHTInternals::OnData_CB callback); + void onError(esp32DHTInternals::OnError_CB callback); + void read(); + const char* getError() const; + + protected: + uint8_t _status; + uint8_t _data[5]; + + private: + static void _handleTimer(DHT* instance); + static void _handleData(DHT* instance); + void _decode(rmt_item32_t* data, int numItems); + void _tryCallback(); + virtual float _getTemperature() = 0; + virtual float _getHumidity() = 0; + + private: + uint8_t _pin; + rmt_channel_t _channel; + esp32DHTInternals::OnData_CB _onData; + esp32DHTInternals::OnError_CB _onError; + esp_timer_handle_t _timer; + TaskHandle_t _task; + RingbufHandle_t _ringBuf; +}; + +class DHT11 : public DHT { + private: + float _getTemperature(); + float _getHumidity(); +}; + +class DHT22 : public DHT { + private: + float _getTemperature(); + float _getHumidity(); +}; diff --git a/src/Afterburner.cpp b/src/Afterburner.cpp index b39c732..378c3ec 100644 --- a/src/Afterburner.cpp +++ b/src/Afterburner.cpp @@ -124,8 +124,8 @@ #define RX_DATA_TIMOUT 50 const int FirmwareRevision = 31; -const int FirmwareSubRevision = 4; -const char* FirmwareDate = "17 Sep 2019"; +const int FirmwareSubRevision = 5; +const char* FirmwareDate = "19 Sep 2019"; #ifdef ESP32 @@ -842,6 +842,7 @@ void loop() pHourMeter->monitor(HeaterFrame2); } updateJSONclients(bReportJSONData); + updateMQTT(); CommState.set(CommStates::Idle); NVstore.doSave(); // now is a good time to store to the NV storage, well away from any blue wire activity break; diff --git a/src/Utility/BTC_JSON.cpp b/src/Utility/BTC_JSON.cpp index 20a2ffc..7a10dcc 100644 --- a/src/Utility/BTC_JSON.cpp +++ b/src/Utility/BTC_JSON.cpp @@ -37,11 +37,13 @@ #include #include "HourMeter.h" +extern CModerator MQTTmoderator; + char defaultJSONstr[64]; CModerator JSONmoderator; CTimerModerator TimerModerator; int timerConflict = 0; -CModerator MQTTmoderator; +CModerator MQTTJSONmoderator; CModerator IPmoderator; CModerator GPIOmoderator; CModerator SysModerator; @@ -57,6 +59,7 @@ bool makeJSONStringGPIO( CModerator& moderator, char* opStr, int len); bool makeJSONStringSysInfo(CModerator& moderator, char* opStr, int len); bool makeJSONStringMQTT(CModerator& moderator, char* opStr, int len); bool makeJSONStringIP(CModerator& moderator, char* opStr, int len); +void DecodeCmd(const char* cmd, String& payload); void interpretJsonCommand(char* pLine) { @@ -76,6 +79,10 @@ void interpretJsonCommand(char* pLine) JsonObject::iterator it; for(it = obj.begin(); it != obj.end(); ++it) { + String payload(it->value.as()); + DecodeCmd(it->key, payload); +/* + if(strcmp("TempDesired", it->key) == 0) { if( !reqDemand(it->value.as(), false) ) { // this request is blocked if OEM controller active JSONmoderator.reset("TempDesired"); @@ -172,6 +179,7 @@ void interpretJsonCommand(char* pLine) } else if(strcmp("Refresh", it->key) == 0) { resetJSONmoderator(); + refreshMQTT(); } else if(strcmp("SystemVoltage", it->key) == 0) { setSystemVoltage(it->value.as()); @@ -219,7 +227,7 @@ void interpretJsonCommand(char* pLine) } // MQTT parameters else if(strcmp("MQuery", it->key) == 0) { - MQTTmoderator.reset(); // force MQTT params to be sent + MQTTJSONmoderator.reset(); // force MQTT params to be sent } else if(strcmp("MEn", it->key) == 0) { sMQTTparams info = NVstore.getMQTTinfo(); @@ -325,7 +333,7 @@ void interpretJsonCommand(char* pLine) ht.lowVolts = uint8_t(fCal * 10); NVstore.setHeaterTuning(ht); } - } + }*/ else if(strcmp("SMenu", it->key) == 0) { sUserSettings us = NVstore.getUserSettings(); us.menuMode = it->value.as(); @@ -637,7 +645,7 @@ void updateJSONclients(bool report) // report MQTT params { - if(makeJSONStringMQTT(MQTTmoderator, jsonStr, sizeof(jsonStr))) { + if(makeJSONStringMQTT(MQTTJSONmoderator, jsonStr, sizeof(jsonStr))) { if (report) { DebugPort.printf("JSON send: %s\r\n", jsonStr); } @@ -710,7 +718,7 @@ void resetJSONmoderator() void initMQTTJSONmoderator() { char jsonStr[800]; - makeJSONStringMQTT(MQTTmoderator, jsonStr, sizeof(jsonStr)); + makeJSONStringMQTT(MQTTJSONmoderator, jsonStr, sizeof(jsonStr)); } void initIPJSONmoderator() @@ -758,3 +766,266 @@ void Expand(std::string& str) } } +void DecodeCmd(const char* cmd, String& payload) +{ + if(strcmp("TempDesired", cmd) == 0) { + if( !reqDemand(payload.toInt(), false) ) { // this request is blocked if OEM controller active + JSONmoderator.reset("TempDesired"); + } + } + else if(strcmp("Run", cmd) == 0) { + refreshMQTT(); + if(payload == "1") { + requestOn(); + } + else if(payload == "0") { + requestOff(); + } + } + else if(strcmp("RunState", cmd) == 0) { + if(payload.toInt()) { + requestOn(); + } + else { + requestOff(); + } + } + else if(strcmp("PumpMin", cmd) == 0) { + setPumpMin(payload.toFloat()); + } + else if(strcmp("PumpMax", cmd) == 0) { + setPumpMax(payload.toFloat()); + } + else if(strcmp("FanMin", cmd) == 0) { + setFanMin(payload.toInt()); + } + else if(strcmp("FanMax", cmd) == 0) { + setFanMax(payload.toInt()); + } + else if(strcmp("CyclicTemp", cmd) == 0) { + setDemandDegC(payload.toInt()); // directly set demandDegC + } + else if((strcmp("CyclicOff", cmd) == 0) || (strcmp("ThermostatOvertemp", cmd) == 0)) { + sUserSettings us = NVstore.getUserSettings(); + us.cyclic.Stop = payload.toInt(); + if(INBOUNDS(us.cyclic.Stop, 0, 10)) { + if(us.cyclic.Stop > 1) + us.cyclic.Stop--; // internal uses a 1 offset + NVstore.setUserSettings(us); + } + } + else if((strcmp("CyclicOn", cmd) == 0) || (strcmp("ThermostatUndertemp", cmd) == 0)) { + sUserSettings us = NVstore.getUserSettings(); + us.cyclic.Start = payload.toInt(); + if(INBOUNDS(us.cyclic.Start, -20, 0)) + NVstore.setUserSettings(us); + } + else if(strcmp("ThermostatMethod", cmd) == 0) { + sUserSettings settings = NVstore.getUserSettings(); + settings.ThermostatMethod = payload.toInt(); + if(INBOUNDS(settings.ThermostatMethod, 0, 3)) + NVstore.setUserSettings(settings); + } + else if(strcmp("ThermostatWindow", cmd) == 0) { + sUserSettings settings = NVstore.getUserSettings(); + settings.ThermostatWindow = payload.toFloat(); + if(INBOUNDS(settings.ThermostatWindow, 0.2f, 10.f)) + NVstore.setUserSettings(settings); + } + else if(strcmp("Thermostat", cmd) == 0) { + if(!setThermostatMode(payload.toInt())) { // this request is blocked if OEM controller active + JSONmoderator.reset("ThermoStat"); + } + } + else if(strcmp("ExtThermoTmout", cmd) == 0) { + sUserSettings us = NVstore.getUserSettings(); + us.ExtThermoTimeout = payload.toInt(); + if(INBOUNDS(us.ExtThermoTimeout, 0, 3600000)) + NVstore.setUserSettings(us); + } + else if(strcmp("NVsave", cmd) == 0) { + if(payload.toInt() == 8861) + saveNV(); + } + else if(strcmp("Watchdog", cmd) == 0) { + doJSONwatchdog(payload.toInt()); + } + else if(strcmp("DateTime", cmd) == 0) { + setDateTime(payload.c_str()); + bTriggerDateTime = true; + } + else if(strcmp("Date", cmd) == 0) { + setDate(payload.c_str()); + bTriggerDateTime = true; + } + else if(strcmp("Time", cmd) == 0) { + setTime(payload.c_str()); + bTriggerDateTime = true; + } + else if(strcmp("Time12hr", cmd) == 0) { + sUserSettings us = NVstore.getUserSettings(); + us.clock12hr = payload.toInt() ? 1 : 0; + NVstore.setUserSettings(us); + NVstore.save(); + } + else if(strcmp("PumpPrime", cmd) == 0) { + reqPumpPrime(payload.toInt()); + } + else if(strcmp("Refresh", cmd) == 0) { + resetJSONmoderator(); + refreshMQTT(); + } + else if(strcmp("SystemVoltage", cmd) == 0) { + setSystemVoltage(payload.toFloat()); + } + else if(strcmp("TimerDays", cmd) == 0) { + // value encoded as "ID Days,Days" + decodeJSONTimerDays(payload.c_str()); + } + else if(strcmp("TimerStart", cmd) == 0) { + // value encoded as "ID HH:MM" + decodeJSONTimerTime(0, payload.c_str()); + } + else if(strcmp("TimerStop", cmd) == 0) { + // value encoded as "ID HH:MM" + decodeJSONTimerTime(1, payload.c_str()); + } + else if(strcmp("TimerRepeat", cmd) == 0) { + // value encoded as "ID val" + decodeJSONTimerNumeric(0, payload.c_str()); + } + else if(strcmp("TimerTemp", cmd) == 0) { + decodeJSONTimerNumeric(1, payload.c_str()); + } + else if(strcmp("TimerConflict", cmd) == 0) { + validateTimer(payload.toInt()); + } + // request specific timer refresh + else if((strcmp("TQuery", cmd) == 0) || (strcmp("TimerRefresh", cmd) == 0) ) { + int timerID = payload.toInt(); + if(timerID) + TimerModerator.reset(timerID-1); + else + TimerModerator.reset(); + } + else if(strcmp("FanSensor", cmd) == 0) { + setFanSensor(payload.toInt()); + } + else if(strcmp("IQuery", cmd) == 0) { + IPmoderator.reset(); // force IP params to be sent + } + // system info + else if(strcmp("SQuery", cmd) == 0) { + SysModerator.reset(); // force MQTT params to be sent + bTriggerSysParams = true; + } + // MQTT parameters + else if(strcmp("MQuery", cmd) == 0) { + MQTTJSONmoderator.reset(); // force MQTT params to be sent + } + else if(strcmp("MEn", cmd) == 0) { + sMQTTparams info = NVstore.getMQTTinfo(); + info.enabled = payload.toInt(); + NVstore.setMQTTinfo(info); + } + else if(strcmp("MPort", cmd) == 0) { + sMQTTparams info = NVstore.getMQTTinfo(); + info.port = payload.toInt(); + NVstore.setMQTTinfo(info); + } + else if(strcmp("MHost", cmd) == 0) { + sMQTTparams info = NVstore.getMQTTinfo(); + strncpy(info.host, payload.c_str(), 127); + info.host[127] = 0; + NVstore.setMQTTinfo(info); + } + else if(strcmp("MUser", cmd) == 0) { + sMQTTparams info = NVstore.getMQTTinfo(); + strncpy(info.username, payload.c_str(), 31); + info.username[31] = 0; + NVstore.setMQTTinfo(info); + } + else if(strcmp("MPasswd", cmd) == 0) { + sMQTTparams info = NVstore.getMQTTinfo(); + strncpy(info.password, payload.c_str(), 31); + info.password[31] = 0; + NVstore.setMQTTinfo(info); + } + else if(strcmp("MQoS", cmd) == 0) { + sMQTTparams info = NVstore.getMQTTinfo(); + info.qos = payload.toInt(); + if(INBOUNDS(info.qos, 0, 2)) { + NVstore.setMQTTinfo(info); + } + } + else if(strcmp("MTopic", cmd) == 0) { + sMQTTparams info = NVstore.getMQTTinfo(); + strncpy(info.topic, payload.c_str(), 31); + info.topic[31] = 0; + NVstore.setMQTTinfo(info); + } + else if(strcmp("UploadSize", cmd) == 0) { + setUploadSize(payload.toInt()); + } + else if(strcmp("GPout1", cmd) == 0) { + setGPIOout(0, payload.toInt() ? true : false); + } + else if(strcmp("GPout2", cmd) == 0) { + setGPIOout(1, payload.toInt() ? true : false); + } + else if(strcmp("GPin1", cmd) == 0) { + simulateGPIOin(payload.toInt() ? 0x01 : 0x00); // simulate key 1 press + } + else if(strcmp("GPin2", cmd) == 0) { + simulateGPIOin(payload.toInt() ? 0x02 : 0x00); // simulate key 2 press + } + else if(strcmp("JSONpack", cmd) == 0) { + sUserSettings us = NVstore.getUserSettings(); + uint8_t packed = payload.toInt() ? 0x00 : 0x01; + us.JSON.LF = packed; + us.JSON.padding = packed; + us.JSON.singleElement = packed; + NVstore.setUserSettings(us); + NVstore.save(); + resetJSONmoderator(); + } + else if(strcmp("TempMode", cmd) == 0) { + sUserSettings us = NVstore.getUserSettings(); + us.degF = payload.toInt() ? 0x01 : 0x00; + NVstore.setUserSettings(us); + NVstore.save(); + } + else if(strcmp("PumpCount", cmd) == 0) { // reset fuel gauge + int Count = payload.toInt(); + if(Count == 0) { + resetFuelGauge(); + } + } + else if(strcmp("PumpCal", cmd) == 0) { + sHeaterTuning ht = NVstore.getHeaterTuning(); + ht.pumpCal = payload.toFloat(); + if(INBOUNDS(ht.pumpCal, 0.001, 1)) { + NVstore.setHeaterTuning(ht); + } + } + else if(strcmp("TempOffset", cmd) == 0) { + sHeaterTuning ht = NVstore.getHeaterTuning(); + ht.tempOfs = payload.toFloat(); + if(INBOUNDS(ht.tempOfs, -10.0, +10.0)) { + NVstore.setHeaterTuning(ht); + } + } + else if(strcmp("LowVoltCutout", cmd) == 0) { + float fCal = payload.toFloat(); + bool bOK = false; + if(NVstore.getHeaterTuning().sysVoltage == 120) + bOK |= (fCal == 0) || INBOUNDS(fCal, 10.0, 12.5); + else + bOK |= (fCal == 0) || INBOUNDS(fCal, 20.0, 25.0); + if(bOK) { + sHeaterTuning ht = NVstore.getHeaterTuning(); + ht.lowVolts = uint8_t(fCal * 10); + NVstore.setHeaterTuning(ht); + } + } +} \ No newline at end of file diff --git a/src/Utility/Moderator.h b/src/Utility/Moderator.h index 9d09e7f..ee24e37 100644 --- a/src/Utility/Moderator.h +++ b/src/Utility/Moderator.h @@ -134,6 +134,24 @@ public: bool addJson(const char* name, const char* value, JsonObject& root) { return szModerator.addJson(name, value, root); }; + bool shouldSend(const char* name, int value) { + return iModerator.shouldSend(name, value); + }; + bool shouldSend(const char* name, uint32_t value) { + return u32Moderator.shouldSend(name, value); + }; + bool shouldSend(const char* name, unsigned long value) { + return u32Moderator.shouldSend(name, value); + }; + bool shouldSend(const char* name, float value) { + return fModerator.shouldSend(name, value); + }; + bool shouldSend(const char* name, uint8_t value) { + return ucModerator.shouldSend(name, value); + }; + bool shouldSend(const char* name, const char* value) { + return szModerator.shouldSend(name, value); + }; // force changes on all held values void reset() { iModerator.reset(); diff --git a/src/Utility/helpers.h b/src/Utility/helpers.h index 07386de..f20b98b 100644 --- a/src/Utility/helpers.h +++ b/src/Utility/helpers.h @@ -96,5 +96,7 @@ void setAPpassword(const char* name); extern void ShowOTAScreen(int percent=0, eOTAmodes updateType=eOTAnormal); +extern void updateMQTT(); +extern void refreshMQTT(); #endif \ No newline at end of file diff --git a/src/WiFi/ABMqtt.cpp b/src/WiFi/ABMqtt.cpp index 85ea99f..c6e3cb2 100644 --- a/src/WiFi/ABMqtt.cpp +++ b/src/WiFi/ABMqtt.cpp @@ -31,15 +31,22 @@ #include "BTCWebServer.h" #include "../Utility/DebugPort.h" #include "../Utility/NVStorage.h" +#include "../Utility/Moderator.h" +#include "../Protocol/Protocol.h" + +extern void DecodeCmd(const char* cmd, String& payload); #define USE_RTOS_MQTTTIMER //#define USE_LOCAL_MQTTSTRINGS +//#define MQTT_DBG_RAWBYTES //IPAddress testMQTTserver(5, 196, 95, 208); // test.mosquito.org //IPAddress testMQTTserver(18, 194, 98, 249); // broker.hivemq.com AsyncMqttClient MQTTclient; char topicnameJSONin[128]; +char topicnameCmd[128]; +CModerator MQTTmoderator; #ifdef USE_LOCAL_MQTTSTRINGS char mqttHost[128]; @@ -84,9 +91,12 @@ void onMqttConnect(bool sessionPresent) // create the topicname we use to accept incoming JSON DebugPort.printf("MQTT: base topic name \"%s\"\r\n", NVstore.getMQTTinfo().topic); sprintf(topicnameJSONin, "%s/JSONin", NVstore.getMQTTinfo().topic); + sprintf(topicnameCmd, "%s/cmd/#", NVstore.getMQTTinfo().topic); // subscribe to that topic DebugPort.printf("MQTT: Subscribing to \"%s\"\r\n", topicnameJSONin); MQTTclient.subscribe(topicnameJSONin, NVstore.getMQTTinfo().qos); + MQTTclient.subscribe(topicnameCmd, NVstore.getMQTTinfo().qos); + MQTTclient.subscribe(statusTopic, NVstore.getMQTTinfo().qos); // spit out an "I'm here" message MQTTclient.publish(statusTopic, NVstore.getMQTTinfo().qos, true, "online"); @@ -113,15 +123,27 @@ void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties DebugPort.println(); #endif // string may not neccesarily be null terminated, make sure it is - char tidyString[1024]; - int maxlen = sizeof(tidyString)-1; + char szPayload[1024]; + int maxlen = sizeof(szPayload)-1; int lenLimit = len < maxlen ? len : maxlen; - strncpy(tidyString, (char*)payload, lenLimit); - tidyString[lenLimit] = 0; - DebugPort.println(tidyString); + strncpy(szPayload, (char*)payload, lenLimit); + szPayload[lenLimit] = 0; + DebugPort.println(szPayload); if(strcmp(topic, topicnameJSONin) == 0) { // check if incoming topic is our JSONin topic - interpretJsonCommand(tidyString); + interpretJsonCommand(szPayload); + } + else if(strncmp(topic, topicnameCmd, strlen(topicnameCmd)-1) == 0) { // check if incoming topic is our cmd topic + const char* cmdTopic = &topic[strlen(topicnameCmd)-1]; + DebugPort.printf("%s %s %s\r\n", topicnameCmd, cmdTopic, szPayload); + String cmdPayload(szPayload); + DecodeCmd(cmdTopic, cmdPayload); + } + else if(strcmp(topic, statusTopic) == 0) { // check if incoming topic is our general status + if(strcmp(szPayload, "1") == 0) { + MQTTmoderator.reset(); + MQTTclient.publish(statusTopic, NVstore.getMQTTinfo().qos, true, "online"); + } } } @@ -269,4 +291,77 @@ bool isMQTTconnected() { return MQTTclient.connected(); } + +void checkTopic(const char* name, int value) +{ + if(MQTTclient.connected()) { + if(MQTTmoderator.shouldSend(name, value)) { + const sMQTTparams params = NVstore.getMQTTinfo(); + char topic[128]; + sprintf(topic, "%s/sts/%s", params.topic, name); + char payload[128]; + sprintf(payload, "%d", value); + MQTTclient.publish(topic, params.qos, false, payload); + } + } +} + +void checkTopic(const char* name, float value) +{ + if(MQTTclient.connected()) { + if(MQTTmoderator.shouldSend(name, value)) { + const sMQTTparams params = NVstore.getMQTTinfo(); + char topic[128]; + sprintf(topic, "%s/sts/%s", params.topic, name); + char payload[128]; + sprintf(payload, "%.1f", value); + MQTTclient.publish(topic, params.qos, false, payload); + } + } +} + +void checkTopic(const char* name, const char* payload) +{ + if(MQTTclient.connected()) { + if(MQTTmoderator.shouldSend(name, payload)) { + const sMQTTparams params = NVstore.getMQTTinfo(); + char topic[128]; + sprintf(topic, "%s/sts/%s", params.topic, name); + MQTTclient.publish(topic, params.qos, false, payload); + } + } +} + +void updateMQTT() +{ + checkTopic("RunState", getHeaterInfo().getRunStateEx()); +// checkTopic("Run", getHeaterInfo().getRunStateEx() ? "{\"RunState\":1}" : "{\"RunState\":0}"); +// checkTopic("RunSts", getHeaterInfo().getRunStateEx() ? "1" : "0"); + checkTopic("Run", getHeaterInfo().getRunStateEx() ? "1" : "0"); + checkTopic("RunString", getHeaterInfo().getRunStateStr()); + float tidyTemp = getTemperatureSensor(); + tidyTemp = int(tidyTemp * 10 + 0.5) * 0.1f; // round to 0.1 resolution + checkTopic("TempCurrent", tidyTemp); + checkTopic("TempDesired", getTemperatureDesired()); + checkTopic("TempBody", getHeaterInfo().getTemperature_HeatExchg()); + checkTopic("ErrorState", getHeaterInfo().getErrState()); + checkTopic("ErrorString", getHeaterInfo().getErrStateStrEx()); // verbose it up! + checkTopic("Thermostat", getThermostatModeActive()); + checkTopic("PumpFixed", getHeaterInfo().getPump_Fixed() ); + checkTopic("PumpActual", getHeaterInfo().getPump_Actual()); + checkTopic("FanRPM", getFanSpeed()); + checkTopic("InputVoltage", getBatteryVoltage(false)); + checkTopic("GlowVoltage", getGlowVolts()); + checkTopic("GlowCurrent", getGlowCurrent()); + sGPIO info; + getGPIOinfo(info); + checkTopic("GPanlg", info.algVal * 100 / 4096); +} + +void refreshMQTT() +{ + MQTTmoderator.reset(); +} + + #endif \ No newline at end of file