This repository has been archived by the owner on Oct 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from bram2202/develop
More properties and OTA support
- Loading branch information
Showing
9 changed files
with
367 additions
and
275 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.