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

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ESP8266.h Source File

ESP8266.h

00001 /* Copyright (C) 2012 mbed.org, MIT License
00002  *
00003  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
00004  * and associated documentation files (the "Software"), to deal in the Software without restriction,
00005  * including without limitation the rights to use, copy, modify, merge, publish, distribute,
00006  * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
00007  * furnished to do so, subject to the following conditions:
00008  *
00009  * The above copyright notice and this permission notice shall be included in all copies or
00010  * substantial portions of the Software.
00011  *
00012  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
00013  * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00014  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
00015  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00016  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00017  *
00018  * @section DESCRIPTION
00019  *
00020  * ESP8266 serial wifi module
00021  *
00022  * Datasheet:
00023  *
00024  * http://www.electrodragon.com/w/Wi07c
00025  */
00026 
00027 #ifndef ESP8266_H
00028 #define ESP8266_H
00029 
00030 #include "mbed.h"
00031 #include "CBuffer.h"
00032 
00033 #define DEFAULT_WAIT_RESP_TIMEOUT 500
00034 #define ESP_TCP_TYPE 1
00035 #define ESP_UDP_TYPE 0 
00036 #define ESP_MBUFFE_MAX 256
00037 
00038 /**
00039  * The ESP8266 class
00040  */
00041 class ESP8266
00042 {
00043 
00044 public:
00045     /**
00046     * Constructor
00047     *
00048     * @param tx mbed pin to use for tx line of Serial interface
00049     * @param rx mbed pin to use for rx line of Serial interface
00050     * @param reset reset pin of the wifi module ()
00051     * @param ssid ssid of the network
00052     * @param phrase WEP, WPA or WPA2 key
00053     * @param baud the baudrate of the serial connection
00054     */
00055     ESP8266( PinName tx, PinName rx, PinName reset, const char * ssid, const char * phrase, uint32_t baud );
00056 
00057     /**
00058     * Connect the wifi module to the ssid contained in the constructor.
00059     *
00060     * @return true if connected, false otherwise
00061     */
00062     bool join();
00063     
00064     bool single_ap();
00065 
00066     /**
00067     * Same as Join: connect to the ssid and get DHCP settings
00068     * @return true if successful
00069     */
00070     bool connect();
00071     
00072     /**
00073     * Check connection to the access point
00074     * @return true if successful
00075     */
00076     bool is_connected();
00077 
00078     /**
00079     * Disconnect the ESP8266 module from the access point
00080     *
00081     * @return true if successful
00082     */
00083     bool disconnect();
00084     
00085     /*
00086     * Start up a UDP or TCP Connection
00087     * @param type 0 for UDP, 1 for TCP
00088     * @param ip A string that contains the IP, no quotes
00089     * @param port Numerical port number to connect to
00090     * @param id number between 0-4, if defined it denotes ID to use in multimode (Default to Single connection mode with -1)
00091     * @return true if sucessful, 0 if fail
00092     */
00093     bool start(bool type, char* ip, int port, int id = -1);
00094     
00095     /*
00096     * Legacy Start for UDP only connection in transparent mode
00097     * @param ip A string that contains the IP, no quotes
00098     * @param id number between 0-4
00099     * @param port Numerical port number to connect to
00100     * @param length number of characters in the message being sent
00101     */
00102     bool startUDP(char* ip, int port, int id, int length);
00103 
00104     /*
00105     * Legacy Start for UDP only connection in transparent mode
00106     * @param ip A string that contains the IP, no quotes
00107     * @param id number between 0-4
00108     * @param port Numerical port number to connect to
00109     * @param length number of characters in the message being sent
00110     */
00111     //bool startUDP(char* ip, int port, int id, int length);
00112 
00113     /*
00114     *Starts the ESP chip as a TCP Server
00115     *@param port Numerical port of the server, default is 333
00116     */
00117     bool startTCPServer(int port = 333);
00118 
00119     /**
00120     * Close a connection
00121     *
00122     * @return true if successful
00123     */
00124     bool close();
00125     
00126     /**
00127     * Return the IP address 
00128     * @return IP address as a string
00129     */
00130     char* getIPAddress();
00131 
00132     /**
00133     * Return the IP address from host name
00134     * @return true on success, false on failure
00135     */    
00136     bool gethostbyname(const char * host, char * ip);
00137 
00138     /**
00139     * Reset the wifi module
00140     */
00141     void reset();
00142     
00143     /**
00144     * Reboot the wifi module
00145     */
00146     bool reboot();
00147 
00148     /**
00149     * Check if characters are available
00150     *
00151     * @return number of available characters
00152     */
00153     int readable();
00154 
00155     /**
00156     * Check if characters are available
00157     *
00158     * @return number of available characters
00159     */
00160     int writeable();
00161 
00162     /**
00163     * Read a character
00164     *
00165     * @return the character read
00166     */
00167     char getc();
00168 
00169     /**
00170     * Write a character
00171     *
00172     * @param the character which will be written
00173     */
00174     int putc(char c);
00175 
00176     /**
00177     * Flush the buffer
00178     */
00179     void flush();
00180 
00181     /**
00182     * Send a command to the wifi module. Check if the module is in command mode. If not enter in command mode
00183     *
00184     * @param str string to be sent
00185     * @param ACK string which must be acknowledge by the wifi module. If ACK == NULL, no string has to be acknowledged. (default: "NO")
00186     * @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)
00187     *
00188     * @return true if successful
00189     */
00190     bool sendCommand(const char * cmd, const char * ack = NULL, char * res = NULL, int timeout = DEFAULT_WAIT_RESP_TIMEOUT);
00191 
00192     /**
00193     * 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.
00194     * Useful to send a command to the module and wait a response.
00195     *
00196     *
00197     * @param str string to be sent
00198     * @param len string length
00199     * @param ACK string which must be acknowledge by the wifi module. If ACK == NULL, no string has to be acknoledged. (default: "NO")
00200     * @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)
00201     *
00202     * @return true if ACK has been found in the response from the wifi module. False otherwise or if there is no response in 5s.
00203     */
00204     int send(const char * buf, int len);
00205 
00206     static ESP8266 * getInstance() {
00207         return inst;
00208     };
00209 
00210 protected:
00211     int strfind(const char *str,const char *chkstr,int pos=0);
00212     char* substr(const char *str , char *outstr , int pos1 , int pos2 );
00213     int strcount(const char *str , char countstr );
00214 
00215 
00216     RawSerial wifi;
00217     DigitalOut reset_pin;
00218     char phrase[30];
00219     char ssid[30];
00220     char ipString[30];
00221     CircBuffer<char> buf_ESP8266;
00222 
00223     static ESP8266 * inst;
00224 
00225     void attach_rx(bool null);
00226     void handler_rx(void);
00227 
00228 
00229     typedef struct STATE {
00230         bool associated;
00231         bool cmdMode;
00232     } State;
00233 
00234     State state;
00235 };
00236 
00237 #endif