First try mqtt subscribe

This commit is contained in:
Carsten Schmiemann 2022-08-22 01:54:57 +02:00
parent f2dc02b91e
commit 829c70623d
7 changed files with 438 additions and 2 deletions

View File

@ -12,3 +12,10 @@
platform = espressif32
board = ttgo-t-beam
framework = arduino
lib_deps =
knolleary/PubSubClient@^2.8
adafruit/Adafruit GFX Library@^1.11.3
adafruit/Adafruit SSD1306@^2.5.7
extentsoftware/TBeamPower@^2.0.4
peterus/esp-logger @ 0.0.1
monitor_speed = 115200

158
src/display.cpp Normal file
View File

@ -0,0 +1,158 @@
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Wire.h>
#include <logger.h>
#include "display.h"
#include "pins.h"
Adafruit_SSD1306 display(128, 64, &Wire, OLED_RST);
// cppcheck-suppress unusedFunction
void setup_display() {
pinMode(OLED_RST, OUTPUT);
digitalWrite(OLED_RST, LOW);
delay(20);
digitalWrite(OLED_RST, HIGH);
Wire.begin(OLED_SDA, OLED_SCL);
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3c, false, false)) {
logPrintlnE( "SSD1306 allocation failed");
while (true) {
}
}
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(1);
display.setCursor(0, 0);
display.ssd1306_command(SSD1306_SETCONTRAST);
display.ssd1306_command(1);
display.display();
}
// cppcheck-suppress unusedFunction
void display_toggle(bool toggle) {
logPrintlnI("Toggling display: ");
if (toggle) {
logPrintlnI("On");
display.ssd1306_command(SSD1306_DISPLAYON);
} else {
logPrintlnI("Off");
display.ssd1306_command(SSD1306_DISPLAYOFF);
}
}
// cppcheck-suppress unusedFunction
void show_display(String header, int wait) {
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(2);
display.setCursor(0, 0);
display.println(header);
display.ssd1306_command(SSD1306_SETCONTRAST);
display.ssd1306_command(1);
display.display();
delay(wait);
}
// cppcheck-suppress unusedFunction
void show_display(String header, String line1, int wait) {
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(2);
display.setCursor(0, 0);
display.println(header);
display.setTextSize(1);
display.setCursor(0, 16);
display.println(line1);
display.ssd1306_command(SSD1306_SETCONTRAST);
display.ssd1306_command(1);
display.display();
delay(wait);
}
// cppcheck-suppress unusedFunction
void show_display(String header, String line1, String line2, int wait) {
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(2);
display.setCursor(0, 0);
display.println(header);
display.setTextSize(1);
display.setCursor(0, 16);
display.println(line1);
display.setCursor(0, 26);
display.println(line2);
display.ssd1306_command(SSD1306_SETCONTRAST);
display.ssd1306_command(1);
display.display();
delay(wait);
}
// cppcheck-suppress unusedFunction
void show_display(String header, String line1, String line2, String line3, int wait) {
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(2);
display.setCursor(0, 0);
display.println(header);
display.setTextSize(1);
display.setCursor(0, 16);
display.println(line1);
display.setCursor(0, 26);
display.println(line2);
display.setCursor(0, 36);
display.println(line3);
display.ssd1306_command(SSD1306_SETCONTRAST);
display.ssd1306_command(1);
display.display();
delay(wait);
}
// cppcheck-suppress unusedFunction
void show_display(String header, String line1, String line2, String line3, String line4, int wait) {
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(2);
display.setCursor(0, 0);
display.println(header);
display.setTextSize(1);
display.setCursor(0, 16);
display.println(line1);
display.setCursor(0, 26);
display.println(line2);
display.setCursor(0, 36);
display.println(line3);
display.setCursor(0, 46);
display.println(line4);
display.ssd1306_command(SSD1306_SETCONTRAST);
display.ssd1306_command(1);
display.display();
delay(wait);
}
// cppcheck-suppress unusedFunction
void show_display(String header, String line1, String line2, String line3, String line4, String line5, int wait) {
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(2);
display.setCursor(0, 0);
display.println(header);
display.setTextSize(1);
display.setCursor(0, 16);
display.println(line1);
display.setCursor(0, 26);
display.println(line2);
display.setCursor(0, 36);
display.println(line3);
display.setCursor(0, 46);
display.println(line4);
display.setCursor(0, 56);
display.println(line5);
display.ssd1306_command(SSD1306_SETCONTRAST);
display.ssd1306_command(1);
display.display();
delay(wait);
}

9
src/display.h Normal file
View File

