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.
- Fix debug print - Add Know issues
- Loading branch information
Bram van Deventer
committed
Aug 23, 2020
1 parent
6377c69
commit 47cf9b2
Showing
2 changed files
with
120 additions
and
112 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 |
---|---|---|
@@ -1,110 +1,109 @@ | ||
#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: "); | ||
Serial.print(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(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; | ||
} |
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
47cf9b2
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi!
I'm very newbie but I would like to build this data sender.
I drew the schema in KiCad.
Could you check it, please if you have a little time?
Thank you!
https://ibb.co/qNwKpL9
47cf9b2
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @hbbst, your schema looks good 👍🏻.
Just keep in mind that if the RX pin is connected, its not possible to flash any firmware.
You can fix this by using something like a jumper pin.
47cf9b2
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great, thanks! :)
Yes, I will flash it before. Jumper is good idea! Thank you!