モータードライバとWi-FiモジュールESP-WROOM-02をmbed LPC1114FN28に繋げて、RCWControllerからコントロールするプログラム

Dependencies:   mbed

Committer:
jksoft
Date:
Fri Jul 22 05:36:02 2016 +0000
Revision:
0:3c24a40c2343
??

Who changed what in which revision?

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