WiFi Enabled Button
 Page :   [ 1 ]    [ 2 ]    [ 3 ]    [ 4 ]    [ 5 ]  

Some simple code to make a REST call to an arbitrary server to do something when you press the button!

 


#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <ESP8266HTTPClient.h>

#define SERVER_URL "http://192.168.1.100/script.php";

ESP8266WiFiMulti WiFiMulti;

const byte INT_PIN = 12;
const byte LED_PIN = 14;

volatile byte action = 0;

unsigned long previousMillis = 0;
 
void setup() {
  
  pinMode(INT_PIN, INPUT_PULLUP);
  pinMode(LED_PIN, OUTPUT);
  digitalWrite(LED_PIN, LOW);

  attachInterrupt(digitalPinToInterrupt(INT_PIN), handlePress, RISING);

  WiFiMulti.addAP("your_access_point", "your_access_point_password");

  while(!((WiFiMulti.run() == WL_CONNECTED))) {  

    digitalWrite(LED_PIN, HIGH);
    delay(500);
    digitalWrite(LED_PIN, LOW);
    delay(500);

  }
 
}
 
void handlePress() {
  action = 1;
}

void loop() {

 unsigned long currentMillis = millis();

 if(((WiFiMulti.run() == WL_CONNECTED))) { 
    digitalWrite(LED_PIN, HIGH);
  } else {
    digitalWrite(LED_PIN, LOW);
  }
 
  if (action > 0){

      if (currentMillis - previousMillis >= 5000) {

        Serial.println(action);
        previousMillis = currentMillis;

        HTTPClient http;
        
        String url = SERVER_URL;
        url += "?key=value";

        http.begin(url); 

        int httpCode = http.GET();

        if(httpCode > 0) {

          if(httpCode == HTTP_CODE_OK) {

          }

        }

      }
      
      action = 0;

  }
 
}

(Page 5 of 5)