Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@donovankeith
Last active October 30, 2020 22:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save donovankeith/6c181b6054d9a51d73abda0174ed2ec5 to your computer and use it in GitHub Desktop.
Save donovankeith/6c181b6054d9a51d73abda0174ed2ec5 to your computer and use it in GitHub Desktop.
Arduino IoT Button with the LoLin Node MCU v3
/**
* IOTButtonForIFTT.ino
* Connects to Wifi, Retrieves a URL, Blinks LED on if Successful
* Be sure to adjust the User Inputs below.
*
* 2017/11/16 Modified by
* Donovan Keith <donovanskeith@gmail.com>
*
* Based On:
* BasicHttpClient.ino by
* Created on: 5.7.2016 by Sajin George
*
* References:
* https://github.com/techiesms/IoT-Button-DIY-Amazon-Dash-Button-/blob/master/IoT_Button.ino
*/
// Imports
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
// USER INPUT
// Request Settings
char iftttTriggerUrl[] = "http://maker.ifttt.com/trigger/EVENT/with/key/KEY"; // `http` is intentional as `https` won't work without a more complex setup.
// WiFi Settings
char wifiNetwork[] = "NetworkName";
char wifiPassword[] = "NetworkPassword";
// Board Setup
const int serialBaudRate = 9600;
const int LED_PIN = 2; // [Reference](https://arduino.stackexchange.com/questions/38477/does-the-node-mcu-v3-lolin-not-have-a-builtin-led)
const int BUTTON_PIN = 16; // D0 [Reference](http://www.c-sharpcorner.com/article/blinking-led-by-esp-12e-nodemcu-v3-module-using-arduinoide/)
// Variables
int lastPressed = millis();
int oneSecond = 1000;
int timeoutDuration = 5 * oneSecond;
int pushCount = 0;
// Forward Declarations
void setupWifi(void);
bool postToIfttt(void);
void setup() {
pinMode(BUTTON_PIN, INPUT);
// setup the led pin and turn it off
pinMode(LED_PIN, OUTPUT);
blinkLED();
blinkLED();
blinkLED();
Serial.begin(serialBaudRate);
Serial.flush();
Serial.println("");
Serial.println("");
Serial.println("DIY IoT IFTTT Button");
Serial.println("====================");
Serial.println("");
setupWifi();
Serial.println("");
Serial.println("Button Presses");
Serial.println("--------------");
}
void blinkLED(){
setLED(true);
delay(500);
setLED(false);
delay(250);
}
void setLED(bool state){
if (state){
// The built-in LED actually turns on when LOW and off when HIGH
digitalWrite(LED_PIN, LOW);
} else {
digitalWrite(LED_PIN, HIGH);
}
}
void loop() {
if (!buttonPressed())
return;
if(WiFi.status() == WL_CONNECTED)
{
requestUrl();
}
else {
Serial.println("ERROR: No Wifi connection, can't log press.");
}
}
bool buttonPressed(){
// Returns `true` the first time a button is pressed (or still pressed)
bool buttonState = digitalRead(BUTTON_PIN);
setLED(buttonState);
if (buttonState){
if (millis() > (lastPressed + timeoutDuration)){
lastPressed = millis();
pushCount++;
Serial.println("Button Pressed: ");
Serial.print(pushCount);
Serial.print(" times");
delay(100); // No need to keep checking while user is releasing button
return true;
}
else {
Serial.println("Button locked until timeout is over.");
delay(100); // No need to keep checking while user is releasing button
}
}
return false;
}
void setupWifi()
{
Serial.println("Wifi Connection");
Serial.println("---------------");
Serial.println("");
Serial.println("Conecting to WiFi Network: ");
Serial.print(wifiNetwork);
WiFi.disconnect();
WiFi.begin(wifiNetwork, wifiPassword);
const int maxDotsPerRow = 26;
int dotCount = 0;
while (WiFi.status() != WL_CONNECTED) {
if (dotCount > maxDotsPerRow){
Serial.println("");
dotCount=0;
}
Serial.print(".");
dotCount++;
delay(500);
}
Serial.println("");
Serial.println("WiFi Connected");
Serial.println("");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
Serial.println("");
}
bool requestUrl()
{
// [Reference](https://techtutorialsx.com/2016/07/17/esp8266-http-get-requests/)
HTTPClient http; //Declare an object of class HTTPClient
http.begin(iftttTriggerUrl); //Specify request destination
int httpCode = http.GET(); //Send the request
if (httpCode > 0) { //Check the returning code
String payload = http.getString(); //Get the request response payload
Serial.println(payload); //Print the response payload
}
http.end(); //Close connection
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment