Author Topic: WiFi 802.11 b/g/n ESP8266 Example  (Read 24829 times)

tolson

  • Global Moderator
  • *****
  • Posts: 854
  • Karma: +20/-0
    • View Profile
    • Thomas Olson Consulting
WiFi 802.11 b/g/n ESP8266 Example
« on: September 26, 2014, 11:23:30 PM »
From the Espressif website...
Quote
ESP8266 is a highly integrated chip designed for the needs of a new connected world. It offers a complete and self-contained Wi-Fi networking solution, allowing it to either host the application or to offload all Wi-Fi networking functions from another application processor.

There are several modules showing up in the wild that use the ESP8266 IC called ESP-x where x is 1 to 5, so far. They provide access over the serial port using an AT command set. The command set is limited, sparsley documented, and in flux. But what is provided seems to work for now.  I am using the ESP-1 module of which there are two versions. The older one is set at 57600 baud. The newer one is set at 115200 baud. The older one provides a 2x4 connector with 3v3, GND, TX, RX, and 4 NC (No Connects). The newer one provides the same 2x4 connector, but has connected the NCs to RST, CH_DP(chip enable), and two GPIOs, GPIO0 and GPIO2. Make sure you pullup the CH_DP and the RST line. They are finacky. So far it seems you can leave the GPIOs floating. The documentation is sparse. The firmware is in flux. Eventually, you should know how to use the GPIOs and how to use them along with the CH_DP and RST line to program the ESP8266 directly. In the meantime, you have to use the available AT commands.


Older Version


Newer Version


ESP-1 mounted on RFduino Proto Shield

The ESP8266 is advertized as a ULP WiFi solution. Perhaps, while it is sleeping, but when being used it draws an average of 80mA. Sometimes it can draw as much as 300mA. So do not connect to a whimpy 3v3 supply. That includes many USB ports, and 100mA regulator circuits. It may seem to work for a short while and then something glitches. The modules firmware seems to be finacky as it is still being developed.. as far as I can tell.

My first project with this chip is to provide a telnet like daemon to access a menu. No login, no security, just testing. This sketch controls the RGB LEDs on the RFduino LED shield. It needs a lot of work to clean it up. But hopefully gives a an idea what is involved.

Code: [Select]
// ESP8266 WIFI Server on RFduino
// by Thomas Olson
// teo201409021.01
// teo201409027.01 // found strlen error
// teo201409029.01 // FW ver 0018000902 requires '\r\n'
// Testing ESP8266 ESP-1 WIFI Module with RFduino
// My modules are Firmware Version 00160901
// This firmware version only runs at 115200 baud. it seems.
// This test code doesn't have much in way of error checking, and
// there seems a lot of errors can happen with this module.
// Not ready for prime time. Still some quarky stuff going on.
/*
ESP-1  <-> RFduino
RX      TX (1)
TX      RX (0)
GND    GND
3V3 - DO NOT use whimpy 3V supplies; needs 80mA avg, 300mA sometimes
CH_DP - pullup to 3v3
RST - pullup to 3v3
GPIO2 - no connect for now
GPIO0 - no connect for now
*/
// Simple text based command service - essentially dumb telnetd
// without any login or security features. no file system.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <itoa.h>

#define myVERSION "ver 0.1.2\r\n"
char menu[] = "Here is help:\r\n hH? - Help\r\n Pp - get IP\r\n Rr - red LED ON/OFF\r\n Gg - green LED ON/OFF\r\n Bb - blue LED ON/OFF\r\n Ii - misc info status\r\n Vv - Version\r\n";

// WIFI stuff
#define SSID  "YOUR_SSID" // your network SSID
#define PASS  "YOUR_PW"  // your AP or router password
#define PORT  "23"          // desired service port (23=telnet)
char myIP[18] = "0.0.0.0"; // get it from module which gets from DHCP
bool wifiModPresent = 0;
bool wifiReady = 0;
bool wifiConnected = 0;
#define AT_ "AT\r\n"
#define AT_GMR "AT+GMR\r\n" // get firmware version
#define AT_CIFSR "AT+CIFSR\r\n" // get IP address
#define AT_CWLAP "AT+CWLAP\r\n" // list Access Points

