Hi,
Thank you for the response.
Please find the attached sketch.
It is getting connected to the wifi and I can make the LED ON and OFF through "Blink LED ON" and "Blink LED OFF" HTML buttons. However it is not continuously blinking, as it should do, when hit the "Blink LED ON". It ONLY make it ON once and stay ON until I hit the buttons again.
PIN 13 (configured as digital out) connected to LED via 400 Ohms resistor.
4 HTML buttons have been created with following names:
(1) Turn ON : to just switch LED on once.
(2) Turn Off : to just switch off LED once.
(3) Blink LED ON : to blink the LED On & OFF, with an interval 500ms, until "Blink LED OFF" button is pressed.
(4) Blink LED OFF : to switch OFF the blinking LED
However, the 1st two buttons (Turn On and Turn Off) do not do anything now as I have made
digital write to the pin command in active (// digitalWrite(ledPin, HIGH)
So only buttons 3 & 4 are active.
There is a sub-routine with the name Blink_LED(boolean VALUE) which takes care of blinking the LED (On and OFF), simply toggling the digitalwrite HIGH and LOW. The Blink_LED(boolean VALUE) is inside the loop() function, and when the Boolean argument "VALUE" is true the LED should blink until the "Blink LED OFF" is pressed. "Blink LED ON" button is to set the Boolean VALUE to true. "Blink LED OFF" will set it false and make digitalWrite(ledPin, LOW).
I tested the Blink_LED(boolean VALUE) function as a separate sketch without WiFi and it is working as it should be.
I feel like although loop() should continuously loop(), it doesn't work like that when connected to WiFi due to some reason!
Any thoughts and suggestions are very much appreciated as I spent more than 4 days to find the reason for this behaviour without much success.
[code]
#include <ESP8266WiFi.h>
const char* ssid = "My_SSID";
const char* password = "My_passward1234";
int ledPin = 13; // GPIO13
WiFiServer server(80);
boolean LEDsw = false;
boolean LEDblink = false;
long previous_time = 0;
long interval = 500;
//following modes: WIFI_AP = access point,
//WIFI_STA = client, WIFI_AP_STA = (AP and client) or WIFI_OFF.
void setup() {
Serial.begin(115200);
delay(10);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
void Blink_LED(boolean VALUE);
// Connect to WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println("Local WiFi");
//***
WiFi.mode(WIFI_AP_STA);
//***
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
// Start the server
server.begin();
Serial.println("Server started");
// Print the IP address
Serial.print("Use this URL to connect: ");
Serial.print("http://");
Serial.print(WiFi.localIP());
Serial.println("/");
}
void loop() {
// Check if a client has connected
WiFiClient client = server.available();
if (!client) {
return;
}
// Wait until the client sends some data
Serial.println("new client");
while (!client.available()) {
delay(1);
}
// Read the first line of the request
String request = client.readStringUntil('\r');
Serial.println(request);
client.flush();
// Match the request for LED
Serial.printf("LEDblink Status = %d", LEDblink);
Serial.println("");
// Get the LED ON
if ((request.indexOf("/LED=ON") != -1) && LEDsw == false) {
//digitalWrite(ledPin, HIGH);
Serial.println("");
Serial.print("REQUEST Index LED=ON:");
Serial.println(request.indexOf("/LED=ON"));
LEDsw = true;
}
//Get the LED OFF
if ((request.indexOf("/LED=OFF") != -1) && LEDsw == HIGH) {
//digitalWrite(ledPin, LOW);
Serial.println("");
Serial.print("REQUEST Index LED=OFF:");
Serial.println(request.indexOf("/LED=OFF"));
LEDsw = LOW;
}
//Get the LED Blink with 500ms interval
if ((request.indexOf("/LED=BLINK_ON") != -1 ) ) {
LEDblink = true;
Serial.println("REQUEST Index Blink_ON:");
Serial.print(request.indexOf("/LED=BLINK_ON"));
}
if ((request.indexOf("/LED=BLINK_OFF") != -1) ) {
LEDblink = false;
Serial.println("");
Serial.printf("Index BLINK OFF = %d", request.indexOf("/LED=BLINK_OFF"));
digitalWrite(ledPin, LOW);
}
Serial.println("");
Serial.printf("LEDblink Status = %d", LEDblink);
Blink_LED(LEDblink);
// Set ledPin according to the request
//digitalWrite(ledPin, value);
// Return the response
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println(""); // do not forget this one
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.print("Led pin is now: ");
if (LEDsw == HIGH) {
client.print("On");
} else {
client.print("Off");
}
client.println("
"); //Line break
client.println("<a href=\"/LED=ON\"\"><button>Turn On </button></a>");
client.println("<>");
client.println("<a href=\"/LED=OFF\"\"><button>Turn Off </button></a>
");
client.println("
");
client.println("<a href=\"/LED=BLINK_ON\"\"><button> Blink LED ON </button></a>");
client.println("<>");
client.println("<a href=\"/LED=BLINK_OFF\"\"><button> Blink LED OFF </button></a>
");
client.println("</html>");
delay(50); //10
Serial.println("");
Serial.println("Client disonnected");
Serial.println("");
}
void Blink_LED(boolean VALUE) {
Serial.println("");
unsigned long current_time = millis();
Serial.printf("Current time =%d", current_time );
Serial.println("");
if (VALUE && ((current_time - previous_time) > interval) ) {
Serial.printf("Blink interval [ms] =%d", (current_time - previous_time));
digitalWrite(ledPin, !digitalRead(ledPin));
previous_time = current_time;
}
}
[/code]
WiFi_ESP8266_LED-CONTROL_6.ino.ino (3.93 KB)