IOTON boards API using mbed SDK - http://ioton.cc/plataforma-ton

Dependents:   ton-bot_teste ton-bot_seguidor_linha ton-bot_seguidor_parede

Fork of IOTON-API by Kleber Silva

Committer:
krebyy
Date:
Thu Jun 29 20:20:49 2017 +0000
Revision:
3:9c7195d31602
Parent:
1:3a73d77c2cef
Update to TON Board V1.2

Who changed what in which revision?

UserRevisionLine numberNew contents of line
krebyy 0:cbba28a205fa 1 #ifndef ESP8266_H
krebyy 0:cbba28a205fa 2 #define ESP8266_H
krebyy 0:cbba28a205fa 3 #include "mbed.h"
krebyy 0:cbba28a205fa 4
krebyy 0:cbba28a205fa 5 /**
krebyy 0:cbba28a205fa 6 * An interface to the ESP8266
krebyy 0:cbba28a205fa 7 */
krebyy 0:cbba28a205fa 8 class ESP8266 {
krebyy 0:cbba28a205fa 9 public:
krebyy 0:cbba28a205fa 10
krebyy 0:cbba28a205fa 11 /**
krebyy 0:cbba28a205fa 12 * Create an interface to the ESP8266
krebyy 0:cbba28a205fa 13 *
krebyy 0:cbba28a205fa 14 * @param tx UART TX line pin
krebyy 0:cbba28a205fa 15 * @param rx UART RX line pin
krebyy 0:cbba28a205fa 16 */
krebyy 0:cbba28a205fa 17 ESP8266(PinName tx, PinName rx) : _serial(tx, rx)
krebyy 0:cbba28a205fa 18 {
krebyy 0:cbba28a205fa 19 _serial.baud(115200);
krebyy 0:cbba28a205fa 20 _connected = false;
krebyy 0:cbba28a205fa 21 }
krebyy 0:cbba28a205fa 22
krebyy 0:cbba28a205fa 23
krebyy 0:cbba28a205fa 24 bool connect(const char* ssid, const char* pass, uint8_t mode = 0)
krebyy 0:cbba28a205fa 25 {
krebyy 1:3a73d77c2cef 26 // Set CWMODE to 1=Station,2=AP,3=BOTH, default mode 1 (Station)
krebyy 1:3a73d77c2cef 27 strcpy(cmdbuff, "AT+CWMODE=1\r\n");
krebyy 1:3a73d77c2cef 28 sendCMD();
krebyy 1:3a73d77c2cef 29 getReply(500, 20);
krebyy 1:3a73d77c2cef 30
krebyy 1:3a73d77c2cef 31 // DHCP Enabled in Station Mode
krebyy 1:3a73d77c2cef 32 strcpy(cmdbuff, "AT+CWDHCP=1,1\r\n");
krebyy 1:3a73d77c2cef 33 sendCMD();
krebyy 1:3a73d77c2cef 34 getReply(500, 20);
krebyy 1:3a73d77c2cef 35
krebyy 0:cbba28a205fa 36 sprintf(cmdbuff,"AT+CWJAP=\"%s\",\"%s\"\r\n", ssid, pass);
krebyy 0:cbba28a205fa 37 sendCMD();
krebyy 1:3a73d77c2cef 38 getReply(10000, 200);
krebyy 0:cbba28a205fa 39
krebyy 0:cbba28a205fa 40 if (strstr(replybuff, "OK") == NULL) return false;
krebyy 0:cbba28a205fa 41
krebyy 0:cbba28a205fa 42 sprintf(cmdbuff,"AT+CIPMUX=%d\r\n", mode);
krebyy 0:cbba28a205fa 43 sendCMD();
krebyy 0:cbba28a205fa 44 getReply(500, 20);
krebyy 0:cbba28a205fa 45
krebyy 0:cbba28a205fa 46 _connected = true;
krebyy 0:cbba28a205fa 47 return true;
krebyy 0:cbba28a205fa 48 }
krebyy 0:cbba28a205fa 49
krebyy 0:cbba28a205fa 50 char* httpGet(const char* host, const char* url, int port = 80)
krebyy 0:cbba28a205fa 51 {
krebyy 0:cbba28a205fa 52 connectToServer(host, port);
krebyy 0:cbba28a205fa 53
krebyy 0:cbba28a205fa 54 sprintf(webbuff,
krebyy 0:cbba28a205fa 55 "GET %s HTTP/1.1\r\nHost: %s\r\nAccept: */*\r\nConnection: close\r\n\r\n",
krebyy 0:cbba28a205fa 56 url, host);
krebyy 0:cbba28a205fa 57
krebyy 0:cbba28a205fa 58 sendWebBuff();
krebyy 0:cbba28a205fa 59
krebyy 0:cbba28a205fa 60 return replybuff;
krebyy 0:cbba28a205fa 61 }
krebyy 0:cbba28a205fa 62
krebyy 0:cbba28a205fa 63 // Send data to the ThingSpeak
krebyy 0:cbba28a205fa 64 bool sendThingSpeak(char* data, const char* apikey)
krebyy 0:cbba28a205fa 65 {
krebyy 0:cbba28a205fa 66 connectToServer("184.106.153.149", 80);
krebyy 0:cbba28a205fa 67
krebyy 0:cbba28a205fa 68 sprintf(webbuff, "GET /update?key=%s&%s\r\n", apikey, data);
krebyy 0:cbba28a205fa 69 sprintf(cmdbuff,"AT+CIPSEND=%d\r\n", strlen(webbuff)); // send buffer character length.
krebyy 0:cbba28a205fa 70 sendCMD();
krebyy 0:cbba28a205fa 71 getReply(200, 50);
krebyy 0:cbba28a205fa 72 SendWEB(); // send data
krebyy 0:cbba28a205fa 73 memset(webbuff, '\0', sizeof(webbuff));
krebyy 0:cbba28a205fa 74
krebyy 0:cbba28a205fa 75 volatile int weberror = 1;
krebyy 0:cbba28a205fa 76 t2.reset();
krebyy 0:cbba28a205fa 77 t2.start();
krebyy 0:cbba28a205fa 78
krebyy 0:cbba28a205fa 79 while(weberror == 1 && t2.read() < 5)
krebyy 0:cbba28a205fa 80 {
krebyy 0:cbba28a205fa 81 getReply(500, 24);
krebyy 0:cbba28a205fa 82 if (strstr(replybuff, "OK") != NULL)
krebyy 0:cbba28a205fa 83 {
krebyy 0:cbba28a205fa 84 weberror=0; // wait for valid SEND OK
krebyy 0:cbba28a205fa 85 }
krebyy 0:cbba28a205fa 86 }
krebyy 0:cbba28a205fa 87
krebyy 0:cbba28a205fa 88 t2.stop();
krebyy 0:cbba28a205fa 89
krebyy 0:cbba28a205fa 90 if(weberror == 1)
krebyy 0:cbba28a205fa 91 { // error
krebyy 0:cbba28a205fa 92 return false;
krebyy 0:cbba28a205fa 93 }
krebyy 0:cbba28a205fa 94 else
krebyy 0:cbba28a205fa 95 { // success
krebyy 0:cbba28a205fa 96 return true;
krebyy 0:cbba28a205fa 97 }
krebyy 0:cbba28a205fa 98 }
krebyy 0:cbba28a205fa 99
krebyy 0:cbba28a205fa 100 bool isConnected(void)
krebyy 0:cbba28a205fa 101 {
krebyy 0:cbba28a205fa 102 return _connected;
krebyy 0:cbba28a205fa 103 }
krebyy 0:cbba28a205fa 104
krebyy 0:cbba28a205fa 105 private:
krebyy 0:cbba28a205fa 106
krebyy 0:cbba28a205fa 107 Serial _serial;
krebyy 0:cbba28a205fa 108 volatile bool _connected;
krebyy 0:cbba28a205fa 109
krebyy 0:cbba28a205fa 110 Timer t1;
krebyy 0:cbba28a205fa 111 Timer t2;
krebyy 0:cbba28a205fa 112 char cmdbuff[64];
krebyy 0:cbba28a205fa 113 char webbuff[1024];
krebyy 0:cbba28a205fa 114 char replybuff[1024];
krebyy 0:cbba28a205fa 115
krebyy 0:cbba28a205fa 116 // Connect to host via TCP
krebyy 0:cbba28a205fa 117 void connectToServer(const char* host, int port)
krebyy 0:cbba28a205fa 118 {
krebyy 0:cbba28a205fa 119 sprintf(cmdbuff,"AT+CIPSTART=\"TCP\",\"%s\",%d\r\n", host, port);
krebyy 0:cbba28a205fa 120 sendCMD();
krebyy 0:cbba28a205fa 121 getReply(2000, 50);
krebyy 0:cbba28a205fa 122 }
krebyy 0:cbba28a205fa 123
krebyy 0:cbba28a205fa 124 bool sendWebBuff(void)
krebyy 0:cbba28a205fa 125 {
krebyy 0:cbba28a205fa 126 sprintf(cmdbuff,"AT+CIPSEND=%d\r\n", strlen(webbuff)); // send buffer character length.
krebyy 0:cbba28a205fa 127 sendCMD();
krebyy 0:cbba28a205fa 128 getReply(200, 50);
krebyy 0:cbba28a205fa 129 SendWEB(); // send data
krebyy 0:cbba28a205fa 130 memset(webbuff, '\0', sizeof(webbuff));
krebyy 0:cbba28a205fa 131 return sendCheck();
krebyy 0:cbba28a205fa 132 }
krebyy 0:cbba28a205fa 133
krebyy 0:cbba28a205fa 134 // Wait for ESP "SEND OK" reply
krebyy 0:cbba28a205fa 135 bool sendCheck()
krebyy 0:cbba28a205fa 136 {
krebyy 0:cbba28a205fa 137 volatile int weberror = 1;
krebyy 0:cbba28a205fa 138 t2.reset();
krebyy 0:cbba28a205fa 139 t2.start();
krebyy 0:cbba28a205fa 140
krebyy 0:cbba28a205fa 141 while(weberror == 1 && t2.read() < 5)
krebyy 0:cbba28a205fa 142 {
krebyy 0:cbba28a205fa 143 getReply(500, 24);
krebyy 0:cbba28a205fa 144 if (strstr(replybuff, "SEND OK") != NULL)
krebyy 0:cbba28a205fa 145 {
krebyy 0:cbba28a205fa 146 weberror=0; // wait for valid SEND OK
krebyy 0:cbba28a205fa 147 }
krebyy 0:cbba28a205fa 148 }
krebyy 0:cbba28a205fa 149 t2.stop();
krebyy 0:cbba28a205fa 150
krebyy 0:cbba28a205fa 151 if(weberror == 1)
krebyy 0:cbba28a205fa 152 { // error
krebyy 0:cbba28a205fa 153 strcpy(cmdbuff, "AT+CIPMUX=0\r\n");
krebyy 0:cbba28a205fa 154 sendCMD();
krebyy 0:cbba28a205fa 155 getReply(500, 10);
krebyy 0:cbba28a205fa 156
krebyy 1:3a73d77c2cef 157 sprintf(cmdbuff, "AT+CIPCLOSE\r\n");
krebyy 1:3a73d77c2cef 158 sendCMD();
krebyy 1:3a73d77c2cef 159 getReply(500, 600);
krebyy 1:3a73d77c2cef 160
krebyy 0:cbba28a205fa 161 return false;
krebyy 0:cbba28a205fa 162 }
krebyy 0:cbba28a205fa 163 else
krebyy 0:cbba28a205fa 164 { // close current connection
krebyy 0:cbba28a205fa 165 sprintf(cmdbuff, "AT+CIPCLOSE\r\n");
krebyy 0:cbba28a205fa 166 sendCMD();
krebyy 0:cbba28a205fa 167 getReply(500, 600);
krebyy 0:cbba28a205fa 168
krebyy 0:cbba28a205fa 169 return true;
krebyy 0:cbba28a205fa 170 }
krebyy 0:cbba28a205fa 171 }
krebyy 0:cbba28a205fa 172
krebyy 0:cbba28a205fa 173 // ESP Command data send
krebyy 0:cbba28a205fa 174 void sendCMD()
krebyy 0:cbba28a205fa 175 {
krebyy 0:cbba28a205fa 176 _serial.printf("%s", cmdbuff);
krebyy 0:cbba28a205fa 177 }
krebyy 0:cbba28a205fa 178
krebyy 0:cbba28a205fa 179 // Large WEB buffer data send
krebyy 0:cbba28a205fa 180 int SendWEB()
krebyy 0:cbba28a205fa 181 {
krebyy 0:cbba28a205fa 182 volatile int i = 0;
krebyy 0:cbba28a205fa 183
krebyy 0:cbba28a205fa 184 if(_serial.writeable())
krebyy 0:cbba28a205fa 185 {
krebyy 0:cbba28a205fa 186 while(webbuff[i] != '\0')
krebyy 0:cbba28a205fa 187 {
krebyy 0:cbba28a205fa 188 _serial.putc(webbuff[i]);
krebyy 0:cbba28a205fa 189 i++;
krebyy 0:cbba28a205fa 190 }
krebyy 0:cbba28a205fa 191 }
krebyy 0:cbba28a205fa 192
krebyy 0:cbba28a205fa 193 return i;
krebyy 0:cbba28a205fa 194 }
krebyy 0:cbba28a205fa 195
krebyy 0:cbba28a205fa 196 // Get Command and ESP status replies
krebyy 0:cbba28a205fa 197 void getReply(int timeout, int getcount)
krebyy 0:cbba28a205fa 198 {
krebyy 0:cbba28a205fa 199 volatile int replycount = 0;
krebyy 0:cbba28a205fa 200
krebyy 0:cbba28a205fa 201 memset(replybuff, '\0', sizeof(replybuff));
krebyy 0:cbba28a205fa 202 t1.reset();
krebyy 0:cbba28a205fa 203 t1.start();
krebyy 0:cbba28a205fa 204
krebyy 0:cbba28a205fa 205 while(t1.read_ms() < timeout && replycount < getcount)
krebyy 0:cbba28a205fa 206 {
krebyy 0:cbba28a205fa 207 if(_serial.readable())
krebyy 0:cbba28a205fa 208 {
krebyy 0:cbba28a205fa 209 replybuff[replycount] = _serial.getc();
krebyy 0:cbba28a205fa 210 replycount++;
krebyy 0:cbba28a205fa 211 }
krebyy 0:cbba28a205fa 212 }
krebyy 0:cbba28a205fa 213
krebyy 0:cbba28a205fa 214 t1.stop();
krebyy 0:cbba28a205fa 215 }
krebyy 0:cbba28a205fa 216 };
krebyy 0:cbba28a205fa 217
krebyy 0:cbba28a205fa 218 #endif