esp8266 - handy sketchfor experimenting

I don't have a problem, just wanted to share my sketch which helps in experimenting with the esp8266

I put it on github so i can easely update/improve

just wanted to share - this forum is a huge source of information (for me and others)

find it at: github

I am currently using firmware 0018000902-AI03 - my device came standard with that.

somewhat longer version:
like so many i am experimenting with the esp8266 wifi module. it is dirt cheap and shows potential. it is also a bit of a pain, as it uses serial connection, and text commands. from the examples i have encountered so far, none seems to have a good way of checking commands actually work. also - the firmware is not very stable (yet! work in progress it seems!)

what does my sketch do:
it shows on the serial port exactly what the esp is sending - showing carriage return/newline characters and the likes in c-format (\r\n)
it has some simple functions to check a command was actually succesful, with timeout option
it monitors the esp8266 for restarts, detects this, so a server can be restarted (mine restarts quite a lot)
it is a framework where you can easely add your own functions, to check and debug.
I have adapted some code i found to showcase a server - credits go to original creators - sources can be found in the code

I am currently using a mega2560 for testing, but the code is prepared for use on a arduino uno, using software serial (not tested yet)

I hope my code can help others to debug/develop.
mind you - this is a work in progress. i am struggling a bit with github, but I will try and clean up my code, and add some comments, and a proper read me file explaining the basics of my program

Thanks for sharing. The past 2 days I've been writing my own utility for displaying what the 8266 sends back to the Ard for each AT command. The first thing I found was that most of the on-line examples fail right at the very first check in setup(), where they wait 1-sec and then filter for a "ready" to come back from the 8266.

For whatever reason, I've found that the responses from the 8266 to some AT commands aren't the same from one time to the next, especially for AT+RST and AT+CIFSR (IP address check). Do you see this?

you're welcome - thanks for the feedback!

what version firmware do you use?

detecting a reset:
I do not check for the exact response from a reset - but certain keywords
one specific string, then the 'ready' string. the first is dependant on the specific firmware i think. I will probably have to adapt this when I try another version of the firmware

cifsr:
i just run the command to show the ip - i did not look into the exact output yet
i did notice it 'remembers' previous networks - also shows old ip's. that might confuse and make it difficult to filter

cwmode gives ok, or no change - both are positive

others seem less problematic - if the response is not ok, or times out, it didn't go well

check the waitforit function that can handle most commands - maybe with some added filters.
as i said - this is a work in progress (both the firmware, and my sketch)

I am currently cleaning up my code - will update in an hour or so - hoping it goes smooth (ancient computer and git don't get along very well)

FigZ:
detecting a reset:
I do not check for the exact response from a reset - but certain keywords
one specific string, then the 'ready' string. the first is dependant on the specific firmware i think. I will probably have to adapt this when I try another version of the firmware

May also want to lower-case the result code, some versions of the firmware return "Ready" instead.

good idea

mind you - my sketch is not meant to work on all firmware versions - just help in debugging and tuning a sketch to your device/firmware, and provide a framework/methods for development.

I included a simple http server - with some different pages blatantly copied from different sources - just to try a simple multipage system
it should auto-start (and restart after the esp bugs out) - will leave it running overnight and see how it does
next step is opening the port on the router so the whole arduino community can test it :smiley:

to do:
ram usage is too heavy for a 328p - moving strings to progmem would help - workaround this by commenting out some of the pages/strings
i want to add some buttons - to see what sort of ipd request that gives - and handle that
(turn a led on and off - that sort of thing)
i want to use a picture with clickable hotspots
it has been ages since i coded html - anybody want to jump in with an example of a light webpage?

all ideas, help, feedback welcome

Here is the dump from yesterday’s version of my utility. I wanted to see what was actually coming back from the 8266.

What it does is run a couple of the AT cmds 3X because they were giving different results. You can see it here. Eg, the 2nd restart didn’t send anything but OK, while the other 2 sent a load of data, ending with “ready”. Also, the IP address isn’t always displayed, sometimes it sends “ERROR”. Per your comment, I’ll change it to display control characters too, from now on. I also get a lot of “busy now …” signals.

I guess it’s version 9.01. My two ESP-01 modules runs at 115200. RAM usage is about 1KB for the entire program.

Note that I wasn’t seeing all of this data when using the original code from online examples. I needed to modify the response code and get rid of the delay() business, as shown below. Maybe data loss due to RX buffer overrun, not sure why.

Data Dump:

ESP8266 Demo ....
- after connect to router, press 'q' 
  to quit network; use before re-start, 
  else get a lot of 'busy' signals
  during the next test. 
>sending reset
AT+RST


OK

 ets Jan  8 2013,rst cause:4, boot mode:(3,6)

wdt reset
load 0x40100000, len 24444, room 16 
tail 12
chksum 0xe0
ho 0 tail 12 room 4
load 0x3ffe8000, len 3168, room 12 
tail 4
chksum 0x93
load 0x3ffe8c60, len 4956, room 4 
tail 8
chksum 0xbd
csum 0xbd

ready

--------------
>sending reset
AT+RST


OK

--------------
>sending reset
AT+RST

busy now ...

 ets Jan  8 2013,rst cause:4, boot mode:(3,6)

wdt reset
load 0x40100000, len 24444, room 16 
tail 12
chksum 0xe0
ho 0 tail 12 room 4
load 0x3ffe8000, len 3168, room 12 
tail 4
chksum 0x93
load 0x3ffe8c60, len 4956, room 4 
tail 8
chksum 0xbd
csum 0xbd

ready

--------------
>getting version
AT+GMR

00160901

OK

--------------
>getting version
AT+GMR

00160901

OK

--------------
>getting version
AT+GMR

00160901

OK

--------------
>checking modes avail
AT+CWMODE=?

+CWMODE:(1-3)

OK

--------------
>connecting router
AT+CWJAP="ssid","password"
AT+CWMODE=1

no change
AT+CWJAP="ssid","password"


OK

--------------
>check if connected
AT+CWJAP?

+CWJAP:"ssid"

OK

--------------
>checking IP address
AT+CIFSR


ERROR

--------------
>checking IP address
AT+CIFSR

192.168.1.123

--------------
>checking IP address
AT+CIFSR


ERROR

--------------
>checking APs avail (10-sec wait);
AT+CWLAP

+CWLAP:(0,"",0)
+CWLAP:(4,"ATT912",-92)
+CWLAP:(3,"NETGEAR10",-82)
+CWLAP:(3,"NETGEAR06",-89)
+CWLAP:(3,"AlbertsHouse",-66)
+CWLAP:(4,"ATT5d9P462",-61)
+CWLAP:(3,"NETGEAR20",-66)
+CWLAP:(3,"MyCharterWiFi2b-2G",-94)
+CWLAP:(3,"NETGEAR60",-76)
+CWLAP:(4,"ssid",-47)
+CWLAP:(3,"yogapanda",-86)
+CWLAP:(3,"MyCharterWiFibf-2G",-84)
+CWLAP:(4,"ATT6P4k9E4",-78)
+CWLAP:(3,"NETGEAR47",-89)
+CWLAP:(4,"ATTUhrZw6a",-85)
+CWLAP:(3,"Meeh",-80)
+CWLAP:(3,"CruzRocket08",-86)
+CWLAP:(4,"2WIRE015",-64)
+CWLAP:(4,"2WIRE206",-82)
+CWLAP:(4,"Viel",-90)
+CWLAP:(3,"MyCharterWiFi86-2G",-77)
+CWLAP:(3,"NETGEAR56",-72)
+CWLAP:(0,"HP4F2C11",-85)
+CWLAP:(4,"KayaPapaya",-44)
+CWLAP:(3,"MyCharterWiFib7-2G",-82)
+CWLAP:(3,"MyCharterWiFia6-2G",-81)
+CWLAP:(3,"DIRECT-roku-852-F21B34",-88)
+CWLAP:(3,"Hollee",-83)

OK

----- all done ---------
>quiting AP
AT+CIPMUX=0


OK
AT+CWQAP


OK

--------------
>quiting AP-2
AT+CWQAP=?


OK

--------------

Program Code:

The ORIGINAL code did data accesses this way, delay(), find, and print:

  //set the multiple connection mode
  sendAndWait("AT+CIPMUX=1","OK",100);
   
  //set the server of port 80 check "no change" or "OK"
  sendAndWait("AT+CIPSERVER=1,80","no change",100);


// Get the data from the WiFi module and send it to 
//  the debug serial port
/******************************************************/
boolean sendAndWait( String AT_Command, char *AT_Response, 
  int wait )
{
  dbgSerial.print(AT_Command);
  Serial.println(AT_Command);
  delay(wait);
  
  while( Serial.available() > 0 ) {
    if( Serial.find(AT_Response) ) {
      dbgSerial.print(" --> ");
      dbgSerial.println(AT_Response);
      return 1;
    }     
  }
  dbgSerial.println(" fail!");
  return 0;
}

========================
What I changed it to was this, continual dump until timeout, no delay() used, 
giving a more comprehensive data dump:

/********************************************/
void test_restart( int cnt )
{
  //test if the module is ready.
  for( int i=0; i<cnt; i++) {    // sometimes doesn't work right.
    dbg.println(">sending restart");
    esp.println("AT+RST");     
    dump_resp(2000, "\n--------------");
  }
}

/* wait a bit, then read,print ESP8266 data output.
******************************************************/
boolean dump_resp( unsigned long wait, char *msg )
{
  unsigned long tmrx, now, prev=millis();
  boolean rcv=false;
  
  if( wait > 10000UL) wait=10000UL;
  while( ((now=millis()) - prev) < wait) {
    if( esp.available() ) {
      dbg.write( esp.read() );
      tmrx=now;  rcv=true;
    }
    if( rcv && ((millis()-tmrx) > 100UL) ) {
       break;
    }
  }
  dbg.println(msg); 
  return( true );
}

you are running version 00160901

i am running 0018000902-AI03

from the output you posted - it is a huge difference
also - newer version runs at lower baudrate (9600) which means more reliable communication, and possibility to use software serial, freeing up the only hardware serial port on the 328p.(i think you can change the baudrate, not sure if my version supports this) I only see the busy message if I send commands without waiting for a previous command to finish. my sketch handles the latter, no problems there

i think you really need to update - all i read about that version showed it is very buggy - luckely mine came with a newer version
from what i found in online searches, updating from 0016... involves a program to flash the new firmware - from then on you have the possibility to use cloud updating (meaning the device downloads its own firmware update from the cloud)

I did take a brief look at your code - i dislike the whole serial.find approach so many people use. You have no control over the timeout. also - you only check for one possible string. Like i noted before, some commands can have several replies which are good (no change or ok, from cwmode)
i basically read a character (when available) and mirror this to the serial port (for viewing and debugging). the character is stored in a buffer. once a line ending character is received, this buffer is used for finding certain strings, using ordinary string functions. I see no use in finding the string before the esp8266 has spewed out all of the line anyway.
once you understand the flow of the program, it should be easy to add hooks for any application.
I tried to comment it, and keep it simple so anyone can use it as a template.

on a side note- i ran my sketch all night. over 3200 pages served. esp8266 restarted 6 times, my code handled it correctly. Looks like it is prone to restarting if I connect from two sources at the same time. even then, not bad for a little cheap device like this.
it is running a simple server, with a test page showing the statistics, refreshing every 10 seconds. if the esp8266 restarts, this auto-refreshing is interrupted, off course. but I did not need to touch the arduino/esp8266, just simply refreshing the page gets it going again. both my old iphone and mac can connect. (my code could easely distinguish between the two and serve a custom mobile page if needed)
more complicated stuff might be prone to more problems, off course. I also do not handle a lockup of the arduino code, a watchdog timer could easely solve this too.
a power outage should not be a problem, the server auto-starts. however, it tries once, and if it fails, it will give up and just wait for user input on the serial port.
I am playing with the idea to try and catch all those cases, get a dynds address, open the port on the router, and see how robust i can make this under heavier use :slight_smile:
anyone got a nice webpage with a big red doomsday button? kinda curious how many people would push it :smiling_imp:

Hey figz,
Thanks for this! This will be a great help for (and old) newbie :slight_smile:
I just got an ESP05 version a few days ago. After much struggling got it up and running. I have the same version firmware as you and have an UNO only. I've followed an instructional that had you remove the 328p from the UNO and use the serial pins to communicate. Of course I reduced the signal levels to accomadate the 8266's 3.3v needs. I am slowly finding out that not all AT commands are the same with different firmware, and took me forever to findout how to check baud rate for example. Do you know of any comprehensive list of AT commands, I've read that Electroddragon seems to have the most complete, but I didn't see the AT+CIOBAUD listed. Makes me wonder what else isn't documented.