now this shit works

Dependencies:   BufferedSerial

Dependents:   IoTWeatherStation

Fork of ESP8266NodeMCUInterface by ESP8266

Committer:
krbng4180
Date:
Mon Mar 14 02:40:12 2016 +0000
Revision:
50:7c4a5bdcb624
Parent:
49:a7741fe989ca
publishing;

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
krbng4180 49:a7741fe989ca 174 /**
krbng4180 49:a7741fe989ca 175 * Send part of a command to the wifi module.
krbng4180 49:a7741fe989ca 176 *
krbng4180 49:a7741fe989ca 177 * @param cmd string to be sent
krbng4180 49:a7741fe989ca 178 * @param len optional length of cmd
krbng4180 49:a7741fe989ca 179 * @param sanitize flag indicating if cmd is actually payload and needs to be escaped
krbng4180 49:a7741fe989ca 180 * @return true if successful
krbng4180 49:a7741fe989ca 181 */
krbng4180 49:a7741fe989ca 182 bool command(const char *cmd);
krbng4180 49:a7741fe989ca 183
geky 44:16da10e7b3f7 184 private:
geky 44:16da10e7b3f7 185 /**
geky 44:16da10e7b3f7 186 * Read a character with timeout
geky 44:16da10e7b3f7 187 *
geky 44:16da10e7b3f7 188 * @return the character read or -1 on timeout
geky 44:16da10e7b3f7 189 */
geky 44:16da10e7b3f7 190 int serialgetc();
geky 44:16da10e7b3f7 191
geky 44:16da10e7b3f7 192 /**
geky 44:16da10e7b3f7 193 * Write a character
geky 44:16da10e7b3f7 194 *
geky 44:16da10e7b3f7 195 * @param the character which will be written
geky 44:16da10e7b3f7 196 * @return -1 on timeout
geky 44:16da10e7b3f7 197 */
geky 44:16da10e7b3f7 198 int serialputc(char c);
geky 44:16da10e7b3f7 199
geky 44:16da10e7b3f7 200 /**
geky 44:16da10e7b3f7 201 * Discards echoed characters
geky 44:16da10e7b3f7 202 *
geky 44:16da10e7b3f7 203 * @return true if successful
geky 44:16da10e7b3f7 204 */
geky 44:16da10e7b3f7 205 bool discardEcho();
geky 44:16da10e7b3f7 206
geky 44:16da10e7b3f7 207 /**
geky 45:3cfb7406d993 208 * Flushes to next prompt
geky 45:3cfb7406d993 209 *
geky 45:3cfb7406d993 210 * @return true if successful
geky 45:3cfb7406d993 211 */
geky 45:3cfb7406d993 212 bool flush();
geky 45:3cfb7406d993 213
krbng4180 49:a7741fe989ca 214
geky 44:16da10e7b3f7 215
geky 44:16da10e7b3f7 216 /**
geky 44:16da10e7b3f7 217 * Execute the command sent by command
geky 44:16da10e7b3f7 218 *
geky 44:16da10e7b3f7 219 * @param resp_buf pointer to buffer to store response from the wifi module
geky 44:16da10e7b3f7 220 * @param resp_len len of buffer to store response from the wifi module, is replaced by read length
geky 44:16da10e7b3f7 221 * @return true if successful
geky 44:16da10e7b3f7 222 */
geky 44:16da10e7b3f7 223 bool execute(char *resp_buffer = 0, int *resp_len = 0);
krbng4180 49:a7741fe989ca 224 bool execute_full_length(char *resp_buffer = 0, int *resp_len = 0);
geky 44:16da10e7b3f7 225
geky 44:16da10e7b3f7 226 protected:
geky 44:16da10e7b3f7 227 BufferedSerial _serial;
geky 44:16da10e7b3f7 228 DigitalOut _reset_pin;
geky 44:16da10e7b3f7 229
geky 44:16da10e7b3f7 230 static ESP8266 * _inst;
geky 44:16da10e7b3f7 231
geky 44:16da10e7b3f7 232 // TODO WISHLIST: ipv6?
geky 44:16da10e7b3f7 233 // this requires nodemcu support
geky 44:16da10e7b3f7 234 char _ip[16];
geky 44:16da10e7b3f7 235
geky 44:16da10e7b3f7 236 int _baud;
geky 44:16da10e7b3f7 237 int _timeout;
geky 44:16da10e7b3f7 238 };
geky 44:16da10e7b3f7 239
samux 1:fb4494783863 240 #endif