Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ESPMetRED.h>
- const char* MQTT_PUBLISH_TOPIC = "ESPBATTBUTTON/OUT";
- const char* MQTT_SUBSCRIBE_TOPIC = "ESPBATTBUTTON/IN";
- const char* CLIENT_ID = "ESPBATTBUTTON";
- const char* WIFI_SSID = "Ahmed";
- const char* WIFI_PASSWORD = "WiFI Password";
- const char* MQTT_SERVER = "192.168.50.30";
- const char* MQTT_USER = "pi";
- const char* MQTT_PASSWORD = "MQTT Password";
- IPAddress ip(192, 168, 50, 40);
- IPAddress gateway(192, 168, 50, 1);
- IPAddress subnet(255, 255, 255, 0);
- IPAddress dns(192, 168, 50, 1);
- ESPMetRED client(ip, gateway, subnet, dns); //starting library with fixed IP
- int buttons[2] = {4, 5}; // Pins on which buttons are attached
- int button; // Variable to store the button which triggered the bootup
- int wake = 12; // Pin which is used to Keep ESP awake until job is finished
- boolean published = false;
- boolean notified = false;
- void setup() {
- // ESP Awake Pin, the pin which keeps CH_PD HIGH, a requirement for normal functioning of ESP8266
- pinMode(wake, OUTPUT);
- digitalWrite(wake, HIGH);
- //Check which button was pressed
- for(int i=0; i<4; i++)
- {
- pinMode(buttons[i], INPUT);
- if(digitalRead(buttons[i]) == HIGH)
- {
- button = buttons[i];
- return; //Push Button identified, Leave the loop
- }
- }
- }
- void loop() {
- client.keepalive(); // Necessary for successful connection between server and client
- if((client.stMqTT()) && (!published)) // stMqTT() function returns the MQTT connection state
- {
- published = true;
- client.Publish("debug", client.JsonString(String(CLIENT_ID), String(button))); //Publish the data about Pushed Button
- }
- if((published) && (digitalRead(button) == HIGH)) // Check if button is released, a debounce step
- {
- if(!notified) // Notify the server that Button is not yet released
- {
- notified = true;
- client.Publish("debug", String(CLIENT_ID) + " [ESP Waiting Release of Button]");
- }
- }
- else if((published) && (digitalRead(button) == LOW)) //Push Button has released, Let's get switched OFF
- {
- client.Publish("debug", String(CLIENT_ID) + " [ESP's Going DOWN]");
- digitalWrite(wake, LOW); //Turns the ESP OFF
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement