post sensor data to esp yun

Dependencies:   WIZnetInterface mbed

Fork of espyun by root root

Committer:
dfrobot
Date:
Fri Nov 25 06:54:18 2016 +0000
Revision:
9:b5c8b5e14195
Parent:
8:472cd389f3eb
MASTER_KEY can be configed

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jehoon 0:af541535ef54 1 #include "mbed.h"
jehoon 0:af541535ef54 2 #include "EthernetInterface.h"
jh_ndm 1:3550659bd872 3 #include "cJSON.h"
jehoon 0:af541535ef54 4
dfrobot 9:b5c8b5e14195 5 #define MASTER_KEY "b1e92cf17fe4482647924941949ab2b07ffab548"
dfrobot 9:b5c8b5e14195 6
846354866 4:e1d2c6f94c69 7 DigitalInOut myIOD10(D10);
846354866 7:bda2a6a118f7 8 DigitalIn myInD11(D11);
846354866 4:e1d2c6f94c69 9 DigitalOut myOutD12(D12);
846354866 4:e1d2c6f94c69 10 AnalogIn myInA1(A1);
jh_ndm 1:3550659bd872 11 int socketPort = 8000;
jh_ndm 1:3550659bd872 12 int httpPort = 80;
dfrobot 8:472cd389f3eb 13 char g_buffer[500];
dfrobot 8:472cd389f3eb 14
jh_ndm 1:3550659bd872 15 Serial pc(PA_13,PA_14);
jh_ndm 1:3550659bd872 16
jh_ndm 1:3550659bd872 17 int MallocSize = 800*sizeof(char);
jh_ndm 1:3550659bd872 18 bool http_GET(TCPSocketConnection &conn);
846354866 4:e1d2c6f94c69 19 bool http_POST(TCPSocketConnection &conn,int vall);
846354866 4:e1d2c6f94c69 20 void receive_dht11();
846354866 4:e1d2c6f94c69 21 unsigned char receive_byte();
846354866 7:bda2a6a118f7 22 void button_led();
846354866 4:e1d2c6f94c69 23 Timeout receive_dht11Timeout;
846354866 7:bda2a6a118f7 24 Timeout button_ledTimeout;
846354866 4:e1d2c6f94c69 25
846354866 4:e1d2c6f94c69 26 void delay(unsigned char ms)
846354866 4:e1d2c6f94c69 27 {
846354866 4:e1d2c6f94c69 28 unsigned int y;
846354866 4:e1d2c6f94c69 29 while(ms--)
846354866 4:e1d2c6f94c69 30 for(y=1920; y>0; y--);
846354866 4:e1d2c6f94c69 31 }
846354866 4:e1d2c6f94c69 32
846354866 4:e1d2c6f94c69 33 void Delay_us(unsigned char us)
846354866 4:e1d2c6f94c69 34 {
846354866 4:e1d2c6f94c69 35 unsigned char y;
846354866 4:e1d2c6f94c69 36 while(us--)
846354866 4:e1d2c6f94c69 37 for(y=2; y>0; y--);
846354866 4:e1d2c6f94c69 38 }
jh_ndm 1:3550659bd872 39
jh_ndm 1:3550659bd872 40 float val;
jh_ndm 1:3550659bd872 41 bool out=false;
846354866 4:e1d2c6f94c69 42 unsigned char RH,RL,TH,TL;
846354866 4:e1d2c6f94c69 43 unsigned char data_byte;
846354866 4:e1d2c6f94c69 44 unsigned int U8FLAG;
846354866 4:e1d2c6f94c69 45 static unsigned char num = 4;
846354866 4:e1d2c6f94c69 46 const char receive_module[4][5] = {"Led", "Gas", "Hum", "Temp", };
dfrobot 8:472cd389f3eb 47
jh_ndm 1:3550659bd872 48 int main() {
jehoon 0:af541535ef54 49 int phy_link;
jh_ndm 1:3550659bd872 50 pc.printf("Wait a second...\r\n");
jh_ndm 1:3550659bd872 51 uint8_t mac_addr[6] = {0x78, 0x08, 0xDC, 0x1c, 0xa8, 0x95};
846354866 4:e1d2c6f94c69 52 receive_dht11Timeout.attach(&receive_dht11,0.2f);
846354866 7:bda2a6a118f7 53 button_ledTimeout.attach(&button_led,0.1f);
jehoon 0:af541535ef54 54
jehoon 0:af541535ef54 55 EthernetInterface eth;
jehoon 0:af541535ef54 56 eth.init(mac_addr); //Use DHCP
jehoon 0:af541535ef54 57 eth.connect();
jh_ndm 1:3550659bd872 58 pc.printf("start IP Address is %s\r\n", eth.getIPAddress());
846354866 7:bda2a6a118f7 59
jh_ndm 1:3550659bd872 60 do{
jh_ndm 1:3550659bd872 61 phy_link = eth.ethernet_link();
jh_ndm 1:3550659bd872 62 pc.printf("...");
846354866 7:bda2a6a118f7 63 pc.printf("phy_link=%d...", phy_link);
jh_ndm 1:3550659bd872 64 wait(2);
jh_ndm 1:3550659bd872 65 }while(!phy_link);
jh_ndm 1:3550659bd872 66 printf("\r\n");
846354866 7:bda2a6a118f7 67
jh_ndm 1:3550659bd872 68 pc.printf("IP Address is %s\r\n", eth.getIPAddress());
jh_ndm 1:3550659bd872 69
jh_ndm 1:3550659bd872 70 TCPSocketConnection conn;
jh_ndm 1:3550659bd872 71 while(true)
jh_ndm 1:3550659bd872 72 {
846354866 7:bda2a6a118f7 73 //wait(0.5f);
846354866 4:e1d2c6f94c69 74 if(num)
846354866 4:e1d2c6f94c69 75 {
846354866 4:e1d2c6f94c69 76 if (num == 4)
846354866 4:e1d2c6f94c69 77 val = TH;
846354866 4:e1d2c6f94c69 78 else if (num == 3)
846354866 4:e1d2c6f94c69 79 val = RH;
846354866 4:e1d2c6f94c69 80 else if (num == 2)
846354866 7:bda2a6a118f7 81 val = myInA1.read_u16();
846354866 4:e1d2c6f94c69 82 else if (num == 1)
846354866 6:bc4901a948b1 83 val = myOutD12.read();
846354866 4:e1d2c6f94c69 84 }
846354866 7:bda2a6a118f7 85 //int mathtest = ceil(val);//math.
jh_ndm 1:3550659bd872 86 pc.printf("start http_post\r\n");
846354866 6:bc4901a948b1 87 wait(1);
846354866 7:bda2a6a118f7 88
846354866 7:bda2a6a118f7 89 conn.connect("iot.espressif.cn", httpPort);
jh_ndm 1:3550659bd872 90
846354866 7:bda2a6a118f7 91 http_POST(conn, val);
jh_ndm 1:3550659bd872 92
846354866 7:bda2a6a118f7 93 conn.close();
846354866 4:e1d2c6f94c69 94 val = 0.0;
846354866 6:bc4901a948b1 95 //mathtest = 0;
jh_ndm 1:3550659bd872 96
jh_ndm 1:3550659bd872 97 if(out)break;
jh_ndm 1:3550659bd872 98 }
846354866 7:bda2a6a118f7 99
846354866 7:bda2a6a118f7 100 //conn.close();
846354866 7:bda2a6a118f7 101 eth.disconnect();
846354866 7:bda2a6a118f7 102 pc.printf("out the program!\r\n");
846354866 7:bda2a6a118f7 103 return 0;
846354866 7:bda2a6a118f7 104 }
846354866 7:bda2a6a118f7 105
846354866 4:e1d2c6f94c69 106 /*http POST*/
846354866 4:e1d2c6f94c69 107 bool http_POST(TCPSocketConnection &conn, int val)
846354866 4:e1d2c6f94c69 108 {
846354866 7:bda2a6a118f7 109 pc.printf("%s():%d\n",__FUNCTION__, __LINE__);
dfrobot 8:472cd389f3eb 110
846354866 4:e1d2c6f94c69 111 cJSON* http_post=NULL;
846354866 4:e1d2c6f94c69 112 http_post = cJSON_CreateObject();
846354866 4:e1d2c6f94c69 113 if(NULL == http_post)
846354866 4:e1d2c6f94c69 114 {
846354866 4:e1d2c6f94c69 115 pc.printf("OUT4\r\n");
846354866 4:e1d2c6f94c69 116 out = true;
846354866 4:e1d2c6f94c69 117 return false;
846354866 4:e1d2c6f94c69 118 }
846354866 7:bda2a6a118f7 119
846354866 4:e1d2c6f94c69 120 cJSON* http_post_json=NULL;
846354866 4:e1d2c6f94c69 121 http_post_json = cJSON_CreateObject();
846354866 4:e1d2c6f94c69 122 if(NULL == http_post_json)
846354866 4:e1d2c6f94c69 123 {
846354866 4:e1d2c6f94c69 124 cJSON_Delete(http_post);
846354866 4:e1d2c6f94c69 125 pc.printf("OUT5\r\n");
846354866 4:e1d2c6f94c69 126 out = true;
846354866 4:e1d2c6f94c69 127 return false;
846354866 4:e1d2c6f94c69 128 }
846354866 4:e1d2c6f94c69 129 cJSON_AddNumberToObject(http_post_json, "x", val);
846354866 4:e1d2c6f94c69 130 //cJSON_AddNumberToObject(http_post_json, "y", 2);
846354866 4:e1d2c6f94c69 131 //cJSON_AddNumberToObject(http_post_json, "z", 2);
846354866 4:e1d2c6f94c69 132 cJSON_AddItemToObject(http_post, "datapoint", http_post_json);
846354866 4:e1d2c6f94c69 133 char* pJSON_http_post = cJSON_PrintUnformatted(http_post);
846354866 7:bda2a6a118f7 134
846354866 4:e1d2c6f94c69 135 if(NULL == pJSON_http_post)
846354866 4:e1d2c6f94c69 136 {
846354866 4:e1d2c6f94c69 137 cJSON_Delete(http_post);
846354866 4:e1d2c6f94c69 138 pc.printf("OUT6\r\n");
846354866 4:e1d2c6f94c69 139 out = true;
846354866 4:e1d2c6f94c69 140 return false;
846354866 4:e1d2c6f94c69 141 }
846354866 4:e1d2c6f94c69 142
846354866 4:e1d2c6f94c69 143 //Pay attention to out of memory
846354866 4:e1d2c6f94c69 144 //char http_POST_cmd[500] = "POST /v1/datastreams/jhtest/datapoint/ HTTP/1.1\r\nUser-Agent: curl/7.22.0 (i686-pc-linux-gnu) libcurl/7.22.0 OpenSSL/1.0.1 zlib/1.2.3.4 libidn/1.23 librtmp/2.3\r\n"\
846354866 4:e1d2c6f94c69 145 // "Host:iot.espressif.cn\r\nAccept: */*\r\nAuthorization: token ebe1671e9d9f42a5231d017ec550552b9bae2cdf\r\n"\
846354866 4:e1d2c6f94c69 146 // "Content-Length: 33\r\nContent-Type: application/x-www-form-urlencoded\r\n\r\n";
dfrobot 9:b5c8b5e14195 147 //char http_POST_cmd[500];// = "POST /v1/datastreams/test/datapoint/ HTTP/1.1\r\nUser-Agent: curl/7.22.0 (i686-pc-linux-gnu) libcurl/7.22.0 OpenSSL/1.0.1 zlib/1.2.3.4 libidn/1.23 librtmp/2.3\r\n"\
846354866 4:e1d2c6f94c69 148 // "Host:iot.espressif.cn\r\nAccept: */*\r\nAuthorization: token b1e92cf17fe4482647924941949ab2b07ffab548\r\n"\
846354866 4:e1d2c6f94c69 149 // "Content-Length: ";
dfrobot 9:b5c8b5e14195 150 sprintf(g_buffer,"POST /v1/datastreams/%s/datapoint/ HTTP/1.1\r\nUser-Agent: curl/7.35.0\r\n"\
dfrobot 9:b5c8b5e14195 151 "Host:iot.espressif.cn\r\nAccept: */*\r\nAuthorization: token %s\r\n"\
dfrobot 9:b5c8b5e14195 152 "Content-Length: ", receive_module[num-1], MASTER_KEY);
846354866 4:e1d2c6f94c69 153 num--;
846354866 4:e1d2c6f94c69 154 if (num == 0)
846354866 4:e1d2c6f94c69 155 num = 4;
846354866 4:e1d2c6f94c69 156
846354866 4:e1d2c6f94c69 157 char len[2];
846354866 4:e1d2c6f94c69 158 len[0]=strlen(pJSON_http_post)/10%10+48;
846354866 4:e1d2c6f94c69 159 len[1]=strlen(pJSON_http_post)%10+48;
dfrobot 9:b5c8b5e14195 160 strcat(g_buffer,len);
dfrobot 9:b5c8b5e14195 161 strcat(g_buffer,"\r\nContent-Type: application/x-www-form-urlencoded\r\n\r\n");
846354866 4:e1d2c6f94c69 162
dfrobot 9:b5c8b5e14195 163 strcat(g_buffer,pJSON_http_post);
dfrobot 9:b5c8b5e14195 164 conn.send_all(g_buffer, strlen(g_buffer));//http_cmd for http,pJSON for socket,should be three changes
dfrobot 9:b5c8b5e14195 165 pc.printf("%s\r\n",g_buffer);
846354866 7:bda2a6a118f7 166
846354866 4:e1d2c6f94c69 167 char *buffer = NULL;
846354866 4:e1d2c6f94c69 168 buffer = (char *)malloc(MallocSize);
846354866 4:e1d2c6f94c69 169 if(!buffer)pc.printf("malloc buffer err\r\n");
846354866 4:e1d2c6f94c69 170
846354866 7:bda2a6a118f7 171 pc.printf("http_POST OUT\r\n");
846354866 7:bda2a6a118f7 172
846354866 7:bda2a6a118f7 173 free(pJSON_http_post);
846354866 7:bda2a6a118f7 174 free(buffer);
846354866 7:bda2a6a118f7 175 cJSON_Delete(http_post);
846354866 7:bda2a6a118f7 176
846354866 7:bda2a6a118f7 177 return true;
846354866 7:bda2a6a118f7 178 }
846354866 7:bda2a6a118f7 179
846354866 7:bda2a6a118f7 180 /*http_GET*/
846354866 7:bda2a6a118f7 181 bool http_GET(TCPSocketConnection &conn)
846354866 7:bda2a6a118f7 182 {
846354866 7:bda2a6a118f7 183 if(conn.is_connected()){
846354866 7:bda2a6a118f7 184 pc.printf("connect is already ok!\r\n");
846354866 7:bda2a6a118f7 185 }else{
846354866 7:bda2a6a118f7 186 if(!conn.connect("iot.espressif.cn", httpPort)) //80 for http,8000 for socket
846354866 7:bda2a6a118f7 187 pc.printf("connect OK!\r\n");
846354866 7:bda2a6a118f7 188 else{
846354866 7:bda2a6a118f7 189 pc.printf("connct ERR!\r\n");
846354866 7:bda2a6a118f7 190 out = true;
846354866 7:bda2a6a118f7 191 return false;
846354866 7:bda2a6a118f7 192 }
846354866 7:bda2a6a118f7 193 }
846354866 7:bda2a6a118f7 194
dfrobot 9:b5c8b5e14195 195 snprintf(g_buffer,sizeof(g_buffer),"GET /v1/datastreams/Led/datapoint/ HTTP/1.1\r\nUser-Agent: curl/7.22.0 (i686-pc-linux-gnu) libcurl/7.22.0 OpenSSL/1.0.1 zlib/1.2.3.4 libidn/1.23 librtmp/2.3\r\n"\
dfrobot 9:b5c8b5e14195 196 "Host:iot.espressif.cn\r\nAccept: */*\r\nAuthorization: token %s\r\n\r\n",MASTER_KEY);
846354866 7:bda2a6a118f7 197
dfrobot 9:b5c8b5e14195 198 conn.send_all(g_buffer, strlen(g_buffer));//http_cmd for http,pJSON for socket,should be three changes
dfrobot 9:b5c8b5e14195 199 pc.printf("%s\r\n",g_buffer);
846354866 7:bda2a6a118f7 200
846354866 4:e1d2c6f94c69 201 int ret;
846354866 7:bda2a6a118f7 202
846354866 4:e1d2c6f94c69 203 while (true) {
846354866 4:e1d2c6f94c69 204 wait(1);
dfrobot 8:472cd389f3eb 205 ret = conn.receive(g_buffer, 500-1);
846354866 4:e1d2c6f94c69 206 pc.printf("ret = %d\r\n",ret);
846354866 4:e1d2c6f94c69 207 if (ret <= 0) {
846354866 4:e1d2c6f94c69 208 break;
846354866 4:e1d2c6f94c69 209 }
dfrobot 8:472cd389f3eb 210 g_buffer[ret] = '\0';
dfrobot 8:472cd389f3eb 211 pc.printf("Received %d chars from server: %s\n", ret, g_buffer);
846354866 4:e1d2c6f94c69 212 }
846354866 7:bda2a6a118f7 213 pc.printf("http_GET OUT\r\n");
846354866 4:e1d2c6f94c69 214 return true;
846354866 4:e1d2c6f94c69 215 }
846354866 4:e1d2c6f94c69 216
846354866 4:e1d2c6f94c69 217 unsigned char receive_byte()
846354866 4:e1d2c6f94c69 218 {
846354866 4:e1d2c6f94c69 219 unsigned char i,temp;
846354866 4:e1d2c6f94c69 220 for(i=0;i<8;i++)
846354866 4:e1d2c6f94c69 221 {
846354866 4:e1d2c6f94c69 222 U8FLAG = 2;
846354866 4:e1d2c6f94c69 223 while(!myIOD10.read() && U8FLAG++);
846354866 4:e1d2c6f94c69 224 Delay_us(30);
846354866 4:e1d2c6f94c69 225 temp=0;
846354866 4:e1d2c6f94c69 226 if(myIOD10.read()==1)
846354866 4:e1d2c6f94c69 227 temp=1;
846354866 4:e1d2c6f94c69 228 U8FLAG = 2;
846354866 4:e1d2c6f94c69 229 while(myIOD10.read() && U8FLAG++);
846354866 4:e1d2c6f94c69 230
846354866 4:e1d2c6f94c69 231 if(U8FLAG==1)break;
846354866 4:e1d2c6f94c69 232 data_byte<<=1;//
846354866 4:e1d2c6f94c69 233 data_byte|=temp;
846354866 4:e1d2c6f94c69 234 }
846354866 4:e1d2c6f94c69 235 return data_byte;
846354866 4:e1d2c6f94c69 236 }
846354866 4:e1d2c6f94c69 237
846354866 4:e1d2c6f94c69 238 void receive_dht11()
846354866 4:e1d2c6f94c69 239 {
846354866 4:e1d2c6f94c69 240 unsigned char T_H,T_L,R_H,R_L,check,num_check;
846354866 4:e1d2c6f94c69 241
846354866 4:e1d2c6f94c69 242
846354866 4:e1d2c6f94c69 243 myIOD10.output();
846354866 4:e1d2c6f94c69 244 myIOD10.write(0);
846354866 4:e1d2c6f94c69 245 delay(20);
846354866 4:e1d2c6f94c69 246
846354866 4:e1d2c6f94c69 247 myIOD10.write(1);
846354866 4:e1d2c6f94c69 248 Delay_us(25);
846354866 4:e1d2c6f94c69 249 myIOD10.write(1);
846354866 4:e1d2c6f94c69 250
846354866 4:e1d2c6f94c69 251 myIOD10.input();
846354866 4:e1d2c6f94c69 252 if(!myIOD10.read())
846354866 4:e1d2c6f94c69 253 {
846354866 4:e1d2c6f94c69 254 while(!myIOD10.read());
846354866 4:e1d2c6f94c69 255 while(myIOD10.read());
846354866 4:e1d2c6f94c69 256 R_H=receive_byte();
846354866 4:e1d2c6f94c69 257 R_L=receive_byte();
846354866 4:e1d2c6f94c69 258 T_H=receive_byte();
846354866 4:e1d2c6f94c69 259 T_L=receive_byte();
846354866 4:e1d2c6f94c69 260 check=receive_byte();
846354866 6:bc4901a948b1 261 //pc.printf("--R_H--%d---R_L---%d--T_H--%d---T_L---%d\r\n", R_H,R_L,T_H,T_L);
846354866 4:e1d2c6f94c69 262 Delay_us(20);
846354866 6:bc4901a948b1 263 num_check=R_H+R_L+T_H+T_L;//pc.printf("--num_check--%d---check---%d\r\n", num_check,check);
846354866 4:e1d2c6f94c69 264 if(num_check==check)
846354866 4:e1d2c6f94c69 265 {
846354866 4:e1d2c6f94c69 266 RH=R_H;
846354866 4:e1d2c6f94c69 267 RL=R_L;
846354866 4:e1d2c6f94c69 268 TH=T_H;
846354866 4:e1d2c6f94c69 269 TL=T_L;
846354866 4:e1d2c6f94c69 270 check=num_check;
846354866 4:e1d2c6f94c69 271 }
846354866 4:e1d2c6f94c69 272 }
846354866 4:e1d2c6f94c69 273 receive_dht11Timeout.attach(&receive_dht11,0.2f);
846354866 4:e1d2c6f94c69 274 }
846354866 7:bda2a6a118f7 275
846354866 7:bda2a6a118f7 276 void button_led()
846354866 7:bda2a6a118f7 277 {
846354866 7:bda2a6a118f7 278 while(myInD11.read())
846354866 7:bda2a6a118f7 279 {
846354866 7:bda2a6a118f7 280 wait(0.01f);
846354866 7:bda2a6a118f7 281 if(!myInD11.read())
846354866 7:bda2a6a118f7 282 {
846354866 7:bda2a6a118f7 283 myOutD12.write(!myOutD12.read());
846354866 7:bda2a6a118f7 284 break;
846354866 7:bda2a6a118f7 285 }
846354866 7:bda2a6a118f7 286 }
846354866 7:bda2a6a118f7 287
846354866 7:bda2a6a118f7 288 button_ledTimeout.attach(&button_led,0.1f);
846354866 7:bda2a6a118f7 289 }