Skip to content

Instantly share code, notes, and snippets.

@Bardoctorus
Created August 17, 2018 12:52
Show Gist options
  • Select an option

  • Save Bardoctorus/9abf8cc8c7d61251b4c377e9ddeb10a4 to your computer and use it in GitHub Desktop.

Select an option

Save Bardoctorus/9abf8cc8c7d61251b4c377e9ddeb10a4 to your computer and use it in GitHub Desktop.
Code for the www.MakeUseOf.com Wi-Fi connected Button tutorial by Ian Buckley
//Code for the www.MakeUseOf.com Wi-Fi connected Button tutorial by Ian Buckley
#include <IFTTTWebhook.h>
#include <ESP8266WiFi.h>
#define ledPin 5
#define wakePin 16
#define ssid "YOUR SSID HERE"
#define password "YOUR WIFI PASSWORD HERE"
#define IFTTT_API_KEY "IFTTT_KEY_GOES_HERE"
#define IFTTT_EVENT_NAME "IFTTT_EVENT_NAME_HERE"
void setup() {
Serial.begin(115200);
while(!Serial) {
}
Serial.println(" ");// print an empty line before and after Button Press
Serial.println("Button Pressed");
Serial.println(" ");
connectToWifi();
IFTTTWebhook hook(IFTTT_API_KEY, IFTTT_EVENT_NAME);
hook.trigger();
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, HIGH);
delay(200);
digitalWrite(ledPin, LOW);
ESP.deepSleep(wakePin);
}
void connectToWifi() {
Serial.print("Connecting to: NAME "); //uncomment next line to show SSID name
//Serial.print(ssid);
WiFi.begin(ssid, password);
Serial.println(" ");
Serial.print("Attempting to connect: ");
//try to connect for 10 seconds
int i = 10;
while(WiFi.status() != WL_CONNECTED && i >=0) {
delay(1000);
Serial.print(i);
Serial.print(", ");
i--;
}
Serial.println(" ");
//print connection result
if(WiFi.status() == WL_CONNECTED){
Serial.print("Connected.");
Serial.println(" ");
Serial.print("NodeMCU ip address: ");
Serial.println(WiFi.localIP());
}
else{
Serial.println("Connection failed - check your credentials or connection");
}
}
void loop(){
//if deep sleep is working, this code will never run.
Serial.println("This should never get printed");
delay(1000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment