From 8f363b02fea605a41f324bd655ac28cb54b140b7 Mon Sep 17 00:00:00 2001 From: Jaro Date: Mon, 26 Oct 2020 20:31:54 +0100 Subject: [PATCH] mqtt version with button2 --- platformio.ini | 1 + src/main.cpp | 99 ++++++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 88 insertions(+), 12 deletions(-) diff --git a/platformio.ini b/platformio.ini index c69537b..06ff046 100644 --- a/platformio.ini +++ b/platformio.ini @@ -14,3 +14,4 @@ board = esp32doit-devkit-v1 framework = arduino monitor_port = COM[13] monitor_speed = 115200 +lib_deps = knolleary/PubSubClient@^2.8 diff --git a/src/main.cpp b/src/main.cpp index 7f0a44f..340cffa 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -26,9 +26,17 @@ */ #include +#include #include +#include "Button2.h" + WiFiUDP udp; +WiFiClient espClient; +PubSubClient client(espClient); + +#define MSG_BUFFER_SIZE (50) +char msg[MSG_BUFFER_SIZE]; // -- Initial name of the Thing. Used e.g. as SSID of the own Access Point. const char thingName[] = "BADBell"; @@ -53,10 +61,9 @@ const char wifiInitialApPassword[] = "bell4home"; #define BELL_PIN 12 // Pin 12 as default #define MINIMAL_GAP 30*1000 // 30 seconds between rings +#define IMPULS 250 // minimal press of button -int val = 0; // value from pin -unsigned long last_low = 0; // time of uptime - +unsigned long last_sended = 0; // last transmit of message // -- Callback method declarations. void configSaved(); @@ -68,11 +75,14 @@ void sendPacket(); DNSServer dnsServer; WebServer server(80); +char mqttServerValue[STRING_LEN]; char ipParamValue[STRING_LEN]; char portParamValue[NUMBER_LEN]; char bellParamValue[NUMBER_LEN]; IotWebConf iotWebConf(thingName, &dnsServer, &server, wifiInitialApPassword, CONFIG_VERSION); + +IotWebConfParameter mqttServerParam = IotWebConfParameter("MQTT server", "mqttServer", mqttServerValue, STRING_LEN,"text","BADnet-gw.lan","BADnet-gw.lan",NULL); IotWebConfParameter ipParam = IotWebConfParameter("Mulicast ip param", "ipParam", ipParamValue, STRING_LEN,"text","224.1.2.3","224.1.2.3",NULL); IotWebConfSeparator separator1 = IotWebConfSeparator(); IotWebConfParameter portParam = IotWebConfParameter("Port param", "portParam", portParamValue, NUMBER_LEN, "number", "1025..65535", "1234", "min='1025' max='65535' step='1'"); @@ -80,6 +90,38 @@ IotWebConfParameter portParam = IotWebConfParameter("Port param", "portParam", p IotWebConfSeparator separator2 = IotWebConfSeparator("Calibration factor"); IotWebConfParameter bellParam = IotWebConfParameter("Bell number param", "bellParam", bellParamValue, NUMBER_LEN, "number", "1", "1", "step='1'"); +Button2 buttonBELL = Button2(BELL_PIN,INPUT_PULLDOWN,true,IMPULS); + +void callback(char* topic, byte* payload, unsigned int length) { + Serial.print("Message arrived ["); + Serial.print(topic); + Serial.print("] "); + for (int i = 0; i < length; i++) { + Serial.print((char)payload[i]); + } + Serial.println(); + + // Switch on the LED if an 1 was received as first character + if ((char)payload[0] == '1') { + sendPacket(); + } + +} + +void change(Button2& btn) { + if (btn.isPressed()) { + + Serial.print("released"); + + if (millis() - last_sended < MINIMAL_GAP) { + Serial.println("Minimal gap not meeted"); + return; + } + + sendPacket(); + } +} + void setup() { Serial.begin(115200); @@ -88,6 +130,7 @@ void setup() // iotWebConf.setStatusPin(STATUS_PIN); // iotWebConf.setConfigPin(CONFIG_PIN); + iotWebConf.addParameter(&mqttServerParam); iotWebConf.addParameter(&ipParam); iotWebConf.addParameter(&separator1); iotWebConf.addParameter(&portParam); @@ -109,22 +152,49 @@ void setup() pinMode(BELL_PIN, INPUT); digitalWrite(BELL_PIN, LOW); + buttonBELL.setChangedHandler(change); + Serial.println("Ready."); } +void reconnect() { + // Loop until we're reconnected + while (!client.connected()) { + Serial.print("Attempting MQTT connection..."); + // Create a random client ID + String clientId = "ESP32Client-BELL"; + clientId += String(bellParamValue); + // Attempt to connect + if (client.connect(clientId.c_str())) { + Serial.println("connected"); + // Once connected, publish an announcement... + client.subscribe("home/bell/sendring"); + } else { + Serial.print("failed, rc="); + Serial.print(client.state()); + Serial.println(" try again in 5 seconds"); + // Wait 5 seconds before retrying + delay(5000); + } + } +} + + void loop() { // -- doLoop should be called as frequently as possible. iotWebConf.doLoop(); - val = digitalRead(BELL_PIN); - if (val == 1) { - if (millis() - last_low > MINIMAL_GAP || last_low == 0) sendPacket(); - else { - Serial.println("Minimal gap not meeted"); - delay(500); - } - last_low = millis(); - } + client.loop(); + buttonBELL.loop(); + + if ((iotWebConf.getState() == IOTWEBCONF_STATE_ONLINE) && (!client.connected())) + { + client.setServer(mqttServerValue, 1883); + client.setCallback(callback); + reconnect(); + } + + } /** @@ -141,6 +211,8 @@ void handleRoot() String s = ""; s += "WIFI BELL FOR BAD HOMEBell for Wifi"; s += "
    "; + s += "
  • MQTT server address: "; + s += mqttServerValue; s += "
  • Multicast IP value: "; s += ipParamValue; s += "
  • Port of multicast message: "; @@ -174,7 +246,10 @@ void sendPacket() udp.endPacket(); udp.flush(); udp.stop(); + + client.publish("home/bell/ring", msg); + last_sended = millis(); } void sendRingPacket()