Require Guidance on Sketch...Single LED control using Arduino and ESP8266

Hi,

I’m a newbie to arduino and have been trying to learn some of the sketches for arduino in the internet.

I have tried for last few days to control LED wirelessly using a web page using Arduino and ESP8266.

But the connection is very inconsistent. I have tried with the blynk app and it works like a charm with no delay. They have very good libraries to execute the control.

Just wondering if i could get help to get a simple sketch to control an LED wirelessly from a web page using both arduino and esp8266.

Thanks in advance.

Gokz

ps: i have been trying with this sketch

#include <SoftwareSerial.h>
 
#define DEBUG true
 
SoftwareSerial esp8266(2,3); // make RX Arduino line is pin 2, make TX Arduino line is pin 3.
                             // This means that you need to connect the TX line from the esp to the Arduino's pin 2
                             // and the RX line from the esp to the Arduino's pin 3
void setup()
{
  Serial.begin(115200);
  esp8266.begin(115200); // your esp's baud rate might be different
  
 
  
  sendData("AT+RST\r\n",2000,DEBUG); // reset module
  sendData("AT+CWMODE=2\r\n",1000,DEBUG); // configure as access point
  sendData("AT+CIFSR\r\n",1000,DEBUG); // get ip address
  sendData("AT+CIPMUX=1\r\n",1000,DEBUG); // configure for multiple connections
  sendData("AT+CIPSERVER=1,80\r\n",1000,DEBUG); // turn on server on port 80
}
 
void loop()
{
  if(esp8266.available()) // check if the esp is sending a message 
  {
    /*
    while(esp8266.available())
    {
      // The esp has data so display its output to the serial window 
      char c = esp8266.read(); // read the next character.
      Serial.write(c);
    } */
    
    if(esp8266.find("+IPD,"))
    {
     delay(1000);
 
     int connectionId = esp8266.read()-48; // subtract 48 because the read() function returns 
                                           // the ASCII decimal value and 0 (the first decimal number) starts at 48
     
     String webpage = "<h1>Hello</h1>&lth2>World!</h2><button>LED1</button>";
 
     String cipSend = "AT+CIPSEND=";
     cipSend += connectionId;
     cipSend += ",";
     cipSend +=webpage.length();
     cipSend +="\r\n";
     
     sendData(cipsend,1000,DEBUG);
     sendData(webpage,1000,DEBUG);
     
     webpage="<button>LED2</button>";
     
     cipSend = "AT+CIPSEND=";
     cipSend += connectionId;
     cipSend += ",";
     cipSend +=webpage.length();
     cipSend +="\r\n";
     
     sendData(cipsend,1000,DEBUG);
     sendData(webpage,1000,DEBUG);
 
     String closeCommand = "AT+CIPCLOSE="; 
     closeCommand+=connectionId; // append connection id
     closeCommand+="\r\n";
     
     sendData(closeCommand,3000,DEBUG);
    }
  }
}
 
 
String sendData(String command, const int timeout, boolean debug)
{
    String response = "";
    
    esp8266.print(command); // send the read character to the esp8266
    
    long int time = millis();
    
    while( (time+timeout) > millis())
    {
      while(esp8266.available())
      {
        
        // The esp has data so display its output to the serial window 
        char c = esp8266.read(); // read the next character.
        response+=c;
      }  
    }
    
    if(debug)
    {
      Serial.print(response);
    }
    
    return response;
}

gokzee:  esp8266.begin(115200); // your esp's baud rate might be different

SoftwareSerial is unreliable at 115200 because it's too fast.

You could connect the ESP8266 to the hardware serial lines of your Arduino and it will work at 115200 but if you're using an Arduino board like the Uno which only has one hardware serial line that will mean you wouldn't be able to use the Serial Monitor to see the response and the ESP8266 data as your sketch currently does.

You can change the ESP8266 baud rate to a lower value using the AT+UART_CUR command to temporarily set the value(will go back to 115200 after turned off/reset) or AT+UART_DEF to persistently set the value. If you're using an older AT firmware version then the commands might be different, see the instruction set reference for your firmware version for the correct command.

I've successfully used the WebServerLed example that comes with the WiFiEsp library: https://github.com/bportaluri/WiFiEsp

Hi,

Thanks for the reply.

this is my esp firmware which i obtained with 115200 baud rate.

AT+GMR

AT version:0.22.0.0(Mar 20 2015 10:04:26)
SDK version:1.0.0
compile time:Mar 20 2015 11:00:32

Will this be a compatible firmware ??

