ESP8266 driver using the NodeMCU interface

Dependencies:   BufferedSerial

Dependents:   esp8266_nodeMCU1 esp8266_2_thingspeak1 Solarator_0-0-2 IoTBurglar_and_Fire_AlarmSystem ... more

Fork of ESP8266Interface by ESP8266

This is an alternative implementation of the ESP8266 driver that uses the NodeMCU firmware. The NodeMCU firmware provides a slightly larger feature set than the default firmware through a Lua interpreter.

Note

This library is currently in Alpha. It is not feature complete and has some bugs, proceed with caution. Fixes and patches are welcome!

Interface changes

  • SSID and passphrase moved out of ESP8266Interface constructor and to ESP8266Interface::connect
  • ESP8266Interface constructor provides optional timeout parameter to specify how long to wait for network operations

Note

NodeMCU defaults to a baud rate of 9600 instead of 115200 used by the default firmware.

Firmware

To install the NodeMCU firmware, follow the instructions on the Firmware Update wiki page using the nodemcu_integer_0.9.6-dev_20150406.bin binary at address 0x00000 instead of boot_v1.1.bin and user1.bin.

Since the NodeMCU firmware defaults to a baud rate of 9600, the Serial Passthrough program can be used to get direct access to the Lua interpreter running on the ESP8266.

Status

Working features:

  • TCP Client
  • UDP Client Transmit (Currently only UDP Server can recieve messages)
  • Single Connection at a time
  • Station Mode (Connects to AP)
  • DNS Lookups

To be implemented:

  • TCP Server
  • UDP Server
  • UDP Client recieve
  • Multiple Connections tracked through Lua variables
  • AP Mode (Act as access point)
  • IPV6 support (Existing issue with NodeMCU)

Files at this revision

API Documentation at this revision

Comitter:
michaeljkoster
Date:
Mon Dec 01 06:22:00 2014 +0000
Parent:
15:37a7a56a424f
Child:
17:d11fa4c3ac65
Commit message:
sync

Changed in this revision

ESP8266/ESP8266.cpp Show annotated file Show diff for this revision Revisions of this file
ESP8266/ESP8266.h Show annotated file Show diff for this revision Revisions of this file
ESP8266Interface.cpp Show annotated file Show diff for this revision Revisions of this file
ESP8266Interface.h Show annotated file Show diff for this revision Revisions of this file
Socket/Endpoint.cpp Show annotated file Show diff for this revision Revisions of this file
Socket/Endpoint.h Show annotated file Show diff for this revision Revisions of this file
Socket/UDPSocket.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/ESP8266/ESP8266.cpp	Sun Nov 30 21:56:03 2014 +0000
+++ b/ESP8266/ESP8266.cpp	Mon Dec 01 06:22:00 2014 +0000
@@ -18,6 +18,7 @@
 
 #include "mbed.h"
 #include "ESP8266.h"
+#include "Endpoint.h"
 #include <string>
 #include <algorithm>
 
@@ -63,14 +64,16 @@
 
     inst = this;
     attach_rx(false);