@ -0,0 +1,9 @@
void setup_display();
void display_toggle(bool toggle);
void show_display(String header, int wait = 0);
void show_display(String header, String line1, int wait = 0);
void show_display(String header, String line1, String line2, int wait = 0);
void show_display(String header, String line1, String line2, String line3, int wait = 0);
void show_display(String header, String line1, String line2, String line3, String line4, int wait = 0);
void show_display(String header, String line1, String line2, String line3, String line4, String line5, int wait = 0);

View File

@ -1,9 +1,131 @@
#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() {
// put your setup code here, to run once:
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() {
// put your main code here, to run repeatedly:
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();
}

17
src/pins.h Normal file
View File

@ -0,0 +1,17 @@
#ifndef PINS_H_
#define PINS_H_
#undef OLED_SDA
#undef OLED_SCL
#undef OLED_RST
#define OLED_SDA 21
#define OLED_SCL 22
#define OLED_RST 16
#define BUTTON_PIN 38 // The middle button GPIO on the T-Beam
#define GPS_RX 12
#define GPS_TX 34
#endif

86
src/power_management.cpp Normal file
View File

@ -0,0 +1,86 @@
#include "power_management.h"
// cppcheck-suppress uninitMemberVar
PowerManagement::PowerManagement() {
}
// cppcheck-suppress unusedFunction
bool PowerManagement::begin(TwoWire &port) {
bool result = axp.begin(port, AXP192_SLAVE_ADDRESS);
if (!result) {
axp.setDCDC1Voltage(3300);
}
return result;
}
// cppcheck-suppress unusedFunction
void PowerManagement::activateLoRa() {
axp.setPowerOutPut(AXP192_LDO2, AXP202_ON);
}
// cppcheck-suppress unusedFunction
void PowerManagement::deactivateLoRa() {
axp.setPowerOutPut(AXP192_LDO2, AXP202_OFF);
}
// cppcheck-suppress unusedFunction
void PowerManagement::activateGPS() {
axp.setPowerOutPut(AXP192_LDO3, AXP202_ON);
}
// cppcheck-suppress unusedFunction
void PowerManagement::deactivateGPS() {
axp.setPowerOutPut(AXP192_LDO3, AXP202_OFF);
}
// cppcheck-suppress unusedFunction
void PowerManagement::activateOLED() {
axp.setPowerOutPut(AXP192_DCDC1, AXP202_ON);
}
// cppcheck-suppress unusedFunction
void PowerManagement::decativateOLED() {
axp.setPowerOutPut(AXP192_DCDC1, AXP202_OFF);
}
// cppcheck-suppress unusedFunction
void PowerManagement::disableChgLed() {
axp.setChgLEDMode(AXP20X_LED_OFF);
}
// cppcheck-suppress unusedFunction
void PowerManagement::enableChgLed() {
axp.setChgLEDMode(AXP20X_LED_LOW_LEVEL);
}
// cppcheck-suppress unusedFunction
void PowerManagement::activateMeasurement() {
axp.adc1Enable(AXP202_BATT_CUR_ADC1 | AXP202_BATT_VOL_ADC1, true);
}
// cppcheck-suppress unusedFunction
void PowerManagement::deactivateMeasurement() {
axp.adc1Enable(AXP202_BATT_CUR_ADC1 | AXP202_BATT_VOL_ADC1, false);
}
// cppcheck-suppress unusedFunction
double PowerManagement::getBatteryVoltage() {
return axp.getBattVoltage() / 1000.0;
}
// cppcheck-suppress unusedFunction
double PowerManagement::getBatteryChargeDischargeCurrent() {
if (axp.isChargeing()) {
return axp.getBattChargeCurrent();
}
return -1.0 * axp.getBattDischargeCurrent();
}
bool PowerManagement::isBatteryConnect() {
return axp.isBatteryConnect();
}
bool PowerManagement::isChargeing() {
return axp.isChargeing();
}

37
src/power_management.h Normal file
View File

@ -0,0 +1,37 @@
#ifndef POWER_MANAGEMENT_H_
#define POWER_MANAGEMENT_H_
#include <Arduino.h>
#include <axp20x.h>
class PowerManagement {
public:
PowerManagement();
bool begin(TwoWire &port);
void activateLoRa();
void deactivateLoRa();
void activateGPS();
void deactivateGPS();
void activateOLED();
void decativateOLED();
void enableChgLed();
void disableChgLed();
void activateMeasurement();
void deactivateMeasurement();
double getBatteryVoltage();
double getBatteryChargeDischargeCurrent();
bool isBatteryConnect();
bool isChargeing();
private:
AXP20X_Class axp;
};
#endif