Created
March 27, 2022 21:04
-
-
Save raulcodes/fe9a4dd6737dfd29835ec7cdaf4fbdd3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*************************************************** | |
****************************************************/ | |
#include <ButtonDebounce.h> | |
#include <ESP8266WiFi.h> | |
#include "Adafruit_MQTT.h" | |
#include "Adafruit_MQTT_Client.h" | |
/************************* WiFi Access Point *********************************/ | |
#define WLAN_SSID "" | |
#define WLAN_PASS "" | |
/************************* MQTT server Setup *********************************/ | |
#define MQTT_SERVER "" | |
#define MQTT_SERVERPORT 1883 | |
#define MQTT_USERNAME "" | |
#define MQTT_KEY "" | |
/************ Global State (you don't need to change this!) ******************/ | |
WiFiClient client; | |
Adafruit_MQTT_Client mqtt(&client, MQTT_SERVER, MQTT_SERVERPORT, MQTT_USERNAME, MQTT_KEY); | |
/****************************** Feeds ***************************************/ | |
Adafruit_MQTT_Publish rauls_lamp = Adafruit_MQTT_Publish(&mqtt, MQTT_USERNAME "rauls_lamp"); | |
Adafruit_MQTT_Publish brookes_lamp = Adafruit_MQTT_Publish(&mqtt, MQTT_USERNAME "brookes_lamp"); | |
/*************************** Sketch Code ************************************/ | |
const int button1Pin = 4; | |
const int button2Pin = 16; | |
const long debounceDelay = 100; | |
static ButtonDebounce rauls_button(button1Pin, debounceDelay); | |
static ButtonDebounce brookes_button(button2Pin, debounceDelay); | |
void MQTT_connect(); | |
void publish_rauls_lamp(int state) { | |
if (state == 0) { | |
Serial.println("rauls_lamp"); | |
rauls_lamp.publish(0); | |
} | |
} | |
void publish_brookes_lamp(int state) { | |
if (state == 0) { | |
Serial.println("brookes_lamp"); | |
brookes_lamp.publish(0); | |
} | |
} | |
void setup() { | |
Serial.begin(115200); | |
rauls_button.setCallback(publish_rauls_lamp); | |
brookes_button.setCallback(publish_brookes_lamp); | |
WIFI_connect(WLAN_SSID, WLAN_PASS, true); | |
} | |
void loop() { | |
MQTT_connect(); | |
brookes_button.update(); | |
rauls_button.update(); | |
if(! mqtt.ping()) { | |
mqtt.disconnect(); | |
} | |
} | |
void WIFI_connect(char* ssid, char* pass, bool debug) { | |
if (debug) { | |
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); | |
if (debug) { | |
Serial.print("."); | |
} | |
} | |
if (debug) { | |
Serial.println(); | |
Serial.println("WiFi connected"); | |
Serial.println("IP address: "); | |
Serial.println(WiFi.localIP()); | |
} | |
} | |
// Function to connect and reconnect as necessary to the MQTT server. | |
// Should be called in the loop function and it will take care of connecting. | |
void MQTT_connect() { | |
int8_t ret; | |
// Stop if already connected. | |
if (mqtt.connected()) { | |
return; | |
} | |
Serial.print("Connecting to MQTT... "); | |
uint8_t retries = 3; | |
while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected | |
Serial.println(mqtt.connectErrorString(ret)); | |
Serial.println("Retrying MQTT connection in 5 seconds..."); | |
mqtt.disconnect(); | |
delay(5000); // wait 5 seconds | |
retries--; | |
if (retries == 0) { | |
// basically die and wait for WDT to reset me | |
while (1); | |
} | |
} | |
Serial.println("MQTT Connected!"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment