Init

Fork of ESP8266Interface by ESP8266

Committer:
mbedAustin
Date:
Thu Apr 30 19:07:37 2015 +0000
Revision:
32:cf071dc33972
Parent:
31:fd0eaf273b11
Child:
36:e1545c6c2cb3
Child:
44:3a7b6083210b
TCP Send / Recieve Working

Who changed what in which revision?

UserRevisionLine numberNew contents of line
samux 1:fb4494783863 1 /* Copyright (C) 2012 mbed.org, MIT License
samux 1:fb4494783863 2 *
samux 1:fb4494783863 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
samux 1:fb4494783863 4 * and associated documentation files (the "Software"), to deal in the Software without restriction,
samux 1:fb4494783863 5 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
samux 1:fb4494783863 6 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
samux 1:fb4494783863 7 * furnished to do so, subject to the following conditions:
samux 1:fb4494783863 8 *
samux 1:fb4494783863 9 * The above copyright notice and this permission notice shall be included in all copies or
samux 1:fb4494783863 10 * substantial portions of the Software.
samux 1:fb4494783863 11 *
samux 1:fb4494783863 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
samux 1:fb4494783863 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
samux 1:fb4494783863 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
samux 1:fb4494783863 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
samux 1:fb4494783863 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
samux 1:fb4494783863 17 *
samux 1:fb4494783863 18 * @section DESCRIPTION
samux 1:fb4494783863 19 *
michaeljkoster 16:3f0efaa57a12 20 * ESP8266 serial wifi module
samux 1:fb4494783863 21 *
samux 1:fb4494783863 22 * Datasheet:
samux 1:fb4494783863 23 *
michaeljkoster 16:3f0efaa57a12 24 * http://www.electrodragon.com/w/Wi07c
samux 1:fb4494783863 25 */
samux 1:fb4494783863 26
michaeljkoster 13:41098c907200 27 #ifndef ESP8266_H
michaeljkoster 13:41098c907200 28 #define ESP8266_H
samux 1:fb4494783863 29
samux 1:fb4494783863 30 #include "mbed.h"
samux 1:fb4494783863 31 #include "CBuffer.h"
samux 1:fb4494783863 32
samux 1:fb4494783863 33 #define DEFAULT_WAIT_RESP_TIMEOUT 500
mbedAustin 30:c035696b9397 34 #define ESP_TCP_TYPE 1
mbedAustin 30:c035696b9397 35 #define ESP_UDP_TYPE 0
samux 1:fb4494783863 36
screamer 5:81e264dbbf03 37 /**
michaeljkoster 13:41098c907200 38 * The ESP8266 class
screamer 5:81e264dbbf03 39 */
michaeljkoster 13:41098c907200 40 class ESP8266
samux 1:fb4494783863 41 {
samux 1:fb4494783863 42
samux 1:fb4494783863 43 public:
screamer 5:81e264dbbf03 44 /**
samux 1:fb4494783863 45 * Constructor
samux 1:fb4494783863 46 *
samux 1:fb4494783863 47 * @param tx mbed pin to use for tx line of Serial interface
samux 1:fb4494783863 48 * @param rx mbed pin to use for rx line of Serial interface
samux 1:fb4494783863 49 * @param reset reset pin of the wifi module ()
samux 1:fb4494783863 50 * @param ssid ssid of the network
screamer 9:f2439377c172 51 * @param phrase WEP, WPA or WPA2 key
mbedAustin 28:91e65e22e63a 52 * @param baud the baudrate of the serial connection
samux 1:fb4494783863 53 */
mbedAustin 28:91e65e22e63a 54 ESP8266( PinName tx, PinName rx, PinName reset, const char * ssid, const char * phrase, uint32_t baud );
samux 1:fb4494783863 55
screamer 5:81e264dbbf03 56 /**
samux 1:fb4494783863 57 * Connect the wifi module to the ssid contained in the constructor.
samux 1:fb4494783863 58 *
samux 1:fb4494783863 59 * @return true if connected, false otherwise
samux 1:fb4494783863 60 */
samux 1:fb4494783863 61 bool join();
samux 1:fb4494783863 62
screamer 5:81e264dbbf03 63 /**
michaeljkoster 15:37a7a56a424f 64 * Same as Join: connect to the ssid and get DHCP settings
screamer 6:5176e0864078 65 * @return true if successful
samux 1:fb4494783863 66 */
michaeljkoster 13:41098c907200 67 bool connect();
michaeljkoster 13:41098c907200 68
screamer 5:81e264dbbf03 69 /**
michaeljkoster 13:41098c907200 70 * Check connection to the access point
screamer 6:5176e0864078 71 * @return true if successful
samux 1:fb4494783863 72 */
michaeljkoster 13:41098c907200 73 bool is_connected();
samux 1:fb4494783863 74
screamer 5:81e264dbbf03 75 /**
michaeljkoster 15:37a7a56a424f 76 * Disconnect the ESP8266 module from the access point
michaeljkoster 15:37a7a56a424f 77 *
michaeljkoster 15:37a7a56a424f 78 * @return true if successful
michaeljkoster 15:37a7a56a424f 79 */
michaeljkoster 15:37a7a56a424f 80 bool disconnect();
michaeljkoster 16:3f0efaa57a12 81
michaeljkoster 16:3f0efaa57a12 82 /*
mbedAustin 30:c035696b9397 83 * Start up a UDP or TCP Connection
mbedAustin 30:c035696b9397 84 * @param type 0 for UDP, 1 for TCP
mbedAustin 30:c035696b9397 85 * @param ip A string that contains the IP, no quotes
mbedAustin 30:c035696b9397 86 * @param port Numerical port number to connect to
mbedAustin 30:c035696b9397 87 * @param id number between 0-4, if defined it denotes ID to use in multimode (Default to Single connection mode with -1)
mbedAustin 30:c035696b9397 88 * @return true if sucessful, 0 if fail
michaeljkoster 16:3f0efaa57a12 89 */
mbedAustin 30:c035696b9397 90 bool start(bool type, char* ip, int port, int id = -1);
mbedAustin 30:c035696b9397 91
mbedAustin 30:c035696b9397 92 /*
mbedAustin 30:c035696b9397 93 * Legacy Start for UDP only connection in transparent mode
mbedAustin 30:c035696b9397 94 * @param ip A string that contains the IP, no quotes
mbedAustin 30:c035696b9397 95 * @param port Numerical port number to connect to
mbedAustin 30:c035696b9397 96 */
mbedAustin 31:fd0eaf273b11 97 bool startUDP(char* ip, int port);
michaeljkoster 15:37a7a56a424f 98
michaeljkoster 15:37a7a56a424f 99 /**
michaeljkoster 16:3f0efaa57a12 100 * Close a connection
michaeljkoster 15:37a7a56a424f 101 *
michaeljkoster 15:37a7a56a424f 102 * @return true if successful
michaeljkoster 15:37a7a56a424f 103 */
michaeljkoster 15:37a7a56a424f 104 bool close();
michaeljkoster 15:37a7a56a424f 105
michaeljkoster 15:37a7a56a424f 106 /**
michaeljkoster 14:4d1128f72e00 107 * Return the IP address
michaeljkoster 14:4d1128f72e00 108 * @return IP address as a string
michaeljkoster 14:4d1128f72e00 109 */
michaeljkoster 14:4d1128f72e00 110 char* getIPAddress();
michaeljkoster 15:37a7a56a424f 111
michaeljkoster 15:37a7a56a424f 112 /**
michaeljkoster 15:37a7a56a424f 113 * Return the IP address from host name
mbedAustin 31:fd0eaf273b11 114 * @return true on success, false on failure
mbedAustin 32:cf071dc33972 115 */
mbedAustin 32:cf071dc33972 116 bool gethostbyname(const char * host, char * ip);
michaeljkoster 15:37a7a56a424f 117
michaeljkoster 14:4d1128f72e00 118 /**
samux 1:fb4494783863 119 * Reset the wifi module
samux 1:fb4494783863 120 */
samux 1:fb4494783863 121 void reset();
samux 3:9aa05e19c62e 122
screamer 5:81e264dbbf03 123 /**
samux 3:9aa05e19c62e 124 * Reboot the wifi module
samux 3:9aa05e19c62e 125 */
samux 3:9aa05e19c62e 126 bool reboot();
samux 1:fb4494783863 127
screamer 5:81e264dbbf03 128 /**
samux 1:fb4494783863 129 * Check if characters are available
samux 1:fb4494783863 130 *
samux 1:fb4494783863 131 * @return number of available characters
samux 1:fb4494783863 132 */
samux 1:fb4494783863 133 int readable();
samux 1:fb4494783863 134
screamer 5:81e264dbbf03 135 /**
samux 1:fb4494783863 136 * Check if characters are available
samux 1:fb4494783863 137 *
samux 1:fb4494783863 138 * @return number of available characters
samux 1:fb4494783863 139 */
samux 1:fb4494783863 140 int writeable();
samux 1:fb4494783863 141
screamer 5:81e264dbbf03 142 /**
samux 1:fb4494783863 143 * Read a character
samux 1:fb4494783863 144 *
samux 1:fb4494783863 145 * @return the character read
samux 1:fb4494783863 146 */
samux 1:fb4494783863 147 char getc();
samux 1:fb4494783863 148
screamer 5:81e264dbbf03 149 /**
samux 1:fb4494783863 150 * Write a character
samux 1:fb4494783863 151 *
samux 1:fb4494783863 152 * @param the character which will be written
samux 1:fb4494783863 153 */
samux 1:fb4494783863 154 int putc(char c);
samux 1:fb4494783863 155
screamer 5:81e264dbbf03 156 /**
michaeljkoster 15:37a7a56a424f 157 * Flush the buffer
michaeljkoster 15:37a7a56a424f 158 */
michaeljkoster 15:37a7a56a424f 159 void flush();
michaeljkoster 15:37a7a56a424f 160
michaeljkoster 15:37a7a56a424f 161 /**
mbedAustin 28:91e65e22e63a 162 * Send a command to the wifi module. Check if the module is in command mode. If not enter in command mode
michaeljkoster 15:37a7a56a424f 163 *
michaeljkoster 15:37a7a56a424f 164 * @param str string to be sent
michaeljkoster 15:37a7a56a424f 165 * @param ACK string which must be acknowledge by the wifi module. If ACK == NULL, no string has to be acknoledged. (default: "NO")
michaeljkoster 15:37a7a56a424f 166 * @param res this field will contain the response from the wifi module, result of a command sent. This field is available only if ACK = "NO" AND res != NULL (default: NULL)
samux 1:fb4494783863 167 *
screamer 6:5176e0864078 168 * @return true if successful
samux 1:fb4494783863 169 */
michaeljkoster 15:37a7a56a424f 170 bool sendCommand(const char * cmd, const char * ack = NULL, char * res = NULL, int timeout = DEFAULT_WAIT_RESP_TIMEOUT);
michaeljkoster 15:37a7a56a424f 171
screamer 5:81e264dbbf03 172 /**
samux 1:fb4494783863 173 * Send a string to the wifi module by serial port. This function desactivates the user interrupt handler when a character is received to analyze the response from the wifi module.
samux 1:fb4494783863 174 * Useful to send a command to the module and wait a response.
samux 1:fb4494783863 175 *
samux 1:fb4494783863 176 *
samux 1:fb4494783863 177 * @param str string to be sent
samux 1:fb4494783863 178 * @param len string length
samux 1:fb4494783863 179 * @param ACK string which must be acknowledge by the wifi module. If ACK == NULL, no string has to be acknoledged. (default: "NO")
samux 1:fb4494783863 180 * @param res this field will contain the response from the wifi module, result of a command sent. This field is available only if ACK = "NO" AND res != NULL (default: NULL)
samux 1:fb4494783863 181 *
samux 1:fb4494783863 182 * @return true if ACK has been found in the response from the wifi module. False otherwise or if there is no response in 5s.
samux 1:fb4494783863 183 */
michaeljkoster 16:3f0efaa57a12 184 int send(const char * buf, int len);
samux 1:fb4494783863 185
michaeljkoster 13:41098c907200 186 static ESP8266 * getInstance() {
samux 1:fb4494783863 187 return inst;
samux 1:fb4494783863 188 };
samux 1:fb4494783863 189
samux 1:fb4494783863 190 protected:
mbedAustin 28:91e65e22e63a 191 RawSerial wifi;
samux 1:fb4494783863 192 DigitalOut reset_pin;
samux 1:fb4494783863 193 char phrase[30];
samux 1:fb4494783863 194 char ssid[30];
michaeljkoster 14:4d1128f72e00 195 char ipString[20];
michaeljkoster 13:41098c907200 196 CircBuffer<char> buf_ESP8266;
samux 1:fb4494783863 197
michaeljkoster 13:41098c907200 198 static ESP8266 * inst;
samux 1:fb4494783863 199
samux 1:fb4494783863 200 void attach_rx(bool null);
samux 1:fb4494783863 201 void handler_rx(void);
samux 1:fb4494783863 202
samux 1:fb4494783863 203
samux 1:fb4494783863 204 typedef struct STATE {
samux 1:fb4494783863 205 bool associated;
michaeljkoster 22:c4360e61486a 206 bool cmdMode;
samux 1:fb4494783863 207 } State;
samux 1:fb4494783863 208
samux 1:fb4494783863 209 State state;
samux 1:fb4494783863 210 };
samux 1:fb4494783863 211
samux 1:fb4494783863 212 #endif