Chat freely about anything...

User avatar
By Sam G
#84584 Hi, I am attempting to use an ESP in access point mode to make an LED blink or turn off. I found some code here that allows me to turn an LED on and off using a button on the website it creates. This is what the sketch looks like currently.
Code: Select all/*
 * Sketch: ESP8266_LED_Control_02C
 * Now with added CSS and a single button
 * Control an LED from a web browser
 * Intended to be run on an ESP8266
 *
 * connect to the ESP8266 AP then
 * use web broswer to go to 192.168.4.1
 *
 */
 
 
#include <ESP8266WiFi.h>
const char WiFiPassword[] = "12345678";
const char AP_NameChar[] = "LEDControl" ;
 
WiFiServer server(80);
 
String header = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n";
String html_1 = "<!DOCTYPE html><html><head><meta name='viewport' content='width=device-width, initial-scale=1.0'/><meta charset='utf-8'><style>body {font-size:140%;} #main {display: table; margin: auto;  padding: 0 10px 0 10px; } h2,{text-align:center; } .button { padding:10px 10px 10px 10px; width:100%;  background-color: #4CAF50; font-size: 120%;}</style><title>LED Control</title></head><body><div id='main'><h2>LED Control</h2>";
String html_2 = "";
String html_4 = "</div></body></html>";
 
String request = "";
int LED_Pin = D1;
 
void setup()
{
    pinMode(LED_Pin, OUTPUT);
 
    boolean conn = WiFi.softAP(AP_NameChar, WiFiPassword);
    server.begin();
 
} // void setup()
 
 
 
void loop()
{
 
    // Check if a client has connected
    WiFiClient client = server.available();
    if (!client)  {  return;  }
 
    // Read the first line of the request
    request = client.readStringUntil('\r');
 
    if       ( request.indexOf("LEDON") > 0 )  { digitalWrite(LED_Pin, HIGH);  }
    else if  ( request.indexOf("LEDOFF") > 0 ) { digitalWrite(LED_Pin, LOW);   }
 
 
    // Get the LED pin status and create the LED status message
    if (digitalRead(LED_Pin) == HIGH)
    {
        // the LED is on so the button needs to say turn it off
       html_2 = "<form id='F1' action='LEDOFF'><input class='button' type='submit' value='Turn of the LED' ></form><br>";
    }
    else                             
    {
        // the LED is off so the button needs to say turn it on
        html_2 = "<form id='F1' action='LEDON'><input class='button' type='submit' value='Turn on the LED' ></form><br>";
    }
 
 
    client.flush();
 
    client.print( header );
    client.print( html_1 );   
    client.print( html_2 );
    client.print( html_4);
 
    delay(5);
  // The client will actually be disconnected when the function returns and 'client' object is detroyed
 
} // void loop()

I was hoping to be able to change it so that instead of turning the LED on, it would make it blink. I have close to zero experience with Arduino, but I tried a couple things to see if I could get it to work. Here's what I was hoping would work:
Code: Select all    if       ( request.indexOf("LEDON") > 0 )  {
      for(int i = 0; i < 1000; i++)
      {
         digitalWrite(LED_Pin, HIGH);
         delay(1000);
         digitalWrite(LED_Pin, LOW); 
      }
    }
    else if  ( request.indexOf("LEDOFF") > 0 ) { digitalWrite(LED_Pin, LOW);   }

When I upload this and go to the address, I can turn on the LED, but it doesn't blink. Additionally, I cannot turn off the LED. I'd appreciate it if someone could let me know what I am doing wrong and tell me the correct way to do it. Thanks!
User avatar
By Sam G
#84601
Anttiduino wrote:1) You need a delay also after the "digitalWrite(LED_Pin, LOW);" in the loop
2) You can turn the LED off only after you leave the loop (1000 seconds)


I added the delay, and now it blinks, but I don't understand what you mean by number 2. How do I leave the loop?
User avatar
By Anttiduino
#84632 2) When your program enters the loop there is no way out of it. You must wait wait for the the loop to end to be able to use the buttons again.

Instead of a loop you should use timers and/or interrupts. Below you can see one example how to do it.

Code: Select all/*
 * Sketch: ESP8266_LED_Control_02
 * Control an LED from a web browser
 * Intended to be run on an ESP8266
 *
 * connect to the ESP8266 AP then
 * use web browser to go to 192.168.4.1
 *
 */
 
#include <ESP8266WiFi.h>
#include <Ticker.h>

const char WiFiPassword[] = "12345678";
const char AP_NameChar[] = "LEDControl" ;
 
WiFiServer server(80);
 
String header = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n";
String html_1 = "<!DOCTYPE html><html><head><title>LED Control</title></head><body><div id='main'><h2>LED Control</h2>";
String html_2 = "<form id='F1' action='LEDON'><input class='button' type='submit' value='LED ON' ></form><br>";
String html_3 = "<form id='F2' action='LEDOFF'><input class='button' type='submit' value='LED OFF' ></form><br>";
String html_4 = "</div></body></html>";
 
String request = "";
bool LED_on = true;
const int LED_Pin = D1;
const int ticks = 2000000; // = 1000000 us = 1 sec between interrupts
 
void ICACHE_RAM_ATTR onTimerISR()
{
  if (LED_on)
    digitalWrite(LED_Pin,!(digitalRead(LED_Pin)));  // Toggle LED Pin
  else
    digitalWrite(LED_Pin,LOW); // LED off
  timer1_write(ticks);
} // void ICACHE_RAM_ATTR
 
void setup()
{
    pinMode(LED_Pin, OUTPUT);

    WiFi.softAP(AP_NameChar, WiFiPassword);
    server.begin();

    timer1_attachInterrupt(onTimerISR);
    timer1_enable(TIM_DIV16, TIM_EDGE, TIM_SINGLE); // 5MHz (1 tick = 5 microseconds)
    timer1_write(ticks);
 
} // void setup()
 
void loop()
{
 
    // Check if a client has connected
    WiFiClient client = server.available();
    if (client)  {

      // Read the first line of the request
      request = client.readStringUntil('\r');
   
      if       ( request.indexOf("LEDON") > 0 )  { LED_on = true; }
      else if  ( request.indexOf("LEDOFF") > 0 ) { LED_on = false; }
   
      client.flush();
   
      client.print( header );
      client.print( html_1 );
      client.print( html_2 );
      client.print( html_3 );
      client.print( html_4);
    }
    delay(5);
  // The client will actually be disconnected when the function returns and 'client' object is detroyed
 
} // void loop()