mirror of
https://github.com/UtilitechAS/amsreader-firmware.git
synced 2026-01-14 07:49:01 +00:00
Only show temp if sensor is attached. Added common functions to get temp and vcc
This commit is contained in:
parent
f484f3eb0e
commit
eff00f1fe0
@ -19,12 +19,14 @@ You can also use a ESP based development board and combine this with a M-Bus mod
|
||||
[Wemos D1 mini](https://docs.wemos.cc/en/latest/d1/d1_mini.html)
|
||||
- M-Bus connected to GPIO5 (D1)
|
||||
- Jump GPIO4 (D2) to GND to force AP mode during boot
|
||||
- Dallas temp sensor connected to GPIO14 (D5)
|
||||
|
||||
### ESP32 based boards
|
||||
|
||||
[Wemos D32](https://docs.wemos.cc/en/latest/d32/d32.html)
|
||||
- M-Bus connected to GPIO21
|
||||
- Jump GPIO4 to GND to force AP mode during boot
|
||||
- Dallas temp sensor connected to GPIO14
|
||||
|
||||
[Adafruit HUZZAH32](https://www.adafruit.com/product/3405)
|
||||
- M-Bus connected to RX
|
||||
|
||||
@ -14,10 +14,7 @@
|
||||
#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
|
||||
|
||||
#include <DallasTemperature.h>
|
||||
#include <OneWire.h>
|
||||
#define TEMP_SENSOR_PIN 5 // Temperature sensor connected to GPIO5
|
||||
#define TEMP_SENSOR_PIN 5
|
||||
|
||||
#if DEBUG_MODE
|
||||
#define SOFTWARE_SERIAL 1
|
||||
@ -32,6 +29,7 @@ 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>
|
||||
@ -42,6 +40,7 @@ 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>
|
||||
@ -64,9 +63,3 @@ HardwareSerial *hanSerial = &Serial;
|
||||
#include <SoftwareSerial.h>
|
||||
SoftwareSerial *hanSerial = new SoftwareSerial(5);
|
||||
#endif
|
||||
|
||||
|
||||
#if defined TEMP_SENSOR_PIN
|
||||
OneWire oneWire(TEMP_SENSOR_PIN);
|
||||
DallasTemperature tempSensor(&oneWire);
|
||||
#endif
|
||||
@ -4,15 +4,17 @@
|
||||
Author: roarf
|
||||
*/
|
||||
|
||||
#if defined(ESP8266)
|
||||
ADC_MODE(ADC_VCC);
|
||||
#endif
|
||||
|
||||
#include "AmsToMqttBridge.h"
|
||||
#include <ArduinoJson.h>
|
||||
#include <MQTT.h>
|
||||
#include <DNSServer.h>
|
||||
|
||||
#if defined(ESP8266)
|
||||
ADC_MODE(ADC_VCC);
|
||||
#endif
|
||||
|
||||
#include "HwTools.h"
|
||||
|
||||
#include "web/AmsWebServer.h"
|
||||
#include "AmsConfiguration.h"
|
||||
#include "HanReader.h"
|
||||
@ -22,6 +24,8 @@ ADC_MODE(ADC_VCC);
|
||||
#include "Kaifa.h"
|
||||
#include "Kamstrup.h"
|
||||
|
||||
HwTools hw;
|
||||
|
||||
DNSServer dnsServer;
|
||||
|
||||
// Configuration
|
||||
@ -40,6 +44,8 @@ Stream* debugger = NULL;
|
||||
// The HAN Port reader, used to read serial data and decode DLMS
|
||||
HanReader hanReader;
|
||||
|
||||
boolean hasTempSensor = false;
|
||||
|
||||
// the setup function runs once when you press reset or power the board
|
||||
void setup() {
|
||||
if(config.hasConfig()) {
|
||||
@ -58,27 +64,25 @@ void setup() {
|
||||
debugger = ser;
|
||||
#endif
|
||||
|
||||
double vcc = hw.getVcc();
|
||||
|
||||
if (debugger) {
|
||||
debugger->println("");
|
||||
debugger->println("Started...");
|
||||
#if defined(ESP8266)
|
||||
debugger->print("Voltage: ");
|
||||
debugger->print(ESP.getVcc());
|
||||
debugger->print(vcc);
|
||||
debugger->println("mV");
|
||||
#endif
|
||||
}
|
||||
|
||||
#if defined(ESP8266)
|
||||
if (ESP.getVcc() < 2800) {
|
||||
if (vcc > 0 && vcc < 3.1) {
|
||||
if(debugger) {
|
||||
debugger->print("Voltage is too low: ");
|
||||
debugger->print(ESP.getVcc());
|
||||
debugger->print(vcc);
|
||||
debugger->println("mV");
|
||||
debugger->flush();
|
||||
}
|
||||
ESP.deepSleep(10000000); //Deep sleep to allow output cap to charge up
|
||||
}
|
||||
#endif
|
||||
|
||||
// Flash the LED, to indicate we can boot as AP now
|
||||
pinMode(LED_PIN, OUTPUT);
|
||||
@ -88,6 +92,14 @@ void setup() {
|
||||
WiFi.softAPdisconnect(true);
|
||||
WiFi.mode(WIFI_OFF);
|
||||
|
||||
if(debugger) {
|
||||
if(hasTempSensor) {
|
||||
debugger->println("Has temp sensor");
|
||||
} else {
|
||||
debugger->println("No temp sensor found");
|
||||
}
|
||||
}
|
||||
|
||||
if(config.hasConfig()) {
|
||||
if(debugger) config.print(debugger);
|
||||
client = new WiFiClient();
|
||||
@ -177,12 +189,13 @@ void loop() {
|
||||
if (millis() / 50 % 64 == 0) led_on();
|
||||
else led_off();
|
||||
|
||||
#if defined(ESP8266)
|
||||
// Make sure there is enough power to run
|
||||
delay(max(10, 3500-ESP.getVcc()));
|
||||
#else
|
||||
delay(10);
|
||||
#endif
|
||||
double vcc = hw.getVcc();
|
||||
if(vcc > 0) {
|
||||
delay(max(10, 3500-(int)(vcc*1000)));
|
||||
} else {
|
||||
delay(10);
|
||||
}
|
||||
|
||||
}
|
||||
readHanPort();
|
||||
@ -271,23 +284,22 @@ void readHanPort() {
|
||||
json["id"] = WiFi.macAddress();
|
||||
json["up"] = millis();
|
||||
json["t"] = time;
|
||||
#if defined(ESP8266)
|
||||
json["vcc"] = ((double) ESP.getVcc()) / 1000;
|
||||
#endif
|
||||
double vcc = hw.getVcc();
|
||||
if(vcc > 0) {
|
||||
json["vcc"] = vcc;
|
||||
}
|
||||
float rssi = WiFi.RSSI();
|
||||
rssi = isnan(rssi) ? -100.0 : rssi;
|
||||
json["rssi"] = rssi;
|
||||
double temp = hw.getTemperature();
|
||||
if(temp != -127) {
|
||||
json["temp"] = temp;
|
||||
}
|
||||
|
||||
// Add a sub-structure to the json object,
|
||||
// to keep the data from the meter itself
|
||||
JsonObject data = json.createNestedObject("data");
|
||||
|
||||
#if defined TEMP_SENSOR_PIN
|
||||
// Get the temperature too
|
||||
tempSensor.requestTemperatures();
|
||||
data["temp"] = tempSensor.getTempCByIndex(0);
|
||||
#endif
|
||||
|
||||
hanToJson(data, config.getMeterType(), hanReader);
|
||||
|
||||
if(!config.getMqttHost().isEmpty() && !config.getMqttPublishTopic().isEmpty()) {
|
||||
@ -387,7 +399,6 @@ void WiFi_connect() {
|
||||
|
||||
WiFi.enableAP(false);
|
||||
WiFi.mode(WIFI_STA);
|
||||
WiFi.setOutputPower(0);
|
||||
if(!config.getWifiIp().isEmpty()) {
|
||||
IPAddress ip, gw, sn(255,255,255,0);
|
||||
ip.fromString(config.getWifiIp());
|
||||
@ -458,9 +469,10 @@ void sendMqttData(String data)
|
||||
json["id"] = WiFi.macAddress();
|
||||
json["up"] = millis();
|
||||
json["data"] = data;
|
||||
#if defined(ESP8266)
|
||||
json["vcc"] = ((double) ESP.getVcc()) / 1000;
|
||||
#endif
|
||||
double vcc = hw.getVcc();
|
||||
if(vcc > 0) {
|
||||
json["vcc"] = vcc;
|
||||
}
|
||||
float rssi = WiFi.RSSI();
|
||||
rssi = isnan(rssi) ? -100.0 : rssi;
|
||||
json["rssi"] = rssi;
|
||||
@ -475,3 +487,5 @@ void sendMqttData(String data)
|
||||
if (debugger) debugger->print("sendMqttData: ");
|
||||
if (debugger) debugger->println(data);
|
||||
}
|
||||
|
||||
|
||||
|
||||
33
src/HwTools.cpp
Normal file
33
src/HwTools.cpp
Normal file
@ -0,0 +1,33 @@
|
||||
#include "HwTools.h"
|
||||
|
||||
|
||||
double HwTools::getVcc() {
|
||||
#if defined(ARDUINO_ESP8266_WEMOS_D1MINI)
|
||||
return (((double) ESP.getVcc()) / 1000) * 1.1; // This board has a voltage divider on VCC, add 10%
|
||||
#elif defined(ESP8266)
|
||||
return ((double) ESP.getVcc()) / 1000;
|
||||
#endif
|
||||
return -1;
|
||||
}
|
||||
|
||||
double HwTools::getTemperature() {
|
||||
|
||||
#if defined TEMP_SENSOR_PIN
|
||||
if(!tempSensorInit) {
|
||||
tempSensor->begin();
|
||||
delay(25);
|
||||
tempSensor->requestTemperatures();
|
||||
hasTempSensor = tempSensor->getTempCByIndex(0) != DEVICE_DISCONNECTED_C;
|
||||
tempSensorInit = true;
|
||||
}
|
||||
|
||||
if(hasTempSensor) {
|
||||
tempSensor->requestTemperatures();
|
||||
return tempSensor->getTempCByIndex(0);
|
||||
} else {
|
||||
return DEVICE_DISCONNECTED_C;
|
||||
}
|
||||
#endif
|
||||
return DEVICE_DISCONNECTED_C;
|
||||
}
|
||||
|
||||
32
src/HwTools.h
Normal file
32
src/HwTools.h
Normal file
@ -0,0 +1,32 @@
|
||||
#ifndef _HWTOOLS_H
|
||||
#define _HWTOOLS_H
|
||||
|
||||
#include "Arduino.h"
|
||||
|
||||
#if defined(ESP8266)
|
||||
#include <ESP8266WiFi.h>
|
||||
#elif defined(ESP32)
|
||||
#include <WiFi.h>
|
||||
#endif
|
||||
|
||||
#include <DallasTemperature.h>
|
||||
#include <OneWire.h>
|
||||
|
||||
class HwTools {
|
||||
public:
|
||||
static double getVcc();
|
||||
static double getTemperature();
|
||||
|
||||
HwTools() {
|
||||
#if defined TEMP_SENSOR_PIN
|
||||
oneWire = new OneWire(TEMP_SENSOR_PIN);
|
||||
tempSensor = new DallasTemperature(this->oneWire);
|
||||
#endif
|
||||
};
|
||||
private:
|
||||
bool tempSensorInit, hasTempSensor;
|
||||
OneWire *oneWire;
|
||||
DallasTemperature *tempSensor;
|
||||
};
|
||||
|
||||
#endif
|
||||
@ -153,10 +153,7 @@ void AmsWebServer::indexHtml() {
|
||||
html.replace("${data.tQO}", tpi > 0 ? String(tqo, 1) : "");
|
||||
html.replace("${display.accumulative}", tpi > 0 ? "" : "none");
|
||||
|
||||
double vcc = 0;
|
||||
#if defined(ESP8266)
|
||||
vcc = ((double) ESP.getVcc()) / 1000;
|
||||
#endif
|
||||
double vcc = hw.getVcc();
|
||||
html.replace("${vcc}", vcc > 0 ? String(vcc, 2) : "");
|
||||
|
||||
float rssi = WiFi.RSSI();
|
||||
@ -321,12 +318,8 @@ void AmsWebServer::dataJson() {
|
||||
json["maxPower"] = maxPwr;
|
||||
json["meterType"] = config->getMeterType();
|
||||
json["currentMillis"] = now;
|
||||
|
||||
double vcc = 0;
|
||||
#if defined(ESP8266)
|
||||
vcc = ((double) ESP.getVcc()) / 1000;
|
||||
#endif
|
||||
json["vcc"] = vcc;
|
||||
double vcc = hw.getVcc();
|
||||
json["vcc"] = vcc > 0 ? vcc : 0;
|
||||
|
||||
json.createNestedObject("wifi");
|
||||
float rssi = WiFi.RSSI();
|
||||
|
||||
@ -4,6 +4,7 @@
|
||||
#include <ArduinoJson.h>
|
||||
#include <MQTT.h>
|
||||
#include "AmsConfiguration.h"
|
||||
#include "HwTools.h"
|
||||
|
||||
#if defined(ARDUINO) && ARDUINO >= 100
|
||||
#include "Arduino.h"
|
||||
@ -28,6 +29,7 @@ public:
|
||||
void setJson(StaticJsonDocument<1024> json);
|
||||
|
||||
private:
|
||||
HwTools hw;
|
||||
AmsConfiguration* config;
|
||||
Stream* debugger;
|
||||
MQTTClient* mqtt;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user