diff --git a/src/AmsToMqttBridge.h b/src/AmsToMqttBridge.h index add1056f..c3cc29ef 100644 --- a/src/AmsToMqttBridge.h +++ b/src/AmsToMqttBridge.h @@ -1,3 +1,6 @@ +#ifndef _AMSTOMQTTBRIDGE_H +#define _AMSTOMQTTBRIDGE_H + #define WIFI_CONNECTION_TIMEOUT 30000; #define INVALID_BUTTON_PIN 0xFFFFFFFF @@ -14,7 +17,6 @@ #define LED_PIN 2 // The blue on-board LED of the ESP8266 custom AMS board #define LED_ACTIVE_HIGH 0 #define AP_BUTTON_PIN 0 -#define TEMP_SENSOR_PIN 5 #if DEBUG_MODE #define SOFTWARE_SERIAL 1 @@ -29,7 +31,6 @@ HardwareSerial *hanSerial = &Serial; #define LED_PIN 5 #define LED_ACTIVE_HIGH 0 #define AP_BUTTON_PIN 4 -#define TEMP_SENSOR_PIN 14 #define SOFTWARE_SERIAL 1 #include @@ -40,7 +41,6 @@ SoftwareSerial *hanSerial = new SoftwareSerial(GPIO_NUM_21); #define LED_PIN D4 #define LED_ACTIVE_HIGH 0 #define AP_BUTTON_PIN D2 -#define TEMP_SENSOR_PIN D5 #define SOFTWARE_SERIAL 1 #include @@ -63,3 +63,5 @@ HardwareSerial *hanSerial = &Serial; #include SoftwareSerial *hanSerial = new SoftwareSerial(5); #endif + +#endif diff --git a/src/AmsToMqttBridge.ino b/src/AmsToMqttBridge.ino index 40e9556d..106d1347 100644 --- a/src/AmsToMqttBridge.ino +++ b/src/AmsToMqttBridge.ino @@ -141,6 +141,8 @@ bool buttonActive = false; unsigned long longPressTime = 5000; bool longPressActive = false; +bool wifiConnected = false; + void loop() { if (digitalRead(AP_BUTTON_PIN) == LOW) { if (buttonActive == false) { @@ -170,8 +172,13 @@ void loop() { // Reconnect to WiFi and MQTT as needed if (WiFi.status() != WL_CONNECTED) { + wifiConnected = false; WiFi_connect(); } else { + if(!wifiConnected) { + wifiConnected = true; + if(debugger) debugger->println("Successfully connected to WiFi!"); + } if (!config.getMqttHost().isEmpty()) { mqtt.loop(); yield(); diff --git a/src/HwTools.cpp b/src/HwTools.cpp index 797a3385..867c2c6e 100644 --- a/src/HwTools.cpp +++ b/src/HwTools.cpp @@ -1,9 +1,8 @@ #include "HwTools.h" - double HwTools::getVcc() { #if defined(ARDUINO_ESP8266_WEMOS_D1MINI) - return (((double) ESP.getVcc()) / 1024) * 1.1; // This board has a voltage divider on VCC, add 10% + return (((double) ESP.getVcc()) / 900); // This board has a voltage divider on VCC. Yes, 900 is correct #elif defined(ESP8266) return ((double) ESP.getVcc()) / 1024; #endif @@ -15,7 +14,7 @@ double HwTools::getTemperature() { #if defined TEMP_SENSOR_PIN if(!tempSensorInit) { tempSensor->begin(); - delay(25); + delay(50); tempSensor->requestTemperatures(); hasTempSensor = tempSensor->getTempCByIndex(0) != DEVICE_DISCONNECTED_C; tempSensorInit = true; diff --git a/src/HwTools.h b/src/HwTools.h index f3773008..5eb1595f 100644 --- a/src/HwTools.h +++ b/src/HwTools.h @@ -12,16 +12,24 @@ #include #include +#if HW_ROARFRED +#define TEMP_SENSOR_PIN 5 +#elif defined(ARDUINO_LOLIN_D32) +#define TEMP_SENSOR_PIN 14 +#elif defined(ARDUINO_ESP8266_WEMOS_D1MINI) +#define TEMP_SENSOR_PIN D5 +#endif + + + class HwTools { public: - static double getVcc(); - static double getTemperature(); + double getVcc(); + double getTemperature(); HwTools() { -#if defined TEMP_SENSOR_PIN oneWire = new OneWire(TEMP_SENSOR_PIN); tempSensor = new DallasTemperature(this->oneWire); -#endif }; private: bool tempSensorInit, hasTempSensor; diff --git a/src/web/AmsWebServer.cpp b/src/web/AmsWebServer.cpp index c8cb7a4b..b631f85f 100644 --- a/src/web/AmsWebServer.cpp +++ b/src/web/AmsWebServer.cpp @@ -159,6 +159,10 @@ void AmsWebServer::indexHtml() { double vcc = hw.getVcc(); html.replace("${vcc}", vcc > 0 ? String(vcc, 2) : ""); + double temp = hw.getTemperature(); + html.replace("${temp}", temp > 0 ? String(temp, 1) : ""); + html.replace("${display.temp}", temp != DEVICE_DISCONNECTED_C ? "" : "none"); + float rssi = WiFi.RSSI(); rssi = isnan(rssi) ? -100.0 : rssi; html.replace("${wifi.rssi}", vcc > 0 ? String(rssi, 0) : ""); @@ -327,6 +331,9 @@ void AmsWebServer::dataJson() { double vcc = hw.getVcc(); json["vcc"] = vcc > 0 ? vcc : 0; + double temp = hw.getTemperature(); + json["temp"] = temp; + json.createNestedObject("wifi"); float rssi = WiFi.RSSI(); rssi = isnan(rssi) ? -100.0 : rssi; diff --git a/web/index.html b/web/index.html index ba858f18..b316eb73 100644 --- a/web/index.html +++ b/web/index.html @@ -7,6 +7,7 @@ +