+    
+    wifi.baud(9600); // initial baud rate of the ESP8266 
+    pc.printf("ESP8266 test\r\n");
 }
 
 bool ESP8266::join()
 {
-
-    for (int i= 0; i < MAX_TRY_JOIN; i++) {
-
-        //join the network
+    string cmd="AT+CWJAP=\""+(string)this->ssid+"\",\""+(string)this->phrase+"\"";
+    if( sendCommand( cmd.c_str(), "OK", NULL, 10000) ){
+        // successfully joined the network
         state.associated = true;
         INFO("\r\nssid: %s\r\nphrase: %s\r\nsecurity: %s\r\n\r\n", this->ssid, this->phrase);
         return true;
@@ -80,7 +83,7 @@
 
 bool ESP8266::connect()
 {
-    return join();
+    return true;
 }
 
 bool ESP8266::is_connected()
@@ -88,8 +91,20 @@
     return true;
 }
 
+bool ESP8266::startUDP(char* ip, int port){
+    char* portstr = "";
+    sprintf(portstr, "%d", port);
+    
+    sendCommand(( "AT+CIPSTART=\"UDP\",\"" + (string) ip + "\"" + (string) portstr + "\"").c_str(), "OK", NULL, 10000);
+    
+    sendCommand("AT+CIPSEND", "OK", NULL, 1000);// go into transparent mode 
+
+    return true;
+}
+
 bool ESP8266::close()
 {
+    sendCommand("AT+CIPCLOSE","OK", NULL, 10000);
     return true;
 }
 
@@ -99,13 +114,14 @@
     if (!state.associated)
         return true;
     // send command to quit AP
-    //    
+    sendCommand("AT+CWQAP", "OK", NULL, 10000); 
     state.associated = false;
     return true;
 }
 
 char* ESP8266::getIPAddress()
 {
+    sendCommand("AT+CWLIF", "OK", NULL, 10000);
     return ipString;
 }
 
@@ -133,22 +149,30 @@
 
 void ESP8266::reset()
 {
+    sendCommand("AT+RST", "ready", NULL, 10000);
+    /*
     reset_pin = 0;
     wait(0.2);
     reset_pin = 1;
     wait(0.2);
+    */
 }
 
 bool ESP8266::reboot()
 {
+    reset();
     return true;
 }
 
 void ESP8266::handler_rx(void)
 {
     //read characters
-    while (wifi.readable())
-        buf_ESP8266.queue(wifi.getc());
+    char c;
+    while (wifi.readable()){
+        c=wifi.getc();
+        buf_ESP8266.queue(c);
+        //pc.printf("%c",c);
+    }
 }
 
 void ESP8266::attach_rx(bool callback)
@@ -188,12 +212,17 @@
     buf_ESP8266.flush();
 }
 
-bool ESP8266::sendCommand(const char * cmd, const char * ack, char * res, int timeout)
+int ESP8266::send(const char * buf, int len)
 {
-    return true;
+    const char* bufptr=buf;
+    while(!wifi.writeable()){}
+    for(int i=0; i<len; i++){
+        wifi.putc((int)*bufptr++);
+        while(!wifi.writeable()){}
+    }return true;
 }
 
