Change OLED to listen on MQTT
This commit is contained in:
@@ -12,5 +12,8 @@
|
|||||||
platform = espressif32
|
platform = espressif32
|
||||||
board = esp32doit-devkit-v1
|
board = esp32doit-devkit-v1
|
||||||
framework = arduino
|
framework = arduino
|
||||||
lib_deps = adafruit/Adafruit SSD1306@^2.4.0
|
lib_deps =
|
||||||
|
adafruit/Adafruit SSD1306@^2.4.0
|
||||||
|
knolleary/PubSubClient@^2.8
|
||||||
|
bblanchon/ArduinoJson@^6.19.4
|
||||||
lib_ldf_mode = chain+
|
lib_ldf_mode = chain+
|
||||||
74
src/main.cpp
74
src/main.cpp
@@ -14,6 +14,16 @@
|
|||||||
#include <ESPmDNS.h>
|
#include <ESPmDNS.h>
|
||||||
#include <Update.h>
|
#include <Update.h>
|
||||||
#include <uri/UriBraces.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* host = "esp32oled";
|
||||||
const char* ssid = "JoNelsDarlings";
|
const char* ssid = "JoNelsDarlings";
|
||||||
@@ -114,6 +124,58 @@ const char* serverIndex =
|
|||||||
"</script>";
|
"</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){
|
void writeToOled(String text){
|
||||||
display.clearDisplay();
|
display.clearDisplay();
|
||||||
display.setTextColor(WHITE);
|
display.setTextColor(WHITE);
|
||||||
@@ -128,7 +190,9 @@ void writeToOled(String text){
|
|||||||
*/
|
*/
|
||||||
void setup(void) {
|
void setup(void) {
|
||||||
Serial.begin(9600);
|
Serial.begin(9600);
|
||||||
|
delay(2000);
|
||||||
|
|
||||||
|
Serial.println("STARTUP");
|
||||||
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
|
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
|
||||||
Serial.println(F("SSD1306 allocation failed"));
|
Serial.println(F("SSD1306 allocation failed"));
|
||||||
for(;;);
|
for(;;);
|
||||||
@@ -137,6 +201,7 @@ void setup(void) {
|
|||||||
|
|
||||||
writeToOled("Startup");
|
writeToOled("Startup");
|
||||||
|
|
||||||
|
randomSeed(micros());
|
||||||
// Connect to WiFi network
|
// Connect to WiFi network
|
||||||
WiFi.begin(ssid, password);
|
WiFi.begin(ssid, password);
|
||||||
Serial.println("");
|
Serial.println("");
|
||||||
@@ -215,6 +280,9 @@ void setup(void) {
|
|||||||
});
|
});
|
||||||
server.begin();
|
server.begin();
|
||||||
|
|
||||||
|
client.setServer(mqtt_server, 1883);
|
||||||
|
client.setCallback(callback);
|
||||||
|
|
||||||
writeToOled("Ready.");
|
writeToOled("Ready.");
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -222,4 +290,10 @@ void setup(void) {
|
|||||||
void loop(void) {
|
void loop(void) {
|
||||||
server.handleClient();
|
server.handleClient();
|
||||||
delay(1);
|
delay(1);
|
||||||
|
|
||||||
|
if (!client.connected()) {
|
||||||
|
reconnect();
|
||||||
|
}
|
||||||
|
client.loop();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user