change config to YAML

fix errors in library implementation
add more functions to library
revert-2-UseTasks
Andrew Woodlee 4 months ago
parent 68471e0e47
commit 0d74682a16

4
.gitignore vendored

@ -1,4 +1,3 @@
.env
**.pem
.pio
include/transformerMonitorServerCert.h
@ -7,4 +6,5 @@ include/transformerMonitorServerCert.h
.vscode/.browse.c_cpp.db*
.vscode/c_cpp_properties.json
.vscode/launch.json
.vscode/ipch
.vscode/ipch
config.yml

@ -4,11 +4,11 @@ ssl:
wifi:
# Your WiFi SSID
ssid: "Your-WiFi-SSID"
# Your WiFi Password
passwd: Your-WiFi-Password
# Your WiFi Password
passwd: Your-Secret-WiFi-Password
mqtt:
# Your MQTT server
server: xformer.utilitymonitor.io
server: your.mqtt-broker.tld
# The port the MQTT server is running on
port: 8883
# Your MQTT User
@ -17,12 +17,20 @@ mqtt:
password: secure-mqtt-pass
# A unique ID
id: unique-id
# Only for the ATM90E36 board
# determines which line to read
# Determines which voltage needs to be set.
XFORMER_MON_LINE='A'
# To be set later:
XFORMER_MON_LINE='A'
sensor:
type: ATM90E36
config:
# voltage rms gain in hex. See docs/Gains.md for more information
ugain: 0x
# Voltage gain for CTs
lgain: 0x
# Voltage gain for neutral line
ngain: 0x
# power freqency
lfreq: 0x
# PGA Gain for current channels
pgagain: 0x
# Only for the ATM90E36 board
# determines which line to read
# Determines which voltage needs to be set.
line: "A"

@ -1,21 +1,5 @@
Import("env")
# dictionary of environment variable names with the names as the values
# the keys may be the same as the values
# the values must be defined in an .env file or the OS environment
# key (can be anything) : value (must be in the .env file or OS environment)
envVars = {
"WIFI_SSID": "XFORMER_MON_WIFI_SSID",
"WIFI_PASSWD": "XFORMER_MON_WIFI_PASSWD",
"SSL_ENABLED": "XFORMER_MON_USE_SSL",
"MQTT_PORT": "XFORMER_MON_MQTT_PORT",
"MQTT_SERVER": "XFORMER_MON_MQTT_SERVER",
"MQTT_USER": "XFORMER_MON_MQTT_USER",
"MQTT_PASS": "XFORMER_MON_MQTT_PASS",
"MQTT_ID": "XFORMER_MON_MQTT_ID",
"TM_CT": "XFORMER_MON_LINE", # if not set, will default to 'A' in code
}
import socket
import sys
@ -35,46 +19,42 @@ with open('config.yml', 'r') as file:
import os
import ssl
wifiConfig = monitorConfigFile["wifi"]
mqttServer = os.getenv(envVars["MQTT_SERVER"])
mqttUser = os.getenv(envVars["MQTT_USER"])
mqttPass = os.getenv(envVars["MQTT_PASS"])
transformerCT = os.getenv(envVars["TM_CT"])
sslEnabled = os.getenv(envVars["SSL_ENABLED"]) == "enabled"
mqttPort = 1883
wifiConfig = monitorConfigFile.get("wifi")
mqttConfig = monitorConfigFile.get("mqtt")
mqttServer = mqttConfig.get("server")
mqttPort = mqttConfig.get("port")
sensorConfig = monitorConfigFile.get("sensor")
transformerLine = sensorConfig['config']['line']
sslEnabled = monitorConfigFile["ssl"]['enabled']
if not sslEnabled:
os.environ[envVars["MQTT_PORT"]] = "1883"
mqttConfig["port"] = 1883
else:
os.environ[envVars["MQTT_PORT"]] = "8883"
mqttPort = 8883
wifiSSID = os.getenv(envVars["WIFI_SSID"])
wifiPasswd = os.getenv(envVars["WIFI_PASSWD"])
mqttConfig["port"] = 8883
if wifiSSID == None:
print(f"Environment variable {envVars['WIFI_SSID']} variable not defined. Define it in a .env file at the root of the project.")
if wifiConfig.get("ssid") == None:
print(f"wifi object variable SSID not defined. Define it in the config.yml file at the root of the project.")
os._exit(1)
if wifiPasswd == None:
print(f"Environment variable {envVars['WIFI_PASSWD']} variable not defined. Define it in a .env file at the root of the project.")
if wifiConfig["password"] == None:
print(f"wifi object variable password not defined. Define it in the config.yml file at the root of the project.")
os._exit(1)
if mqttServer == None:
print(f"Environment variable {envVars['MQTT_SERVER']} variable not defined. Define it in a .env file at the root of the project.")
if mqttConfig["server"] == None:
print(f"mqtt object variable server not defined. Define it in the config.yml file at the root of the project.")
os._exit(1)
env.Append(CPPDEFINES=[
("TM_WIFI_SSID", wifiSSID ),
("TM_WIFI_PASSWD", wifiPasswd ),
("TM_MQTT_PORT", mqttPort),
("TM_MQTT_SVR", mqttServer),
("TM_MQTT_USER", mqttUser),
("TM_MQTT_PASSWD", mqttPass),
("TM_MQTT_CT", transformerCT)
("TM_WIFI_SSID", wifiConfig.get('ssid')),
("TM_WIFI_PASSWD", wifiConfig.get('password') ),
("TM_MQTT_PORT", mqttConfig.get('port')),
("TM_MQTT_SVR", mqttConfig.get("server")),
("TM_MQTT_USER", mqttConfig.get("user")),
("TM_MQTT_PASSWD", mqttConfig.get("password")),
("TM_CT", transformerLine)
])
if sslEnabled:

