yangyang

Dependencies:   WIZnetInterface mbed

Fork of espyun1 by jiang hao

Revision:
4:e1d2c6f94c69
Parent:
2:00baa8aaaaf1
Child:
6:bc4901a948b1
--- a/main.cpp	Fri Nov 04 03:02:15 2016 +0000
+++ b/main.cpp	Fri Nov 04 12:09:56 2016 +0000
@@ -2,7 +2,9 @@
 #include "EthernetInterface.h"
 #include "cJSON.h"
 
-//AnalogIn Gas(A2);
+DigitalInOut myIOD10(D10);
+DigitalOut myOutD12(D12);
+AnalogIn myInA1(A1);
 int socketPort = 8000;
 int httpPort = 80;
 
@@ -13,18 +15,41 @@
 int MallocSize = 800*sizeof(char);
 bool socket_GET(TCPSocketConnection &conn);
 bool http_GET(TCPSocketConnection &conn);
-bool http_POST(TCPSocketConnection &conn);
+bool http_POST(TCPSocketConnection &conn,int vall);
 bool socket_POST(TCPSocketConnection &conn,int vall);
 bool closeConnect(TCPSocketConnection &conn);
+void receive_dht11();
+unsigned char receive_byte();
+Timeout receive_dht11Timeout;
+
+void delay(unsigned char ms)
+{ 
+    unsigned int y;
+    while(ms--)
+        for(y=1920; y>0; y--);
+}
+
+void Delay_us(unsigned char us)
+{
+    unsigned char y;
+    while(us--)
+        for(y=2; y>0; y--);
+}
 
 float val;
 int mathtest;
 bool out=false;
+unsigned char RH,RL,TH,TL;
+unsigned char data_byte;
+unsigned int  U8FLAG;
+static unsigned char num = 4;
+const char receive_module[4][5] = {"Led", "Gas", "Hum", "Temp", };
 
 int main() { 
     int phy_link;
     pc.printf("Wait a second...\r\n");
     uint8_t mac_addr[6] = {0x78, 0x08, 0xDC, 0x1c, 0xa8, 0x95}; 
+    receive_dht11Timeout.attach(&receive_dht11,0.2f);
     
     EthernetInterface eth;
     eth.init(mac_addr); //Use DHCP
@@ -45,11 +70,26 @@
     while(true)
     {   
         wait(1);
-        //val = Gas.read()*1023;
-        //int mathtest = ceil(val);//math.h
-        //pc.printf("Gas.read=%02f,n=%d\r\n",val,mathtest);
+        int temp = TH;
+        int hum = RH;
+        int gas = myInA1.read();
+        int led = myOutD12.read();
+        static int val;
+        if(num)
+        {
+            if (num == 4)
+                val = TH;
+            else if (num == 3)
+                val = RH;
+            else if (num == 2)
+                val = gas;
+            else if (num == 1)
+                val = led;
+        }
+        //ceil(val);//math.h
+        pc.printf("send_data=%d,n=%d\r\n",val,mathtest);
         pc.printf("start http_post\r\n");
-        if(http_POST(conn))
+        if(http_POST(conn, val))
         {
             if(!conn.close()){//短连接,断开一次连接一次
                 pc.printf("conn.close OK\r\n");    
@@ -63,8 +103,8 @@
 
         }
         
-        //val = 0.0;  
-        //mathtest = 0; 
+        val = 0.0;  
+        mathtest = 0; 
         
         if(out)break;
     }
@@ -74,6 +114,104 @@
     return 0;                     
 }
 
+/*http POST*/
+bool http_POST(TCPSocketConnection &conn, int val)
+{
+    pc.printf("1111\r\n");
+    //wait(0.5);
+    if(conn.is_connected()){
+        pc.printf("connect is already ok!\r\n");    
+    }else{
+        if(!conn.connect("iot.espressif.cn", httpPort)) //80 for http,8000 for socket
+            pc.printf("connect OK!\r\n");
+        else{
+            pc.printf("connct ERR!\r\n");
+            out = true;
+            return false;    
+        }
+    }
+    pc.printf("2222\r\n");
+    cJSON* http_post=NULL;  
+    http_post = cJSON_CreateObject();    
+    if(NULL == http_post)
+    {
+         pc.printf("OUT4\r\n");
+         out = true;
+         return false;
+    }
+    pc.printf("3333\r\n");
+    cJSON* http_post_json=NULL;  
+    http_post_json = cJSON_CreateObject();    
+    if(NULL == http_post_json)
+    {
+         cJSON_Delete(http_post);
+         pc.printf("OUT5\r\n");
+         out = true;
+         return false;
+    }
+    cJSON_AddNumberToObject(http_post_json, "x", val);
+    //cJSON_AddNumberToObject(http_post_json, "y", 2);
+    //cJSON_AddNumberToObject(http_post_json, "z", 2);
+    cJSON_AddItemToObject(http_post, "datapoint", http_post_json);
+    char* pJSON_http_post = cJSON_PrintUnformatted(http_post);
+    pc.printf("4444\r\n");
+    if(NULL == pJSON_http_post)
+    {
+        cJSON_Delete(http_post);
+        pc.printf("OUT6\r\n");
+        out = true;
+        return false;
+    }
+    
+    //Pay attention to out of memory
+    //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"\
+                     // "Host:iot.espressif.cn\r\nAccept: */*\r\nAuthorization: token ebe1671e9d9f42a5231d017ec550552b9bae2cdf\r\n"\
+                     // "Content-Length: 33\r\nContent-Type: application/x-www-form-urlencoded\r\n\r\n";
+    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"\
+    //                  "Host:iot.espressif.cn\r\nAccept: */*\r\nAuthorization: token b1e92cf17fe4482647924941949ab2b07ffab548\r\n"\
+    //                  "Content-Length: ";
+    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"\
+                      "Host:iot.espressif.cn\r\nAccept: */*\r\nAuthorization: token b1e92cf17fe4482647924941949ab2b07ffab548\r\n"\
+                      "Content-Length: ", receive_module[num-1]);
+    num--;
+    if (num == 0)
+        num = 4;
+        
+    char len[2]; 
+    len[0]=strlen(pJSON_http_post)/10%10+48;
+    len[1]=strlen(pJSON_http_post)%10+48;
+    strcat(http_POST_cmd,len);
+    strcat(http_POST_cmd,"\r\nContent-Type: application/x-www-form-urlencoded\r\n\r\n");
+     
+    strcat(http_POST_cmd,pJSON_http_post);
+    conn.send_all(http_POST_cmd, strlen(http_POST_cmd));//http_cmd for http,pJSON for socket,should be three changes
+    pc.printf("%s\r\n",http_POST_cmd); 
+    pc.printf("5555\r\n");
+    char *buffer = NULL;
+    buffer = (char *)malloc(MallocSize);
+    if(!buffer)pc.printf("malloc buffer err\r\n");
+
+    int ret;
+    pc.printf("6666\r\n");
+    while (true) {
+        wait(1);
+        ret = conn.receive(buffer, MallocSize-1);
+        pc.printf("ret = %d\r\n",ret);
+        if (ret <= 0) {
+            break;
+        }
+        buffer[ret] = '\0';
+        pc.printf("Received %d chars from server: %s\n", ret, buffer);     
+    }
+    pc.printf("http_POST OUT\r\n");
+
+    free(pJSON_http_post);
+    free(buffer);
+    cJSON_Delete(http_post);
+    pc.printf("7777\r\n");
+    return true;
+}
+
 /*socket GET*/
 bool socket_GET(TCPSocketConnection &conn)
 {
@@ -165,7 +303,7 @@
         }
     }
     
