Sunday, May 3, 2015

Arduino Due + ESP8266: Web Control LED

This example show how to implement a simple web server on Arduino Due with ESP8266 ESP-12 WiFi Module. User visit it with GET method to turn ON/OFF the on-board LED of Arduino Due.


to turn ON LED:
http://ipaddress?led=1
to turn OFF LED:
http://ipaddress?led=0

This demo run with ESP-12, suppose it work on any ESP8266 module.


In fact, it's not too stable! Sometime will lost if cannot detect "+IPD".

Connection:
ESP-12 VCC connect to separated power of 3.3V
Due GND - ESP-12 GND
Due TX3 - ESP-12 RX
Due RX3 - ESP-12 TX
ESP-12 CH_PD connect to ESP-12 VCC

Due connect with PC USB, such that we can monitor the status of ESP-12.

Start Serial Monitor, at 9600 baud, to reset and start run the program.

Due8266WebControlLed.ino
/*
Arduino Due - ESP 8266 WiFi Module
Serial (Tx/Rx) communicate to PC via USB
Serial3 (Tx3/Rx3) connect to ESP8266
Tx3 - ESP8266 Rx
Rx3 - ESP8266 Tx
ESP8266 CH_PD Connect to ESP8266 VCC
*/

#define ESP8266 Serial3
String SSID = "TestAP";
String PASSWORD = "12345678";

int LED = 13;

boolean FAIL_8266 = false;

void setup() {
  pinMode(LED, OUTPUT);
  
  digitalWrite(LED, LOW);
  delay(300);
  digitalWrite(LED, HIGH);
  delay(200);
  digitalWrite(LED, LOW);
  delay(300);
  digitalWrite(LED, HIGH);
  delay(200);
  digitalWrite(LED, LOW);
  delay(300);
  digitalWrite(LED, HIGH);
  delay(200);
  digitalWrite(LED, LOW);

  do{
    Serial.begin(9600);
    ESP8266.begin(9600);
  
    //Wait Serial Monitor to start
    while(!Serial);
    Serial.println("--- Start ---");

    ESP8266.println("AT+RST");
    delay(1000);
    if(ESP8266.find("ready"))
    {
      Serial.println("Module is ready");
      ESP8266.println("AT+CWMODE=1");
      delay(2000);
      
      //Quit existing AP, for demo
      Serial.println("Quit AP");
      ESP8266.println("AT+CWQAP");
      delay(1000);
      
      clearESP8266SerialBuffer();
      if(cwJoinAP())
      {
        Serial.println("CWJAP Success");
        FAIL_8266 = false;
        
        delay(3000);
        clearESP8266SerialBuffer();
        //Get and display my IP
        sendESP8266Cmdln("AT+CIFSR", 1000);  
        //Set multi connections
        sendESP8266Cmdln("AT+CIPMUX=1", 1000);
        //Setup web server on port 80
        sendESP8266Cmdln("AT+CIPSERVER=1,80",1000);
        
        Serial.println("Server setup finish");
      }else{
        Serial.println("CWJAP Fail");
        delay(500);
        FAIL_8266 = true;
      }
    }else{
      Serial.println("Module have no response.");
      delay(500);
      FAIL_8266 = true;
    }
  }while(FAIL_8266);
  
  digitalWrite(LED, HIGH);
}

void loop(){
  if(ESP8266.available()) 
  {
    Serial.println("Something received");
    delay(1000);
    if(ESP8266.find("+IPD,"))
    {
      String action;
      
      Serial.println("+IPD, found");
      int connectionId = ESP8266.read()-48;
      Serial.println("connectionId: " + String(connectionId));
      
      ESP8266.find("led=");
      char s = ESP8266.read();
      if(s=='0'){
        action = "led=0";
        digitalWrite(LED, LOW);
      }else if(s=='1'){
        action = "led=1";
        digitalWrite(LED, HIGH);
      }else{
        action = "led=?";
      }
      
      Serial.println(action);
      sendHTTPResponse(connectionId, action);
      
      //Close TCP/UDP
      String cmdCIPCLOSE = "AT+CIPCLOSE="; 
      cmdCIPCLOSE += connectionId;
      sendESP8266Cmdln(cmdCIPCLOSE, 1000);
    }
  }
}

void sendHTTPResponse(int id, String content)
{
  String response;
  response = "HTTP/1.1 200 OK\r\n";
  response += "Content-Type: text/html; charset=UTF-8\r\n"; 
  response += "Content-Length: ";
  response += content.length();
  response += "\r\n";
  response +="Connection: close\r\n\r\n";
  response += content;

  String cmd = "AT+CIPSEND=";
  cmd += id;
  cmd += ",";
  cmd += response.length();
  
  Serial.println("--- AT+CIPSEND ---");
  sendESP8266Cmdln(cmd, 1000);
  
  Serial.println("--- data ---");
  sendESP8266Data(response, 1000);
}

