Thursday, May 28, 2015

Arduino Due + ESP8266 web server controlled LED

It's a web server using Arduino Due + ESP8266 WiFi module, act as web server to turn ON/OFF Arduino Due on-board LED. It can be controlled by a Android App (in my another blog post: Arduino-er - Android App to control Arduino+ESP8266 web connected LED).


The ESP8266 set in CWMODE=2, its IP should be 192.168.4.1 by default.

Note: It have problem if client side sent command too fast, in connection close and connect too close.

Connection:


Due8266WebControlLed_AP.ino
/*
Arduino Due - ESP 8266 WiFi Module

Act as AP mode, CWMODE=2

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

for firmware:
"v0.9.5.2 AT Firmware"
(http://goo.gl/oRdG3s)
AT version:0.21.0.0
SDK version:0.9.5

*/

#define ESP8266 Serial3

int LED = 13;

boolean FAIL_8266 = false;

#define BUFFER_SIZE 128
char buffer[BUFFER_SIZE];

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(115200);
    ESP8266.begin(115200);
  
    //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=2");
      delay(1000);
      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");
        
      FAIL_8266 = false;
    }else{
      Serial.println("Module have no response.");
      delay(500);
      FAIL_8266 = true;
    }
  }while(FAIL_8266);
  
  digitalWrite(LED, HIGH);
  ESP8266.setTimeout(1000);
}

void loop(){

  if(ESP8266.available()) // check if the esp is sending a message 
  {
    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;
}

//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("==============================");
}


Related:
Standalone ESP8266/ESP-12 (without controller/Arduino): web control on-board LED

23 comments:

  1. please help
    i get eror when download program to my arduino uno


    Arduino: 1.5.8 (Windows 7), Board: "Arduino Uno"

    android_led_wifi.ino: In function 'void setup()':
    android_led_wifi.ino:20:17: error: 'Serial3' was not declared in this scope
    android_led_wifi.ino:48:5: note: in expansion of macro 'ESP8266'
    android_led_wifi.ino:20:17: error: 'Serial3' was not declared in this scope
    android_led_wifi.ino:81:3: note: in expansion of macro 'ESP8266'
    android_led_wifi.ino: In function 'void loop()':
    android_led_wifi.ino:20:17: error: 'Serial3' was not declared in this scope
    android_led_wifi.ino:86:6: note: in expansion of macro 'ESP8266'
    android_led_wifi.ino: In function 'boolean waitOKfromESP8266(int)':
    android_led_wifi.ino:20:17: error: 'Serial3' was not declared in this scope
    android_led_wifi.ino:150:8: note: in expansion of macro 'ESP8266'
    android_led_wifi.ino: In function 'void sendESP8266Cmdln(String, int)':
    android_led_wifi.ino:20:17: error: 'Serial3' was not declared in this scope
    android_led_wifi.ino:163:3: note: in expansion of macro 'ESP8266'
    android_led_wifi.ino: In function 'void sendESP8266Data(String, int)':
    android_led_wifi.ino:20:17: error: 'Serial3' was not declared in this scope
    android_led_wifi.ino:172:3: note: in expansion of macro 'ESP8266'
    android_led_wifi.ino: In function 'void clearESP8266SerialBuffer()':
    android_led_wifi.ino:20:17: error: 'Serial3' was not declared in this scope
    android_led_wifi.ino:181:10: note: in expansion of macro 'ESP8266'
    Error compiling.

    This report would have more information with
    "Show verbose output during compilation"
    enabled in File > Preferences.


    how to solve that ?

    ReplyDelete
    Replies
    1. This example work on Arduino Due. "Arduino Uno" haven't Serial3.

      Delete
    2. change 1st line to
      #include
      SoftwareSerial ESP8266(2,3);
      //Tx pin of esp at 2nd pin of arduino
      //Rx pin of esp at 3rd pin of arduino

      Delete
  2. the problem #define ESP8266 serial3 are same when i use Arduino IDE.. which part i need to readjust if i want use arduino IDE?

    ReplyDelete
    Replies
    1. The point is Arduino Uno" haven't Serial3.

      To use Serial3 for Due, you have to select Tools-Board (Arduino Due). But your Arduino is Uno, you cannot use it.

      Somebody said can use Software Serial (it available on Uno), but I haven't successed

      Delete
  3. you say "Somebody said can use Software Serial (it available on Uno)" thats all right, i've success use that.
    but its still laggy. the ESP still unstable, sometimes i cant connected to. can you share me how to fix this bug?

    ReplyDelete
    Replies
    1. Sorry, I haven't successed to use Software Serial.

      Delete
    2. You can use alt softserial . it works well better than software serial

      Delete
    3. Thx Techiax for your advise.

      Delete
    4. what do you mean about alt softserialTechiax..
      can you show me please..

      Delete
    5. http://arduino-er.blogspot.com/2015/12/altsoftserial-library-for-arduino-boards.html

      Delete
  4. and than, please tell me why i get connection refuse if i control led via android ?

    ReplyDelete
    Replies
    1. In my experience of using ESP 8266, there are too many version of firmware! Most probably the firmware not update.

      may be your firmware not my firmware.

      Delete
    2. i cant update firmware, can you help me?

      Delete
  5. It Is Giving In Serial Monitor That Module has No Response What I am Doing Wrong ?

    "--- 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.
    --- 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.
    --- 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.
    --- 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.
    --- Start ---"

    ReplyDelete
  6. Try resetting the esp8266 once you get this message

    ReplyDelete
  7. ESP82666 + Arduino Due is supported with the help bridge: serial-> ESP8266 series. How to change your program. I want to ESP8266 were self-employed

    ReplyDelete
    Replies
    1. Sorry I.m from Poland. Google translate my text. Please without word "help".
      I met with examples of programming: USB converter - Uart (FTDI) - Esp8266. Arduino Due has 3 UART. How it used to program the ESP8266. Sorry my language. I think, u understand.

      Delete
    2. That means you want somethinf like this?
      PC USB - FTDI USB to Serial - Arduino Serial TX1/RX1 - Arduino another Serial TX2/RX2 - ESP8266

      Will it be over complicate?

      if you have FTDI adapter, you can direct connect:
      PC USB - FTDI - ESP8266
      No need Arduino. refer ESP8266-01 open box, power-up test, with minimum config.

      Or connect:
      PC USB - Arduino Due - ESP8266
      no need FTDI. refer Connect ESP8266 (WiFi module) to Arduino Due.

      Delete
    3. I think: http://www.instructables.com/id/Use-Arduino-Due-to-program-and-test-ESP8266/.I want change code in your program, but I don`t know. I want to ESP8266 were self-employed

      Delete
  8. hello Rys Spol,

    To program ESP8266 using Arduino Due, it is very clear in instructables post. The Due simple direct Serial RX to Serial1 TX, Serial1 RX to Serial TX, and shift voltage.

    I will advise using FTDI USB-to-Serial adapter, the instructables post advise it also.

    After then, the Due is use as a adapter, same function as USB-to-Serial. Then you are program ESP8266, NOT Arduino. Current version of Arduino IDE already support board for ESP8266, you have to Install ESP8266 Board to Arduino IDE.

    If you use FTDI USB-to-Serial and ESP-12 (more IO pin), you can following the post Program standalone ESP8266/ESP-12 WiFi module with Arduino IDE.

    With ESP8266 Board installed to Arduino IDE, it also provide example of Simple Web Server example of standalone ESP8266/ESP-12 WiFi Module

    The post Standalone ESP8266/ESP-12: web control on-board LED was modified from ESP8266 example, have same function of this example, but program for ESP8266, NOT Arduino.

    For self-employed ESP8266, you should program ESP8266 directly, follow the code in Standalone ESP8266/ESP-12: web control on-board LED, not this one.

    ReplyDelete
  9. I have three sensors hooked to esp8266-12, all the programs are written on the Arduino IDE
    I have no concrete idea how to go about develop an app for it. can you be able to help me?

    ReplyDelete