I couldn't find the documentation for 0.22, the oldest I could find was 0.30: https://cdn.sparkfun.com/.../4/0/3/4A-ESP8266__AT_Instruction_Set__EN_v0.30.pdf but that one still has the same AT commands for setting the baud rate. If AT+UART_CUR and AT+UART_DEF don't work then try AT+UART instead, it does the same thing as AT+UART_DEF but is deprecated so I'm assuming that was the command before AT+UART_CUR and AT+UART_DEF were added.

Unfortunately the WiFiEsp library I posted a link to above requires AT firmware 0.25 or later so it's not compatible with your firmware but you can always just use AT commands directly as in your sketch you posted.

Hi,

Thanks again.

Do you have a link to any of the required firmwares for ESP8266 so that i can try some of your sketches.

My current one is pretty old i guess for it to function properly.

Regards,

Gokz

Which ESP8266 module/board/shield are you using?

ESP8266 ESP-01 1MB

IF_781B13BB.jpg

Are you sure your ESP-01 has 1MB of flash, not 512 KB?

I have read in the net that Black color ESP-01 has twice the size of the blue colored ESP-01.

Hence i assumed that it would be 1MB

The reason I asked is that the newer AT firmware versions(anything greater than 0.40 I think) require at least 1MB. So if you have 1MB flash you should be able to use AT+CIUPDATE to do an OTA firmware update. I think it will update you to AT firmware v1.0. v1.1 is out now(http://bbs.espressif.com/viewtopic.php?t=2199) but I don't know if it's available for OTA update yet. You can use a USB serial adapter to update the firmware using the Flash Download Tool(http://bbs.espressif.com/viewtopic.php?f=57&t=433).

Hi,

I tried your sketch to turn on and off LED.

The serial monitor shows the following while in baud rate :115200

I know my firmware version is 0.22, but just checking if this is another issue.

���[WiFiEsp] Initializing ESP module [WiFiEsp] >>> TIMEOUT >>> [WiFiEsp] >>> TIMEOUT >>> [WiFiEsp] >>> TIMEOUT >>> [WiFiEsp] >>> TIMEOUT >>> [WiFiEsp] >>> TIMEOUT >>> [WiFiEsp] Cannot initialize ESP module [WiFiEsp] >>> TIMEOUT >>> [WiFiEsp] No tag found WiFi shield not present

regards,

Gokz

also the command AT+CIUPDATE gives me an error in the serial monitor.

My other firmware updates were all unstable except the one which is posted here.

gokzee: The serial monitor shows the following while in baud rate :115200

I know my firmware version is 0.22, but just checking if this is another issue.

You're using a firmware version that the WiFiEsp specifically says is not compatible and SoftwareSerial at a baud rate I've already told you is not stable. It's no surprise it didn't work.

gokzee: also the command AT+CIUPDATE gives me an error in the serial monitor.

What error?

gokzee: My other firmware updates were all unstable

What updates, what do you mean by unstable

gokzee: except the one which is posted here.

Which one is that?

Here you can find the ESP8266 AT Instruction Set v0.22 20.03.2015 v0.22.

Hi pert & Whandall,

I tried my best to update the firmware of esp-01 but couldn’t find a proper firmware for it to flash.

My ESP-01 was unstable with some of the latest firmwares. I guess i had a faulty product.

I got a product called WEMOS D1(retired version) from market yesterday.

It connects to wifi well and is stable.

i tried the example sketches and it worked well.

I was also able to control a single LED wirelessly using a web server without any issues.

But when i wrote a sketch to control 8 leds, it dint function well.

I’m only able to control a single LED at a time.

My digital ports are all working but i’m able to control only one led at a time with any one digital port

Guess i’m wrong somewhere.

#include <ESP8266WiFi.h>

const char* ssid = "chittady";
const char* password = "1234";

int ledPin = 3;
int ledPin1 = 1;
int ledPin2 = 16;
int ledPin3 = 5;
int ledPin4 = 4;
int ledPin5 = 0;
int ledPin6 = 2;
int ledPin7 = 14;
int ledPin8 = 12;
int ledPin9 = 3;
WiFiServer server(80);

void setup() {
Serial.begin(115200);
delay(10);

pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);

// Connect to WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);

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 : ");
Serial.print("http://");
Serial.print(WiFi.localIP());
Serial.println("/");

digitalWrite(ledPin, LOW);
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, LOW);
digitalWrite(ledPin4, LOW);

}

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

int value = LOW;
if (request.indexOf("/LED=ON") != -1) {
digitalWrite(ledPin, !digitalRead(ledPin));
value = HIGH;
}
if (request.indexOf("/LED=OFF") != -1){
digitalWrite(ledPin, LOW);
value = LOW;
}
if (request.indexOf("/LED=1") != -1){
Serial.println("LED 1 ");
digitalWrite(ledPin1, !digitalRead(ledPin1));
value = LOW;
}
if (request.indexOf("/LED=2") != -1){
digitalWrite(ledPin2, !digitalRead(ledPin2));
value = LOW;
}
if (request.indexOf("/LED=3") != -1){
digitalWrite(ledPin3, !digitalRead(ledPin3));
value = LOW;
}
if (request.indexOf("/LED=4") != -1){
digitalWrite(ledPin4, !digitalRead(ledPin4));
value = LOW;
}
if (request.indexOf("/LED=5") != -1){
digitalWrite(ledPin5, !digitalRead(ledPin5));
value = LOW;
}
if (request.indexOf("/LED=6") != -1){
digitalWrite(ledPin6, !digitalRead(ledPin6));
value = LOW;
}
if (request.indexOf("/LED=7") != -1){
digitalWrite(ledPin7, !digitalRead(ledPin7));
value = LOW;
}
if (request.indexOf("/LED=8") != -1){
digitalWrite(ledPin8, !digitalRead(ledPin8));
value = LOW;
}

if (request.indexOf("/LED=9") != -1){
digitalWrite(ledPin9, !digitalRead(ledPin9));
value = LOW;
}

// 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(value == HIGH) {
client.print("On");
} else {
client.print("Off");
}
client.println("

");
client.println("Click <a href="/LED=ON">here</a> turn the LED on pin 0 ON
");
client.println("Click <a href="/LED=1">here</a> turn the LED on pin 1 ON/OFF
");
client.println("Click <a href="/LED=2">here</a> turn the LED on pin 2 ON/OFF
");
client.println("Click <a href="/LED=3">here</a> turn the LED on pin 3 ON/OFF
");
client.println("Click <a href="/LED=4">here</a> turn the LED on pin 4 ON/OFF
");
client.println("Click <a href="/LED=5">here</a> turn the LED on pin 5 ON/OFF
");
client.println("Click <a href="/LED=6">here</a> turn the LED on pin 6 ON/OFF
");
client.println("Click <a href="/LED=7">here</a> turn the LED on pin 7 ON/OFF
");
client.println("Click <a href="/LED=8">here</a> turn the LED on pin 8 ON/OFF
");
client.println("Click <a href="/LED=9">here</a> turn the LED on pin 9 ON/OFF
");
client.println("</html>");

delay(1);
Serial.println("Client disconnected");
Serial.println("");

}

Where am i going wrong ??

Thanks in Advance.

Best,

Gokz

gokzee:
when i wrote a sketch to control 8 leds, it dint function well.
I’m only able to control a single LED at a time.
My digital ports are all working but i’m able to control only one led at a time with any one digital port
Guess i’m wrong somewhere.

You really need to provide more information on the issues you are encountering. I’m trying to help you out here but you’re making it way more work on my part than it needs to be. That’s not very respectful of my time. Nevertheless, I’m a nice guy with a D1(retired) so I took the time to copy your code, fix it, upload it, and test it and found your problems:

You need to set the pinMode for all the LED pins, not just the first one.

gokzee:

client.println("Click <a href="/LED=ON">here</a> turn the LED on pin 0 ON

");

You need to escape the quotes in these strings or your code won’t even compile. It should be:

client.println("Click <a href=\"/LED=ON\">here</a> turn the LED on pin 0 ON
");

gokzee:

int ledPin = 3;

int ledPin1 = 1;
int ledPin2 = 16;
int ledPin3 = 5;
int ledPin4 = 4;
int ledPin5 = 0;
int ledPin6 = 2;
int ledPin7 = 14;
int ledPin8 = 12;
int ledPin9 = 3;

I recommend you to use the Dn(e.g. D1, D2, D3) pin number notation with the WeMos D1(retired) so that your pin numbers will match the numbers printed on the board.

Please always use Tools > Auto Format on your code before posting it to the forum.

Hi pert,

Sorry for the trouble. I guess i was too generic when i described the problem. I,m very new to the arduino world and couldn’t dig deeper to understand the real problem.

Thanks for the solution. It worked when i set the pinmode for all leds. That was what i was missing.

I was a bit confused with the pin labels as it was my first day with wemos D1.

Will keep the pin labels in mind for my next sketch. :slight_smile:

Thanks once again.

Best,

Gokz

Glad to hear it's working.

gokzee: I was a bit confused with the pin labels as it was my first day with wemos D1.

That one got me too on my first experience with the D1 since the standard Arduino boards don't use that pin notation I assumed I was supposed to ignore the "D" and use just the pin number as usual. I was convinced there was an error in the pin definitions or layout of the board until I dug into the source code and realized what was going on. I never had so much trouble getting an LED to blink!