ESP8266 Socket Library. AT Thinker firmware.

Dependents:   ESP8266_MQTT_HelloWorld ESP8266_IFTTT_Test ECE_4180_Lab_4 websocketmbed ... more

Fork of ESP8266Interface by ESP8266

This repository has been superceded

This project has moved to https://developer.mbed.org/teams/ESP8266/code/esp8266-driver/

This library works with the AT Thinker firmware.

Note

This library is currently in Beta. It is not feature complete and has some bugs, proceed with caution! Fixes and patches are welcome and appreciated!

Currently the ESP8266Interface Library has the following Abilities:

Working

  • TCP Client
  • UDP Client
  • Transparent mode (single connection of 1 type at a time)
  • Station Mode (connects to AP)

To be implemented

  • TCP Server
  • UDP Server
  • Multi Connection Mode (able to have up to 5 sockets at a time)
  • AP Mode (Make ESP Chip act like access point)
  • DNS Support (currently websites must be looked up by IP)
  • Error Recovery

Nice but not necessary

  • colorized text for ESP AT Commands in Command line (easier to differentiate from other text)
Committer:
mbedAustin
Date:
Tue Apr 28 18:52:20 2015 +0000
Revision:
28:91e65e22e63a
Parent:
22:c4360e61486a
Child:
30:c035696b9397
aquiring IP Address correctly, but not returning it from inner call level. Safety commit before mucking around

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
samux 1:fb4494783863 34
screamer 5:81e264dbbf03 35 /**
michaeljkoster 13:41098c907200 36 * The ESP8266 class
screamer 5:81e264dbbf03 37 */
michaeljkoster 13:41098c907200 38 class ESP8266
samux 1:fb4494783863 39 {
samux 1:fb4494783863 40
samux 1:fb4494783863 41 public:
screamer 5:81e264dbbf03 42 /**
samux 1:fb4494783863 43 * Constructor
samux 1:fb4494783863 44 *
samux 1:fb4494783863 45 * @param tx mbed pin to use for tx line of Serial interface
samux 1:fb4494783863 46 * @param rx mbed pin to use for rx line of Serial interface
samux 1:fb4494783863 47 * @param reset reset pin of the wifi module ()
samux 1:fb4494783863 48 * @param ssid ssid of the network
screamer 9:f2439377c172 49 * @param phrase WEP, WPA or WPA2 key
mbedAustin 28:91e65e22e63a 50 * @param baud the baudrate of the serial connection
samux 1:fb4494783863 51 */
mbedAustin 28:91e65e22e63a 52 ESP8266( PinName tx, PinName rx, PinName reset, const char * ssid, const char * phrase, uint32_t baud );
samux 1:fb4494783863 53
screamer 5:81e264dbbf03 54 /**
samux 1:fb4494783863 55 * Connect the wifi module to the ssid contained in the constructor.
samux 1:fb4494783863 56 *
samux 1:fb4494783863 57 * @return true if connected, false otherwise
samux 1:fb4494783863 58 */
samux 1:fb4494783863 59 bool join();
samux 1:fb4494783863 60
screamer 5:81e264dbbf03 61 /**
michaeljkoster 15:37a7a56a424f 62 * Same as Join: connect to the ssid and get DHCP settings
screamer 6:5176e0864078 63 * @return true if successful
samux 1:fb4494783863 64 */
michaeljkoster 13:41098c907200 65 bool connect();
michaeljkoster 13:41098c907200 66
screamer 5:81e264dbbf03 67 /**
michaeljkoster 13:41098c907200 68 * Check connection to the access point
screamer 6:5176e0864078 69 * @return true if successful
samux 1:fb4494783863 70 */
michaeljkoster 13:41098c907200 71 bool is_connected();
samux 1:fb4494783863 72
screamer 5:81e264dbbf03 73 /**
michaeljkoster 15:37a7a56a424f 74 * Disconnect the ESP8266 module from the access point
michaeljkoster 15:37a7a56a424f 75 *
michaeljkoster 15:37a7a56a424f 76 * @return true if successful
michaeljkoster 15:37a7a56a424f 77 */
michaeljkoster 15:37a7a56a424f 78 bool disconnect();
michaeljkoster 16:3f0efaa57a12 79
michaeljkoster 16:3f0efaa57a12 80 /*
michaeljkoster 16:3f0efaa57a12 81 * Start up a UDP Connection
michaeljkoster 16:3f0efaa57a12 82 */
michaeljkoster 16:3f0efaa57a12 83 bool startUDP(char* ip, int port);
michaeljkoster 15:37a7a56a424f 84
michaeljkoster 15:37a7a56a424f 85 /**
michaeljkoster 16:3f0efaa57a12 86 * Close a connection
michaeljkoster 15:37a7a56a424f 87 *
michaeljkoster 15:37a7a56a424f 88 * @return true if successful
michaeljkoster 15:37a7a56a424f 89 */
michaeljkoster 15:37a7a56a424f 90 bool close();
michaeljkoster 15:37a7a56a424f 91
michaeljkoster 15:37a7a56a424f 92 /**
michaeljkoster 14:4d1128f72e00 93 * Return the IP address
michaeljkoster 14:4d1128f72e00 94 * @return IP address as a string
michaeljkoster 14:4d1128f72e00 95 */
michaeljkoster 14:4d1128f72e00 96 char* getIPAddress();
michaeljkoster 15:37a7a56a424f 97
michaeljkoster 15:37a7a56a424f 98 /**
michaeljkoster 15:37a7a56a424f 99 * Return the IP address from host name
michaeljkoster 15:37a7a56a424f 100 * @return IP address as a string
michaeljkoster 15:37a7a56a424f 101 */ bool gethostbyname(const char * host, char * ip);
michaeljkoster 15:37a7a56a424f 102
michaeljkoster 14:4d1128f72e00 103 /**
samux 1:fb4494783863 104 * Reset the wifi module
samux 1:fb4494783863 105 */
samux 1:fb4494783863 106 void reset();
samux 3:9aa05e19c62e 107
screamer 5:81e264dbbf03 108 /**
samux 3:9aa05e19c62e 109 * Reboot the wifi module
samux 3:9aa05e19c62e 110 */
samux 3:9aa05e19c62e 111 bool reboot();
samux 1:fb4494783863 112
screamer 5:81e264dbbf03 113 /**
samux 1:fb4494783863 114 * Check if characters are available
samux 1:fb4494783863 115 *
samux 1:fb4494783863 116 * @return number of available characters
samux 1:fb4494783863 117 */
samux 1:fb4494783863 118 int readable();
samux 1:fb4494783863 119
screamer 5:81e264dbbf03 120 /**
samux 1:fb4494783863 121 * Check if characters are available
samux 1:fb4494783863 122 *
samux 1:fb4494783863 123 * @return number of available characters
samux 1:fb4494783863 124 */
samux 1:fb4494783863 125 int writeable();
samux 1:fb4494783863 126
screamer 5:81e264dbbf03 127 /**
samux 1:fb4494783863 128 * Read a character
samux 1:fb4494783863 129 *
samux 1:fb4494783863 130 * @return the character read
samux 1:fb4494783863 131 */
samux 1:fb4494783863 132 char getc();
samux 1:fb4494783863 133
screamer 5:81e264dbbf03 134 /**
samux 1:fb4494783863 135 * Write a character
samux 1:fb4494783863 136 *
samux 1:fb4494783863 137 * @param the character which will be written
samux 1:fb4494783863 138 */
samux 1:fb4494783863 139 int putc(char c);
samux 1:fb4494783863 140
screamer 5:81e264dbbf03 141 /**
michaeljkoster 15:37a7a56a424f 142 * Flush the buffer
michaeljkoster 15:37a7a56a424f 143 */
michaeljkoster 15:37a7a56a424f 144 void flush();
michaeljkoster 15:37a7a56a424f 145
michaeljkoster 15:37a7a56a424f 146 /**
mbedAustin 28:91e65e22e63a 147 * Send a command to the wifi module. Check if the module is in command mode. If not enter in command mode
michaeljkoster 15:37a7a56a424f 148 *
michaeljkoster 15:37a7a56a424f 149 * @param str string to be sent
michaeljkoster 15:37a7a56a424f 150 * @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 151 * @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 152 *
screamer 6:5176e0864078 153 * @return true if successful
samux 1:fb4494783863 154 */
michaeljkoster 15:37a7a56a424f 155 bool sendCommand(const char * cmd, const char * ack = NULL, char * res = NULL, int timeout = DEFAULT_WAIT_RESP_TIMEOUT);
michaeljkoster 15:37a7a56a424f 156
screamer 5:81e264dbbf03 157 /**
samux 1:fb4494783863 158 * 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 159 * Useful to send a command to the module and wait a response.
samux 1:fb4494783863 160 *
samux 1:fb4494783863 161 *
samux 1:fb4494783863 162 * @param str string to be sent
samux 1:fb4494783863 163 * @param len string length
samux 1:fb4494783863 164 * @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 165 * @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 166 *
samux 1:fb4494783863 167 * @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 168 */
michaeljkoster 16:3f0efaa57a12 169 int send(const char * buf, int len);
samux 1:fb4494783863 170
michaeljkoster 13:41098c907200 171 static ESP8266 * getInstance() {
samux 1:fb4494783863 172 return inst;
samux 1:fb4494783863 173 };
samux 1:fb4494783863 174
samux 1:fb4494783863 175 protected:
mbedAustin 28:91e65e22e63a 176 RawSerial wifi;
samux 1:fb4494783863 177 DigitalOut reset_pin;
samux 1:fb4494783863 178 char phrase[30];
samux 1:fb4494783863 179 char ssid[30];
michaeljkoster 14:4d1128f72e00 180 char ipString[20];
michaeljkoster 13:41098c907200 181 CircBuffer<char> buf_ESP8266;
samux 1:fb4494783863 182
michaeljkoster 13:41098c907200 183 static ESP8266 * inst;
samux 1:fb4494783863 184
samux 1:fb4494783863 185 void attach_rx(bool null);
samux 1:fb4494783863 186 void handler_rx(void);
samux 1:fb4494783863 187
samux 1:fb4494783863 188
samux 1:fb4494783863 189 typedef struct STATE {
samux 1:fb4494783863 190 bool associated;
michaeljkoster 22:c4360e61486a 191 bool cmdMode;
samux 1:fb4494783863 192 } State;
samux 1:fb4494783863 193
samux 1:fb4494783863 194 State state;
samux 1:fb4494783863 195 };
samux 1:fb4494783863 196
samux 1:fb4494783863 197 #endif