-
Notifications
You must be signed in to change notification settings - Fork 140
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
plapointe6
committed
Nov 25, 2018
1 parent
7a57609
commit 231a590
Showing
6 changed files
with
467 additions
and
2 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,2 +1,43 @@ | ||
# Esp8266MQTTClient | ||
Wifi and MQTT handling for ESP8266 | ||
# MQTT and Wifi handling for ESP8266 | ||
|
||
This library is intended to encapsulate the handling of wifi and MQTT connections of an ESP8266. | ||
You just need to provide your credentials and it will manage these things : | ||
- Connecting to a wifi network | ||
- Connecting to a MQTT broker | ||
- Running an HTTP server secured by a password to allow remote update. | ||
- Automatically detects connection lost either from the wifi client of MQTT broker. | ||
- Automatically attempt to reconnect when the connection is lost. | ||
- Provide a callback handling to advise when we are connected to the MQTT broker. | ||
- Allow to print usefull debug informations. | ||
|
||
Was tested with a Wemos D1 mini, feel free to test it with another ESP8266 and provide feedback. | ||
|
||
|
||
## Dependency | ||
|
||
The MQTT communication depends on the PubSubClient Library (https://github.com/knolleary/pubsubclient). | ||
|
||
## Functions | ||
|
||
- publish(String topic, String message) : publish a message to the provided MQTT topic | ||
- subscribe(String topic, callback) : subscribe to the specified topic and call the provided callback passing the received message. | ||
- bool isConnected() : Return true if everything is connected. | ||
- executeDelayed(long milliseconds, callback) : As ESP8366 does not like to be interrupted too long with the delay function, this function will allow a delayed execution of a function whitout interruption the sketch. | ||
|
||
## Exemple | ||
|
||
See "Esp8266MQTTClient.ino" for the complete exemple | ||
|
||
```c++ | ||
Esp8266MQTTClient client(...); | ||
|
||
void onConnectionEstablished(){ | ||
|
||
client.subscribe("mytopic/test", [] (const String &payload) { | ||
Serial.println(payload); | ||
}); | ||
|
||
client.publish("mytopic/test", "This is a message"); | ||
} | ||
|
||
``` |
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,40 @@ | ||
#include "Esp8266MQTTClient.h" | ||
|
||
void onConnectionEstablished(); | ||
|
||
Esp8266MQTTClient client( | ||
"ssid", // Wifi ssid | ||
"pass", // Wifi password | ||
"192.168.1.101", // MQTT broker ip | ||
1883, // MQTT broker port | ||
"usr", // MQTT username | ||
"mqttpass", // MQTT password | ||
"test1", // Client name | ||
onConnectionEstablished, // Connection established callback | ||
true, // Enable web updater | ||
true // Enable debug messages | ||
); | ||
|
||
void setup() | ||
{ | ||
Serial.begin(115200); | ||
} | ||
|
||
void onConnectionEstablished() | ||
{ | ||
client.subscribe("mytopic/test", [] (const String &payload) | ||
{ | ||
Serial.println(payload); | ||
}); | ||
|
||
client.publish("mytopic/test", "This is a message"); | ||
|
||
client.executeDelayed(5 * 1000, []() { | ||
client.publish("mytopic/test", "This is a message sent 5 seconds later"); | ||
}); | ||
} | ||
|
||
void loop() | ||
{ | ||
client.loop(); | ||
} |
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,23 @@ | ||
####################################### | ||
# Syntax Coloring Map For Esp8266MQTTClient | ||
####################################### | ||
|
||
####################################### | ||
# Datatypes (KEYWORD1) | ||
####################################### | ||
|
||
Esp8266MQTTClient KEYWORD1 | ||
|
||
####################################### | ||
# Methods and Functions (KEYWORD2) | ||
####################################### | ||
|
||
loop KEYWORD2 | ||
isConnected KEYWORD2 | ||
publish KEYWORD2 | ||
subscribe KEYWORD2 | ||
executeDelayed KEYWORD2 | ||
|
||
####################################### | ||
# Constants (LITERAL1) | ||
####################################### |
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,9 @@ | ||
name=Esp8266MQTTClient | ||
version=1.0 | ||
author=Patrick Lapointe <patrick.lapointe@hotmail.ca> | ||
maintainer=Patrick Lapointe <patrick.lapointe@hotmail.ca> | ||
sentence=A library that provides a wifi and MQTT connection to an ESP8266 | ||
paragraph=This library allow to connect and manage the connection to a wifi network and a MQTT broker. Also, it implement the secure HTTP updater. Intended to be used with an ESP8266. Dependecy : PubSubClient library | ||
category=Communication | ||
url=https://github.com/plapointe6/Esp8266MQTTClient | ||
architectures=* |
Oops, something went wrong.