Skip to content
This repository has been archived by the owner on Oct 7, 2022. It is now read-only.

Commit

Permalink
Merge pull request #4 from bram2202/develop
Browse files Browse the repository at this point in the history
More properties and OTA support
  • Loading branch information
bram2202 authored Apr 29, 2021
2 parents 47cf9b2 + 6b8cf22 commit 9c4288a
Show file tree
Hide file tree
Showing 9 changed files with 367 additions and 275 deletions.
39 changes: 39 additions & 0 deletions Logger.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include "Logger.h"
#include "Settings.h"

enum LEVEL
{
DEBUG = 1,
INFO = 2,
WARN = 3
};

// Create Wifi Connector
Logger::Logger(String name)
{
_name = name;
}

void Logger::debug(String msg)
{
if (LOG_LEVEL <= DEBUG)
{
Serial.println("DEBUG - " + _name + ":" + msg);
}
}

void Logger::info(String msg)
{
if (LOG_LEVEL <= INFO)
{
Serial.println("INFO - " + _name + ":" + msg);
}
}

void Logger::warn(String msg)
{
if (LOG_LEVEL <= WARN)
{
Serial.println("WARN - " + _name + ":" + msg);
}
}
14 changes: 14 additions & 0 deletions Logger.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#pragma once
#include <Arduino.h>

class Logger
{
private:
String _name;

public:
Logger(String name = "Logger");
void info(String msg);
void debug(String msg);
void warn(String msg);
};
220 changes: 111 additions & 109 deletions MQTTPublisher.cpp
Original file line number Diff line number Diff line change
@@ -1,109 +1,111 @@
#include "MQTTPublisher.h"
#include "Settings.h"

WiFiClient espClient;
PubSubClient client(espClient);

MQTTPublisher::MQTTPublisher(bool inDebugMode)
{
randomSeed(micros());
debugMode = inDebugMode;
}

MQTTPublisher::~MQTTPublisher()
{
client.publish(MQTT_HOSTNAME, "offline");
client.disconnect();
}

bool MQTTPublisher::reconnect()
{
lastConnectionAttempt = millis();

if (debugMode)
{
Serial.println("MQTT) Attempt connection to server: " + String(MQTT_HOST_NAME));
}

// Create a random client ID
String clientId = String(MQTT_HOSTNAME) + "-";
clientId += String(random(0xffff), HEX);

// Attempt to connect
bool clientConnected;
if (String(MQTT_USER_NAME).length())
{
Serial.println("MQTT) Connecting with credientials");
clientConnected = client.connect(clientId.c_str(), MQTT_USER_NAME, MQTT_PASSWORD);
}
else
{
Serial.println("MQTT) Connecting without credentials");
clientConnected = client.connect(clientId.c_str());
}

if (clientConnected)
{
if (debugMode) {
Serial.println("MQTT) connected");
}

hasMQTT = true;

// Once connected, publish an announcement...
client.publish(MQTT_HOSTNAME, "online");

return true;
} else {

if (debugMode)
{
Serial.println("MQTT) failed, rc=");
Serial.println(client.state());
}

}

return false;
}


void MQTTPublisher::start()
{
if (String(MQTT_HOST_NAME).length() == 0 || MQTT_PORT == 0)
{
Serial.println("MQTT) disabled. No hostname or port set.");
return; //not configured
}

if (debugMode){
Serial.println("MQTT) enabled. Connecting.");
}

client.setServer(MQTT_HOST_NAME, MQTT_PORT);
reconnect();
isStarted = true;
}

void MQTTPublisher::stop()
{
isStarted = false;
}

void MQTTPublisher::handle()
{
if (!isStarted)
return;

if (!client.connected() && millis() - lastConnectionAttempt > RECONNECT_TIMEOUT) {
hasMQTT = false;
if (!reconnect()) return;
}
}

bool MQTTPublisher::publishOnMQTT(String prepend, String topic, String value)
{
auto retVal = client.publish((prepend.c_str() + topic).c_str(), value.c_str());
yield();
return retVal;
}
#include "MQTTPublisher.h"
#include "Settings.h"

WiFiClient espClient;
PubSubClient client(espClient);

MQTTPublisher::MQTTPublisher(String clientId)
{
randomSeed(micros());
_clientId = clientId;
logger = Logger("MQTTPublisher");
logger.debug("ClientId:" + _clientId);
}

MQTTPublisher::~MQTTPublisher()
{
client.publish(getTopic("status").c_str(), "offline");
client.disconnect();
}

String MQTTPublisher::getTopic(String name)
{
if (USE_CLIENT_ID)
{
return String(MQTT_PREFIX) + '/' + _clientId + '/' + name;
}
else
{
return String(MQTT_PREFIX) + '/' + name;
}
}

bool MQTTPublisher::reconnect()
{
lastConnectionAttempt = millis();

logger.debug("Attempt connection to server: " + String(MQTT_HOST_NAME));

// Attempt to connect
bool clientConnected;
if (String(MQTT_USER_NAME).length())
{
logger.info("Connecting with credientials");
clientConnected = client.connect(_clientId.c_str(), MQTT_USER_NAME, MQTT_PASSWORD);
}
else
{
logger.info("Connecting without credentials");
clientConnected = client.connect(_clientId.c_str());
}

if (clientConnected)
{
logger.debug("connected");

hasMQTT = true;

// Once connected, publish an announcement...
client.publish(getTopic("status").c_str(), "online");

return true;
}
else
{
logger.warn("failed, rc=" + client.state());
}

return false;
}

void MQTTPublisher::start()
{
if (String(MQTT_HOST_NAME).length() == 0 || MQTT_PORT == 0)
{
logger.warn("disabled. No hostname or port set.");
return; //not configured
}

logger.debug("enabled. Connecting.");

client.setServer(MQTT_HOST_NAME, MQTT_PORT);
reconnect();
isStarted = true;
}

void MQTTPublisher::stop()
{
isStarted = false;
}

void MQTTPublisher::handle()
{
if (!isStarted)
return;

if (!client.connected() && millis() - lastConnectionAttempt > RECONNECT_TIMEOUT)
{
hasMQTT = false;
if (!reconnect())
return;
}
}

bool MQTTPublisher::publishOnMQTT(String topicSuffix, String msg)
{
String topic = getTopic(topicSuffix);
logger.debug("Publish to '" + topic + "':" + msg);
auto retVal = client.publish(topic.c_str(), msg.c_str());
yield();
return retVal;
}
10 changes: 7 additions & 3 deletions MQTTPublisher.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <vector>
#include "PubSubClient.h"
#include "WiFiClient.h"
#include "Logger.h"

#define RECONNECT_TIMEOUT 15000

Expand All @@ -14,20 +15,23 @@ extern bool hasWIFI;
class MQTTPublisher
{
private:
bool debugMode;
Logger logger;
bool _debugMode;
String _clientId;
bool isStarted;

uint32_t lastConnectionAttempt = 0; // last reconnect
uint32_t lastUpdateMqtt; // last data send

bool reconnect();
String getTopic(String name);
public:
MQTTPublisher(bool inDebugMode = false);
MQTTPublisher(String clientId = String(ESP.getChipId(), HEX));
~MQTTPublisher();

void start();
void stop();

void handle();
bool publishOnMQTT(String prepend, String topic, String value);
bool publishOnMQTT(String topic, String msg);
};
Loading

0 comments on commit 9c4288a

Please sign in to comment.