-    char http_cmd[] = "GET /v1/datastreams/Gas/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"\
+    char http_cmd[] = "GET /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"\
                       "Host:iot.espressif.cn\r\nAccept: */*\r\nAuthorization: token ebe1671e9d9f42a5231d017ec550552b9bae2cdf\r\n\r\n";
     
     conn.send_all(http_cmd, strlen(http_cmd));//http_cmd for http,pJSON for socket,should be three changes
@@ -188,7 +326,7 @@
         pc.printf("Received %d chars from server: %s\n", ret, buffer);     
     }
     pc.printf("http_GET OUT\r\n");
-    /* ///conn.close已经放到main中,55 行
+    /* ///conn.close
     if(conn.close()){
         pc.printf("conn.close OK\r\n");    
     }else{
@@ -199,97 +337,6 @@
     return true;
 }
 
-/*http POST*/
-bool http_POST(TCPSocketConnection &conn)
-{
-    pc.printf("1111\r\n");
-    wait(0.5);
-    if(conn.is_connected()){
-        pc.printf("connect is already ok!\r\n");    
-    }else{
-        if(!conn.connect("iot.espressif.cn", httpPort)) //80 for http,8000 for socket
-            pc.printf("connect OK!\r\n");
-        else{
-            pc.printf("connct ERR!\r\n");
-            out = true;
-            return false;    
-        }
-    }
-    pc.printf("2222\r\n");
-    cJSON* http_post=NULL;  
-    http_post = cJSON_CreateObject();    
-    if(NULL == http_post)
-    {
-         pc.printf("OUT4\r\n");
-         out = true;
-         return false;
-    }
-    pc.printf("3333\r\n");
-    cJSON* http_post_json=NULL;  
-    http_post_json = cJSON_CreateObject();    
-    if(NULL == http_post_json)
-    {
-         cJSON_Delete(http_post);
-         pc.printf("OUT5\r\n");
-         out = true;
-         return false;
-    }
-    cJSON_AddNumberToObject(http_post_json, "x", 2);
-    //cJSON_AddNumberToObject(http_post_json, "y", 2);
-    //cJSON_AddNumberToObject(http_post_json, "z", 2);
-    cJSON_AddItemToObject(http_post, "datapoint", http_post_json);
-    char* pJSON_http_post = cJSON_PrintUnformatted(http_post);
-    pc.printf("4444\r\n");
-    if(NULL == pJSON_http_post)
-    {
-        cJSON_Delete(http_post);
-        pc.printf("OUT6\r\n");
-        out = true;
-        return false;
-    }
-    
-    //Pay attention to out of memory
-    //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"\
-                     // "Host:iot.espressif.cn\r\nAccept: */*\r\nAuthorization: token ebe1671e9d9f42a5231d017ec550552b9bae2cdf\r\n"\
-                     // "Content-Length: 33\r\nContent-Type: application/x-www-form-urlencoded\r\n\r\n";
-    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"\
-                      "Host:iot.espressif.cn\r\nAccept: */*\r\nAuthorization: token 48661aa81484f501362ed5ef4cc85e67eb2a3e3d\r\n"\
-                      "Content-Length: ";
-    char len[2]; 
-    len[0]=strlen(pJSON_http_post)/10%10+48;
-    len[1]=strlen(pJSON_http_post)%10+48;
-    strcat(http_POST_cmd,len);
-    strcat(http_POST_cmd,"\r\nContent-Type: application/x-www-form-urlencoded\r\n\r\n");
-     
-    strcat(http_POST_cmd,pJSON_http_post);
-    conn.send_all(http_POST_cmd, strlen(http_POST_cmd));//http_cmd for http,pJSON for socket,should be three changes
-    pc.printf("%s\r\n",http_POST_cmd); 
-    pc.printf("5555\r\n");
-    char *buffer = NULL;
-    buffer = (char *)malloc(MallocSize);
-    if(!buffer)pc.printf("malloc buffer err\r\n");
-
-    int ret;
-    pc.printf("6666\r\n");
-    while (true) {
-        wait(1);
-        ret = conn.receive(buffer, MallocSize-1);
-        pc.printf("ret = %d\r\n",ret);
-        if (ret <= 0) {
-            break;
-        }
-        buffer[ret] = '\0';
-        pc.printf("Received %d chars from server: %s\n", ret, buffer);     
-    }
-    pc.printf("http_POST OUT\r\n");
-
-    free(pJSON_http_post);
-    free(buffer);
-    cJSON_Delete(http_post);
-    pc.printf("7777\r\n");
-    return true;
-}
-
 /*socket POST*/
 bool socket_POST(TCPSocketConnection &conn,int vall)
 {   
@@ -468,3 +515,68 @@
 
     return true;
 }
+
+unsigned char receive_byte()
+{
+    unsigned char i,temp;
+    for(i=0;i<8;i++)
+    {
+        U8FLAG = 2;
+        while(!myIOD10.read() && U8FLAG++);
+        Delay_us(30);
+        temp=0;
+        if(myIOD10.read()==1)
+        temp=1;
+        U8FLAG = 2;
+        while(myIOD10.read() && U8FLAG++);
+        
+        if(U8FLAG==1)break;
+        data_byte<<=1;//
+        data_byte|=temp;
+    }
+    return data_byte;
+}
+
+void receive_dht11()
+{
+    unsigned char T_H,T_L,R_H,R_L,check,num_check;
+    
+   
+    myIOD10.output();
+    myIOD10.write(0);
+    delay(20);
+    
+    myIOD10.write(1);
+    Delay_us(25);
+    myIOD10.write(1);
+    
+    myIOD10.input();
+    if(!myIOD10.read())
+    {
+        while(!myIOD10.read());
+        while(myIOD10.read());
+        R_H=receive_byte();
+        R_L=receive_byte();
+        T_H=receive_byte();
+        T_L=receive_byte();
+        check=receive_byte();
+        pc.printf("--R_H--%d---R_L---%d--T_H--%d---T_L---%d\r\n", R_H,R_L,T_H,T_L);
+        Delay_us(20);
+        num_check=R_H+R_L+T_H+T_L;pc.printf("--num_check--%d---check---%d\r\n", num_check,check);
+        if(num_check==check)
+        {
+            RH=R_H;
+            RL=R_L;
+            TH=T_H;
+            TL=T_L;
+            check=num_check;
+        }
+    }
+    
+    //pc.printf("humidity [H] -out--%d\r\n", RH);
+    //pc.printf("humidity [L] -out--%d\r\n", RL);
+    //pc.printf("temperature [H] -out--%d\r\n", TH);
+    //pc.printf("temperature [L] -out--%d\r\n", TL);
+    
+    receive_dht11Timeout.attach(&receive_dht11,0.2f);
+}