add MQTT and OTA update support
This commit is contained in:
parent
3746d1a4d6
commit
34bd9746d1
@ -13,10 +13,11 @@ platform = espressif8266
|
||||
framework = arduino
|
||||
monitor_speed = 115200
|
||||
upload_speed = 500000
|
||||
upload_protocol = espota
|
||||
upload_port = led-marquee-sign.fablab.local
|
||||
|
||||
# Serial1 (sign data) maps to D4 by default
|
||||
[env:d1_mini]
|
||||
board = d1_mini
|
||||
|
||||
[env:nodemcuv2]
|
||||
board = nodemcuv2
|
||||
lib_deps = adafruit/Adafruit MQTT Library@^2.5.8
|
||||
lib_ignore = WiFi101
|
||||
|
65
src/main.cpp
65
src/main.cpp
@ -1,6 +1,11 @@
|
||||
#include <ESP8266WiFi.h>
|
||||
#include <ESP8266WiFiMulti.h>
|
||||
#include <ESP8266mDNS.h>
|
||||
#include <ESP8266WebServer.h>
|
||||
#include <WiFiUdp.h>
|
||||
#include <ArduinoOTA.h>
|
||||
#include "Adafruit_MQTT.h"
|
||||
#include "Adafruit_MQTT_Client.h"
|
||||
|
||||
#include "credentials.h"
|
||||
#include "webserver.h"
|
||||
@ -10,6 +15,16 @@
|
||||
static ESP8266WiFiMulti wifiMulti;
|
||||
static SocketServer socketServer;
|
||||
|
||||
#define AIO_SERVER credentials::mqtt_hostname.c_str()
|
||||
#define AIO_SERVERPORT credentials::mqtt_port
|
||||
#define AIO_USERNAME credentials::mqtt_username.c_str()
|
||||
#define AIO_KEY credentials::mqtt_password.c_str()
|
||||
WiFiClient client;
|
||||
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);
|
||||
Adafruit_MQTT_Subscribe mqtt_subscribe_topic = Adafruit_MQTT_Subscribe(&mqtt, "homeassistant/marquee_sign/text");
|
||||
|
||||
void MQTT_connect();
|
||||
|
||||
void setup() {
|
||||
// serial console, for use via USB (also exposed to TXD0/RXD0 GPIOs)
|
||||
Serial.begin(115200);
|
||||
@ -20,7 +35,10 @@ void setup() {
|
||||
Serial1.begin(9600);
|
||||
|
||||
// configure WiFi and connect to the network
|
||||
WiFi.mode(WIFI_STA);
|
||||
WiFi.hostname(credentials::mdnsHostname);
|
||||
wifiMulti.addAP(credentials::ssid.c_str(), credentials::psk.c_str());
|
||||
|
||||
Serial.println("Connecting to Wi-Fi network...");
|
||||
while (wifiMulti.run() != WL_CONNECTED) {
|
||||
delay(1000);
|
||||
@ -32,16 +50,13 @@ void setup() {
|
||||
Serial.print(", IP address: ");
|
||||
Serial.println(WiFi.localIP());
|
||||
|
||||
ArduinoOTA.begin();
|
||||
|
||||
mqtt.subscribe(&mqtt_subscribe_topic);
|
||||
|
||||
// might be handy to have the IP written on the sign
|
||||
sendTextToSign(WiFi.localIP().toString());
|
||||
|
||||
// publish this service using mDNS so that it's easy to find it in the LAN
|
||||
if (MDNS.begin(credentials::mdnsHostname)) {
|
||||
Serial.println("mDNS started up successfully");
|
||||
} else {
|
||||
Serial.println("Error: failed to start mDNS");
|
||||
}
|
||||
|
||||
// configure HTTP endpoints
|
||||
configureWebServer();
|
||||
|
||||
@ -53,4 +68,40 @@ void setup() {
|
||||
void loop() {
|
||||
webServer.handleClient();
|
||||
socketServer.handleClient();
|
||||
|
||||
ArduinoOTA.handle();
|
||||
|
||||
MQTT_connect();
|
||||
|
||||
Adafruit_MQTT_Subscribe *subscription;
|
||||
while ((subscription = mqtt.readSubscription(5000))) {
|
||||
const char* signText = (char *)mqtt_subscribe_topic.lastread;
|
||||
|
||||
sendTextToSign(signText);
|
||||
}
|
||||
}
|
||||
|
||||
void MQTT_connect() {
|
||||
int8_t ret;
|
||||
|
||||
// Stop if already connected.
|
||||
if (mqtt.connected()) {
|
||||
return;
|
||||
}
|
||||
|
||||
uint8_t retries = 6;
|
||||
while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
|
||||
mqtt.disconnect();
|
||||
|
||||
// wait 10s till next try
|
||||
for (int i = 0; i < 10000; i += 5) {
|
||||
ArduinoOTA.handle();
|
||||
delay(5);
|
||||
}
|
||||
|
||||
retries--;
|
||||
if (retries == 0) {
|
||||
ESP.restart();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user