Seperated mqtt functions

This commit is contained in:
Arne van Iterson 2024-03-10 17:23:53 +01:00
parent 3dcd0560c2
commit bceda8433b
7 changed files with 107 additions and 57 deletions

View File

@ -11,7 +11,23 @@
"settings": {
"files.associations": {
"*.html": "html",
"cmath": "cpp"
"cmath": "cpp",
"array": "cpp",
"chrono": "cpp",
"deque": "cpp",
"format": "cpp",
"forward_list": "cpp",
"initializer_list": "cpp",
"list": "cpp",
"vector": "cpp",
"xhash": "cpp",
"xstring": "cpp",
"xtree": "cpp",
"xutility": "cpp",
"string": "cpp",
"unordered_map": "cpp",
"unordered_set": "cpp",
"string_view": "cpp"
}
}
}

View File

@ -1,9 +1,24 @@
#include "./einkanalog.h"
EinkAnalogDisplay::EinkAnalogDisplay(GxEPD2_BW<GxEPD2_213_B72, (uint16_t)250U> *display, uint8_t dacPin)
EinkAnalogDisplay::EinkAnalogDisplay(GxEPD2_BW<GxEPD2_213_B72, (uint16_t)250U> *display, uint8_t dacPin, bool GxEPD_DEBUG)
{
this->display = display;
this->dac = dacPin;
if (GxEPD_DEBUG)
{
this->display->init(115200);
} else {
this->display->init(0);
}
// Init display
display->fillScreen(GxEPD_WHITE);
display->setTextColor(GxEPD_BLACK);
display->setFont(&Pixeltype8pt7b);
display->setRotation(1);
display->setPartialWindow(0, 0, display->width(), display->height());
display->firstPage();
}
EinkAnalogDisplay::~EinkAnalogDisplay()

View File

@ -1,7 +1,19 @@
#ifndef EIAD_H
#define EIAD_H
// Globals
#include <global.h>
// GxEPD2 Driver
#include <GxEPD2_BW.h>
// Fonts
#include <Adafruit_GFX.h>
#include "../include/fonts/Pixeltype8pt7b.h"
#include "../include/fonts/Pixeltype16pt7b.h"
#include "../include/fonts/Fipps_Regular8pt7b.h"
#include "../include/fonts/SilomBol7pt7b.h"
const unsigned char gfx_wireless[] PROGMEM = {
0x00, 0xe0, 0x10, 0xc8, 0x28, 0xa8, 0x00};
@ -18,7 +30,7 @@ private:
void centerText(const char *text, int16_t x, int16_t y);
public:
EinkAnalogDisplay(GxEPD2_BW<GxEPD2_213_B72, (uint16_t)250U> *display, uint8_t dacPin);
EinkAnalogDisplay(GxEPD2_BW<GxEPD2_213_B72, (uint16_t)250U> *display, uint8_t dacPin, bool GxEPD_DEBUG = false);
~EinkAnalogDisplay();
void drawFace(int16_t min, int16_t max, char *name, char *unit, uint8_t lines = 5);
int setArm(float value);

View File

@ -7,9 +7,6 @@
#include <WiFi.h>
#include <WiFiMulti.h>
// MQTT
#include <PubSubClient.h>
// DAC
#define DAC1 25
@ -18,16 +15,6 @@
#define BTN_L 22
#define BTN_R 21
// Display
#include <GxEPD2_BW.h>
// Fonts
#include <Adafruit_GFX.h>
#include "../include/fonts/Pixeltype8pt7b.h"
#include "../include/fonts/Pixeltype16pt7b.h"
#include "../include/fonts/Fipps_Regular8pt7b.h"
#include "../include/fonts/SilomBol7pt7b.h"
// Local helper files
#include "./einkanalog/einkanalog.h"
#include "./helpers/mqtt.h"

View File

@ -1,6 +1,19 @@
#include "mqtt.h"
void callback(char *topic, byte *payload, unsigned int length)
MqttTopic::MqttTopic(WiFiClient* espClient): wifiClient(espClient), client(PubSubClient(*espClient))
{
IPAddress server(192, 168, 2, 196);
this->client.setServer(server, 1883);
this->client.setCallback(this->callback);
}
MqttTopic::~MqttTopic()
{
this->client.disconnect();
}
void MqttTopic::callback(char *topic, byte *payload, unsigned int length)
{
Serial.print("Message arrived [");
Serial.print(topic);
@ -12,3 +25,27 @@ void callback(char *topic, byte *payload, unsigned int length)
// Serial.println(String(array).toFloat() / 1000.0);
// ead.setArm(String(array).toFloat() / 1000.0);
}
void MqttTopic::loop()
{
while (!this->client.connected())
{
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (this->client.connect("espead", "arne", "ThinkCentreM58"))
{
Serial.println("connected");
this->client.subscribe("homeassistant/ead/#");
}
else
{
Serial.print("failed, rc=");
Serial.print(this->client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
this->client.loop();
}

View File

@ -1,7 +1,24 @@
#ifndef MQTT_H
#define MQTT_H
// Globals
#include <global.h>
void callback(char *topic, byte *payload, unsigned int length);
// MQTT
#include <PubSubClient.h>
class MqttTopic
{
private:
WiFiClient* wifiClient; // Pointer to WiFiClient, for use with PubSubClient
PubSubClient client;
uint8_t topic = 0;
public:
MqttTopic(WiFiClient* espClient);
~MqttTopic();
static void callback(char *topic, byte *payload, unsigned int length);
void loop();
};
#endif

View File

@ -1,12 +1,8 @@
#include "global.h"
// Globals
#include <global.h>
// Wifi
WiFiMulti WiFiMulti;
WiFiClient espClient;
IPAddress server(192, 168, 2, 196);
// MQTT
PubSubClient client(espClient);
// Buttons
Button2 btn_L, btn_R;
@ -16,7 +12,7 @@ GxEPD2_BW<GxEPD2_213_B72, GxEPD2_213_B72::HEIGHT> display(GxEPD2_213_B72(/*CS=5*
// Helpers
EinkAnalogDisplay ead(&display, DAC1);
MqttTopic mqtt(&espClient);
void btnHandler(Button2 &btn)
{
@ -37,7 +33,6 @@ void btnHandler(Button2 &btn)
void setup()
{
Serial.begin(115200);
display.init(115200);
delay(10);
Serial.println("T5 Dashboard " __DATE__ " " __TIME__);
@ -51,13 +46,6 @@ void setup()
btn_R.setClickHandler(btnHandler);
btn_R.setDoubleClickHandler(btnHandler);
display.fillScreen(GxEPD_WHITE);
display.setTextColor(GxEPD_BLACK);
display.setFont(&Pixeltype8pt7b);
display.setRotation(1);
display.setPartialWindow(0, 0, display.width(), display.height());
display.firstPage();
ead.drawFace(0, 100, "Welcome", "", 11);
display.nextPage();
@ -101,9 +89,6 @@ void setup()
// display.fillScreen(GxEPD_WHITE);
// display.nextPage();
client.setServer(server, 1883);
client.setCallback(callback);
display.fillScreen(GxEPD_WHITE);
ead.drawFace(0, 9, "Solax Prod.", "kW", 10);
ead.drawMeta(WiFi.localIP().toString().c_str());
@ -112,26 +97,7 @@ void setup()
void loop()
{
while (!client.connected())
{
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect("espead", "arne", "ThinkCentreM58"))
{
Serial.println("connected");
client.subscribe("homeassistant/ead/#");
}
else
{
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
btn_L.loop();
btn_R.loop();
client.loop();
mqtt.loop();
}