データの保存、更新、取得ができるWebサービス「milkcocoa」に接続し、データのプッシュ、送信、取得ができるライブラリを使ったサンプルです。 https://mlkcca.com/

Dependencies:   Milkcocoa mbed

Committer:
jksoft
Date:
Fri Dec 18 04:34:22 2015 +0000
Revision:
1:e2ca99ac317b
ESP8266?????????????;

Who changed what in which revision?

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