diff --git a/platformio.ini b/platformio.ini index 446aa93..6dfd66f 100644 --- a/platformio.ini +++ b/platformio.ini @@ -1,16 +1,19 @@ -; PlatformIO Project Configuration File -; -; Build options: build flags, source filter -; Upload options: custom upload port, speed and extra flags -; Library options: dependencies, extra library storages -; Advanced options: extra scripting -; -; Please visit documentation for the other options and examples -; https://docs.platformio.org/page/projectconf.html - -[env:esp32doit-devkit-v1] -platform = espressif32 -board = esp32doit-devkit-v1 -framework = arduino -lib_deps = adafruit/Adafruit SSD1306@^2.4.0 -lib_ldf_mode = chain+ \ No newline at end of file +; PlatformIO Project Configuration File +; +; Build options: build flags, source filter +; Upload options: custom upload port, speed and extra flags +; Library options: dependencies, extra library storages +; Advanced options: extra scripting +; +; Please visit documentation for the other options and examples +; https://docs.platformio.org/page/projectconf.html + +[env:esp32doit-devkit-v1] +platform = espressif32 +board = esp32doit-devkit-v1 +framework = arduino +lib_deps = + adafruit/Adafruit SSD1306@^2.4.0 + knolleary/PubSubClient@^2.8 + bblanchon/ArduinoJson@^6.19.4 +lib_ldf_mode = chain+ diff --git a/src/main.cpp b/src/main.cpp index 47c70ee..c3839a6 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -14,6 +14,16 @@ #include #include #include +#include +#include + +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 = ""; +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(); + }