yangyang

Dependencies:   WIZnetInterface mbed

Fork of espyun1 by jiang hao

Committer:
846354866
Date:
Sat Nov 05 07:50:59 2016 +0000
Revision:
6:bc4901a948b1
Parent:
4:e1d2c6f94c69
Child:
7:bda2a6a118f7
sensor iot for espressif(no chinese) version0.1

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