Skip to content

Instantly share code, notes, and snippets.

@mattie47
Last active February 9, 2020 09:56
Show Gist options
  • Save mattie47/21aa266a2f699a98d8d3117917a03b21 to your computer and use it in GitHub Desktop.
Save mattie47/21aa266a2f699a98d8d3117917a03b21 to your computer and use it in GitHub Desktop.
ESP-01 Dual Push Button Arduino Sketch
/*
ESP8266 Wi-Fi Button
Article on code: https://techlife.nz/blog/battery-powered-esp-01-push-button/
*/
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
const char* deviceName = "esp-push-button";
// Replace with your SSID and Password
const char* ssid = "SSID";
const char* password = "PASS";
const char* mqtt_server = "192.168.1.x";
//Static IP address configuration
IPAddress staticIP(192,168,1,66); //ESP static ip
IPAddress gateway(192,168,1,254); //IP Address of your WiFi Router (Gateway)
IPAddress subnet(255,255,255,0); //Subnet mask
WiFiClient espClient;
PubSubClient client(espClient);
int button = 2;
int auxButtonState = 2; // temporary variable for reading the button pin status
void buttonState(){
pinMode(0, OUTPUT); // Pull GPIO 0 LOW after boot
digitalWrite(0, LOW);
pinMode(button, INPUT); // declare push button as input
auxButtonState = digitalRead(button);
Serial.print("Aux Butten State: ");
Serial.println(auxButtonState);
if (auxButtonState == 0) { // GPIO pin seems to be high by default on wemos
Serial.println("Aux Button pushed");
}
else {
// Serial.println("Aux Button NOT pushed");
}
}
void setup() {
Serial.begin(115200);
// delay(5000);
buttonState();
initWifi();
mqttConnection();
// Deep sleep mode until RESET pin is connected to a LOW signal (pushbutton is pressed)
ESP.deepSleep(0);
}
void loop() {
// sleeping so wont get here
}
// Establish a Wi-Fi connection with your router
void initWifi() {
Serial.print("\nConnecting to: ");
Serial.print(ssid);
WiFi.config(staticIP, subnet, gateway);
WiFi.hostname(deviceName); // DHCP Hostname (useful for finding device for static lease)
WiFi.begin(ssid, password);
int timeout = 10 * 4; // 10 seconds
while (WiFi.status() != WL_CONNECTED && (timeout-- > 0)) {
delay(500);
Serial.print(".");
}
Serial.println("");
if (WiFi.status() != WL_CONNECTED) {
Serial.println("Failed to connect to WiFi, going back to sleep");
ESP.deepSleep(0);
}
Serial.print("WiFi connected in: ");
Serial.print(millis());
Serial.println("msec");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
// MQTT message
void mqttConnection() {
int retries = 0;
client.setServer(mqtt_server, 1883);
while (!client.connected()){
Serial.print("Attempting MQTT connection...");
if (client.connect("ESP8266Client")) {
Serial.println("MQTT Connected");
}
else {
retries ++;
if (retries >= 5) {
Serial.println("MQTT Failed 5 times. Going to sleep...");
ESP.deepSleep(0);
}
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 2 seconds");
// Wait 2 seconds before retrying
delay(2000);
}
}
if (auxButtonState == 0){
client.publish("ESP-Push-Button/btn2", "");
Serial.print("Sending Aux button state...");
}
else
{
client.publish("ESP-Push-Button/btn1", "");
}
Serial.println("Closing the MQTT connection");
client.disconnect();
}
[env:esp01_1m]
platform = espressif8266
board = esp01_1m
framework = arduino
lib_deps = PubSubClient
monitor_speed = 115200
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment