Post topics, source code that relate to the Arduino Platform

User avatar
By scargill
#3322 The BUSY item is a well established bug - it is in the AT code - not your Arduino code. It hampers all attempts to do anything with these boards - hopefully the latest release of the SDK will have done something about it - and the AT code update - which will apparently come out this Friday, will help - though I tested an early version and the busy item was still there but at long last it now times out. I would not be considering anything other than playing until the underlying firmware issues are resolved - and there are maybe 3 or 4 attempts at this happening right now.
User avatar
By nodduino
#4098
jpcleve wrote:i found this link http://www.snip2code.com/Snippet/194415/Webserver-for-Arduino-ESP8266 tonight, and with a little tweaking, wound up with the sketch below. I finally have a working http server demo! Pretty awesome little chip.

Code: Select all//Leonardo
//Serial_ & dbgTerminal = Serial;
//HardwareSerial & espSerial = Serial1;

////UNO & M328P
//#include <SoftwareSerial.h>
//SoftwareSerial dbgTerminal(10, 11); // RX, TX
//HardwareSerial & espSerial = Serial;

//
////MEGA2560
HardwareSerial & dbgTerminal = Serial;
HardwareSerial & espSerial = Serial1;

// set pin numbers:
const int ledPin =  13;      // the number of the LED pin
const int ESP8266_CHPD = 4;

// Variables will change:
int ledState = HIGH;             // ledState used to set the LED

#define BUFFER_SIZE 128
char buffer[BUFFER_SIZE];

void setup() {
  pinMode(ledPin, OUTPUT); 
  //pinMode(ESP8266_CHPD, OUTPUT);

  dbgTerminal.begin(115200); // Serial monitor
  espSerial.begin(115200); // ESP8266

  //while (!dbgTerminal) {
  // ; // wait for serial port to connect. Needed for Leonardo only
  //}

  dbgTerminal.println(F("ESP8266 demo."));

  //hardReset();
  //delay(2000);

  clearSerialBuffer();

  //connect to router
  connectWiFi("SSID", "PASSWORD");

  //test if the module is ready
  dbgTerminal.print("AT : ");
  dbgTerminal.println( GetResponse("AT",1000) );

  //Change to mode 1
  dbgTerminal.print("AT+CWMODE=1 : ");
  dbgTerminal.println( GetResponse("AT+CWMODE=1",1000) );

  //set the multiple connection mode
  dbgTerminal.print(F("AT+CIPMUX=1 : "));
  dbgTerminal.println( GetResponse("AT+CIPMUX=1",1000) );

  //set the server of port 80 check "no change" or "OK"
  dbgTerminal.print(F("AT+CIPSERVER=1,8888 : "));
  dbgTerminal.println( GetResponse("AT+CIPSERVER=1,8888", 1000) );

  //set time out
  dbgTerminal.print("AT+CIPSTO=15 : ");
  dbgTerminal.println( GetResponse("AT+CIPSTO=15",1000) );

  //print the ip addr
  dbgTerminal.print(F("ip address : "));
  dbgTerminal.println( GetResponse("AT+CIFSR", 1000) );
  delay(200);


  dbgTerminal.println();
  dbgTerminal.println(F("Start Webserver"));

  digitalWrite(ledPin,ledState); 
}

void loop() {
  int ch_id, packet_len;
  char *pb; 
  espSerial.readBytesUntil('\n', buffer, BUFFER_SIZE);

  if(strncmp(buffer, "+IPD,", 5)==0) {
    // request: +IPD,ch,len:data
    sscanf(buffer+5, "%d,%d", &ch_id, &packet_len);
    if (packet_len > 0) {
      // read serial until packet_len character received
      // start from :
      pb = buffer+5;
      while(*pb!=':') pb++;
      pb++;
      if (strncmp(pb, "GET /led", 8) == 0) {
        dbgTerminal.print(millis());
        dbgTerminal.print(" : ");
        dbgTerminal.println(buffer);
        dbgTerminal.print( "get led from ch :" );
        dbgTerminal.println(ch_id);

        delay(100);
        clearSerialBuffer();

        if (ledState == LOW)
          ledState = HIGH;
        else
          ledState = LOW;
        digitalWrite(ledPin, ledState);

        homepage(ch_id);

      }
      else if (strncmp(pb, "GET / ", 6) == 0) {
        dbgTerminal.print(millis());
        dbgTerminal.print(" : ");
        dbgTerminal.println(buffer);
        dbgTerminal.print( "get Status from ch:" );
        dbgTerminal.println(ch_id);

        delay(100);
        clearSerialBuffer();

        homepage(ch_id);
      }
    }
  }
  clearBuffer();
}

void homepage(int ch_id) {
  String Header;

  Header =  "HTTP/1.1 200 OK\r\n";
  Header += "Content-Type: text/html\r\n";
  Header += "Connection: close\r\n"; 
  //Header += "Refresh: 5\r\n";

  String Content;
  Content = "Hello World!";
  Content += String(ledState);

  Header += "Content-Length: ";
  Header += (int)(Content.length());
  Header += "\r\n\r\n";


  espSerial.print("AT+CIPSEND=");
  espSerial.print(ch_id);
  espSerial.print(",");
  espSerial.println(Header.length()+Content.length());
  delay(10);

  // for debug buffer serial error
  //while (espSerial.available() >0 )  {
  //  char c = espSerial.read();
  //  dbgTerminal.write(c);
  //  if (c == '>') {
  //      espSerial.print(Header);
  //      espSerial.print(Content);
  //  }
  //}

  if (espSerial.find(">")) {
    espSerial.print(Header);
    espSerial.print(Content);
    delay(10);
  }

  //  Serial1.print("AT+CIPCLOSE=");
  //  Serial1.println(ch_id);


}

// Get the data from the WiFi module and send it to the debug serial port
String GetResponse(String AT_Command, int wait){
  String tmpData;
 
  espSerial.println(AT_Command);
  delay(wait);
  while (espSerial.available() >0 )  {
    char c = espSerial.read();
    tmpData += c;
   
    if ( tmpData.indexOf(AT_Command) > -1 )         
      tmpData = "";
    else
      tmpData.trim();       
         
   }
   return tmpData;
}

boolean hardReset() {
  String tmpData;

  digitalWrite(ESP8266_CHPD,LOW);
  delay(100);
  digitalWrite(ESP8266_CHPD,HIGH);
  delay(1000);

  while ( espSerial.available() > 0 ) {
    char c = espSerial.read();
    tmpData +=c;
    espSerial.write(c);
    if ( tmpData.indexOf("Ready") > -1 ) {
      //Serial.println("Ready");
      clearBuffer();
      return 1;
    }
  }
}

void clearSerialBuffer(void) {
  while ( espSerial.available() > 0 ) {
    espSerial.read();
  }
}

void clearBuffer(void) {
  for (int i =0;i<BUFFER_SIZE;i++ ) {
    buffer[i]=0;
  }
}

boolean connectWiFi(String NetworkSSID,String NetworkPASS) {
  String cmd = "AT+CWJAP=\"";
  cmd += NetworkSSID;
  cmd += "\",\"";
  cmd += NetworkPASS;
  cmd += "\"";

  dbgTerminal.println(cmd);
  dbgTerminal.println(GetResponse(cmd,8000));
}




Enjoy!

John


Hi John,

after a few trials and error I was finally able to make it work. Thank you, nice piece of software!
Instead of having a simple "Hello World", I have tried to go a bit further and enclosed it in the <HTML> and <BODY> tags, but I am getting a lot of "wrong syntax" errors in the debug window. Looks like the ESP8266 is trying to resend the text several times after the connection have been closed.

This is what I have modified in "homepage":

Content = "<html>\r\n<body>\r\n";
Content += "Hello World!\r\n";
Content += String(ledState);
Content += "\r\n</body>\r\n</html>\r\n";

apparently \r\n won't work, because I see the led status value after the "Hello World!" label, but the <br /> tag somewhat work, in the sense that I can see the two texts on separate lines but I am also getting error in the debug window.
Maybe the header has to be modified when adding HTML tags?
Sorry for the basic HTML question, but i am no expert in this field.

Nodduino
User avatar
By gaara
#4299 Hi,

I've used this sketch and it works ok, however, it seems that it is ridiculously slow. I've used a similar sketch and got the same result. I was wondering whether it is a configuration issue, a hardware issue or if that's just the way it is. It takes about 2 seconds best case scenario and about 11 seconds or no response worst case to do a simple GET. Is this normal? If not what do you think the problem could be? What kind of speeds are you guys getting?

Regards,

Alex
User avatar
By pupazzognappo
#5856 Hi guys. I'm working on a sketch derived from the one is attached to this topic.
My sketch is a bit ore complex and larger , and i had the " no more response" problem.

I did not resolve that but i think that this is cause by the Hardware serial buffer size of arduino that is smaller than the esp8266 buffer size. So, if it's got a lot of information , arduino could not read them as quickly as we are hoping ----> overflow.