int LED_RED = 2;
int LED_GREEN = 3;
int LED_BLUE = 4;


void setup() {
  pinMode(LED_RED, OUTPUT);
  pinMode(LED_GREEN, OUTPUT);
  pinMode(LED_BLUE, OUTPUT);
 
  Serial.begin(115200);
  Serial.setTimeout(2000);
//  delay(2000); // give me time to start serial monitor

  digitalWrite(LED_RED,0);
  digitalWrite(LED_GREEN,0);
  digitalWrite(LED_BLUE,0);

  // Of course if we got the ready, we know we have access to
  // the module. But doing a simple AT will verify it again.
 
  while(!wifiModPresent){
    wifiMod(); // see if we get response from basic AT command
    delay(1000);
  }
 
  // setup service.
  setupTCPservice();
//  wifiConnected = 1;
}

void loop() {

 
  if(wifiConnected){
    char recvBuf[128];

    bzero(recvBuf,128);
    Serial.flush();
    Serial.readBytesUntil('\n',recvBuf,128);

    switch (recvBuf[0]){
      case 'r':
      case 'l':
      case 'u':
      case 'O':
        // probably initial ready,link,unlink,OK
        break;
      case '+':
        // probably +IPD, which is what we want
        decodeIPDpacket(recvBuf);
        break;
      default:
        break;
    } //switch()   

  // toggle the GREEN LED after receipt or timeout
  //digitalWrite(LED_GREEN, 1);
  //delay(100);
  //digitalWrite(LED_GREEN, 0);
  //delay(100);
 
  } //if()

} //loop()


void decodeIPDpacket(char *recvBuf){
int ch,len,rc;
char tmp[128];
unsigned char cmd[128]; // currently not expecting more than 128 bytes.
    // assumes +IPD, was received at beginning of recvBuf.
    bzero(cmd,128);
    sscanf(&recvBuf[5],"%d,%d:%s",&ch,&len,cmd);
      if(len > 128)len=128;
      cmd[len] = 0;

      switch (cmd[0]){
        case '?':
        case 'h':
        case 'H':
          // Help menu
          espSend(ch,menu);
          delay(10);
          break;
        case 'a': // list Access Points
          Serial.print(AT_CWLAP);
          if((rc = Serial.readBytes(tmp,128)) == 0);
          if (rc > 0){
            espSend(ch,tmp);
            espSend(ch,"Can't find Access Pointsa\r\n");
          }         

        case 'r':
          // turn RED LED OFF
          digitalWrite(LED_RED,0);
          espSend(ch,"red LED off\r\n");
          break;
        case 'R':
          // turn RED LED ON
          digitalWrite(LED_RED,1);
          espSend(ch,"red LED on\r\n");
          break;
        case 'b':
          // turn BLUE LED OFF
          digitalWrite(LED_BLUE,0);
          espSend(ch,"blue LED off\r\n");
          break;
        case 'B':
          // turn BLUE LED ON
          digitalWrite(LED_BLUE,1);
          espSend(ch,"blue LED on\r\n");
          break;
        case 'g':
          // turn GREEN LED OFF
          digitalWrite(LED_GREEN,0);
          espSend(ch,"green LED off\r\n");
          break;
        case 'G':
          // turn GREEN LED ON
          digitalWrite(LED_GREEN,1);
          espSend(ch,"green LED on\r\n");
          break;
        case 'V':
          Serial.print(AT_GMR);
          if((rc = Serial.readBytes(tmp,40)) == 0);
          if (rc > 0){
            espSend(ch,tmp);
            espSend(ch,"ERROR: Firmware version not found\r\n");
          }
          break;
        case 'v':
          espSend(ch,myVERSION);
          break;
        case 'i':
        case 'I':
          //Serial.println("Info Requested");
          if(wifiModPresent) espSend(ch,"wifiModPresent=1\r\n");
          if(wifiReady) espSend(ch,"wifiReady=1\r\n");
          if(wifiConnected) espSend(ch,"wifiConnected=1\r\n");
          break;
        case 'p':
        case 'P':
          Serial.print(AT_CIFSR);
          if((rc = Serial.readBytes(tmp,40)) == 0);
          if (rc > 0){
            espSend(ch,tmp);
          } else {
            espSend(ch,"WARNING: IP not found\r\n");
          }
          break;
        case 'm':
        case 'M':
          wifiMod();
          if(wifiModPresent) espSend(ch,"wifiModPresent=1\r\n");
          break;
        default:
          espSend(ch,"unknown command\r\n");
          break;
      }
     
      delay(10);
      espSend(ch,"\r\nRFduinoESP:~ # ");
}

bool espSend(int ch, char *packet){
char packetLen[20];
  bzero(packetLen,20);
  itoa(strlen(packet),packetLen,10);
//  Serial.setTimeout(5000);
  Serial.print("AT+CIPSEND="); // send command
  Serial.print(ch);
  Serial.print(',');
//  Serial.println(strlen(packet));
  Serial.print(packetLen);
  Serial.print("\r\n");
  if(Serial.find(">")){        // wait for > to send data.
    Serial.print(packet);
    delay(20);
    return 1;
  }else{
    delay(20);
    return 0;
  }
}

// See if WiFi Module is present. Should respond to AT with OK.
void wifiMod(){
delay(10); 
  Serial.print("AT\r\n");
  if(Serial.find("OK")){
    wifiModPresent = 1;
    digitalWrite(LED_BLUE,1);
  }else{
    wifiModPresent = 0;
  }
}
 
// I tested my ESP-1 using a serial monitor and it seems to
// remember some things. So don't really need to redo it again.
void wifiReset(){
  Serial.setTimeout(5000); 
  Serial.print("AT+RST\r\n"); // Reset the ESP8266
  delay(10);
  if(Serial.find("ready")){
    wifiReady = 1;
  }else{
    wifiReady = 0;
  }
}

// Again, the module seems to remember it's connection information
// so don't really need to do it again if not mobile. Watching my
// dhcp logs the module gets a IP all by itself. In my case that
// means it already made it's way through the AP authentication.
void wifiConnect(){
  // connect to WIFI AP(Access Point) or Router
  Serial.print("AT+CWMODE=3\r\n"); //
  Serial.find("OK");
  delay(10);
//  Serial.flush();
  Serial.print("AT+CWJAP=\"");
  Serial.print(SSID);
  Serial.print("\",\"");
  Serial.print(PASS);
  Serial.print("\"\r\n");
  if(Serial.find("OK")){
    wifiConnected = 1;
    digitalWrite(LED_GREEN,1);
  }else{
    wifiConnected = 0;
  }
}

void setupTCPservice(){
//  wifiMod(); // I do this in setup already
//  wifiReset(); // perhaps not needed as is done on powerup.
//  wifiConnect(); // perhaps only needed once.   
 
  // setup Service
  Serial.print("AT+CIPMUX=1\r\n"); // allow multiple connections
  if(Serial.find("OK")){
    digitalWrite(LED_RED,1);
  }
  delay(1000);
  Serial.print("AT+CIPSERVER=1,"); // turn on TCP service
  Serial.print(PORT);              // on PORT number
  Serial.print("\r\n");
  if(Serial.find("OK")){
    wifiConnected = 1;
    digitalWrite(LED_GREEN,1);
  }else{
    wifiConnected = 0;
  }
  delay(1000); // delay then turn off LEDs
//  digitalWrite(LED_RED,0);
//  digitalWrite(LED_GREEN,0);
//  digitalWrite(LED_BLUE,0);
}


I've tied the ESP-1 serial port to the default RFduino serial port on pins 0 and 1, so I have to unplug the ESP shield to program the RFduino. If I wasn't using the RGB pins and the button pins I could have assigned the serial port to different pins leaving the GPIO0and1 for programming.

Have fun.

[EDIT:] I've updated the sketch to work with firmware versions upto 0018000902
« Last Edit: October 07, 2014, 08:38:34 PM by tolson »

lsnyman

  • RFduino Sr. Member
  • ****
  • Posts: 142
  • Karma: +1/-0
    • View Profile
Re: WiFi 802.11 b/g/n ESP8266 Example
« Reply #1 on: September 29, 2014, 01:43:42 PM »
Good work Tolson. Where did you buy these?

tolson

  • Global Moderator
  • *****
  • Posts: 854
  • Karma: +20/-0
    • View Profile
    • Thomas Olson Consulting
Re: WiFi 802.11 b/g/n ESP8266 Example
« Reply #2 on: September 29, 2014, 06:40:28 PM »
EBAY, of course. First ones were directly from China. Then I found they have a USA depot. Delivery dropped from 10 days to 5 days.  Got some more ordered. There are 4 other versions from SMD to ones with chip antannae to external antenna connector. I'd like to evaluate all 5 eventually.

lsnyman

  • RFduino Sr. Member
  • ****
  • Posts: 142
  • Karma: +1/-0
    • View Profile
Re: WiFi 802.11 b/g/n ESP8266 Example
« Reply #3 on: September 30, 2014, 09:00:47 AM »
Cool. I ordered a couple. Let me know if you make any more progress.

tolson

  • Global Moderator
  • *****
  • Posts: 854
  • Karma: +20/-0
    • View Profile
    • Thomas Olson Consulting
