I'm trying to build a remote control with an ESP8266 that calls a URL each time a button is pressed.
This functionality is working but I need to wait for 5 seconds each time I press a button, next button pressed in the period in between will result in no action at all, is like having a sleep or delay function but I don't have any.
I'm attaching pictures of my board and this is the code:
#include <ESP8266HTTPClient.h>
#include <ESP8266WiFi.h>
void setup() {
Serial.begin(115200); //Serial connection
WiFi.begin("Fibertel WiFi731 2.4", "0052843636"); //WiFi connection
while (WiFi.status() != WL_CONNECTED) { //Wait for the WiFI connection completion
delay(500);
Serial.println("Waiting for connection");
}
pinMode( D1, INPUT );
pinMode( D2, INPUT );
pinMode( D3, INPUT );
pinMode( D4, INPUT );
pinMode( D5, INPUT );
pinMode( D6, INPUT );
pinMode( D7, INPUT );
pinMode( D8, INPUT );
pinMode( LED_BUILTIN, OUTPUT );
}
void loop() {
byte button1State = digitalRead( D1 );
if( button1State == HIGH )
toggleLights("http://192.168.0.199/leds.cgi?led=0");
byte button2State = digitalRead( D2 );
if( button2State == HIGH )
toggleLights("http://192.168.0.199/leds.cgi?led=1");
byte button5State = digitalRead( D5 );
if( button5State == HIGH )
toggleLights("http://192.168.0.199/leds.cgi?led=3");
byte button6State = digitalRead( D6 );
if( button6State == HIGH )
toggleLights("http://192.168.0.199/leds.cgi?led=4");
byte button7State = digitalRead( D7 );
if( button7State == HIGH )
toggleLights("http://192.168.0.199/leds.cgi?led=5");
byte button3State = digitalRead( D8 );
if( button3State == HIGH )
toggleLights("http://192.168.0.199/leds.cgi?led=2");
}
void toggleLights(String destination) {
if(WiFi.status()== WL_CONNECTED){ //Check WiFi connection status
HTTPClient http; //Declare object of class HTTPClient
http.begin(destination); //Specify request destination
http.addHeader("Content-Type", "text/plain"); //Specify content-type header
http.setAuthorization("b", "b"); //Specify username/password header
int httpCode = http.GET();
String payload = http.getString(); //Get the response payload
Serial.println(httpCode); //Print HTTP return code
Serial.println(payload); //Print request response payload
http.end(); //Close connection
}else{
Serial.println("Error in WiFi connection");
}
}