-int ESP8266::send(const char * str, int len, const char * ACK, char * res, int timeout)
+bool ESP8266::sendCommand(const char * cmd, const char * ACK, char * res, int timeout)
 {
     char read;
     size_t found = string::npos;
@@ -201,25 +230,38 @@
     Timer tmr;
     int result = 0;
 
-    DBG("will send: %s\r\n",str);
+    DBG("will send: %s\r\n",cmd);
 
     attach_rx(false);
 
     //We flush the buffer
     while (wifi.readable())
         wifi.getc();
+    
+    while(!wifi.writeable()){};
 
     if (!ACK || !strcmp(ACK, "NO")) {
-        for (int i = 0; i < len; i++)
-            result = (putc(str[i]) == str[i]) ? result + 1 : result;
+        for (int i = 0; i < sizeof(cmd); i++){
+            result = (putc(cmd[i]) == cmd[i]) ? result + 1 : result;
+            while(!wifi.writeable()){};
+        }
+        putc(13); //CR
+        while(!wifi.writeable()){};
+        putc(10); //LF
+
     } else {
         //We flush the buffer
         while (wifi.readable())
             wifi.getc();
 
         tmr.start();
-        for (int i = 0; i < len; i++)
-            result = (putc(str[i]) == str[i]) ? result + 1 : result;
+        for (int i = 0; i < sizeof(cmd); i++){
+            result = (putc(cmd[i]) == cmd[i]) ? result + 1 : result;
+            while(!wifi.writeable()){};
+        }
+        putc(13); //CR
+        while(!wifi.writeable()){};
+        putc(10); //LF
 
         while (1) {
             if (tmr.read_ms() > timeout) {
@@ -233,6 +275,7 @@
                 return -1;
             } else if (wifi.readable()) {
                 read = wifi.getc();
+                pc.putc(read);//debug
                 if ( read != '\r' && read != '\n') {
                     checking += read;
                     found = checking.find(ACK);
@@ -299,3 +342,16 @@
     DBG("result: %d\r\n", result)
     return result;
 }
+
+void ESP8266::ATcommand(char* command){
+    char* cmd=command;
+    while(!wifi.writeable() || wifi.readable()){}
+    while(*cmd){
+        wifi.putc((int)*cmd++);
+        wait(.005); // wait for the echo
+        while(!wifi.writeable() || wifi.readable()){}
+    }
+    wifi.putc(13); //CR
+    while(!wifi.writeable() || wifi.readable()){}
+    wifi.putc(10); //LF
+}
--- a/ESP8266/ESP8266.h	Sun Nov 30 21:56:03 2014 +0000
+++ b/ESP8266/ESP8266.h	Mon Dec 01 06:22:00 2014 +0000
@@ -17,11 +17,11 @@
  *
  * @section DESCRIPTION
  *
- * ESP8266 RN131-C, wifi module
+ * ESP8266 serial wifi module
  *
  * Datasheet:
  *
- * http://dlnmh9ip6v2uc.cloudfront.net/datasheets/Wireless/WiFi/ESP8266-RN-UM.pdf
+ * http://www.electrodragon.com/w/Wi07c
  */
 
 #ifndef ESP8266_H
@@ -75,9 +75,14 @@
     * @return true if successful
     */
     bool disconnect();
+    
+    /*
+    * Start up a UDP Connection
+    */
+    bool startUDP(char* ip, int port);
 
     /**
-    * Close a tcp connection
+    * Close a connection
     *
     * @return true if successful
     */
@@ -160,7 +165,7 @@
     *
     * @return true if ACK has been found in the response from the wifi module. False otherwise or if there is no response in 5s.
     */
-    int send(const char * str, int len, const char * ACK = NULL, char * res = NULL, int timeout = DEFAULT_WAIT_RESP_TIMEOUT);
+    int send(const char * buf, int len);
 
     static ESP8266 * getInstance() {
         return inst;
@@ -176,6 +181,7 @@
 
     static ESP8266 * inst;
 
+    void ATcommand(char* command);
     void attach_rx(bool null);
     void handler_rx(void);
 
--- a/ESP8266Interface.cpp	Sun Nov 30 21:56:03 2014 +0000
+++ b/ESP8266Interface.cpp	Mon Dec 01 06:22:00 2014 +0000
@@ -12,9 +12,9 @@
     return 0;
 }
 
-int ESP8266Interface::connect()
+bool ESP8266Interface::connect()
 {
-    return ESP8266::join();
+    return ESP8266::connect();
 }
 
 int ESP8266Interface::disconnect()
--- a/ESP8266Interface.h	Sun Nov 30 21:56:03 2014 +0000
+++ b/ESP8266Interface.h	Mon Dec 01 06:22:00 2014 +0000
@@ -21,6 +21,7 @@
 #define ESP8266INTERFACE_H_
 
 #include "ESP8266.h"
+#include "Endpoint.h"
 
  /**
  * Interface using ESP8266 to connect to an IP-based network
@@ -49,7 +50,7 @@
   * Bring the interface up, start DHCP if needed.
   * \return 0 on success, a negative number on failure
   */
-  int connect();
+  bool connect();
   
   /** Disconnect
   * Bring the interface down
--- a/Socket/Endpoint.cpp	Sun Nov 30 21:56:03 2014 +0000
+++ b/Socket/Endpoint.cpp	Mon Dec 01 06:22:00 2014 +0000
@@ -38,6 +38,8 @@
     //Resolve DNS address or populate hard-coded IP address
     ESP8266->gethostbyname(host, _ipAddress);
     _port = port;
+    //Start the UDP Endpoint 
+    ESP8266->startUDP(_ipAddress,_port);
     return 0;
 }
 
--- a/Socket/Endpoint.h	Sun Nov 30 21:56:03 2014 +0000
+++ b/Socket/Endpoint.h	Mon Dec 01 06:22:00 2014 +0000
@@ -59,6 +59,7 @@
 protected:
     char _ipAddress[16];
     int _port;
+    
     ESP8266 * ESP8266;
 };
 
--- a/Socket/UDPSocket.cpp	Sun Nov 30 21:56:03 2014 +0000
+++ b/Socket/UDPSocket.cpp	Mon Dec 01 06:22:00 2014 +0000
@@ -106,5 +106,5 @@
 
 bool UDPSocket::readEndpoint(Endpoint & ep)
 {
-    return false;
+    return true;
 }