mirror of
https://github.com/UtilitechAS/amsreader-firmware.git
synced 2026-02-14 03:55:13 +00:00
Moved configuration object to main file and load the config before start. Fixed issue where partiy is switched for Kamstrup half way when in debug mode
This commit is contained in:
@@ -3,15 +3,16 @@
|
||||
Stream* HanConfigAp::debugger;
|
||||
|
||||
bool HanConfigAp::hasConfig() {
|
||||
return config.hasConfig();
|
||||
return config->hasConfig();
|
||||
}
|
||||
|
||||
void HanConfigAp::setup(int accessPointButtonPin, Stream* debugger)
|
||||
void HanConfigAp::setup(int accessPointButtonPin, configuration* config, Stream* debugger)
|
||||
{
|
||||
this->debugger = debugger;
|
||||
this->config = config;
|
||||
|
||||
// Test if we're missing configuration
|
||||
if (!config.hasConfig())
|
||||
if (!config->hasConfig())
|
||||
{
|
||||
print("No config. We're booting as AP. Look for SSID ");
|
||||
println(this->AP_SSID);
|
||||
@@ -20,8 +21,7 @@ void HanConfigAp::setup(int accessPointButtonPin, Stream* debugger)
|
||||
else
|
||||
{
|
||||
// Load the configuration
|
||||
config.load();
|
||||
if (this->debugger) config.print(this->debugger);
|
||||
if (this->debugger) config->print(this->debugger);
|
||||
|
||||
if (accessPointButtonPin != INVALID_BUTTON_PIN)
|
||||
{
|
||||
|
||||
@@ -24,15 +24,16 @@
|
||||
|
||||
class HanConfigAp {
|
||||
public:
|
||||
void setup(int accessPointButtonPin, Stream* debugger);
|
||||
void setup(int accessPointButtonPin, configuration* config, Stream* debugger);
|
||||
bool loop();
|
||||
bool hasConfig();
|
||||
configuration config;
|
||||
bool isActivated = false;
|
||||
|
||||
private:
|
||||
const char* AP_SSID = "AMS2MQTT";
|
||||
|
||||
configuration* config;
|
||||
|
||||
// DNS server
|
||||
const byte DNS_PORT = 53;
|
||||
DNSServer dnsServer;
|
||||
|
||||
@@ -24,11 +24,18 @@ bool configuration::save()
|
||||
address += saveString(address, ssid);
|
||||
address += saveString(address, ssidPassword);
|
||||
address += saveByte(address, meterType);
|
||||
address += saveString(address, mqttHost);
|
||||
address += saveInt(address, mqttPort);
|
||||
address += saveString(address, mqttClientID);
|
||||
address += saveString(address, mqttPublishTopic);
|
||||
address += saveString(address, mqttSubscribeTopic);
|
||||
|
||||
|
||||
if(mqttHost) {
|
||||
address += saveBool(address, true);
|
||||
address += saveString(address, mqttHost);
|
||||
address += saveInt(address, mqttPort);
|
||||
address += saveString(address, mqttClientID);
|
||||
address += saveString(address, mqttPublishTopic);
|
||||
address += saveString(address, mqttSubscribeTopic);
|
||||
} else {
|
||||
address += saveBool(address, false);
|
||||
}
|
||||
|
||||
if (isSecure()) {
|
||||
address += saveBool(address, true);
|
||||
@@ -60,13 +67,13 @@ bool configuration::load()
|
||||
int address = EEPROM_CONFIG_ADDRESS;
|
||||
bool success = false;
|
||||
|
||||
ssid = (char*)String("").c_str();
|
||||
ssidPassword = (char*)String("").c_str();
|
||||
ssid = 0;
|
||||
ssidPassword = 0;
|
||||
meterType = (byte)0;
|
||||
mqttHost = (char*)String("").c_str();
|
||||
mqttClientID = (char*)String("").c_str();
|
||||
mqttPublishTopic = (char*)String("").c_str();
|
||||
mqttSubscribeTopic = (char*)String("").c_str();
|
||||
mqttHost = 0;
|
||||
mqttClientID = 0;
|
||||
mqttPublishTopic = 0;
|
||||
mqttSubscribeTopic = 0;
|
||||
mqttUser = 0;
|
||||
mqttPass = 0;
|
||||
mqttPort = 1883;
|
||||
@@ -78,22 +85,26 @@ bool configuration::load()
|
||||
|
||||
EEPROM.begin(EEPROM_SIZE);
|
||||
int cs = EEPROM.read(address);
|
||||
if (cs >= 71)
|
||||
if (cs == EEPROM_CHECK_SUM)
|
||||
{
|
||||
address++;
|
||||
|
||||
address += readString(address, &ssid);
|
||||
address += readString(address, &ssidPassword);
|
||||
address += readByte(address, &meterType);
|
||||
address += readString(address, &mqttHost);
|
||||
address += readInt(address, &mqttPort);
|
||||
address += readString(address, &mqttClientID);
|
||||
address += readString(address, &mqttPublishTopic);
|
||||
address += readString(address, &mqttSubscribeTopic);
|
||||
|
||||
bool mqtt = false;
|
||||
address += readBool(address, &mqtt);
|
||||
if(mqtt) {
|
||||
address += readString(address, &mqttHost);
|
||||
address += readInt(address, &mqttPort);
|
||||
address += readString(address, &mqttClientID);
|
||||
address += readString(address, &mqttPublishTopic);
|
||||
address += readString(address, &mqttSubscribeTopic);
|
||||
}
|
||||
|
||||
bool secure = false;
|
||||
address += readBool(address, &secure);
|
||||
|
||||
if (secure)
|
||||
{
|
||||
address += readString(address, &mqttUser);
|
||||
@@ -105,9 +116,6 @@ bool configuration::load()
|
||||
mqttPass = 0;
|
||||
}
|
||||
|
||||
success = true;
|
||||
}
|
||||
if(cs >= 72) {
|
||||
address += readByte(address, &authSecurity);
|
||||
if (authSecurity > 0) {
|
||||
address += readString(address, &authUser);
|
||||
@@ -116,12 +124,11 @@ bool configuration::load()
|
||||
authUser = 0;
|
||||
authPass = 0;
|
||||
}
|
||||
}
|
||||
if(cs >= 73) {
|
||||
|
||||
address += readInt(address, &fuseSize);
|
||||
}
|
||||
if(cs >= 74) {
|
||||
address += readByte(address, &distSys);
|
||||
|
||||
success = true;
|
||||
}
|
||||
EEPROM.end();
|
||||
return success;
|
||||
|
||||
@@ -42,7 +42,7 @@ protected:
|
||||
|
||||
private:
|
||||
const int EEPROM_SIZE = 512;
|
||||
const byte EEPROM_CHECK_SUM = 74; // Used to check if config is stored. Change if structure changes
|
||||
const byte EEPROM_CHECK_SUM = 75; // Used to check if config is stored. Change if structure changes
|
||||
const int EEPROM_CONFIG_ADDRESS = 0;
|
||||
|
||||
int saveString(int pAddress, char* pString);
|
||||
|
||||
@@ -46,6 +46,8 @@ OneWire oneWire(TEMP_SENSOR_PIN);
|
||||
DallasTemperature tempSensor(&oneWire);
|
||||
#endif
|
||||
|
||||
configuration config;
|
||||
|
||||
// Object used to boot as Access Point
|
||||
HanConfigAp ap;
|
||||
|
||||
@@ -68,11 +70,18 @@ void setup() {
|
||||
debugger = &Serial;
|
||||
#endif
|
||||
|
||||
if(config.hasConfig()) {
|
||||
config.load();
|
||||
}
|
||||
|
||||
if(config.meterType == 3) {
|
||||
Serial.begin(2400, SERIAL_8N1);
|
||||
} else {
|
||||
Serial.begin(2400, SERIAL_8E1);
|
||||
}
|
||||
while (!Serial);
|
||||
|
||||
if (debugger) {
|
||||
// Setup serial port for debugging
|
||||
debugger->begin(2400, SERIAL_8E1);
|
||||
//debugger->begin(115200);
|
||||
while (!debugger);
|
||||
debugger->println("");
|
||||
debugger->println("Started...");
|
||||
}
|
||||
@@ -84,7 +93,7 @@ void setup() {
|
||||
delay(1000);
|
||||
|
||||
// Initialize the AP
|
||||
ap.setup(AP_BUTTON_PIN, debugger);
|
||||
ap.setup(AP_BUTTON_PIN, &config, debugger);
|
||||
|
||||
led_off();
|
||||
|
||||
@@ -92,27 +101,21 @@ void setup() {
|
||||
{
|
||||
setupWiFi();
|
||||
|
||||
if(ap.config.mqttHost) {
|
||||
mqtt.begin(ap.config.mqttHost, *client);
|
||||
if(config.mqttHost) {
|
||||
mqtt.begin(config.mqttHost, *client);
|
||||
|
||||
// Notify everyone we're here!
|
||||
sendMqttData("Connected!");
|
||||
}
|
||||
// Configure uart for AMS data
|
||||
if(ap.config.meterType == 3) {
|
||||
Serial.begin(2400, SERIAL_8N1);
|
||||
} else {
|
||||
Serial.begin(2400, SERIAL_8E1);
|
||||
}
|
||||
while (!Serial);
|
||||
|
||||
hanReader.setup(&Serial, debugger);
|
||||
|
||||
// Compensate for the known Kaifa bug
|
||||
hanReader.compensateFor09HeaderBug = (ap.config.meterType == 1);
|
||||
hanReader.compensateFor09HeaderBug = (config.meterType == 1);
|
||||
}
|
||||
|
||||
ws.setup(&ap.config, debugger);
|
||||
ws.setup(&config, debugger);
|
||||
}
|
||||
|
||||
// the loop function runs over and over again until power down or reset
|
||||
@@ -129,7 +132,7 @@ void loop()
|
||||
WiFi_connect();
|
||||
}
|
||||
|
||||
if (ap.config.mqttHost) {
|
||||
if (config.mqttHost) {
|
||||
mqtt.loop();
|
||||
delay(10); // <- fixes some issues with WiFi stability
|
||||
if(!mqtt.connected()) {
|
||||
@@ -175,7 +178,7 @@ void setupWiFi()
|
||||
|
||||
// Connect to WiFi
|
||||
WiFi.mode(WIFI_STA);
|
||||
WiFi.begin(ap.config.ssid, ap.config.ssidPassword);
|
||||
WiFi.begin(config.ssid, config.ssidPassword);
|
||||
|
||||
// Wait for WiFi connection
|
||||
if (debugger) debugger->print("\nWaiting for WiFi to connect...");
|
||||
@@ -205,7 +208,7 @@ void mqttMessageReceived(String &topic, String &payload)
|
||||
|
||||
void readHanPort()
|
||||
{
|
||||
if (hanReader.read() && ap.config.hasConfig())
|
||||
if (hanReader.read() && config.hasConfig())
|
||||
{
|
||||
// Flash LED on, this shows us that data is received
|
||||
led_on();
|
||||
@@ -233,9 +236,9 @@ void readHanPort()
|
||||
data["temp"] = tempSensor.getTempCByIndex(0);
|
||||
#endif
|
||||
|
||||
hanToJson(data, ap.config.meterType, hanReader);
|
||||
hanToJson(data, config.meterType, hanReader);
|
||||
|
||||
if(ap.config.mqttHost != 0 && strlen(ap.config.mqttHost) != 0 && ap.config.mqttPublishTopic != 0 && strlen(ap.config.mqttPublishTopic) != 0) {
|
||||
if(config.mqttHost != 0 && strlen(config.mqttHost) != 0 && config.mqttPublishTopic != 0 && strlen(config.mqttPublishTopic) != 0) {
|
||||
// Write the json to the debug port
|
||||
if (debugger) {
|
||||
debugger->print("Sending data to MQTT: ");
|
||||
@@ -247,7 +250,7 @@ void readHanPort()
|
||||
String msg;
|
||||
serializeJson(json, msg);
|
||||
|
||||
mqtt.publish(ap.config.mqttPublishTopic, msg.c_str());
|
||||
mqtt.publish(config.mqttPublishTopic, msg.c_str());
|
||||
mqtt.loop();
|
||||
}
|
||||
ws.setJson(json);
|
||||
@@ -264,14 +267,14 @@ void WiFi_connect() {
|
||||
debugger->println();
|
||||
debugger->println();
|
||||
debugger->print("Connecting to WiFi network ");
|
||||
debugger->println(ap.config.ssid);
|
||||
debugger->println(config.ssid);
|
||||
}
|
||||
|
||||
if (WiFi.status() != WL_CONNECTED)
|
||||
{
|
||||
// Make one first attempt at connect, this seems to considerably speed up the first connection
|
||||
WiFi.disconnect();
|
||||
WiFi.begin(ap.config.ssid, ap.config.ssidPassword);
|
||||
WiFi.begin(config.ssid, config.ssidPassword);
|
||||
delay(1000);
|
||||
}
|
||||
|
||||
@@ -290,7 +293,7 @@ void WiFi_connect() {
|
||||
debugger->println(WiFi.status());
|
||||
}
|
||||
WiFi.disconnect();
|
||||
WiFi.begin(ap.config.ssid, ap.config.ssidPassword);
|
||||
WiFi.begin(config.ssid, config.ssidPassword);
|
||||
vTimeout = millis() + WIFI_CONNECTION_TIMEOUT;
|
||||
}
|
||||
yield();
|
||||
@@ -310,24 +313,24 @@ void MQTT_connect()
|
||||
{
|
||||
if(debugger) {
|
||||
debugger->print("Connecting to MQTT: ");
|
||||
debugger->print(ap.config.mqttHost);
|
||||
debugger->print(config.mqttHost);
|
||||
debugger->print(", port: ");
|
||||
debugger->print(ap.config.mqttPort);
|
||||
debugger->print(config.mqttPort);
|
||||
debugger->println();
|
||||
}
|
||||
// Wait for the MQTT connection to complete
|
||||
while (!mqtt.connected()) {
|
||||
// Connect to a unsecure or secure MQTT server
|
||||
if ((ap.config.mqttUser == 0 && mqtt.connect(ap.config.mqttClientID)) ||
|
||||
(ap.config.mqttUser != 0 && mqtt.connect(ap.config.mqttClientID, ap.config.mqttUser, ap.config.mqttPass)))
|
||||
if ((config.mqttUser == 0 && mqtt.connect(config.mqttClientID)) ||
|
||||
(config.mqttUser != 0 && mqtt.connect(config.mqttClientID, config.mqttUser, config.mqttPass)))
|
||||
{
|
||||
if (debugger) debugger->println("\nSuccessfully connected to MQTT!");
|
||||
|
||||
// Subscribe to the chosen MQTT topic, if set in configuration
|
||||
if (ap.config.mqttSubscribeTopic != 0 && strlen(ap.config.mqttSubscribeTopic) > 0)
|
||||
if (config.mqttSubscribeTopic != 0 && strlen(config.mqttSubscribeTopic) > 0)
|
||||
{
|
||||
mqtt.subscribe(ap.config.mqttSubscribeTopic);
|
||||
if (debugger) debugger->printf(" Subscribing to [%s]\r\n", ap.config.mqttSubscribeTopic);
|
||||
mqtt.subscribe(config.mqttSubscribeTopic);
|
||||
if (debugger) debugger->printf(" Subscribing to [%s]\r\n", config.mqttSubscribeTopic);
|
||||
}
|
||||
}
|
||||
else
|
||||
@@ -355,7 +358,7 @@ void MQTT_connect()
|
||||
void sendMqttData(String data)
|
||||
{
|
||||
// Make sure we have configured a publish topic
|
||||
if (ap.config.mqttPublishTopic == 0 || strlen(ap.config.mqttPublishTopic) == 0)
|
||||
if (config.mqttPublishTopic == 0 || strlen(config.mqttPublishTopic) == 0)
|
||||
return;
|
||||
|
||||
// Make sure we're connected
|
||||
@@ -374,7 +377,7 @@ void sendMqttData(String data)
|
||||
serializeJson(json, msg);
|
||||
|
||||
// Send the json over MQTT
|
||||
mqtt.publish(ap.config.mqttPublishTopic, msg.c_str());
|
||||
mqtt.publish(config.mqttPublishTopic, msg.c_str());
|
||||
|
||||
if (debugger) debugger->print("sendMqttData: ");
|
||||
if (debugger) debugger->println(data);
|
||||
|
||||
Reference in New Issue
Block a user