mqtt wrapper first version working
This commit is contained in:
56
src/main.cpp
56
src/main.cpp
@@ -4,7 +4,6 @@
|
||||
#include <DebugLogEnable.h>
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <PubSubClient.h>
|
||||
|
||||
#include <config.h>
|
||||
#include <PCF85063_Driver.h>
|
||||
@@ -16,32 +15,21 @@
|
||||
|
||||
#include <digitalIO.h>
|
||||
|
||||
#define DEBUGLOG_DEFAULT_LOG_LEVEL_DEBUG
|
||||
#include <mqtt.h>
|
||||
#include "utils.h"
|
||||
|
||||
/////////////// GLOBALS ///////////////
|
||||
Config& conf = Config::getInstance();
|
||||
Config &conf = Config::getInstance();
|
||||
/////////////// GLOBALS ///////////////
|
||||
|
||||
void callback(char *topic, uint8_t *payload, unsigned int length)
|
||||
void testAction(const ArduinoJson::JsonDocument &doc)
|
||||
{
|
||||
std::string pl;
|
||||
pl.resize(length);
|
||||
std::snprintf(pl.data(), length, "%s", payload);
|
||||
LOG_INFO("Message: Topic [", topic, "], Payload [", pl.c_str(), "]");
|
||||
std::string message;
|
||||
ArduinoJson::serializeJsonPretty(doc, message);
|
||||
LOG_INFO("Received on testAction\n", message.c_str());
|
||||
}
|
||||
|
||||
void myTask(void *mqtt)
|
||||
{
|
||||
auto client = (PubSubClient *)(mqtt);
|
||||
while (client->connected())
|
||||
{
|
||||
client->loop();
|
||||
vTaskDelay(pdMS_TO_TICKS(100));
|
||||
}
|
||||
LOG_ERROR("Mqtt Loop Ended, client disconnected");
|
||||
vTaskDelete(NULL); // delete the current task
|
||||
};
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(9600);
|
||||
@@ -75,15 +63,11 @@ void loop()
|
||||
LOG_INFO("Temperature sensors connected ->", sensors);
|
||||
|
||||
//////////////// NETWORK ////////////////
|
||||
// MQTT Test //
|
||||
NetworkClient tcp;
|
||||
PubSubClient mqtt(tcp);
|
||||
mqtt.setServer(conf.m_mqttHost.c_str(), conf.m_mqttPort);
|
||||
mqtt.setCallback(callback);
|
||||
auto mqtt = MQTTwrapper();
|
||||
//////////////// NETWORK ////////////////
|
||||
|
||||
//////////////// NETWORK ////////////////
|
||||
/////////////// CALLBACK ////////////////
|
||||
|
||||
//////////////// NETWORK ////////////////
|
||||
/////////////// CALLBACK ////////////////
|
||||
Network.onEvent(
|
||||
[ð, &rtc, &mqtt, &buzzer, &led](arduino_event_id_t event, arduino_event_info_t info) -> void
|
||||
{
|
||||
@@ -98,7 +82,7 @@ void loop()
|
||||
{
|
||||
if (eth.getNtpTime(ntpTime) && rtc.setDatetime(drivers::PCF85063::fromEpoch(ntpTime)))
|
||||
{
|
||||
buzzer.beep(250, NOTE_F);
|
||||
//buzzer.beep(250, NOTE_F);
|
||||
led.setColor({255, 255, 0});
|
||||
const drivers::PCF85063::datetime_t dt(drivers::PCF85063::fromEpoch(ntpTime));
|
||||
LOG_INFO("NTP Time: ", drivers::PCF85063::datetime2str(dt).c_str());
|
||||
@@ -108,10 +92,9 @@ void loop()
|
||||
}
|
||||
while (mqttRetries++ < conf.m_mqttRetries)
|
||||
{
|
||||
if (!mqtt.connected() && mqtt.connect(conf.m_mqttClientName.c_str()))
|
||||
if (mqtt.connect())
|
||||
{
|
||||
mqtt.subscribe("test/esp32-in");
|
||||
xTaskCreatePinnedToCore(myTask, "mqttLoop", 4096, &mqtt, 2, NULL, 1);
|
||||
mqtt.subscribe("test/esp32-in", testAction);
|
||||
break;
|
||||
}
|
||||
delay(100);
|
||||
@@ -128,7 +111,10 @@ void loop()
|
||||
|
||||
const std::string timeStr(rtc.getTimeStr());
|
||||
LOG_INFO("Current Datetime", timeStr.c_str());
|
||||
mqtt.publish("test/esp32-out", ("[" + std::to_string(k) + "] -> " + timeStr).c_str());
|
||||
ArduinoJson::JsonDocument ts;
|
||||
ts["loopIterator"] = k;
|
||||
ts["currentTime"] = timeStr;
|
||||
mqtt.publish("test/esp32-out", ts);
|
||||
|
||||
uint8_t i(0);
|
||||
for (auto v : tmp.getTempAll())
|
||||
@@ -172,11 +158,13 @@ void loop()
|
||||
LOG_INFO("Buzzing -> ", buzzing ? "True" : "False");
|
||||
}
|
||||
|
||||
if(io.digitalIORead(9)) { // verde
|
||||
if (io.digitalIORead(9))
|
||||
{ // verde
|
||||
conf.resetConfig();
|
||||
}
|
||||
|
||||
if(io.digitalIORead(10)) { // giallo
|
||||
if (io.digitalIORead(10))
|
||||
{ // giallo
|
||||
esp_restart();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user