Cleanup and fix bugs found during testing

This commit is contained in:
Gunnar Skjold
2020-02-15 22:13:04 +01:00
parent 9c09740a41
commit f6066fbbf3
6 changed files with 49 additions and 11 deletions

View File

@@ -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 <SoftwareSerial.h>
@@ -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 <SoftwareSerial.h>
@@ -63,3 +63,5 @@ HardwareSerial *hanSerial = &Serial;
#include <SoftwareSerial.h>
SoftwareSerial *hanSerial = new SoftwareSerial(5);
#endif
#endif

View File

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

View File

@@ -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;

View File

@@ -12,16 +12,24 @@
#include <DallasTemperature.h>
#include <OneWire.h>
#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;

View File

@@ -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;