Change OLED to listen on MQTT
This commit is contained in:
74
src/main.cpp
74
src/main.cpp
@@ -14,6 +14,16 @@
|
||||
#include <ESPmDNS.h>
|
||||
#include <Update.h>
|
||||
#include <uri/UriBraces.h>
|
||||
#include <PubSubClient.h>
|
||||
#include <ArduinoJson.h>
|
||||
|
||||
const char* mqtt_server = "mqtt.ddns.kawomi.com";
|
||||
|
||||
WiFiClient espClient;
|
||||
PubSubClient client(espClient);
|
||||
unsigned long lastMsg = 0;
|
||||
#define MSG_BUFFER_SIZE (50)
|
||||
char msg[MSG_BUFFER_SIZE];
|
||||
|
||||
const char* host = "esp32oled";
|
||||
const char* ssid = "JoNelsDarlings";
|
||||
@@ -114,6 +124,58 @@ const char* serverIndex =
|
||||
"</script>";
|
||||
|
||||
|
||||
void reconnect() {
|
||||
// Loop until we're reconnected
|
||||
while (!client.connected()) {
|
||||
Serial.print("Attempting MQTT connection...");
|
||||
// Create a random client ID
|
||||
String clientId = "ESP32Client-";
|
||||
clientId += String(random(0xffff), HEX);
|
||||
// Attempt to connect
|
||||
if (client.connect(clientId.c_str())) {
|
||||
Serial.println("MQTT connected");
|
||||
client.publish("OLED", "Connected");
|
||||
client.subscribe("Address/Livorno/Office/OLED/setText");
|
||||
} else {
|
||||
Serial.print("MQTT failed, rc=");
|
||||
Serial.print(client.state());
|
||||
Serial.println(" try again in 5 seconds");
|
||||
// Wait 5 seconds before retrying
|
||||
delay(5000);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void callback(char* topic, byte* payload, unsigned int length) {
|
||||
Serial.print("Message arrived [");
|
||||
Serial.print(topic);
|
||||
|
||||
String recBuf="";
|
||||
Serial.print("] ");
|
||||
for (int i = 0; i < length; i++) {
|
||||
recBuf += (char)payload[i];
|
||||
}
|
||||
Serial.println(recBuf);
|
||||
|
||||
StaticJsonDocument<1024> jsonDoc;
|
||||
deserializeJson(jsonDoc,recBuf);
|
||||
const char* theText=jsonDoc["text"];
|
||||
int theSize=jsonDoc["size"];
|
||||
|
||||
display.setCursor(40, 0);
|
||||
display.setTextSize(9);
|
||||
display.print("#");
|
||||
display.display();
|
||||
delay(500);
|
||||
|
||||
display.setCursor(0, 0);
|
||||
display.setTextSize(theSize);
|
||||
display.clearDisplay();
|
||||
display.print(theText);
|
||||
display.display();
|
||||
}
|
||||
|
||||
|
||||
void writeToOled(String text){
|
||||
display.clearDisplay();
|
||||
display.setTextColor(WHITE);
|
||||
@@ -128,7 +190,9 @@ void writeToOled(String text){
|
||||
*/
|
||||
void setup(void) {
|
||||
Serial.begin(9600);
|
||||
delay(2000);
|
||||
|
||||
Serial.println("STARTUP");
|
||||
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
|
||||
Serial.println(F("SSD1306 allocation failed"));
|
||||
for(;;);
|
||||
@@ -137,6 +201,7 @@ void setup(void) {
|
||||
|
||||
writeToOled("Startup");
|
||||
|
||||
randomSeed(micros());
|
||||
// Connect to WiFi network
|
||||
WiFi.begin(ssid, password);
|
||||
Serial.println("");
|
||||
@@ -215,6 +280,9 @@ void setup(void) {
|
||||
});
|
||||
server.begin();
|
||||
|
||||
client.setServer(mqtt_server, 1883);
|
||||
client.setCallback(callback);
|
||||
|
||||
writeToOled("Ready.");
|
||||
|
||||
}
|
||||
@@ -222,4 +290,10 @@ void setup(void) {
|
||||
void loop(void) {
|
||||
server.handleClient();
|
||||
delay(1);
|
||||
|
||||
if (!client.connected()) {
|
||||
reconnect();
|
||||
}
|
||||
client.loop();
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user