boolean waitOKfromESP8266(int timeout)
{
  do{
    Serial.println("wait OK...");
    delay(1000);
    if(ESP8266.find("OK"))
    {
      return true;
    }

  }while((timeout--)>0);
  return false;
}

boolean cwJoinAP()
{
  String cmd="AT+CWJAP=\"" + SSID + "\",\"" + PASSWORD + "\"";
  ESP8266.println(cmd);
  return waitOKfromESP8266(10);
}

//Send command to ESP8266, assume OK, no error check
//wait some time and display respond
void sendESP8266Cmdln(String cmd, int waitTime)
{
  ESP8266.println(cmd);
  delay(waitTime);
  clearESP8266SerialBuffer();
}

//Basically same as sendESP8266Cmdln()
//But call ESP8266.print() instead of call ESP8266.println()
void sendESP8266Data(String data, int waitTime)
{
  ESP8266.print(data);
  delay(waitTime);
  clearESP8266SerialBuffer();
}

//Clear and display Serial Buffer for ESP8266
void clearESP8266SerialBuffer()
{
  Serial.println("= clearESP8266SerialBuffer() =");
  while (ESP8266.available() > 0) {
    char a = ESP8266.read();
    Serial.write(a);
  }
  Serial.println("==============================");
}


17 comments:

  1. --- Start ---
    Module have no response.
    --- Start ---
    Module is ready
    Quit AP
    = clearESP8266SerialBuffer() =

    AT+CWMODE=1

    no change
    AT+CWQAP


    OK
    ==============================
    wait OK...
    wait OK...
    wait OK...
    wait OK...
    wait OK...
    wait OK...
    wait OK...
    wait OK...
    wait OK...
    wait OK...
    wait OK...
    CWJAP Fail
    --- Start ---
    Module is ready
    Quit AP
    = clearESP8266SerialBuffer() =

    AT+CWMODE=1

    no change
    AT+CWQAP


    OK

    What´s the problem??
    Thanks

    ReplyDelete
    Replies
    1. hello,

      I retest it with "v0.9.5.2 AT Firmware", with both baud set 115200. It still work.

      In your case, it cannot join AP. What's your AP? WiF router? mobile share hotspot? have you change SSID and PASSWORD to match your setting? Have to set MAC filter in your router?

      I have experience before, it can join AP of my home WiFi, mobile share hotspot of Samsung S3, Redmi 2, but cannot join share hotspot of HTC One X.

      Delete
  2. This tutorial web control led..

    Can this code to input sensor data to web?

    ReplyDelete
    Replies
    1. yes, you can modify do read sensor then response.

      Delete
  3. --- Start ---
    Module have no response.

    I use ESP 01 is that a problem ?

    ReplyDelete
    Replies
    1. It's seem that your ESP01 cannot reset, or connection in-correct...hard to confirm, sorry!

      Delete
    2. Do I flash the esp, if I buy it new?

      Delete
    3. hello bg,
      It's suggested to check the firmware. All my esp chips come with old version firmware.

      Delete
  4. This comment has been removed by the author.

    ReplyDelete
    Replies
    1. sorry to post this comment here by mistake, actually it should be posted at http://arduino-er.blogspot.tw/2015/11/standalone-esp8266esp-12-web-control-on.html

      will re-post this comment over there at above link

      Delete
    2. --- Start ---
      Module have no response.
      --- Start ---
      Module have no response.
      --- Start ---
      Module have no response.
      --- Start ---
      Module have no response.
      --- Start ---
      Module have no response.
      --- Start ---
      Module have no response.

      I already change "SSID" and "PASSWORD".
      What's happen with this?

      Delete
    3. hello 侯冠廷,

      "Module have no response" means no "ready" return by the command "AT+RST".

      - Make sure the connection is correct
      - Make sure the ESP is work, at least minimum: http://arduino-er.blogspot.com/2015/04/esp8266-open-box-power-up-test-with.html
      (if you have USB-to-serial adapter, suggest to check your ESP in PC first, such that you can know what it return. Take caution on the voltage of your ESP and the USB-to-serial adapter)

      Delete
  5. Can i get the Project Source code as Zip in mail......??????

    ReplyDelete
  6. hello thank you very much for the code, I served much to control a car from the web ... but wanted to see if I could help to control it from an ethernet shield, since I do not and have done it. Could leave the code if anyone is interested.


    ReplyDelete
  7. this tutorial is for over the internet or within the network?

    ReplyDelete
  8. --- Start ---
    Module is ready
    Quit AP
    = clearESP8266SerialBuffer() =

    Eric, What's happen with this?
    Thankss!

    ReplyDelete