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:
alexhrao
Date:
Fri May 24 23:36:22 2019 +0000
Revision:
53:4224de23da9e
Parent:
49:cac09a34102c
Final changes for preliminary new node support

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 /**
alexhrao 49:cac09a34102c 128 * Read a string with blocking.
alexhrao 49:cac09a34102c 129 *
alexhrao 49:cac09a34102c 130 * Reads from the connection. This method <b>is</b> blocking, so take care
alexhrao 49:cac09a34102c 131 * to work around this as necessary. It is also <b>not</b> thread safe, so
alexhrao 49:cac09a34102c 132 * it must be locked - it is up to the caller to arrange this.
geky 44:16da10e7b3f7 133 *
geky 44:16da10e7b3f7 134 * @param buffer the buffer that will be written
alexhrao 49:cac09a34102c 135 * @param len the length of the buffer. Up to this many bytes will be read.
alexhrao 49:cac09a34102c 136 * @param offset the offset to read from, inclusive.
alexhrao 49:cac09a34102c 137 * @return -1 on failure, or the number of bytes read.
geky 44:16da10e7b3f7 138 */
alexhrao 49:cac09a34102c 139 int recv(char *buffer, int len, int offset);
alexhrao 49:cac09a34102c 140
alexhrao 49:cac09a34102c 141 bool recv2(char* buffer, int* len);
geky 44:16da10e7b3f7 142
geky 44:16da10e7b3f7 143 /**
geky 44:16da10e7b3f7 144 * Check if wifi is writable
geky 44:16da10e7b3f7 145 *
geky 44:16da10e7b3f7 146 * @return 1 if wifi is writable
geky 44:16da10e7b3f7 147 */
geky 44:16da10e7b3f7 148 int writeable();
geky 44:16da10e7b3f7 149
geky 44:16da10e7b3f7 150 /**
geky 44:16da10e7b3f7 151 * Check if wifi is readable
geky 44:16da10e7b3f7 152 *
geky 44:16da10e7b3f7 153 * @return number of characters available
geky 44:16da10e7b3f7 154 */
geky 44:16da10e7b3f7 155 int readable();
geky 44:16da10e7b3f7 156
geky 44:16da10e7b3f7 157 /**
geky 44:16da10e7b3f7 158 * Return the IP address
geky 44:16da10e7b3f7 159 * @return IP address as a string
geky 44:16da10e7b3f7 160 */
geky 44:16da10e7b3f7 161 const char *getIPAddress();
geky 44:16da10e7b3f7 162
geky 44:16da10e7b3f7 163 /**
geky 44:16da10e7b3f7 164 * Return the IP address from host name
geky 44:16da10e7b3f7 165 * @return true on success, false on failure
geky 44:16da10e7b3f7 166 */
geky 44:16da10e7b3f7 167 bool getHostByName(const char *host, char *ip);
geky 44:16da10e7b3f7 168
geky 44:16da10e7b3f7 169 /**
geky 44:16da10e7b3f7 170 * Reset the wifi module
geky 44:16da10e7b3f7 171 */
geky 44:16da10e7b3f7 172 bool reset();
geky 44:16da10e7b3f7 173
geky 44:16da10e7b3f7 174 /**
geky 44:16da10e7b3f7 175 * Obtains the current instance of the ESP8266
geky 44:16da10e7b3f7 176 */
geky 44:16da10e7b3f7 177 static ESP8266 *getInstance() {
geky 44:16da10e7b3f7 178 return _inst;
geky 44:16da10e7b3f7 179 };
geky 44:16da10e7b3f7 180
geky 44:16da10e7b3f7 181 private:
geky 44:16da10e7b3f7 182 /**
geky 44:16da10e7b3f7 183 * Read a character with timeout
geky 44:16da10e7b3f7 184 *
geky 44:16da10e7b3f7 185 * @return the character read or -1 on timeout
geky 44:16da10e7b3f7 186 */
geky 44:16da10e7b3f7 187 int serialgetc();
geky 44:16da10e7b3f7 188
geky 44:16da10e7b3f7 189 /**
geky 44:16da10e7b3f7 190 * Write a character
geky 44:16da10e7b3f7 191 *
geky 44:16da10e7b3f7 192 * @param the character which will be written
geky 44:16da10e7b3f7 193 * @return -1 on timeout
geky 44:16da10e7b3f7 194 */
geky 44:16da10e7b3f7 195 int serialputc(char c);
geky 44:16da10e7b3f7 196
geky 44:16da10e7b3f7 197 /**
geky 44:16da10e7b3f7 198 * Discards echoed characters
geky 44:16da10e7b3f7 199 *
geky 44:16da10e7b3f7 200 * @return true if successful
geky 44:16da10e7b3f7 201 */
geky 44:16da10e7b3f7 202 bool discardEcho();
geky 44:16da10e7b3f7 203
geky 44:16da10e7b3f7 204 /**
geky 45:3cfb7406d993 205 * Flushes to next prompt
geky 45:3cfb7406d993 206 *
geky 45:3cfb7406d993 207 * @return true if successful
geky 45:3cfb7406d993 208 */
geky 45:3cfb7406d993 209 bool flush();
geky 45:3cfb7406d993 210
geky 45:3cfb7406d993 211 /**
geky 44:16da10e7b3f7 212 * Send part of a command to the wifi module.
geky 44:16da10e7b3f7 213 *
geky 44:16da10e7b3f7 214 * @param cmd string to be sent
geky 44:16da10e7b3f7 215 * @param len optional length of cmd
geky 44:16da10e7b3f7 216 * @param sanitize flag indicating if cmd is actually payload and needs to be escaped
geky 44:16da10e7b3f7 217 * @return true if successful
geky 44:16da10e7b3f7 218 */
geky 44:16da10e7b3f7 219 bool command(const char *cmd);
geky 44:16da10e7b3f7 220
geky 44:16da10e7b3f7 221 /**
geky 44:16da10e7b3f7 222 * Execute the command sent by command
geky 44:16da10e7b3f7 223 *
geky 44:16da10e7b3f7 224 * @param resp_buf pointer to buffer to store response from the wifi module
geky 44:16da10e7b3f7 225 * @param resp_len len of buffer to store response from the wifi module, is replaced by read length
geky 44:16da10e7b3f7 226 * @return true if successful
geky 44:16da10e7b3f7 227 */
geky 44:16da10e7b3f7 228 bool execute(char *resp_buffer = 0, int *resp_len = 0);
geky 44:16da10e7b3f7 229
geky 44:16da10e7b3f7 230 protected:
geky 44:16da10e7b3f7 231 BufferedSerial _serial;
geky 44:16da10e7b3f7 232 DigitalOut _reset_pin;
geky 44:16da10e7b3f7 233
geky 44:16da10e7b3f7 234 static ESP8266 * _inst;
geky 44:16da10e7b3f7 235
geky 44:16da10e7b3f7 236 // TODO WISHLIST: ipv6?
geky 44:16da10e7b3f7 237 // this requires nodemcu support
geky 44:16da10e7b3f7 238 char _ip[16];
geky 44:16da10e7b3f7 239
geky 44:16da10e7b3f7 240 int _baud;
geky 44:16da10e7b3f7 241 int _timeout;
geky 44:16da10e7b3f7 242 };
geky 44:16da10e7b3f7 243
samux 1:fb4494783863 244 #endif