A fork of the original interface for OS/2. Features a correctly-implemented recv (but retains the old behavior via recv2).

Dependencies:   BufferedSerial

Dependents:   weather_clock weather_clock

Committer:
geky
Date:
Wed Jun 03 21:44:20 2015 +0000
Revision:
44:16da10e7b3f7
Parent:
32:cf071dc33972
Child:
45:3cfb7406d993
Child:
46:4abb80f0a920
Reimplemented ESP8266 driver using the Nodemcu firmware

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 44:16da10e7b3f7 36
geky 44:16da10e7b3f7 37 /**
geky 44:16da10e7b3f7 38 * The ESP8266 class
geky 44:16da10e7b3f7 39 */
geky 44:16da10e7b3f7 40 class ESP8266
geky 44:16da10e7b3f7 41 {
geky 44:16da10e7b3f7 42
geky 44:16da10e7b3f7 43 public:
geky 44:16da10e7b3f7 44 /**
geky 44:16da10e7b3f7 45 * Constructor
geky 44:16da10e7b3f7 46 *
geky 44:16da10e7b3f7 47 * @param tx mbed pin to use for tx line of Serial interface
geky 44:16da10e7b3f7 48 * @param rx mbed pin to use for rx line of Serial interface
geky 44:16da10e7b3f7 49 * @param reset reset pin of the wifi module ()
geky 44:16da10e7b3f7 50 * @param baud the baudrate of the serial connection
geky 44:16da10e7b3f7 51 * @param timeout the timeout of the serial connection
geky 44:16da10e7b3f7 52 */
geky 44:16da10e7b3f7 53 ESP8266(PinName tx, PinName rx, PinName reset, int baud = 9600, int timeout = 3000);
geky 44:16da10e7b3f7 54
geky 44:16da10e7b3f7 55 /**
geky 44:16da10e7b3f7 56 * Initialize the wifi hardware
geky 44:16da10e7b3f7 57 *
geky 44:16da10e7b3f7 58 * @return true if successful
geky 44:16da10e7b3f7 59 */
geky 44:16da10e7b3f7 60 bool init();
geky 44:16da10e7b3f7 61
geky 44:16da10e7b3f7 62 /**
geky 44:16da10e7b3f7 63 * Connect the wifi module to the specified ssid.
geky 44:16da10e7b3f7 64 *
geky 44:16da10e7b3f7 65 * @param ssid ssid of the network
geky 44:16da10e7b3f7 66 * @param phrase WEP, WPA or WPA2 key
geky 44:16da10e7b3f7 67 * @return true if successful
geky 44:16da10e7b3f7 68 */
geky 44:16da10e7b3f7 69 bool connect(const char *ssid, const char *phrase);
geky 44:16da10e7b3f7 70
geky 44:16da10e7b3f7 71 /**
geky 44:16da10e7b3f7 72 * Check connection to the access point
geky 44:16da10e7b3f7 73 * @return true if successful
geky 44:16da10e7b3f7 74 */
geky 44:16da10e7b3f7 75 bool is_connected();
geky 44:16da10e7b3f7 76
geky 44:16da10e7b3f7 77 /**
geky 44:16da10e7b3f7 78 * Disconnect the ESP8266 module from the access point
geky 44:16da10e7b3f7 79 *
geky 44:16da10e7b3f7 80 * @return true if successful
geky 44:16da10e7b3f7 81 */
geky 44:16da10e7b3f7 82 bool disconnect();
geky 44:16da10e7b3f7 83
geky 44:16da10e7b3f7 84 /*
geky 44:16da10e7b3f7 85 * Start up a UDP or TCP Connection
geky 44:16da10e7b3f7 86 *
geky 44:16da10e7b3f7 87 * @param type 0 for UDP, 1 for TCP
geky 44:16da10e7b3f7 88 * @param ip A string that contains the IP, no quotes
geky 44:16da10e7b3f7 89 * @param port Numerical port number to connect to
geky 44:16da10e7b3f7 90 * @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 91 * @return true if sucessful, 0 if fail
geky 44:16da10e7b3f7 92 */
geky 44:16da10e7b3f7 93 bool open(bool type, char* ip, int port, int id = -1);
geky 44:16da10e7b3f7 94
geky 44:16da10e7b3f7 95 /**
geky 44:16da10e7b3f7 96 * Close a connection
geky 44:16da10e7b3f7 97 *
geky 44:16da10e7b3f7 98 * @return true if successful
geky 44:16da10e7b3f7 99 */
geky 44:16da10e7b3f7 100 bool close();
geky 44:16da10e7b3f7 101
geky 44:16da10e7b3f7 102 /**
geky 44:16da10e7b3f7 103 * Read a character or block
geky 44:16da10e7b3f7 104 *
geky 44:16da10e7b3f7 105 * @return the character read or -1 on error
geky 44:16da10e7b3f7 106 */
geky 44:16da10e7b3f7 107 int getc();
geky 44:16da10e7b3f7 108
geky 44:16da10e7b3f7 109 /**
geky 44:16da10e7b3f7 110 * Write a character
geky 44:16da10e7b3f7 111 *
geky 44:16da10e7b3f7 112 * @param the character which will be written
geky 44:16da10e7b3f7 113 * @return -1 on error
geky 44:16da10e7b3f7 114 */
geky 44:16da10e7b3f7 115 int putc(char c);
geky 44:16da10e7b3f7 116
geky 44:16da10e7b3f7 117 /**
geky 44:16da10e7b3f7 118 * Write a string
geky 44:16da10e7b3f7 119 *
geky 44:16da10e7b3f7 120 * @param buffer the buffer that will be written
geky 44:16da10e7b3f7 121 * @param len the length of the buffer
geky 44:16da10e7b3f7 122 * @return true on success
geky 44:16da10e7b3f7 123 */
geky 44:16da10e7b3f7 124 bool send(const char *buffer, int len);
geky 44:16da10e7b3f7 125
geky 44:16da10e7b3f7 126 /**
geky 44:16da10e7b3f7 127 * Read a string without blocking
geky 44:16da10e7b3f7 128 *
geky 44:16da10e7b3f7 129 * @param buffer the buffer that will be written
geky 44:16da10e7b3f7 130 * @param len the length of the buffer, is replaced by read length
geky 44:16da10e7b3f7 131 * @return true on success
geky 44:16da10e7b3f7 132 */
geky 44:16da10e7b3f7 133 bool recv(char *buffer, int *len);
geky 44:16da10e7b3f7 134
geky 44:16da10e7b3f7 135 /**
geky 44:16da10e7b3f7 136 * Check if wifi is writable
geky 44:16da10e7b3f7 137 *
geky 44:16da10e7b3f7 138 * @return 1 if wifi is writable
geky 44:16da10e7b3f7 139 */
geky 44:16da10e7b3f7 140 int writeable();
geky 44:16da10e7b3f7 141
geky 44:16da10e7b3f7 142 /**
geky 44:16da10e7b3f7 143 * Check if wifi is readable
geky 44:16da10e7b3f7 144 *
geky 44:16da10e7b3f7 145 * @return number of characters available
geky 44:16da10e7b3f7 146 */
geky 44:16da10e7b3f7 147 int readable();
geky 44:16da10e7b3f7 148
geky 44:16da10e7b3f7 149 /**
geky 44:16da10e7b3f7 150 * Return the IP address
geky 44:16da10e7b3f7 151 * @return IP address as a string
geky 44:16da10e7b3f7 152 */
geky 44:16da10e7b3f7 153 const char *getIPAddress();
geky 44:16da10e7b3f7 154
geky 44:16da10e7b3f7 155 /**
geky 44:16da10e7b3f7 156 * Return the IP address from host name
geky 44:16da10e7b3f7 157 * @return true on success, false on failure
geky 44:16da10e7b3f7 158 */
geky 44:16da10e7b3f7 159 bool getHostByName(const char *host, char *ip);
geky 44:16da10e7b3f7 160
geky 44:16da10e7b3f7 161 /**
geky 44:16da10e7b3f7 162 * Reset the wifi module
geky 44:16da10e7b3f7 163 */
geky 44:16da10e7b3f7 164 bool reset();
geky 44:16da10e7b3f7 165
geky 44:16da10e7b3f7 166 /**
geky 44:16da10e7b3f7 167 * Obtains the current instance of the ESP8266
geky 44:16da10e7b3f7 168 */
geky 44:16da10e7b3f7 169 static ESP8266 *getInstance() {
geky 44:16da10e7b3f7 170 return _inst;
geky 44:16da10e7b3f7 171 };
geky 44:16da10e7b3f7 172
geky 44:16da10e7b3f7 173 private:
geky 44:16da10e7b3f7 174 /**
geky 44:16da10e7b3f7 175 * Read a character with timeout
geky 44:16da10e7b3f7 176 *
geky 44:16da10e7b3f7 177 * @return the character read or -1 on timeout
geky 44:16da10e7b3f7 178 */
geky 44:16da10e7b3f7 179 int serialgetc();
geky 44:16da10e7b3f7 180
geky 44:16da10e7b3f7 181 /**
geky 44:16da10e7b3f7 182 * Write a character
geky 44:16da10e7b3f7 183 *
geky 44:16da10e7b3f7 184 * @param the character which will be written
geky 44:16da10e7b3f7 185 * @return -1 on timeout
geky 44:16da10e7b3f7 186 */
geky 44:16da10e7b3f7 187 int serialputc(char c);
geky 44:16da10e7b3f7 188
geky 44:16da10e7b3f7 189 /**
geky 44:16da10e7b3f7 190 * Discards echoed characters
geky 44:16da10e7b3f7 191 *
geky 44:16da10e7b3f7 192 * @return true if successful
geky 44:16da10e7b3f7 193 */
geky 44:16da10e7b3f7 194 bool discardEcho();
geky 44:16da10e7b3f7 195
geky 44:16da10e7b3f7 196 /**
geky 44:16da10e7b3f7 197 * Send part of a command to the wifi module.
geky 44:16da10e7b3f7 198 *
geky 44:16da10e7b3f7 199 * @param cmd string to be sent
geky 44:16da10e7b3f7 200 * @param len optional length of cmd
geky 44:16da10e7b3f7 201 * @param sanitize flag indicating if cmd is actually payload and needs to be escaped
geky 44:16da10e7b3f7 202 * @return true if successful
geky 44:16da10e7b3f7 203 */
geky 44:16da10e7b3f7 204 bool command(const char *cmd);
geky 44:16da10e7b3f7 205
geky 44:16da10e7b3f7 206 /**
geky 44:16da10e7b3f7 207 * Sanitizes and sends payload to wifi module
geky 44:16da10e7b3f7 208 *
geky 44:16da10e7b3f7 209 * @param data data to send
geky 44:16da10e7b3f7 210 * @param len length of data
geky 44:16da10e7b3f7 211 * @return true if successful
geky 44:16da10e7b3f7 212 */
geky 44:16da10e7b3f7 213 bool payload(const char *data, int len);
geky 44:16da10e7b3f7 214
geky 44:16da10e7b3f7 215 /**
geky 44:16da10e7b3f7 216 * Execute the command sent by command
geky 44:16da10e7b3f7 217 *
geky 44:16da10e7b3f7 218 * @param resp_buf pointer to buffer to store response from the wifi module
geky 44:16da10e7b3f7 219 * @param resp_len len of buffer to store response from the wifi module, is replaced by read length
geky 44:16da10e7b3f7 220 * @return true if successful
geky 44:16da10e7b3f7 221 */
geky 44:16da10e7b3f7 222 bool execute(char *resp_buffer = 0, int *resp_len = 0);
geky 44:16da10e7b3f7 223
geky 44:16da10e7b3f7 224 protected:
geky 44:16da10e7b3f7 225 BufferedSerial _serial;
geky 44:16da10e7b3f7 226 DigitalOut _reset_pin;
geky 44:16da10e7b3f7 227
geky 44:16da10e7b3f7 228 static ESP8266 * _inst;
geky 44:16da10e7b3f7 229
geky 44:16da10e7b3f7 230 // TODO WISHLIST: ipv6?
geky 44:16da10e7b3f7 231 // this requires nodemcu support
geky 44:16da10e7b3f7 232 char _ip[16];
geky 44:16da10e7b3f7 233
geky 44:16da10e7b3f7 234 int _baud;
geky 44:16da10e7b3f7 235 int _timeout;
geky 44:16da10e7b3f7 236 };
geky 44:16da10e7b3f7 237
samux 1:fb4494783863 238 #endif