Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
plapointe6 committed Nov 25, 2018
1 parent 7a57609 commit 231a590
Show file tree
Hide file tree
Showing 6 changed files with 467 additions and 2 deletions.
45 changes: 43 additions & 2 deletions README.md
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");
}

```
40 changes: 40 additions & 0 deletions exemples/Esp8266MQTTClient/Esp8266MQTTClient.ino
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();
}
23 changes: 23 additions & 0 deletions keywords.txt
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)
#######################################
9 changes: 9 additions & 0 deletions library.properties
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=*
Loading

0 comments on commit 231a590

Please sign in to comment.