Re: WiFi 802.11 b/g/n ESP8266 Example
« Reply #4 on: September 30, 2014, 02:15:34 PM »
My modules came with the firmware version 00150900 which supported the extra pinouts for controlling updatability mode, I think. The sparse documents say the 0015 is the SDK version and the 0900 is the firmware revision.
I was able to use the esp8266_flasher.exe program to install ESP_8266_BIN0.92.bin using wine on Linux. Worked great. It brought the version to 00170901 which gives the ability to update the firmware over WiFi from some mysterious place on the internet  (they like using the "cloud" term.) using a simple command AT+CIUPDATE. Doing that brought the version to 0018000902 (don't know why it has an extra couple 00 in the middle). Now the trick is discovering what is new or changed. That last version now requires the \r\n terminator. Previous version supposedly wanted \r\n but respond immediately on the \r for most commands.

If those first 4 digits really are the SDK revision, it would appear that the SDK itself is in quick flux as well. 15 to 18  in just a few weeks, it would seem.

Have fun!

lsnyman

  • RFduino Sr. Member
  • ****
  • Posts: 142
  • Karma: +1/-0
    • View Profile
Re: WiFi 802.11 b/g/n ESP8266 Example
« Reply #5 on: October 06, 2014, 09:26:45 PM »
I dont seem to be able to communicate with the device.
I tried to do it via FTDI Basic 3.3v board directly issuing the AT commands but get no response. I do see the Blue LED flash every time I send a command.

Then I tried your sketch with Rfduino. I load the sketch with the USB module and then disconnect it , correct?
I then connect the ESP8266-1 to the serial pins and power the whole thing off a 3.3v Bench supply.
Again I see the Blue LED flash dimply every few seconds. I have added my SSID and Pw.
The Telnet command is Telnet esp2?
Am I missing something?

tolson

  • Global Moderator
  • *****
  • Posts: 854
  • Karma: +20/-0
    • View Profile
    • Thomas Olson Consulting
Re: WiFi 802.11 b/g/n ESP8266 Example
« Reply #6 on: October 06, 2014, 10:23:31 PM »
Hi Liam,

The "esp2" is just what I have named the host.

The ESP will query your DHCP server for an IP address it should use. By default, my DHCP server doles out a dynamic address from a pool of IPs. The logs show me what MAC address got what IP address. Once I know what the MAC address is I  enter that into my DHCP server to dole out a static IP for that MAC address from then on. My DNS server is configured to use that IP address to resolve to the hostname I prefer. In this case esp2.



tolson

  • Global Moderator
  • *****
  • Posts: 854
  • Karma: +20/-0
    • View Profile
    • Thomas Olson Consulting
Re: WiFi 802.11 b/g/n ESP8266 Example
« Reply #7 on: October 06, 2014, 10:35:41 PM »
I dont seem to be able to communicate with the device.
I tried to do it via FTDI Basic 3.3v board directly issuing the AT commands but get no response. I do see the Blue LED flash every time I send a command.

Using the FTDI 3.3v USB is a good idea to play with the ESP to start with. If you have the new version the baud rate 115200 8N1. The simplest command to start with is just "AT\n\r" which should just return OK\r\n.

Oh, another thing; the AT seems to work with "AT\r\n" or "at\r\n". But the extended commands have to be all caps. AT+GMR\r\n and at+GMR\r\n work, but AT+gmr\r\n or at+gmr\r\n doesn't.
Also noted that there is no grace in typing mistakes. IE. can' backspace to correct a typo. Have to start over.

lsnyman

  • RFduino Sr. Member
  • ****
  • Posts: 142
  • Karma: +1/-0
    • View Profile
Re: WiFi 802.11 b/g/n ESP8266 Example
« Reply #8 on: October 06, 2014, 10:42:36 PM »
Thanks Tolson
I dont see it in the DHCP list on my Verizon router.
Not sure this device is working.
Is there a simple test using the FTDIBasic?

tolson

  • Global Moderator
  • *****
  • Posts: 854
  • Karma: +20/-0
    • View Profile
    • Thomas Olson Consulting
Re: WiFi 802.11 b/g/n ESP8266 Example
« Reply #9 on: October 06, 2014, 10:58:05 PM »
How do you have all 8 pins connected. I found on mine I had to leave the two GPIO pins floating. The RST pin and the DH_CP pin tied to the Vcc line.

When the ESP powers up it will try to do DHCP and then print a bunch of junk to the terminal then finally "ready".
After that, the easiest command to test with is just AT\r\n.

But actually, it won't get to the DHCP server until it gets through the AP with the correct SSID and PW.
But the AT basic command will still work to commuicate with the ESP.
« Last Edit: October 06, 2014, 11:00:01 PM by tolson »

lsnyman

  • RFduino Sr. Member
  • ****
  • Posts: 142
  • Karma: +1/-0
    • View Profile
Re: WiFi 802.11 b/g/n ESP8266 Example
« Reply #10 on: October 07, 2014, 12:23:02 PM »
Ok, Finally I succeeded in getting an IP address and I gave it a name ESP. I loaded your sketch on RFduino and connected to the ESP. I can ping the ESP and when I try to do the TELNET, it seems to connect but I just get a blank screen with a line cursor. Is there something else I need to do?

tolson

  • Global Moderator
  • *****
  • Posts: 854
  • Karma: +20/-0
    • View Profile
    • Thomas Olson Consulting
Re: WiFi 802.11 b/g/n ESP8266 Example
« Reply #11 on: October 07, 2014, 12:29:00 PM »
Can you determine which version of firmware you have?
AT+GMR

lsnyman

  • RFduino Sr. Member
  • ****
  • Posts: 142
  • Karma: +1/-0
    • View Profile
Re: WiFi 802.11 b/g/n ESP8266 Example
« Reply #12 on: October 07, 2014, 03:45:52 PM »
Yes 00160901

lsnyman

  • RFduino Sr. Member
  • ****
  • Posts: 142
  • Karma: +1/-0
    • View Profile
Re: WiFi 802.11 b/g/n ESP8266 Example
« Reply #13 on: October 07, 2014, 04:20:38 PM »
Where did you find the esp8266_flasher.exe?

tolson

  • Global Moderator
  • *****
  • Posts: 854
  • Karma: +20/-0
    • View Profile
    • Thomas Olson Consulting
Re: WiFi 802.11 b/g/n ESP8266 Example
« Reply #14 on: October 07, 2014, 05:06:04 PM »

 

anything