yangyang

Dependencies:   WIZnetInterface mbed

Fork of espyun1 by jiang hao

Files at this revision

API Documentation at this revision

Comitter:
846354866
Date:
Wed Nov 09 09:25:49 2016 +0000
Parent:
6:bc4901a948b1
Commit message:
sensor iot for espressif(no chinese) version-0.1

Changed in this revision

WIZnetInterface.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r bc4901a948b1 -r bda2a6a118f7 WIZnetInterface.lib
--- a/WIZnetInterface.lib	Sat Nov 05 07:50:59 2016 +0000
+++ b/WIZnetInterface.lib	Wed Nov 09 09:25:49 2016 +0000
@@ -1,1 +1,1 @@
-https://developer.mbed.org/users/846354866/code/WIZnetInterface/#f6a5f6d1978b
+https://developer.mbed.org/users/846354866/code/WIZnetInterface/#26fc4b6c5784
diff -r bc4901a948b1 -r bda2a6a118f7 main.cpp
--- a/main.cpp	Sat Nov 05 07:50:59 2016 +0000
+++ b/main.cpp	Wed Nov 09 09:25:49 2016 +0000
@@ -3,6 +3,7 @@
 #include "cJSON.h"
 
 DigitalInOut myIOD10(D10);
+DigitalIn myInD11(D11);
 DigitalOut myOutD12(D12);
 AnalogIn myInA1(A1);
 int socketPort = 8000;
@@ -10,17 +11,17 @@
 
 Serial pc(PA_13,PA_14);
 
-char path[]="/v1/datastreams/test/datapoint/";
-
 int MallocSize = 800*sizeof(char);
-bool socket_GET(TCPSocketConnection &conn);
+//bool socket_GET(TCPSocketConnection &conn);
 bool http_GET(TCPSocketConnection &conn);
 bool http_POST(TCPSocketConnection &conn,int vall);
-bool socket_POST(TCPSocketConnection &conn,int vall);
-bool closeConnect(TCPSocketConnection &conn);
+//bool socket_POST(TCPSocketConnection &conn,int vall);
+//bool closeConnect(TCPSocketConnection &conn);
 void receive_dht11();
 unsigned char receive_byte();
+void button_led();
 Timeout receive_dht11Timeout;
+Timeout button_ledTimeout;
 
 void delay(unsigned char ms)
 { 
@@ -43,31 +44,33 @@
 unsigned int  U8FLAG;
 static unsigned char num = 4;
 const char receive_module[4][5] = {"Led", "Gas", "Hum", "Temp", };
-
+#if 1
 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);
+    button_ledTimeout.attach(&button_led,0.1f);
     
     EthernetInterface eth;
     eth.init(mac_addr); //Use DHCP
     eth.connect();
     pc.printf("start IP Address is %s\r\n", eth.getIPAddress());
-    /*
+    
     do{
         phy_link = eth.ethernet_link();
         pc.printf("...");
+        pc.printf("phy_link=%d...", phy_link);
         wait(2);
     }while(!phy_link);
     printf("\r\n");
-    */
+    
     pc.printf("IP Address is %s\r\n", eth.getIPAddress());
 
     TCPSocketConnection conn;
-    
     while(true)
     {   
+        //wait(0.5f);
         if(num)
         {
             if (num == 4)
@@ -75,47 +78,77 @@
             else if (num == 3)
                 val = RH;
             else if (num == 2)
-                val = myInA1.read();
+                val = myInA1.read_u16();
             else if (num == 1)
                 val = myOutD12.read();
         }
-        //int mathtest = ceil(val);//math.h
-        pc.printf("send_data=%d\r\n",val);
+        //int mathtest = ceil(val);//math.
         pc.printf("start http_post\r\n");
         wait(1);
-        if(http_POST(conn, val))
-        {
-            if(!conn.close()){//短连接,断开一次连接一次
-                pc.printf("conn.close OK\r\n");    
-            }else{
-                pc.printf("conn.close ERR\r\n"); 
-                break;   
-            }
-        }else{
-             if(out)break;   
-             pc.printf("connect close err!!!to do something\r\n");
+        
+        conn.connect("iot.espressif.cn", httpPort);
 
-        }
+        http_POST(conn, val);
         
+        conn.close();
         val = 0.0;  
         //mathtest = 0; 
         
         if(out)break;
     }
