-
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.
Merge pull request #10 from plapointe6/dev
1.4.1 to 1.5 changes
- Loading branch information
Showing
8 changed files
with
370 additions
and
103 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
7 changes: 7 additions & 0 deletions
7
...s/Esp8266MQTTClient/Esp8266MQTTClient.ino → ...les/SimpleMQTTClient/SimpleMQTTClient.ino
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
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,61 @@ | ||
/* | ||
twoMQTTClientHandling.ino | ||
The purpose of this exemple is to illustrate how to handle more than one MQTT connection a the same time in the same sketch. | ||
Getting into "SimpleMQTTClient.ino" before this one is recommended (there is more comments) | ||
*/ | ||
|
||
#include "EspMQTTClient.h" | ||
|
||
void onConnectionEstablishedClient2(); | ||
|
||
// The client #1 will handle wifi connection (connecting, retrying, etc) and MQTT connection to 192.168.1.100 | ||
EspMQTTClient client1( | ||
"WifiSSID", | ||
"WifiPassword", | ||
"192.168.1.100", | ||
"MQTTUsername", | ||
"MQTTPassword", | ||
"TestClient1" | ||
); | ||
|
||
// The client #2 will handle MQTT connection to 192.168.1.101. | ||
EspMQTTClient client2( | ||
"192.168.1.101", | ||
1883, | ||
"MQTTUsername", | ||
"MQTTPassword", | ||
"TestClient2" | ||
); | ||
|
||
void setup() | ||
{ | ||
Serial.begin(115200); | ||
|
||
// We redirect the connection established callback of client2 to onConnectionEstablishedClient2. | ||
// This will prevent the two client from calling the same callback (default to onConnectionEstablished) | ||
client2.setOnConnectionEstablishedCallback(onConnectionEstablishedClient2); | ||
} | ||
|
||
// For client1 | ||
void onConnectionEstablished() | ||
{ | ||
client1.subscribe("mytopic/test", [](const String & payload) { | ||
Serial.println(payload); | ||
}); | ||
client1.publish("mytopic/test", "This is a message from client1"); | ||
} | ||
|
||
// For client2 | ||
void onConnectionEstablishedClient2() | ||
{ | ||
client2.subscribe("mytopic/test", [](const String & payload) { | ||
Serial.println(payload); | ||
}); | ||
client2.publish("mytopic/test", "This is a message from client2"); | ||
} | ||
|
||
void loop() | ||
{ | ||
client1.loop(); | ||
client2.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
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,9 +1,9 @@ | ||
name=EspMQTTClient | ||
version=1.4 | ||
version=1.5 | ||
author=Patrick Lapointe <[email protected]> | ||
maintainer=Patrick Lapointe <[email protected]> | ||
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 | ||
sentence=A library that provides a wifi and MQTT connection to an ESP8266/ESP32 | ||
paragraph=This library allow to connect and manage the connection to a wifi network and a MQTT broker. Intended to be used with an ESP8266 an ESP32. Dependecy : PubSubClient library | ||
category=Communication | ||
url=https://github.com/plapointe6/EspMQTTClient | ||
architectures=* |
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,103 @@ | ||
#ifndef ESP32_HTTP_UPDATE_SERVER_H | ||
#define ESP32_HTTP_UPDATE_SERVER_H | ||
|
||
/* | ||
Based on the HTTP update exemple of ESP32 core | ||
*/ | ||
|
||
#include <WebServer.h> | ||
#include <Update.h> | ||
|
||
#define ESP32_WEB_UPDATE_HTML "<form method='POST' action='' enctype='multipart/form-data'><input type='file' name='update'><input type='submit' value='Update'></form>" | ||
#define ESP32_WEB_UPDATE_SUCCESS_RESPONSE "<META http-equiv=\"refresh\" content=\"10;URL=/\">Update Success! Rebooting...\n" | ||
|
||
class ESP32HTTPUpdateServer | ||
{ | ||
private: | ||
WebServer*_server; | ||
|
||
char* _username; | ||
char* _password; | ||
bool _serialDebugging; | ||
|
||
public: | ||
ESP32HTTPUpdateServer(bool serialDebugging = false) | ||
{ | ||
_server = NULL; | ||
_username = ""; | ||
_password = ""; | ||
} | ||
|
||
void setup(WebServer* server, char* path = "/", char* username = "", char* password = "") | ||
{ | ||
_server = server; | ||
_username = username; | ||
_password = password; | ||
|
||
// Get of the index handling | ||
_server->on(path, HTTP_GET, [&]() { | ||
// Force authentication if a user and password are defined | ||
if (_username != "" && _password != "" && !_server->authenticate(_username, _password)) | ||
return _server->requestAuthentication(); | ||
|
||
_server->sendHeader("Connection", "close"); | ||
_server->send(200, "text/html", ESP32_WEB_UPDATE_HTML); | ||
}); | ||
|
||
// Post of the file handling | ||
_server->on(path, HTTP_POST, [&]() { | ||
_server->client().setNoDelay(true); | ||
_server->send_P(200, "text/html", (Update.hasError()) ? "FAIL" : ESP32_WEB_UPDATE_SUCCESS_RESPONSE); | ||
delay(100); | ||
_server->client().stop(); | ||
ESP.restart(); | ||
}, [&]() { | ||
HTTPUpload& upload = _server->upload(); | ||
|
||
if (upload.status == UPLOAD_FILE_START) | ||
{ | ||
// Check if we are authenticated | ||
if (_username == "" || _password == "" || _server->authenticate(_username, _password)) | ||
{ | ||
if (_serialDebugging) | ||
Serial.printf("Unauthenticated Update\n"); | ||
|
||
return; | ||
} | ||
|
||
// Debugging message for upload start | ||
if (_serialDebugging) | ||
{ | ||
Serial.setDebugOutput(true); | ||
Serial.printf("Update: %s\n", upload.filename.c_str()); | ||
} | ||
|
||
// Starting update | ||
bool error = Update.begin(); | ||
if (_serialDebugging && error) | ||
Update.printError(Serial); | ||
} | ||
else if (upload.status == UPLOAD_FILE_WRITE) | ||
{ | ||
if (Update.write(upload.buf, upload.currentSize) != upload.currentSize && _serialDebugging) | ||
Update.printError(Serial); | ||
} | ||
else if (upload.status == UPLOAD_FILE_END) | ||
{ | ||
if (Update.end(true) && _serialDebugging) | ||
Serial.printf("Update Success: %u\nRebooting...\n", upload.totalSize); | ||
else if(_serialDebugging) | ||
Update.printError(Serial); | ||
|
||
if(_serialDebugging) | ||
Serial.setDebugOutput(false); | ||
} | ||
else if(_serialDebugging) | ||
Serial.printf("Update Failed Unexpectedly (likely broken connection): status=%d\n", upload.status); | ||
}); | ||
|
||
_server->begin(); | ||
} | ||
}; | ||
|
||
#endif |
Oops, something went wrong.