From e4cc86451fe44efce2002c349e8a3218239ef91e Mon Sep 17 00:00:00 2001 From: Philipp Kramer Date: Thu, 19 Dec 2024 16:30:02 +0100 Subject: [PATCH] add esp8266 and mqtt publish and color set --- platformio.ini | 22 +++- src/main.cpp | 328 ++++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 307 insertions(+), 43 deletions(-) diff --git a/platformio.ini b/platformio.ini index 15592be..a4a56e2 100644 --- a/platformio.ini +++ b/platformio.ini @@ -17,7 +17,7 @@ monitor_speed = 115200 build_flags = - '-D MQTT_BASETOPIC="lightbutton_test"' + '-D MQTT_BASETOPIC="lightbuttons-A"' lib_deps= ethernet @@ -26,3 +26,23 @@ lib_deps= adafruit/Adafruit seesaw Library@^1.7.8 adafruit/Adafruit MQTT Library@^2.5.8 + mathertel/OneButton@^2.6.1 + + +[env:wemosd1] +platform = espressif8266 +board = d1_mini +framework = arduino + +monitor_speed = 115200 + + +build_flags = + '-D MQTT_BASETOPIC="lightbuttons-A"' + -D WIFI + +lib_deps= + SPI + adafruit/Adafruit seesaw Library@^1.7.8 + adafruit/Adafruit MQTT Library@^2.5.8 + mathertel/OneButton@^2.6.1 diff --git a/src/main.cpp b/src/main.cpp index d23d06e..79bd85e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,5 +1,10 @@ #include -#include +#ifndef WIFI + #include +#endif +#ifdef WIFI + #include +#endif #include "Adafruit_NeoKey_1x4.h" #include "seesaw_neopixel.h" @@ -9,6 +14,8 @@ #include "credentials.h" +#include + static bool eth_connected = false; @@ -18,22 +25,74 @@ WiFiClient client; //WiFiClientSecure client; // Setup the MQTT client class by passing in the WiFi client and MQTT server and login details. -//Adafruit_MQTT_Client mqtt(&client, MQTT_SERVER, MQTT_SERVERPORT, MQTT_USERNAME, MQTT_PASSWORD); +#ifdef MQTT_USERNAME +Adafruit_MQTT_Client mqtt(&client, MQTT_SERVER, MQTT_SERVERPORT, MQTT_USERNAME, MQTT_PASSWORD); +#else Adafruit_MQTT_Client mqtt(&client, MQTT_SERVER, MQTT_SERVERPORT); +#endif +Adafruit_MQTT_Publish statusuptimePub = Adafruit_MQTT_Publish(&mqtt, MQTT_BASETOPIC"/status/uptime"); Adafruit_MQTT_Publish statusPub = Adafruit_MQTT_Publish(&mqtt, MQTT_BASETOPIC"/status"); +#ifndef WIFI void WiFiEvent(WiFiEvent_t event); +#endif + void MQTT_connect(); -bool nokey_enabled=false; + +bool neokey_enabled=false; Adafruit_NeoKey_1x4 neokey; // Create the NeoKey object uint32_t Wheel(byte WheelPos); NeoKey1x4Callback blink(keyEvent evt); + + +OneButton button1; +void click1(); +void doubleclick1(); +void longPressStart1(); +Adafruit_MQTT_Publish button1Pub = Adafruit_MQTT_Publish(&mqtt, MQTT_BASETOPIC"/button1"); +Adafruit_MQTT_Subscribe button1Color = Adafruit_MQTT_Subscribe(&mqtt, MQTT_BASETOPIC"/color/button1"); +void button1ColorCB(uint32_t c); +uint32_t releasecolor1; + +OneButton button2; +void click2(); +void doubleclick2(); +void longPressStart2(); +Adafruit_MQTT_Publish button2Pub = Adafruit_MQTT_Publish(&mqtt, MQTT_BASETOPIC"/button2"); +Adafruit_MQTT_Subscribe button2Color = Adafruit_MQTT_Subscribe(&mqtt, MQTT_BASETOPIC"/color/button2"); +void button2ColorCB(uint32_t c); +uint32_t releasecolor2; + +OneButton button3; +void click3(); +void doubleclick3(); +void longPressStart3(); +Adafruit_MQTT_Publish button3Pub = Adafruit_MQTT_Publish(&mqtt, MQTT_BASETOPIC"/button3"); +Adafruit_MQTT_Subscribe button3Color = Adafruit_MQTT_Subscribe(&mqtt, MQTT_BASETOPIC"/color/button3"); +void button3ColorCB(uint32_t c); +uint32_t releasecolor3; + +OneButton button4; +void click4(); +void doubleclick4(); +void longPressStart4(); +Adafruit_MQTT_Publish button4Pub = Adafruit_MQTT_Publish(&mqtt, MQTT_BASETOPIC"/button4"); +Adafruit_MQTT_Subscribe button4Color = Adafruit_MQTT_Subscribe(&mqtt, MQTT_BASETOPIC"/color/button4"); +void button4ColorCB(uint32_t c); +uint32_t releasecolor4; + + + +uint32_t presscolor = 0x555555; + + + //define a callback for key presses NeoKey1x4Callback blink(keyEvent evt) { uint8_t key = evt.bit.NUM; @@ -61,44 +120,14 @@ void setup() { while (! Serial) delay(10); - // Connect to WiFi access point. - /* - Serial.println(); Serial.println(); - Serial.print("Connecting to "); - Serial.println(WLAN_SSID); - - WiFi.begin(WLAN_SSID, WLAN_PASS); - while (WiFi.status() != WL_CONNECTED) { - delay(500); - Serial.print("."); - } - Serial.println(); - - Serial.println("WiFi connected"); - Serial.println("IP address: "); Serial.println(WiFi.localIP()); - */ - - // Add a handler for network events. This is misnamed "WiFi" because the ESP32 is historically WiFi only, - // but in our case, this will react to Ethernet events. - Serial.print("Registering event handler for ETH events..."); - WiFi.onEvent(WiFiEvent); - Serial.print("Starting ETH interface..."); - ETH.begin(); - - // Send Hello World Notification - - - - - if (! neokey.begin(0x30)) { // begin with I2C address, default is 0x30 Serial.println("Could not start NeoKey, check wiring?"); - nokey_enabled=false; + neokey_enabled=false; }else{ - nokey_enabled=true; + neokey_enabled=true; } - if (nokey_enabled) + if (neokey_enabled) { Serial.println("NeoKey started!"); @@ -119,25 +148,152 @@ void setup() { neokey.registerCallback(i, blink); } } + + + + + + +#ifdef WIFI //Only for Wifi + // Connect to WiFi access point. + Serial.println(); Serial.println(); + + Serial.print("Connecting to "); + Serial.println(WLAN_SSID); + + + + WiFi.begin(WLAN_SSID, WLAN_PASS); + while (WiFi.status() != WL_CONNECTED) { + for (uint16_t i=0; i 1000){ + if (millis() - last_status_send > 60000){ last_status_send=millis(); if (!eth_connected){ Serial.println("Waiting for ethernet connection"); }else{ Serial.print(F("\nSending Hello!")); Serial.print("..."); - if (! statusPub.publish("Hello")) { + if (! statusuptimePub.publish((uint32_t)millis())) { Serial.println(F("Failed")); } else { Serial.println(F("OK!")); @@ -145,11 +301,11 @@ void loop() { } } - delay(10); // don't print too fast } // React to Ethernet events:- +#ifndef WIFI void WiFiEvent(WiFiEvent_t event) { switch (event) { @@ -201,7 +357,7 @@ void WiFiEvent(WiFiEvent_t event) break; } } - +#endif /******************************************/ @@ -237,13 +393,101 @@ void MQTT_connect() { while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected Serial.println(mqtt.connectErrorString(ret)); Serial.println("Retrying MQTT connection in 5 seconds..."); + for (uint16_t i=0; i