131 lines
4 KiB
C++
131 lines
4 KiB
C++
#include <Arduino.h>
|
|
#include <WiFi.h>
|
|
#include <Wire.h>
|
|
#include <logger.h>
|
|
#include <display.h>
|
|
#include <pins.h>
|
|
#include <power_management.h>
|
|
#include <PubSubClient.h>
|
|
|
|
const char* WIFI_SSID = "IoT_Temp";
|
|
const char* WIFI_PASS = "xyz";
|
|
const char* mqtt_server = "10.1.0.9";
|
|
|
|
WiFiClient espClient;
|
|
PubSubClient client(espClient);
|
|
long lastMsg = 0;
|
|
char msg[50];
|
|
int value = 0;
|
|
|
|
float pv_v = 0, pv_a = 0, batt_v = 0, batt_i = 0, batt_p = 0, grid_p = 0, batt_soc = 0;
|
|
|
|
PowerManagement powerManagement;
|
|
|
|
void callback(char* topic, byte* message, unsigned int length) {
|
|
logPrintlnD("Message arrived on topic: ");
|
|
logPrintlnD(topic);
|
|
logPrintlnD("Message: ");
|
|
byte messageTemp;
|
|
|
|
for (int i = 0; i < length; i++) {
|
|
messageTemp += message[i];
|
|
}
|
|
if (String(topic) == "N/48e7da87c8df/solarcharger/279/Pv/V") {
|
|
//pv_v = messageTemp.toFloat();
|
|
logPrintlnI((char*)messageTemp);
|
|
}
|
|
}
|
|
|
|
void reconnect() {
|
|
// Loop until we're reconnected
|
|
while (!client.connected()) {
|
|
logPrintlnI("Attempting MQTT connection...");
|
|
show_display("PV Monitor", "MQTT Connection", "disconnected", "reconnecting...");
|
|
// Attempt to connect
|
|
if (client.connect("PV_Solar_Monitor")) {
|
|
Serial.println("connected");
|
|
// Subscribe
|
|
client.subscribe("N/48e7da87c8df/solarcharger/279/Pv/V");
|
|
client.subscribe("N/48e7da87c8df/solarcharger/279/Yield/Power");
|
|
client.subscribe("N/48e7da87c8df/solarcharger/279/Yield/System");
|
|
client.subscribe("N/48e7da87c8df/solarcharger/279/Dc/0/Current");
|
|
client.subscribe("N/48e7da87c8df/grid/40/Ac/Power");
|
|
client.subscribe("N/48e7da87c8df/vebus/276/Ac/ActiveIn/L1/P");
|
|
client.subscribe("N/48e7da87c8df/battery/512/Soc");
|
|
client.subscribe("N/48e7da87c8df/battery/512/Dc/0/Voltage");
|
|
client.subscribe("N/48e7da87c8df/battery/512/Dc/0/Current");
|
|
} else {
|
|
logPrintlnE("failed, rc=");
|
|
logPrintlnE((char*)client.state());
|
|
logPrintlnE(" try again in 5 seconds");
|
|
// Wait 5 seconds before retrying
|
|
delay(5000);
|
|
}
|
|
}
|
|
}
|
|
|
|
void setup() {
|
|
Serial.begin(115200);
|
|
logPrintlnI("Booting solar monitor app...");
|
|
logPrintlnI("Init power management...");
|
|
Wire.begin();
|
|
if (!powerManagement.begin(Wire)) {
|
|
logPrintlnI("AXP192 init done!");
|
|
} else {
|
|
logPrintlnE("AXP192 init failed!");
|
|
}
|
|
powerManagement.deactivateLoRa();
|
|
powerManagement.activateOLED();
|
|
powerManagement.deactivateGPS();
|
|
powerManagement.activateMeasurement();
|
|
|
|
logPrintlnI("Init display...");
|
|
setup_display();
|
|
show_display("PV Monitor", "is booting", "please wait", 3000);
|
|
logPrintlnI( "Set WLAN Mode to STA...");
|
|
WiFi.mode(WIFI_STA);
|
|
logPrintlnI("Resetting...");
|
|
WiFi.disconnect();
|
|
logPrintlnI("Connecting ...");
|
|
show_display("PV Monitor", "WiFi Connection", "connecting", "please wait", 500);
|
|
WiFi.begin(WIFI_SSID, WIFI_PASS);
|
|
while (WiFi.status() != WL_CONNECTED) {
|
|
Serial.println("Retry connection to WiFi..");
|
|
show_display("PV Monitor", "WiFi Connection", "retry connection", 500);
|
|
}
|
|
IPAddress ip = WiFi.localIP();
|
|
char* ip_address = new char[40]();
|
|
sprintf(ip_address, "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
|
|
|
|
show_display("PV Monitor", "WiFi Connection", "success", ip_address, 2000);
|
|
|
|
show_display("PV Monitor", "MQTT Connection", "connecting to cerbo", "please wait", 500);
|
|
client.setServer(mqtt_server, 1883);
|
|
client.setCallback(callback);
|
|
|
|
}
|
|
|
|
void loop() {
|
|
static bool BatteryIsConnected = false;
|
|
static String batteryVoltage = "";
|
|
static String batteryChargeCurrent = "";
|
|
|
|
static unsigned int rate_limit_check_battery = 0;
|
|
if (!(rate_limit_check_battery++ % 60))
|
|
BatteryIsConnected = powerManagement.isBatteryConnect();
|
|
if (BatteryIsConnected) {
|
|
batteryVoltage = String(powerManagement.getBatteryVoltage(), 2);
|
|
batteryChargeCurrent = String(powerManagement.getBatteryChargeDischargeCurrent(), 0);
|
|
}
|
|
|
|
if (powerManagement.isChargeing()) {
|
|
powerManagement.enableChgLed();
|
|
} else {
|
|
powerManagement.disableChgLed();
|
|
}
|
|
|
|
if (!client.connected()) {
|
|
reconnect();
|
|
}
|
|
client.loop();
|
|
} |