@ -64,12 +64,15 @@ ATM90E26_IC eic;
#else
// extract the ATM90E36 CT LINE from its macro
#ifdef TM_ATM90E36_CT_LINE
char const *ctLine = STR(TM_ATM90E36_CT_LINE);
char const ctLine = (char) STR(TM_ATM90E36_CT_LINE);
#else
char const ctLine = 'A';
#endif
#include <ATM90E36_IC.h>
ATM90E36_IC eic;
ATM90E36 ic;
ATM90E36_IC eic(ctLine, ic);
ATM90E36_IC SetupEic(ctLine, ic);
#endif
@ -99,4 +102,4 @@ struct tempSensors { // Structure declaration
tempSensors monitorTempSensors{cabinetTemp, oilTemp};
StaticJsonDocument<256> dataStore[60];
// StaticJsonDocument<256> dataStore[60];

@ -1,9 +1,8 @@
#include "ATM90E36_IC.h"
ATM90E36_IC::ATM90E36_IC(char ctLineLetter) {
ATM90E36_IC::ATM90E36_IC(const char ctLineLetter, ATM90E36 ic) {
ATM90E36 ic;
this->eic = &ic;
switch (ctLineLetter)
@ -14,39 +13,29 @@
this->ctLine = ctLineLetter;
break;
}
/*
The ATM90E36 has to be setup via SPI.
SPI for the ESP32:
- MOSI: 23
- MISO: 19
- CLK: 18
- CS: 5
*/
// use the ATM90E36 over SPI
eic->begin(10, 0x60,0x060,0x060,0x060, 0x060,0x60, 0x60 );
}
void ATM90E36_IC::begin(){
this->eic->begin(5, 0x003C, 0x100, 0x100, 0x100,0x100, 0x100, 0x100);
}
double ATM90E36_IC::GetLineVoltage()
{
switch (ctLine)
switch (ctLine)
{
case 'A':
this->meterStatus = eic->GetLineVoltageA();
return eic->GetLineVoltageA();
case 'B':
this->meterStatus = eic->GetLineVoltageB();
return eic->GetLineVoltageB();
case 'C':
this->meterStatus = eic->GetLineVoltageC();
return eic->GetLineVoltageC();
break;
default:
status = 0;
break;
}
this->meterStatus = eic->GetLineVoltageA();
return meterStatus;
}
double ATM90E36_IC::GetSysStatus()
{
return eic->GetSysStatus0();
}
double ATM90E36_IC::GetActivePower()
@ -54,3 +43,21 @@
this->meterStatus = eic->GetActivePowerA();
return meterStatus;
}
double ATM90E36_IC::GetActivePower()
{
double ap;
switch (ctLine)
{
case 'A':
ap = eic->GetActivePowerA();
break;
case 'B':
ap = eic->GetActivePowerB();
break;
case 'C':
ap = this->eic->GetActivePowerC();
break;
}
return ap;
}

@ -9,9 +9,12 @@ private:
int status;
public:
ATM90E36 *eic;
ATM90E36_IC(char ctLineLetter);
ATM90E36_IC(const char ctLineLetter, ATM90E36 ic);
void begin();
double GetLineVoltage();
double GetActivePower();
double GetSysStatus();
double GetPassivePower();
};

