Kleber Silva / IOTON-API

Dependents:   ton_demo ton_template

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ESP8266.h Source File

ESP8266.h

00001 #ifndef ESP8266_H
00002 #define ESP8266_H
00003 #include "mbed.h"
00004 
00005 /**
00006 * An interface to the ESP8266
00007 */
00008 class ESP8266 {
00009 public:
00010 
00011     /**
00012     * Create an interface to the ESP8266
00013     *
00014     * @param tx UART TX line pin
00015     * @param rx UART RX line pin
00016     */
00017     ESP8266(PinName tx, PinName rx) : _serial(tx, rx)
00018     {
00019         _serial.baud(115200);
00020         _connected = false;
00021     }
00022 
00023 
00024     bool connect(const char* ssid, const char* pass, uint8_t mode = 0)
00025     {
00026         // Set CWMODE to 1=Station,2=AP,3=BOTH, default mode 1 (Station)
00027         strcpy(cmdbuff, "AT+CWMODE=1\r\n");
00028         sendCMD();
00029         getReply(500, 20);
00030 
00031         // DHCP Enabled in Station Mode
00032         strcpy(cmdbuff, "AT+CWDHCP=1,1\r\n");
00033         sendCMD();
00034         getReply(500, 20);
00035 
00036         sprintf(cmdbuff,"AT+CWJAP=\"%s\",\"%s\"\r\n", ssid, pass);
00037         sendCMD();
00038         getReply(10000, 200);
00039 
00040         if (strstr(replybuff, "OK") == NULL) return false;
00041 
00042         sprintf(cmdbuff,"AT+CIPMUX=%d\r\n", mode);
00043         sendCMD();
00044         getReply(500, 20);
00045 
00046         _connected = true;
00047         return true;
00048     }
00049 
00050     char* httpGet(const char* host, const char* url, int port = 80)
00051     {
00052         connectToServer(host, port);
00053 
00054         sprintf(webbuff,
00055             "GET %s HTTP/1.1\r\nHost: %s\r\nAccept: */*\r\nConnection: close\r\n\r\n",
00056             url, host);
00057 
00058         sendWebBuff();
00059 
00060         return replybuff;
00061     }
00062 
00063     // Send data to the ThingSpeak
00064     bool sendThingSpeak(char* data, const char* apikey)
00065     {
00066         connectToServer("184.106.153.149", 80);
00067 
00068         sprintf(webbuff, "GET /update?key=%s&%s\r\n", apikey, data);
00069         sprintf(cmdbuff,"AT+CIPSEND=%d\r\n", strlen(webbuff)); // send buffer character length.
00070         sendCMD();
00071         getReply(200, 50);
00072         SendWEB();  // send data
00073         memset(webbuff, '\0', sizeof(webbuff));
00074 
00075         volatile int weberror = 1;
00076         t2.reset();
00077         t2.start();
00078 
00079         while(weberror == 1 && t2.read() < 5)
00080         {
00081             getReply(500, 24);
00082             if (strstr(replybuff, "OK") != NULL)
00083             {
00084                 weberror=0;   // wait for valid SEND OK
00085             }
00086         }
00087 
00088         t2.stop();
00089 
00090         if(weberror == 1)
00091         { // error
00092             return false;
00093         }
00094         else
00095         { // success
00096             return true;
00097         }
00098     }
00099 
00100     bool isConnected(void)
00101     {
00102         return _connected;
00103     }
00104 
00105 private:
00106 
00107     Serial _serial;
00108     volatile bool _connected;
00109 
00110     Timer t1;
00111     Timer t2;
00112     char cmdbuff[64];
00113     char webbuff[1024];
00114     char replybuff[1024];
00115 
00116     // Connect to host via TCP
00117     void connectToServer(const char* host, int port)
00118     {
00119         sprintf(cmdbuff,"AT+CIPSTART=\"TCP\",\"%s\",%d\r\n", host, port);
00120         sendCMD();
00121         getReply(2000, 50);
00122     }
00123 
00124     bool sendWebBuff(void)
00125     {
00126         sprintf(cmdbuff,"AT+CIPSEND=%d\r\n", strlen(webbuff)); // send buffer character length.
00127         sendCMD();
00128         getReply(200, 50);
00129         SendWEB();  // send data
00130         memset(webbuff, '\0', sizeof(webbuff));
00131         return sendCheck();
00132     }
00133 
00134     //  Wait for ESP "SEND OK" reply
00135     bool sendCheck()
00136     {
00137         volatile int weberror = 1;
00138         t2.reset();
00139         t2.start();
00140 
00141         while(weberror == 1 && t2.read() < 5)
00142         {
00143             getReply(500, 24);
00144             if (strstr(replybuff, "SEND OK") != NULL)
00145             {
00146                 weberror=0;   // wait for valid SEND OK
00147             }
00148         }
00149         t2.stop();
00150 
00151         if(weberror == 1)
00152         { // error
00153             strcpy(cmdbuff, "AT+CIPMUX=0\r\n");
00154             sendCMD();
00155             getReply(500, 10);
00156 
00157             sprintf(cmdbuff, "AT+CIPCLOSE\r\n");
00158             sendCMD();
00159             getReply(500, 600);
00160 
00161             return false;
00162         }
00163         else
00164         { // close current connection
00165             sprintf(cmdbuff, "AT+CIPCLOSE\r\n");
00166             sendCMD();
00167             getReply(500, 600);
00168 
00169             return true;
00170         }
00171     }
00172 
00173     // ESP Command data send
00174     void sendCMD()
00175     {
00176         _serial.printf("%s", cmdbuff);
00177     }
00178 
00179     // Large WEB buffer data send
00180     int SendWEB()
00181     {
00182         volatile int i = 0;
00183 
00184         if(_serial.writeable())
00185         {
00186             while(webbuff[i] != '\0')
00187             {
00188                 _serial.putc(webbuff[i]);
00189                 i++;
00190             }
00191         }
00192 
00193         return i;
00194     }
00195 
00196     // Get Command and ESP status replies
00197     void getReply(int timeout, int getcount)
00198     {
00199         volatile int replycount = 0;
00200 
00201         memset(replybuff, '\0', sizeof(replybuff));
00202         t1.reset();
00203         t1.start();
00204 
00205         while(t1.read_ms() < timeout && replycount < getcount)
00206         {
00207             if(_serial.readable())
00208             {
00209                 replybuff[replycount] = _serial.getc();
00210                 replycount++;
00211             }
00212         }
00213 
00214         t1.stop();
00215     }
00216 };
00217 
00218 #endif