esp 8266, a low cost wifi establishes tcp connection with a cloud server. A POST http request is sent. AT commands are used for communication.
main.cpp@0:824af42f88ff, 2016-05-06 (annotated)
- Committer:
- pakrishn
- Date:
- Fri May 06 13:49:18 2016 +0000
- Revision:
- 0:824af42f88ff
esp8266 sends POST http request to ubidots website
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
pakrishn | 0:824af42f88ff | 1 | #include "mbed.h" |
pakrishn | 0:824af42f88ff | 2 | #include "DHT.h" |
pakrishn | 0:824af42f88ff | 3 | |
pakrishn | 0:824af42f88ff | 4 | Serial pc(USBTX, USBRX); |
pakrishn | 0:824af42f88ff | 5 | Serial esp(PTC17, PTC16); //FRDM K64F |
pakrishn | 0:824af42f88ff | 6 | |
pakrishn | 0:824af42f88ff | 7 | DHT sensor(D4, DHT11); |
pakrishn | 0:824af42f88ff | 8 | DigitalIn pirInputPin(D2); |
pakrishn | 0:824af42f88ff | 9 | |
pakrishn | 0:824af42f88ff | 10 | Timer t1; |
pakrishn | 0:824af42f88ff | 11 | |
pakrishn | 0:824af42f88ff | 12 | int count,ended,timeout,getcount,replycount; |
pakrishn | 0:824af42f88ff | 13 | char replybuff[1024]; |
pakrishn | 0:824af42f88ff | 14 | char cmdbuff[255]; |
pakrishn | 0:824af42f88ff | 15 | |
pakrishn | 0:824af42f88ff | 16 | void SendCMD(),getreply(),getMotionSensor(),gettemp(), ESPconfig(); |
pakrishn | 0:824af42f88ff | 17 | void sendUbidotsData(int sensor, float value); |
pakrishn | 0:824af42f88ff | 18 | |
pakrishn | 0:824af42f88ff | 19 | |
pakrishn | 0:824af42f88ff | 20 | int main() |
pakrishn | 0:824af42f88ff | 21 | { |
pakrishn | 0:824af42f88ff | 22 | pc.baud(115200); |
pakrishn | 0:824af42f88ff | 23 | esp.baud(115200); |
pakrishn | 0:824af42f88ff | 24 | ESPconfig(); |
pakrishn | 0:824af42f88ff | 25 | while(1){ |
pakrishn | 0:824af42f88ff | 26 | wait(2); |
pakrishn | 0:824af42f88ff | 27 | gettemp(); |
pakrishn | 0:824af42f88ff | 28 | wait(2); |
pakrishn | 0:824af42f88ff | 29 | getMotionSensor(); |
pakrishn | 0:824af42f88ff | 30 | } |
pakrishn | 0:824af42f88ff | 31 | |
pakrishn | 0:824af42f88ff | 32 | } |
pakrishn | 0:824af42f88ff | 33 | |
pakrishn | 0:824af42f88ff | 34 | |
pakrishn | 0:824af42f88ff | 35 | |
pakrishn | 0:824af42f88ff | 36 | //configuration of esp8266 |
pakrishn | 0:824af42f88ff | 37 | void ESPconfig() |
pakrishn | 0:824af42f88ff | 38 | { |
pakrishn | 0:824af42f88ff | 39 | timeout=1000;getcount=600; |
pakrishn | 0:824af42f88ff | 40 | wait(1); |
pakrishn | 0:824af42f88ff | 41 | strcpy(cmdbuff,"AT\r\n"); |
pakrishn | 0:824af42f88ff | 42 | SendCMD(); |
pakrishn | 0:824af42f88ff | 43 | timeout=1; |
pakrishn | 0:824af42f88ff | 44 | getreply(); |
pakrishn | 0:824af42f88ff | 45 | wait(1); |
pakrishn | 0:824af42f88ff | 46 | |
pakrishn | 0:824af42f88ff | 47 | pc.printf("Reset & get Firmware\r\n"); |
pakrishn | 0:824af42f88ff | 48 | strcpy(cmdbuff,"AT+RST\r\n"); |
pakrishn | 0:824af42f88ff | 49 | SendCMD(); |
pakrishn | 0:824af42f88ff | 50 | timeout=5; |
pakrishn | 0:824af42f88ff | 51 | getreply(); |
pakrishn | 0:824af42f88ff | 52 | pc.printf(replybuff); |
pakrishn | 0:824af42f88ff | 53 | |
pakrishn | 0:824af42f88ff | 54 | wait(2); |
pakrishn | 0:824af42f88ff | 55 | |
pakrishn | 0:824af42f88ff | 56 | pc.printf("\nGet Version\r\n"); |
pakrishn | 0:824af42f88ff | 57 | strcpy(cmdbuff,"AT+GMR\r\n"); |
pakrishn | 0:824af42f88ff | 58 | SendCMD(); |
pakrishn | 0:824af42f88ff | 59 | timeout=4; |
pakrishn | 0:824af42f88ff | 60 | getreply(); |
pakrishn | 0:824af42f88ff | 61 | pc.printf(replybuff); |
pakrishn | 0:824af42f88ff | 62 | |
pakrishn | 0:824af42f88ff | 63 | wait(3); |
pakrishn | 0:824af42f88ff | 64 | |
pakrishn | 0:824af42f88ff | 65 | // set CWMODE to 1=Station,2=AP,3=BOTH, default mode 1 (Station) |
pakrishn | 0:824af42f88ff | 66 | pc.printf("\nSetting Mode\r\n"); |
pakrishn | 0:824af42f88ff | 67 | strcpy(cmdbuff, "AT+CWMODE=1\r\n"); |
pakrishn | 0:824af42f88ff | 68 | SendCMD(); |
pakrishn | 0:824af42f88ff | 69 | timeout=4; |
pakrishn | 0:824af42f88ff | 70 | getreply(); |
pakrishn | 0:824af42f88ff | 71 | pc.printf(replybuff); |
pakrishn | 0:824af42f88ff | 72 | |
pakrishn | 0:824af42f88ff | 73 | wait(2); |
pakrishn | 0:824af42f88ff | 74 | |
pakrishn | 0:824af42f88ff | 75 | // set CIPMUX to 0=Single,1=Multi |
pakrishn | 0:824af42f88ff | 76 | pc.printf("\nSetting Connection Mode\r\n"); |
pakrishn | 0:824af42f88ff | 77 | strcpy(cmdbuff, "AT+CIPMUX=1\r\n"); |
pakrishn | 0:824af42f88ff | 78 | SendCMD(); |
pakrishn | 0:824af42f88ff | 79 | timeout=4; |
pakrishn | 0:824af42f88ff | 80 | getreply(); |
pakrishn | 0:824af42f88ff | 81 | pc.printf(replybuff); |
pakrishn | 0:824af42f88ff | 82 | |
pakrishn | 0:824af42f88ff | 83 | |
pakrishn | 0:824af42f88ff | 84 | wait(2); |
pakrishn | 0:824af42f88ff | 85 | |
pakrishn | 0:824af42f88ff | 86 | pc.printf("\nConnecting to AP\r\n"); |
pakrishn | 0:824af42f88ff | 87 | pc.printf("ssid = %s pwd = %s\r\n","ssid","password"); |
pakrishn | 0:824af42f88ff | 88 | strcpy(cmdbuff, "AT+CWJAP=\""); |
pakrishn | 0:824af42f88ff | 89 | strcat(cmdbuff, "galaxy"); |
pakrishn | 0:824af42f88ff | 90 | strcat(cmdbuff, "\",\""); |
pakrishn | 0:824af42f88ff | 91 | strcat(cmdbuff, "123456789"); |
pakrishn | 0:824af42f88ff | 92 | strcat(cmdbuff, "\"\r\n"); |
pakrishn | 0:824af42f88ff | 93 | SendCMD(); |
pakrishn | 0:824af42f88ff | 94 | timeout=10; |
pakrishn | 0:824af42f88ff | 95 | getreply(); |
pakrishn | 0:824af42f88ff | 96 | pc.printf(replybuff); |
pakrishn | 0:824af42f88ff | 97 | |
pakrishn | 0:824af42f88ff | 98 | wait(5); |
pakrishn | 0:824af42f88ff | 99 | |
pakrishn | 0:824af42f88ff | 100 | |
pakrishn | 0:824af42f88ff | 101 | |
pakrishn | 0:824af42f88ff | 102 | pc.printf("\nGet Connection Status\r\n"); |
pakrishn | 0:824af42f88ff | 103 | strcpy(cmdbuff, "AT+CIPSTATUS\r\n"); |
pakrishn | 0:824af42f88ff | 104 | SendCMD(); |
pakrishn | 0:824af42f88ff | 105 | timeout=5; |
pakrishn | 0:824af42f88ff | 106 | getreply(); |
pakrishn | 0:824af42f88ff | 107 | pc.printf(replybuff); |
pakrishn | 0:824af42f88ff | 108 | |
pakrishn | 0:824af42f88ff | 109 | wait(1); |
pakrishn | 0:824af42f88ff | 110 | |
pakrishn | 0:824af42f88ff | 111 | } |
pakrishn | 0:824af42f88ff | 112 | //temperature sensor reads the temp |
pakrishn | 0:824af42f88ff | 113 | void gettemp() |
pakrishn | 0:824af42f88ff | 114 | { |
pakrishn | 0:824af42f88ff | 115 | float temperature = 0.0f; |
pakrishn | 0:824af42f88ff | 116 | int error = 0; |
pakrishn | 0:824af42f88ff | 117 | |
pakrishn | 0:824af42f88ff | 118 | wait(2.0f); |
pakrishn | 0:824af42f88ff | 119 | error = sensor.readData(); |
pakrishn | 0:824af42f88ff | 120 | if (error == 0) { |
pakrishn | 0:824af42f88ff | 121 | temperature = sensor.ReadTemperature(FARENHEIT); |
pakrishn | 0:824af42f88ff | 122 | pc.printf("Temperature in Farenheit %3.2f\n",temperature); |
pakrishn | 0:824af42f88ff | 123 | } |
pakrishn | 0:824af42f88ff | 124 | else{ |
pakrishn | 0:824af42f88ff | 125 | pc.printf("Error: %d\n",error); |
pakrishn | 0:824af42f88ff | 126 | } |
pakrishn | 0:824af42f88ff | 127 | |
pakrishn | 0:824af42f88ff | 128 | sendUbidotsData(2, temperature); |
pakrishn | 0:824af42f88ff | 129 | wait(2); |
pakrishn | 0:824af42f88ff | 130 | } |
pakrishn | 0:824af42f88ff | 131 | |
pakrishn | 0:824af42f88ff | 132 | //pir sensor detects if there is motion |
pakrishn | 0:824af42f88ff | 133 | void getMotionSensor(){ |
pakrishn | 0:824af42f88ff | 134 | pc.printf("get motion sensor"); |
pakrishn | 0:824af42f88ff | 135 | int val = pirInputPin.read(); // Continuously Read output of PIR sensor on D2 pin |
pakrishn | 0:824af42f88ff | 136 | if (val==0) // val=0 means there is a motion |
pakrishn | 0:824af42f88ff | 137 | { |
pakrishn | 0:824af42f88ff | 138 | for(int i =0;i<3 && val==0;i++) |
pakrishn | 0:824af42f88ff | 139 | { |
pakrishn | 0:824af42f88ff | 140 | val = pirInputPin.read(); |
pakrishn | 0:824af42f88ff | 141 | wait(1); |
pakrishn | 0:824af42f88ff | 142 | } |
pakrishn | 0:824af42f88ff | 143 | if (val==0) |
pakrishn | 0:824af42f88ff | 144 | sendUbidotsData(1, 1.0); |
pakrishn | 0:824af42f88ff | 145 | } |
pakrishn | 0:824af42f88ff | 146 | else |
pakrishn | 0:824af42f88ff | 147 | wait(2); |
pakrishn | 0:824af42f88ff | 148 | } |
pakrishn | 0:824af42f88ff | 149 | |
pakrishn | 0:824af42f88ff | 150 | //post dat ato ubidots website |
pakrishn | 0:824af42f88ff | 151 | //pir=1,temp=2 |
pakrishn | 0:824af42f88ff | 152 | void sendUbidotsData(int sensor, float value){ |
pakrishn | 0:824af42f88ff | 153 | char postData[255]; |
pakrishn | 0:824af42f88ff | 154 | char valueChar[10]; |
pakrishn | 0:824af42f88ff | 155 | char valLenChar[7]; |
pakrishn | 0:824af42f88ff | 156 | int valLength = sprintf(valueChar,"%3.1f",value); |
pakrishn | 0:824af42f88ff | 157 | valLength = valLength+13; |
pakrishn | 0:824af42f88ff | 158 | sprintf(valLenChar,"%ld",valLength); |
pakrishn | 0:824af42f88ff | 159 | |
pakrishn | 0:824af42f88ff | 160 | if(sensor == 1) |
pakrishn | 0:824af42f88ff | 161 | strcpy(postData, "POST /api/v1.6/variables/variable_id1/values/?token=token_id HTTP/1.1\r\n"); |
pakrishn | 0:824af42f88ff | 162 | else{ |
pakrishn | 0:824af42f88ff | 163 | strcpy(postData, "POST /api/v1.6/variables/variable_id2/values/?token=token_id HTTP/1.1\r\n"); |
pakrishn | 0:824af42f88ff | 164 | } |
pakrishn | 0:824af42f88ff | 165 | strcat(postData, "Host: things.ubidots.com\r\n"); |
pakrishn | 0:824af42f88ff | 166 | strcat(postData, "Content-Type: application/json\r\n"); |
pakrishn | 0:824af42f88ff | 167 | strcat(postData, "Content-Length: "); |
pakrishn | 0:824af42f88ff | 168 | strcat(postData, valLenChar); |
pakrishn | 0:824af42f88ff | 169 | strcat(postData, "\r\n\r\n"); |
pakrishn | 0:824af42f88ff | 170 | strcat(postData, "{\"value\": \""); |
pakrishn | 0:824af42f88ff | 171 | strcat(postData, valueChar); |
pakrishn | 0:824af42f88ff | 172 | strcat(postData, "\"}\r\n\r\n"); |
pakrishn | 0:824af42f88ff | 173 | |
pakrishn | 0:824af42f88ff | 174 | timeout=1000;getcount=600; |
pakrishn | 0:824af42f88ff | 175 | |
pakrishn | 0:824af42f88ff | 176 | pc.printf("\nStart TCP\r\n"); |
pakrishn | 0:824af42f88ff | 177 | strcpy(cmdbuff, "AT+CIPSTART=4,\"TCP\",\"things.ubidots.com\",80\r\n"); |
pakrishn | 0:824af42f88ff | 178 | SendCMD(); |
pakrishn | 0:824af42f88ff | 179 | timeout=5; |
pakrishn | 0:824af42f88ff | 180 | getreply(); |
pakrishn | 0:824af42f88ff | 181 | pc.printf(replybuff); |
pakrishn | 0:824af42f88ff | 182 | |
pakrishn | 0:824af42f88ff | 183 | wait(5); |
pakrishn | 0:824af42f88ff | 184 | |
pakrishn | 0:824af42f88ff | 185 | pc.printf("\nConnecting\r\n"); |
pakrishn | 0:824af42f88ff | 186 | sprintf(cmdbuff, "AT+CIPSEND=4,%d\r\n",strlen(postData));//strlen(postData) |
pakrishn | 0:824af42f88ff | 187 | SendCMD(); |
pakrishn | 0:824af42f88ff | 188 | wait(3); |
pakrishn | 0:824af42f88ff | 189 | timeout=5; |
pakrishn | 0:824af42f88ff | 190 | getreply(); |
pakrishn | 0:824af42f88ff | 191 | pc.printf(replybuff); |
pakrishn | 0:824af42f88ff | 192 | |
pakrishn | 0:824af42f88ff | 193 | wait(2); |
pakrishn | 0:824af42f88ff | 194 | |
pakrishn | 0:824af42f88ff | 195 | pc.printf("\nPOST Request\r\n"); |
pakrishn | 0:824af42f88ff | 196 | strcpy(cmdbuff, postData); |
pakrishn | 0:824af42f88ff | 197 | SendCMD(); |
pakrishn | 0:824af42f88ff | 198 | wait(7); |
pakrishn | 0:824af42f88ff | 199 | timeout=5; |
pakrishn | 0:824af42f88ff | 200 | getreply(); |
pakrishn | 0:824af42f88ff | 201 | pc.printf(replybuff); |
pakrishn | 0:824af42f88ff | 202 | |
pakrishn | 0:824af42f88ff | 203 | wait(5); |
pakrishn | 0:824af42f88ff | 204 | pc.printf("\nClose TCP\r\n"); |
pakrishn | 0:824af42f88ff | 205 | strcpy(cmdbuff, "AT+CIPCLOSE\r\n"); |
pakrishn | 0:824af42f88ff | 206 | SendCMD(); |
pakrishn | 0:824af42f88ff | 207 | timeout=5; |
pakrishn | 0:824af42f88ff | 208 | getreply(); |
pakrishn | 0:824af42f88ff | 209 | pc.printf(replybuff); |
pakrishn | 0:824af42f88ff | 210 | |
pakrishn | 0:824af42f88ff | 211 | wait(2); |
pakrishn | 0:824af42f88ff | 212 | } |
pakrishn | 0:824af42f88ff | 213 | |
pakrishn | 0:824af42f88ff | 214 | |
pakrishn | 0:824af42f88ff | 215 | // ESP Command data send |
pakrishn | 0:824af42f88ff | 216 | void SendCMD() |
pakrishn | 0:824af42f88ff | 217 | { |
pakrishn | 0:824af42f88ff | 218 | esp.printf("%s", cmdbuff); |
pakrishn | 0:824af42f88ff | 219 | } |
pakrishn | 0:824af42f88ff | 220 | |
pakrishn | 0:824af42f88ff | 221 | void getreply() |
pakrishn | 0:824af42f88ff | 222 | { |
pakrishn | 0:824af42f88ff | 223 | memset(replybuff, '\0', sizeof(replybuff)); |
pakrishn | 0:824af42f88ff | 224 | t1.reset(); t1.start();replycount=0; |
pakrishn | 0:824af42f88ff | 225 | while(t1.read_ms()< timeout && replycount < getcount) { |
pakrishn | 0:824af42f88ff | 226 | if(esp.readable()) { |
pakrishn | 0:824af42f88ff | 227 | replybuff[replycount] = esp.getc();replycount++; |
pakrishn | 0:824af42f88ff | 228 | } |
pakrishn | 0:824af42f88ff | 229 | } |
pakrishn | 0:824af42f88ff | 230 | t1.stop(); |
pakrishn | 0:824af42f88ff | 231 | } |