mqtt version with button2

This commit is contained in:
2020-10-26 20:31:54 +01:00
parent e9683b6a16
commit 8f363b02fe
2 changed files with 88 additions and 12 deletions

View File

@@ -14,3 +14,4 @@ board = esp32doit-devkit-v1
framework = arduino framework = arduino
monitor_port = COM[13] monitor_port = COM[13]
monitor_speed = 115200 monitor_speed = 115200
lib_deps = knolleary/PubSubClient@^2.8

View File

@@ -26,9 +26,17 @@
*/ */
#include <IotWebConf.h> #include <IotWebConf.h>
#include <PubSubClient.h>
#include <Regexp.h> #include <Regexp.h>
#include "Button2.h"
WiFiUDP udp; 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. // -- Initial name of the Thing. Used e.g. as SSID of the own Access Point.
const char thingName[] = "BADBell"; const char thingName[] = "BADBell";
@@ -53,10 +61,9 @@ const char wifiInitialApPassword[] = "bell4home";
#define BELL_PIN 12 // Pin 12 as default #define BELL_PIN 12 // Pin 12 as default
#define MINIMAL_GAP 30*1000 // 30 seconds between rings #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_sended = 0; // last transmit of message
unsigned long last_low = 0; // time of uptime
// -- Callback method declarations. // -- Callback method declarations.
void configSaved(); void configSaved();
@@ -68,11 +75,14 @@ void sendPacket();
DNSServer dnsServer; DNSServer dnsServer;
WebServer server(80); WebServer server(80);
char mqttServerValue[STRING_LEN];
char ipParamValue[STRING_LEN]; char ipParamValue[STRING_LEN];
char portParamValue[NUMBER_LEN]; char portParamValue[NUMBER_LEN];
char bellParamValue[NUMBER_LEN]; char bellParamValue[NUMBER_LEN];
IotWebConf iotWebConf(thingName, &dnsServer, &server, wifiInitialApPassword, CONFIG_VERSION); 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); IotWebConfParameter ipParam = IotWebConfParameter("Mulicast ip param", "ipParam", ipParamValue, STRING_LEN,"text","224.1.2.3","224.1.2.3",NULL);
IotWebConfSeparator separator1 = IotWebConfSeparator(); IotWebConfSeparator separator1 = IotWebConfSeparator();
IotWebConfParameter portParam = IotWebConfParameter("Port param", "portParam", portParamValue, NUMBER_LEN, "number", "1025..65535", "1234", "min='1025' max='65535' step='1'"); 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"); IotWebConfSeparator separator2 = IotWebConfSeparator("Calibration factor");
IotWebConfParameter bellParam = IotWebConfParameter("Bell number param", "bellParam", bellParamValue, NUMBER_LEN, "number", "1", "1", "step='1'"); 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() void setup()
{ {
Serial.begin(115200); Serial.begin(115200);
@@ -88,6 +130,7 @@ void setup()
// iotWebConf.setStatusPin(STATUS_PIN); // iotWebConf.setStatusPin(STATUS_PIN);
// iotWebConf.setConfigPin(CONFIG_PIN); // iotWebConf.setConfigPin(CONFIG_PIN);
iotWebConf.addParameter(&mqttServerParam);
iotWebConf.addParameter(&ipParam); iotWebConf.addParameter(&ipParam);
iotWebConf.addParameter(&separator1); iotWebConf.addParameter(&separator1);
iotWebConf.addParameter(&portParam); iotWebConf.addParameter(&portParam);
@@ -109,22 +152,49 @@ void setup()
pinMode(BELL_PIN, INPUT); pinMode(BELL_PIN, INPUT);
digitalWrite(BELL_PIN, LOW); digitalWrite(BELL_PIN, LOW);
buttonBELL.setChangedHandler(change);
Serial.println("Ready."); 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() void loop()
{ {
// -- doLoop should be called as frequently as possible. // -- doLoop should be called as frequently as possible.
iotWebConf.doLoop(); iotWebConf.doLoop();
val = digitalRead(BELL_PIN); client.loop();
if (val == 1) { buttonBELL.loop();
if (millis() - last_low > MINIMAL_GAP || last_low == 0) sendPacket();
else { if ((iotWebConf.getState() == IOTWEBCONF_STATE_ONLINE) && (!client.connected()))
Serial.println("Minimal gap not meeted"); {
delay(500); client.setServer(mqttServerValue, 1883);
} client.setCallback(callback);
last_low = millis(); reconnect();
} }
} }
/** /**
@@ -141,6 +211,8 @@ void handleRoot()
String s = "<!DOCTYPE html><html lang=\"en\"><head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1, user-scalable=no\"/>"; String s = "<!DOCTYPE html><html lang=\"en\"><head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1, user-scalable=no\"/>";
s += "<title>WIFI BELL FOR BAD HOME</title></head><body>Bell for Wifi"; s += "<title>WIFI BELL FOR BAD HOME</title></head><body>Bell for Wifi";
s += "<ul>"; s += "<ul>";
s += "<li>MQTT server address: ";
s += mqttServerValue;
s += "<li>Multicast IP value: "; s += "<li>Multicast IP value: ";
s += ipParamValue; s += ipParamValue;
s += "<li>Port of multicast message: "; s += "<li>Port of multicast message: ";
@@ -175,6 +247,9 @@ void sendPacket()
udp.flush(); udp.flush();
udp.stop(); udp.stop();
client.publish("home/bell/ring", msg);
last_sended = millis();
} }
void sendRingPacket() void sendRingPacket()