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:
geky
Date:
Thu Jun 04 19:26:35 2015 +0000
Parent:
44:16da10e7b3f7
Child:
47:04632d22a723
Commit message:
Sanitized both transmitted and recieved strings

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
--- a/ESP8266/ESP8266.cpp	Wed Jun 03 21:44:20 2015 +0000
+++ b/ESP8266/ESP8266.cpp	Thu Jun 04 19:26:35 2015 +0000
@@ -137,8 +137,14 @@
     // Setup functions for sending and recieving characters on the esp side
     if (!(command("cm='';") &&
           command("function cs(n) c:send(n) end;") &&
-          command("function cr(n) print(cm:sub(1,n)); cm=cm:sub(n+1,-1) end;") && 
           command("function ca() print(#cm) end;") &&
+          command("function cr(n) " 
+                    "d=cm:sub(1,n):gsub('.', function(s) "
+                      "return s.format('\\\\%03d', s:byte(1)) "
+                    "end);"
+                    "cm=cm:sub(n+1,-1);" 
+                    "print(d) "
+                  "end;") && 
           command("c:on('receive',function(c,n) cm=cm..n end)") && 
           execute()))
         return false;
@@ -183,10 +189,22 @@
 }
 
 bool ESP8266::send(const char *buffer, int len) {
-    if (!(command("cs('") &&
-          payload(buffer, len) &&
-          command("')") &&
-          execute()))
+    if (!command("cs('"))
+        return false;
+          
+    for (int i = 0; i < len; i++) {
+        int a = buffer[i] / 100;
+        int b = (buffer[i] - a*100) / 10;
+        int c = (buffer[i] - a*100 - b*10);
+        
+        if (serialputc('\\') < 0 ||
+            serialputc(a + '0') < 0 ||
+            serialputc(b + '0') < 0 ||
+            serialputc(c + '0') < 0)
+            return false;
+    }
+    
+    if (!(command("')") && execute()))
         return false;
     
     return true;
@@ -198,11 +216,35 @@
     
     if (!(command("cr(") &&
           command(len_buf) &&
-          command(")") && 
-          execute(buffer, len)))
+          command(")")))
+        return false;
+        
+   if (!(command("\r\n") && discardEcho()))
         return false;
+        
+    // Read in response
+    for (int i = 0; i < *len; i++) {
+        int e = serialgetc();
+        
+        if (e == '\r') {
+            *len = i;
+            break;
+        } else if (e != '\\') {
+            return false;
+        }
+        
+        int a = serialgetc();
+        int b = serialgetc();
+        int c = serialgetc();
+        
+        if (a < 0 || b < 0 || c < 0)
+            return false;
+            
+        buffer[i] = (a-'0')*100 + (b-'0')*10 + (c-'0');
+    }
     
-    return true;
+    // Flush to next prompt
+    return flush();
 }
 
 int ESP8266::putc(char c) {
@@ -322,6 +364,17 @@
         
         if (c < 0)
             return false;
+        else if (c == '\n')
+            return true;
+    }
+}
+
+bool ESP8266::flush() {
+    while (true) {
+        int c = serialgetc();
+        
+        if (c < 0)
+            return false;
         else if (c == '>')
             return true;
     }
@@ -336,39 +389,13 @@
     }
     
     return true;
-}
-
-bool ESP8266::payload(const char *data, int len) {
-    for (int i = 0; i < len; i++) {
-        int a = data[i] / 100;
-        int b = (data[i] - a*100) / 10;
-        int c = (data[i] - a*100 - b*10);
-        
-        if (serialputc('\\') < 0 ||
-            serialputc(a + '0') < 0 ||
-            serialputc(b + '0') < 0 ||
-            serialputc(c + '0') < 0)
-            return false;
-    }
-    
-    return true;
-}   
+} 
 
 bool ESP8266::execute(char *resp_buf, int *resp_len) {
     // Finish command with a newline
-    if (serialputc('\r') < 0 || serialputc('\n') < 0)
+    if (!(command("\r\n") && discardEcho()))
         return false;
         
-    // Drop echoed string
-    while (true) {
-        int c = serialgetc();
-        
-        if (c < 0)
-            return false;
-        else if (c == '\n')
-            break;
-    }
-        
     // Read in response if any
     if (resp_buf && resp_len) {
         int i;
@@ -390,6 +417,5 @@
         DBG("command response:\t %.*s", *resp_len, resp_buf);
     }
     
-    // Flush to next prompt
-    return discardEcho();
+    return flush();
 }
--- a/ESP8266/ESP8266.h	Wed Jun 03 21:44:20 2015 +0000
+++ b/ESP8266/ESP8266.h	Thu Jun 04 19:26:35 2015 +0000
@@ -194,6 +194,13 @@
     bool discardEcho();
     
     /**
+    * Flushes to next prompt
+    *
+    * @return true if successful
+    */
+    bool flush();
+    
+    /**
     * Send part of a command to the wifi module.
     *
     * @param cmd string to be sent
@@ -204,15 +211,6 @@
     bool command(const char *cmd);
     
     /**
-    * Sanitizes and sends payload to wifi module 
-    *
-    * @param data data to send
-    * @param len length of data
-    * @return true if successful
-    */
-    bool payload(const char *data, int len);
-    
-    /**
     * Execute the command sent by command
     *
     * @param resp_buf pointer to buffer to store response from the wifi module