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)
Committer:
geky
Date:
Thu Jun 04 20:45:00 2015 +0000
Revision:
48:6031f70e3914
Parent:
47:04632d22a723
Fix for not using full buffer in TCP recieve

Who changed what in which revision?

UserRevisionLine numberNew contents of line
geky 44:16da10e7b3f7 1 /* Copyright (C) 2012 mbed.org, MIT License
geky 44:16da10e7b3f7 2 *
geky 44:16da10e7b3f7 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
geky 44:16da10e7b3f7 4 * and associated documentation files (the "Software"), to deal in the Software without restriction,
geky 44:16da10e7b3f7 5 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
geky 44:16da10e7b3f7 6 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
geky 44:16da10e7b3f7 7 * furnished to do so, subject to the following conditions:
geky 44:16da10e7b3f7 8 *
geky 44:16da10e7b3f7 9 * The above copyright notice and this permission notice shall be included in all copies or
geky 44:16da10e7b3f7 10 * substantial portions of the Software.
geky 44:16da10e7b3f7 11 *
geky 44:16da10e7b3f7 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
geky 44:16da10e7b3f7 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
geky 44:16da10e7b3f7 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
geky 44:16da10e7b3f7 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
geky 44:16da10e7b3f7 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
geky 44:16da10e7b3f7 17 *
geky 44:16da10e7b3f7 18 * @section DESCRIPTION
geky 44:16da10e7b3f7 19 *
geky 44:16da10e7b3f7 20 * ESP8266 serial wifi module
geky 44:16da10e7b3f7 21 *
geky 44:16da10e7b3f7 22 * Datasheet:
geky 44:16da10e7b3f7 23 *
geky 44:16da10e7b3f7 24 * http://www.electrodragon.com/w/Wi07c
geky 44:16da10e7b3f7 25 */
geky 44:16da10e7b3f7 26
geky 44:16da10e7b3f7 27 #ifndef ESP8266_H
geky 44:16da10e7b3f7 28 #define ESP8266_H
geky 44:16da10e7b3f7 29
geky 44:16da10e7b3f7 30 #include "mbed.h"
geky 44:16da10e7b3f7 31 #include "CBuffer.h"
geky 44:16da10e7b3f7 32 #include "BufferedSerial.h"
geky 44:16da10e7b3f7 33
geky 44:16da10e7b3f7 34 #define ESP_TCP_TYPE 1
geky 44:16da10e7b3f7 35 #define ESP_UDP_TYPE 0
geky 46:4abb80f0a920 36 #define ESP_MAX_LINE 62
geky 44:16da10e7b3f7 37
geky 44:16da10e7b3f7 38 /**
geky 44:16da10e7b3f7 39 * The ESP8266 class
geky 44:16da10e7b3f7 40 */
geky 44:16da10e7b3f7 41 class ESP8266
geky 44:16da10e7b3f7 42 {
geky 44:16da10e7b3f7 43
geky 44:16da10e7b3f7 44 public:
geky 44:16da10e7b3f7 45 /**
geky 44:16da10e7b3f7 46 * Constructor
geky 44:16da10e7b3f7 47 *
geky 44:16da10e7b3f7 48 * @param tx mbed pin to use for tx line of Serial interface
geky 44:16da10e7b3f7 49 * @param rx mbed pin to use for rx line of Serial interface
geky 44:16da10e7b3f7 50 * @param reset reset pin of the wifi module ()
geky 44:16da10e7b3f7 51 * @param baud the baudrate of the serial connection
geky 44:16da10e7b3f7 52 * @param timeout the timeout of the serial connection
geky 44:16da10e7b3f7 53 */
geky 44:16da10e7b3f7 54 ESP8266(PinName tx, PinName rx, PinName reset, int baud = 9600, int timeout = 3000);
geky 44:16da10e7b3f7 55
geky 44:16da10e7b3f7 56 /**
geky 44:16da10e7b3f7 57 * Initialize the wifi hardware
geky 44:16da10e7b3f7 58 *
geky 44:16da10e7b3f7 59 * @return true if successful
geky 44:16da10e7b3f7 60 */
geky 44:16da10e7b3f7 61 bool init();
geky 44:16da10e7b3f7 62
geky 44:16da10e7b3f7 63 /**
geky 44:16da10e7b3f7 64 * Connect the wifi module to the specified ssid.
geky 44:16da10e7b3f7 65 *
geky 44:16da10e7b3f7 66 * @param ssid ssid of the network
geky 44:16da10e7b3f7 67 * @param phrase WEP, WPA or WPA2 key
geky 44:16da10e7b3f7 68 * @return true if successful
geky 44:16da10e7b3f7 69 */
geky 44:16da10e7b3f7 70 bool connect(const char *ssid, const char *phrase);
geky 44:16da10e7b3f7 71
geky 44:16da10e7b3f7 72 /**
geky 44:16da10e7b3f7 73 * Check connection to the access point
geky 44:16da10e7b3f7 74 * @return true if successful
geky 44:16da10e7b3f7 75 */
geky 44:16da10e7b3f7 76 bool is_connected();
geky 44:16da10e7b3f7 77
geky 44:16da10e7b3f7 78 /**
geky 44:16da10e7b3f7 79 * Disconnect the ESP8266 module from the access point
geky 44:16da10e7b3f7 80 *
geky 44:16da10e7b3f7 81 * @return true if successful
geky 44:16da10e7b3f7 82 */
geky 44:16da10e7b3f7 83 bool disconnect();
geky 44:16da10e7b3f7 84
geky 44:16da10e7b3f7 85 /*
geky 44:16da10e7b3f7 86 * Start up a UDP or TCP Connection
geky 44:16da10e7b3f7 87 *
geky 44:16da10e7b3f7 88 * @param type 0 for UDP, 1 for TCP
geky 44:16da10e7b3f7 89 * @param ip A string that contains the IP, no quotes
geky 44:16da10e7b3f7 90 * @param port Numerical port number to connect to
geky 44:16da10e7b3f7 91 * @param id number between 0-4, if defined it denotes ID to use in multimode (Default to Single connection mode with -1)
geky 44:16da10e7b3f7 92 * @return true if sucessful, 0 if fail
geky 44:16da10e7b3f7 93 */
geky 44:16da10e7b3f7 94 bool open(bool type, char* ip, int port, int id = -1);
geky 44:16da10e7b3f7 95
geky 44:16da10e7b3f7 96 /**
geky 44:16da10e7b3f7 97 * Close a connection
geky 44:16da10e7b3f7 98 *
geky 44:16da10e7b3f7 99 * @return true if successful
geky 44:16da10e7b3f7 100 */
geky 44:16da10e7b3f7 101 bool close();
geky 44:16da10e7b3f7 102
geky 44:16da10e7b3f7 103 /**
geky 44:16da10e7b3f7 104 * Read a character or block
geky 44:16da10e7b3f7 105 *
geky 44:16da10e7b3f7 106 * @return the character read or -1 on error
geky 44:16da10e7b3f7 107 */
geky 44:16da10e7b3f7 108 int getc();
geky 44:16da10e7b3f7 109
geky 44:16da10e7b3f7 110 /**
geky 44:16da10e7b3f7 111 * Write a character
geky 44:16da10e7b3f7 112 *
geky 44:16da10e7b3f7 113 * @param the character which will be written
geky 44:16da10e7b3f7 114 * @return -1 on error
geky 44:16da10e7b3f7 115 */
geky 44:16da10e7b3f7 116 int putc(char c);
geky 44:16da10e7b3f7 117
geky 44:16da10e7b3f7 118 /**
geky 44:16da10e7b3f7 119 * Write a string
geky 44:16da10e7b3f7 120 *
geky 44:16da10e7b3f7 121 * @param buffer the buffer that will be written
geky 44:16da10e7b3f7 122 * @param len the length of the buffer
geky 44:16da10e7b3f7 123 * @return true on success
geky 44:16da10e7b3f7 124 */
geky 44:16da10e7b3f7 125 bool send(const char *buffer, int len);
geky 44:16da10e7b3f7 126
geky 44:16da10e7b3f7 127 /**
geky 44:16da10e7b3f7 128 * Read a string without blocking
geky 44:16da10e7b3f7 129 *
geky 44:16da10e7b3f7 130 * @param buffer the buffer that will be written
geky 44:16da10e7b3f7 131 * @param len the length of the buffer, is replaced by read length
geky 44:16da10e7b3f7 132 * @return true on success
geky 44:16da10e7b3f7 133 */
geky 44:16da10e7b3f7 134 bool recv(char *buffer, int *len);
geky 44:16da10e7b3f7 135
geky 44:16da10e7b3f7 136 /**
geky 44:16da10e7b3f7 137 * Check if wifi is writable
geky 44:16da10e7b3f7 138 *
geky 44:16da10e7b3f7 139 * @return 1 if wifi is writable
geky 44:16da10e7b3f7 140 */
geky 44:16da10e7b3f7 141 int writeable();
geky 44:16da10e7b3f7 142
geky 44:16da10e7b3f7 143 /**
geky 44:16da10e7b3f7 144 * Check if wifi is readable
geky 44:16da10e7b3f7 145 *
geky 44:16da10e7b3f7 146 * @return number of characters available
geky 44:16da10e7b3f7 147 */
geky 44:16da10e7b3f7 148 int readable();
geky 44:16da10e7b3f7 149
geky 44:16da10e7b3f7 150 /**
geky 44:16da10e7b3f7 151 * Return the IP address
geky 44:16da10e7b3f7 152 * @return IP address as a string
geky 44:16da10e7b3f7 153 */
geky 44:16da10e7b3f7 154 const char *getIPAddress();
geky 44:16da10e7b3f7 155
geky 44:16da10e7b3f7 156 /**
geky 44:16da10e7b3f7 157 * Return the IP address from host name
geky 44:16da10e7b3f7 158 * @return true on success, false on failure
geky 44:16da10e7b3f7 159 */
geky 44:16da10e7b3f7 160 bool getHostByName(const char *host, char *ip);
geky 44:16da10e7b3f7 161
geky 44:16da10e7b3f7 162 /**
geky 44:16da10e7b3f7 163 * Reset the wifi module
geky 44:16da10e7b3f7 164 */
geky 44:16da10e7b3f7 165 bool reset();
geky 44:16da10e7b3f7 166
geky 44:16da10e7b3f7 167 /**
geky 44:16da10e7b3f7 168 * Obtains the current instance of the ESP8266
geky 44:16da10e7b3f7 169 */
geky 44:16da10e7b3f7 170 static ESP8266 *getInstance() {
geky 44:16da10e7b3f7 171 return _inst;
geky 44:16da10e7b3f7 172 };
geky 44:16da10e7b3f7 173
geky 44:16da10e7b3f7 174 private:
geky 44:16da10e7b3f7 175 /**
geky 44:16da10e7b3f7 176 * Read a character with timeout
geky 44:16da10e7b3f7 177 *
geky 44:16da10e7b3f7 178 * @return the character read or -1 on timeout
geky 44:16da10e7b3f7 179 */
geky 44:16da10e7b3f7 180 int serialgetc();
geky 44:16da10e7b3f7 181
geky 44:16da10e7b3f7 182 /**
geky 44:16da10e7b3f7 183 * Write a character
geky 44:16da10e7b3f7 184 *
geky 44:16da10e7b3f7 185 * @param the character which will be written
geky 44:16da10e7b3f7 186 * @return -1 on timeout
geky 44:16da10e7b3f7 187 */
geky 44:16da10e7b3f7 188 int serialputc(char c);
geky 44:16da10e7b3f7 189
geky 44:16da10e7b3f7 190 /**
geky 44:16da10e7b3f7 191 * Discards echoed characters
geky 44:16da10e7b3f7 192 *
geky 44:16da10e7b3f7 193 * @return true if successful
geky 44:16da10e7b3f7 194 */
geky 44:16da10e7b3f7 195 bool discardEcho();
geky 44:16da10e7b3f7 196
geky 44:16da10e7b3f7 197 /**
geky 45:3cfb7406d993 198 * Flushes to next prompt
geky 45:3cfb7406d993 199 *
geky 45:3cfb7406d993 200 * @return true if successful
geky 45:3cfb7406d993 201 */
geky 45:3cfb7406d993 202 bool flush();
geky 45:3cfb7406d993 203
geky 45:3cfb7406d993 204 /**
geky 44:16da10e7b3f7 205 * Send part of a command to the wifi module.
geky 44:16da10e7b3f7 206 *
geky 44:16da10e7b3f7 207 * @param cmd string to be sent
geky 44:16da10e7b3f7 208 * @param len optional length of cmd
geky 44:16da10e7b3f7 209 * @param sanitize flag indicating if cmd is actually payload and needs to be escaped
geky 44:16da10e7b3f7 210 * @return true if successful
geky 44:16da10e7b3f7 211 */
geky 44:16da10e7b3f7 212 bool command(const char *cmd);
geky 44:16da10e7b3f7 213
geky 44:16da10e7b3f7 214 /**
geky 44:16da10e7b3f7 215 * Execute the command sent by command
geky 44:16da10e7b3f7 216 *
geky 44:16da10e7b3f7 217 * @param resp_buf pointer to buffer to store response from the wifi module
geky 44:16da10e7b3f7 218 * @param resp_len len of buffer to store response from the wifi module, is replaced by read length
geky 44:16da10e7b3f7 219 * @return true if successful
geky 44:16da10e7b3f7 220 */
geky 44:16da10e7b3f7 221 bool execute(char *resp_buffer = 0, int *resp_len = 0);
geky 44:16da10e7b3f7 222
geky 44:16da10e7b3f7 223 protected:
geky 44:16da10e7b3f7 224 BufferedSerial _serial;
geky 44:16da10e7b3f7 225 DigitalOut _reset_pin;
geky 44:16da10e7b3f7 226
geky 44:16da10e7b3f7 227 static ESP8266 * _inst;
geky 44:16da10e7b3f7 228
geky 44:16da10e7b3f7 229 // TODO WISHLIST: ipv6?
geky 44:16da10e7b3f7 230 // this requires nodemcu support
geky 44:16da10e7b3f7 231 char _ip[16];
geky 44:16da10e7b3f7 232
geky 44:16da10e7b3f7 233 int _baud;
geky 44:16da10e7b3f7 234 int _timeout;
geky 44:16da10e7b3f7 235 };
geky 44:16da10e7b3f7 236
samux 1:fb4494783863 237 #endif