@ -14,7 +14,7 @@ board = esp32doit-devkit-v1
framework = arduino
extra_scripts = pre:envSetup.py
monitor_speed = 115200
upload_port = COM5
upload_port = COM4
[env:dev]
build_flags = -D DEV ${env.build_flags}

@ -25,7 +25,6 @@ void setup() {
- MOSI: 23
- CS: 5
*/
SPI.begin(SCK, MISO, MOSI, SS);
delay(2000);
Serial.begin(115200);
while (!Serial) {
@ -35,7 +34,9 @@ void setup() {
Serial.println("Start ATM90E36");
/*Initialise the ATM90E36 + SPI port */
// ss pin is the first parameter
eic.begin(5, 60, 100, 100, 100,100, 100, 100);
SPI.begin(SCK, MISO, MOSI, SS);
delay(1000);
eic.begin(5, 0x003C, 0x100, 0x100, 0x100,0x100, 0x100, 0x100);
delay(1000);
}

@ -17,30 +17,52 @@ void setupEnergyMonitor();
// const char* test_client_cert = ""; //to verify the client
void setup()
{
// configure time
// TODO: make dst and timezone configurable
int timezone = 3;
int dst = 0;
configTime(timezone * 3600, dst * 0, "pool.ntp.org", "time.nist.gov");
// Initialize serial and wait for port to open:
Serial.begin(115200);
while (!Serial)
{
}
// while (!Serial)
// {
// }
delay(1000);
delay(20000);
#ifdef TM_MQTT_SSL
wifiClient.setCACert(root_ca);
#endif
// Start the DS18B20 sensors
monitorTempSensors.cabinet.begin();
monitorTempSensors.oil.begin();
// monitorTempSensors.cabinet.begin();
// monitorTempSensors.oil.begin();
setupMQTTClient();
#ifdef ATM90E26_EIC
ATM90E26_IC eic;
#else
/* Initialize the serial port to host */
/*
The ATM90E36 has to be setup via SPI.
SPI for the ESP32:
- CLK: 18
- MISO: 19
- MOSI: 23
- CS: 5
*/
SPI.begin(SCK, MISO, MOSI, SS);
delay(1000);
eic.begin();
#endif
}
void connect()
@ -80,7 +102,6 @@ void connect()
Serial.println("\nconnected!");
// mqttClient.subscribe("/transformer-mon");
}
void messageReceived(String &topic, String &payload)
@ -95,6 +116,8 @@ void messageReceived(String &topic, String &payload)
void loop()
{
lastMillis = millis();
mqttClient.loop();
delay(10); // <- fixes some issues with WiFi stability
@ -103,19 +126,17 @@ void loop()
connect();
}
mqttClient.publish("xfmormermon", "buffer");
// mqttClient.publish("xfmormermon", "buffer");
// publish a message roughly every second.
// // publish a message roughly every second.
if (millis() - lastMillis > 1000)
{
lastMillis = millis();
monitorTempSensors.cabinet.requestTemperatures();
monitorTempSensors.oil.requestTemperatures();
// unsigned short volts = getEICSysStatus();
unsigned short volts = 121.2;
// float cabinetTemperatureC = monitorTempSensors.cabinet.getTempCByIndex(0);
// float cabinetTemperatureF = monitorTempSensors.cabinet.getTempFByIndex(0);
float cabinetTemperatureC = monitorTempSensors.cabinet.getTempCByIndex(0);
float cabinetTemperatureF = monitorTempSensors.cabinet.getTempFByIndex(0);
StaticJsonDocument<256> doc;
// Get the current time and store it in a variable
time_t now;
@ -127,17 +148,20 @@ void loop()
// set {"time":"2021-05-04T13:13:04Z"}
doc["time"] = timeBuffer;
doc["meterStatus"] = eic.GetLineVoltage();
// doc["meterStatus"] = 40;
doc["voltage"] = volts;
JsonObject temp = doc.createNestedObject("temps");
temp["cabinet"] = monitorTempSensors.cabinet.getTempCByIndex(0);
temp["oil"] = monitorTempSensors.oil.getTempCByIndex(0);
temp["cabinet"] = 40;
temp["oil"] = 70;
// temp["cabinet"] = monitorTempSensors.cabinet.getTempCByIndex(0);
// temp["oil"] = monitorTempSensors.oil.getTempCByIndex(0);
dataStore->add(doc);
// dataStore->add(doc);
char mqttBuffer[256];
serializeJson(doc, mqttBuffer);
mqttClient.publish("xfomermon/", mqttBuffer);
mqttClient.publish("xfomermon", mqttBuffer);
}
Serial.println("Sleeping 10s");
delay(10000);

Loading…
Cancel
Save