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