Changes to make Aidon work. Will clean up later

This commit is contained in:
Gunnar Skjold 2019-02-12 21:07:18 +01:00
parent 0471e61ba5
commit ab6b7e7724

View File

@ -10,6 +10,7 @@
#include <ArduinoJson.h>
#include <PubSubClient.h>
#include <HanReader.h>
#include <Aidon.h>
#include <Kaifa.h>
#include <Kamstrup.h>
#include "configuration.h"
@ -66,7 +67,7 @@ void setup()
if (!ap.isActivated)
{
setupWiFi();
hanReader.setup(&Serial, 2400, SERIAL_8E1, debugger);
hanReader.setup(&Serial, 2400, SERIAL_8E1, 0);
// Compensate for the known Kaifa bug
hanReader.compensateFor09HeaderBug = (ap.config.meterType == 1);
@ -112,6 +113,7 @@ void setupWiFi()
WiFi.enableAP(false);
// Connect to WiFi
WiFi.mode(WIFI_STA);
WiFi.begin(ap.config.ssid, ap.config.ssidPassword);
while (WiFi.status() != WL_CONNECTED) {
@ -119,9 +121,9 @@ void setupWiFi()
}
// Initialize WiFi and MQTT clients
if (ap.config.isSecure())
client = new WiFiClientSecure();
else
// if (ap.config.isSecure())
// client = new WiFiClientSecure();
// else
client = new WiFiClient();
mqtt = PubSubClient(*client);
mqtt.setServer(ap.config.mqtt, ap.config.mqttPort);
@ -193,79 +195,75 @@ void readHanPort()
void readHanPort_Aidon(int listSize)
{
if (listSize == (int)Aidon::List1 || listSize == (int)Aidon::List2 || listSize == (int)Aidon::List3)
{
if (listSize == (int)Aidon::List1)
{
if (debugger) debugger->println(" (list #1 has no ID)");
}
else
{
String id = hanReader.getString((int)Aidon_List2::ListVersionIdentifier);
if (debugger) debugger->println(id);
}
if (listSize == (int)Aidon::List1 || listSize == (int)Aidon::List2)
{
if (listSize == (int)Aidon::List2)
{
String id = hanReader.getString((int)Aidon_List2::ListVersionIdentifier);
if (debugger) debugger->println(id);
}
// Get the timestamp (as unix time) from the package
time_t time = hanReader.getPackageTime();
if (debugger) debugger->print("Time of the package is: ");
if (debugger) debugger->println(time);
// Get the timestamp (as unix time) from the package
time_t time = hanReader.getPackageTime();
if (debugger) debugger->print("Time of the package is: ");
if (debugger) debugger->println(time);
// Define a json object to keep the data
//StaticJsonBuffer<500> jsonBuffer;
DynamicJsonBuffer jsonBuffer;
JsonObject& root = jsonBuffer.createObject();
// Define a json object to keep the data
StaticJsonBuffer<500> jsonBuffer;
JsonObject& root = jsonBuffer.createObject();
// Any generic useful info here
root["id"] = WiFi.macAddress();
root["up"] = millis();
root["t"] = time;
// Any generic useful info here
// root["id"] = WiFi.macAddress();
// root["up"] = millis();
// root["t"] = time;
// Add a sub-structure to the json object,
// to keep the data from the meter itself
JsonObject& data = root.createNestedObject("data");
// Add a sub-structure to the json object,
// to keep the data from the meter itself
JsonObject& data = root.createNestedObject("data");
// Get the temperature too
tempSensor.requestTemperatures();
float temperature = tempSensor.getTempCByIndex(0);
data["temp"] = String(temperature);
// Get the temperature too
tempSensor.requestTemperatures();
float temperature = tempSensor.getTempCByIndex(0);
data["temp"] = temperature;
// Based on the list number, get all details
// according to OBIS specifications for the meter
if (listSize == (int)Aidon::List1)
{
data["P"] = hanReader.getInt((int)Aidon_List1::ActivePowerImported);
}
else if (listSize == (int)Aidon::List2)
{
data["lv"] = hanReader.getString((int)Aidon_List2::ListVersionIdentifier);
data["id"] = hanReader.getString((int)Aidon_List2::MeterID);
data["type"] = hanReader.getString((int)Aidon_List2::MeterType);
data["P"] = hanReader.getInt((int)Aidon_List2::ActiveImportPower);
data["Q"] = hanReader.getInt((int)Aidon_List2::ReactiveImportPower);
data["I1"] = hanReader.getInt((int)Aidon_List2::CurrentL1);
data["I2"] = hanReader.getInt((int)Aidon_List2::CurrentL2);
data["I3"] = hanReader.getInt((int)Aidon_List2::CurrentL3);
data["U1"] = hanReader.getInt((int)Aidon_List2::VoltageL1);
data["U2"] = hanReader.getInt((int)Aidon_List2::VoltageL2);
data["U3"] = hanReader.getInt((int)Aidon_List2::VoltageL3);
}
// Based on the list number, get all details
// according to OBIS specifications for the meter
if (listSize == (int)Aidon::List1)
{
data["P"] = hanReader.getInt((int)Aidon_List1::ActiveImportPower);
}
else if (listSize == (int)Aidon::List2)
{
// data["lv"] = hanReader.getString((int)Aidon_List2::ListVersionIdentifier);
// data["id"] = hanReader.getString((int)Aidon_List2::MeterID);
// data["type"] = hanReader.getString((int)Aidon_List2::MeterType);
data["P"] = hanReader.getInt((int)Aidon_List2::ActiveImportPower);
data["Q"] = hanReader.getInt((int)Aidon_List2::ReactiveExportPower);
data["I1"] = ((double) hanReader.getInt((int)Aidon_List2::CurrentL1)) / 10;
data["I2"] = ((double) hanReader.getInt((int)Aidon_List2::CurrentL2)) / 10;
data["I3"] = ((double) hanReader.getInt((int)Aidon_List2::CurrentL3)) / 10;
data["U1"] = ((double) hanReader.getInt((int)Aidon_List2::VoltageL1)) / 10;
data["U2"] = ((double) hanReader.getInt((int)Aidon_List2::VoltageL2)) / 10;
data["U3"] = ((double) hanReader.getInt((int)Aidon_List2::VoltageL3)) / 10;
}
// Write the json to the debug port
if (debugger) {
debugger->print("Sending data to MQTT: ");
root.printTo(*debugger);
debugger->println();
}
// Write the json to the debug port
if (debugger) {
debugger->print("Sending data to MQTT: ");
root.printTo(*debugger);
debugger->println();
}
// Make sure we have configured a publish topic
if (ap.config.mqttPublishTopic == 0 || strlen(ap.config.mqttPublishTopic) == 0)
return;
// Make sure we have configured a publish topic
if (ap.config.mqttPublishTopic == 0 || strlen(ap.config.mqttPublishTopic) == 0)
return;
// Publish the json to the MQTT server
char msg[1024];
root.printTo(msg, 1024);
mqtt.publish(ap.config.mqttPublishTopic, msg);
}
// Publish the json to the MQTT server
char msg[1024];
root.printTo(msg, 1024);
mqtt.publish(ap.config.mqttPublishTopic, msg);
mqtt.loop();
}
}
void readHanPort_Kamstrup(int listSize)
@ -539,11 +537,13 @@ void MQTT_connect()
// Wait 2 seconds before retrying
mqtt.disconnect();
delay(2000);
}
// Allow some resources for the WiFi connection
yield();
delay(2000);
}
}
@ -572,4 +572,4 @@ void sendMqttData(String data)
// Send the json over MQTT
mqtt.publish(ap.config.mqttPublishTopic, msg.c_str());
}
}