-                
+    
+    //conn.close();            
+    eth.disconnect();
+    pc.printf("out the program!\r\n");
+    return 0;                     
+}
+#else
+
+int main() { 
+    pc.printf("Wait a second...\r\n");
+    uint8_t mac_addr[6] = {0x78, 0x08, 0xDC, 0x1c, 0xa8, 0x95}; 
+    
+    EthernetInterface eth;
+    eth.init(mac_addr); //Use DHCP
+    eth.connect();
+    pc.printf("start IP Address is %s\r\n", eth.getIPAddress());
+    eth.ethernet_link();
+    pc.printf("IP Address is %s\r\n", eth.getIPAddress());
+
+    TCPSocketConnection conn;
+    
+    
+    while(true)
+    {   
+        int ret = 0;
+        pc.printf("start http_post\r\n");
+        //ret = conn.connect("192.168.1.41", 10086);
+        ret = conn.connect("iot.espressif.cn", httpPort);
+        
+        pc.printf("connect ret=%d\n",ret);
+        ret = conn.send_all("asdfghh", 7);//http_cmd for http,pJSON for 
+        pc.printf("send ret=%d\n",ret); 
+        wait(1.0f);
+        ret = conn.close();
+        pc.printf("close ret=%d\n",ret);  
+        wait(1.0f);
+    }
+    
+           
     eth.disconnect();
     pc.printf("out the program!\r\n");
     return 0;                     
 }
 
+#endif
 /*http POST*/
 bool http_POST(TCPSocketConnection &conn, int val)
 {
-    pc.printf("1111\r\n");
+    pc.printf("%s():%d\n",__FUNCTION__, __LINE__);
     //wait(0.5);
-    if(conn.is_connected()){
+    /*if(conn.is_connected()){
         pc.printf("connect is already ok!\r\n");    
-    }else{
+    }else{pc.printf("%s():%d\n",__FUNCTION__, __LINE__);
         if(!conn.connect("iot.espressif.cn", httpPort)) //80 for http,8000 for socket
             pc.printf("connect OK!\r\n");
         else{
@@ -123,8 +156,8 @@
             out = true;
             return false;    
         }
-    }
-    pc.printf("2222\r\n");
+    }*///conn.connect("iot.espressif.cn", httpPort);
+    
     cJSON* http_post=NULL;  
     http_post = cJSON_CreateObject();    
     if(NULL == http_post)
@@ -133,7 +166,7 @@
          out = true;
          return false;
     }
-    pc.printf("3333\r\n");
+    
     cJSON* http_post_json=NULL;  
     http_post_json = cJSON_CreateObject();    
     if(NULL == http_post_json)
@@ -148,7 +181,7 @@
     //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);
@@ -164,7 +197,7 @@
     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"\
+    sprintf(http_POST_cmd,"POST /v1/datastreams/%s/datapoint/ HTTP/1.1\r\nUser-Agent: curl/7.35.0\r\n"\
                       "Host:iot.espressif.cn\r\nAccept: */*\r\nAuthorization: token b1e92cf17fe4482647924941949ab2b07ffab548\r\n"\
                       "Content-Length: ", receive_module[num-1]);
     num--;
@@ -180,16 +213,63 @@
     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);pc.printf("%s():%d\n",__FUNCTION__, __LINE__);
+        ret = conn.receive_all(buffer, MallocSize-1);pc.printf("%s():%d\n",__FUNCTION__, __LINE__);
+        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);
+    
+    return true;
+}
+
+/*http_GET*/
+bool http_GET(TCPSocketConnection &conn)
+{
+    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;    
+        }
+    }
+    
+    char http_cmd[] = "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"\
+                      "Host:iot.espressif.cn\r\nAccept: */*\r\nAuthorization: token b1e92cf17fe4482647924941949ab2b07ffab548\r\n\r\n";
+    
+    conn.send_all(http_cmd, strlen(http_cmd));//http_cmd for http,pJSON for socket,should be three changes
+    pc.printf("%s\r\n",http_cmd);
+    
+    //char *buffer = NULL;
+    //buffer = (char *)malloc(MallocSize);
+    //if(!buffer)pc.printf("malloc buffer err\r\n");
+    char buffer[500];
     int ret;
-    pc.printf("6666\r\n");
+
     while (true) {
         wait(1);
-        ret = conn.receive(buffer, MallocSize-1);
+        ret = conn.receive(buffer, 500-1);
         pc.printf("ret = %d\r\n",ret);
         if (ret <= 0) {
             break;
@@ -197,12 +277,15 @@
         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");
+    pc.printf("http_GET OUT\r\n");
+    /* 
+    if(conn.close()){
+        pc.printf("conn.close OK\r\n");    
+    }else{
+        pc.printf("conn.close ERR\r\n");    
+    }
+    */
+    //free(buffer);
     return true;
 }
 
@@ -270,3 +353,18 @@
     
     receive_dht11Timeout.attach(&receive_dht11,0.2f);
 }
+
+void button_led()
+{  
+    while(myInD11.read())
+    {
+        wait(0.01f);
+        if(!myInD11.read())
+        {
+            myOutD12.write(!myOutD12.read());
+            break;
+        }
+    }
+    
+    button_ledTimeout.attach(&button_led,0.1f);
+}