nucleo f767zi onenet edp test

Dependencies:   cJSON_lib Common_lib EdpKit_lib

Files at this revision

API Documentation at this revision

Comitter:
ws1992108
Date:
Wed Apr 08 04:01:46 2020 +0000
Parent:
75:1e3673a86f39
Commit message:
by ws

Changed in this revision

Common_lib.lib Show annotated file Show diff for this revision Revisions of this file
EdpKit_lib.lib Show annotated file Show diff for this revision Revisions of this file
cJSON_lib.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 1e3673a86f39 -r f524071998a0 Common_lib.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Common_lib.lib	Wed Apr 08 04:01:46 2020 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/TaylorGy/code/Common_lib/#ea00e3a8793b
diff -r 1e3673a86f39 -r f524071998a0 EdpKit_lib.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/EdpKit_lib.lib	Wed Apr 08 04:01:46 2020 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/ws1992108/code/EdpKit_lib/#bfbe3a09a808
diff -r 1e3673a86f39 -r f524071998a0 cJSON_lib.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/cJSON_lib.lib	Wed Apr 08 04:01:46 2020 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/TaylorGy/code/cJSON_lib/#c809010834e2
diff -r 1e3673a86f39 -r f524071998a0 main.cpp
--- a/main.cpp	Thu Sep 12 16:00:11 2019 +0100
+++ b/main.cpp	Wed Apr 08 04:01:46 2020 +0000
@@ -1,14 +1,84 @@
 #include "mbed.h"
+#include "EdpKit.h"
+
+#define     TCPADD     "jjfaedp.hedevice.com"
+#define     TCPPORT    876
+#define 	DEVICE_ID	"10443217"
+#define		API_KEY		"s9H5cODRLi8xjfTp2Nw9mZaKFeU="
 
 // Network interface
 NetworkInterface *net;
+TCPSocket socket;
+Thread thread_recv, thread_ping;
+DigitalOut myled1(LED1);
+DigitalOut myled2(LED2);
+
+
+void socket_recv()
+{
+    nsapi_size_or_error_t result;
+	char *cmdid;
+    char *req;
+    uint16 cmdid_len;
+    uint32 req_len;
+    uint8 type;
+    RecvBuffer *recv_buf;
+    
+    
+    printf("start recv thread.\n");
+    
+    while (true) {
+        recv_buf = NewBuffer();
+        result = socket.recv(recv_buf->_data, 256);
+	    if (result < 0) {
+	        printf("Error! socket.recv() returned: %d\n", result);
+			continue;
+	    }
+	    
+	    recv_buf->_write_pos = result;
+	    ReadByte(recv_buf, &type);
+	    
+	    if(type == 0xA0)
+	    {
+		    result = UnpackCmdReq(recv_buf, &cmdid, &cmdid_len, &req, &req_len);
+		    
+		    if(result == 0)
+		    {
+		    	printf("[%.*s]\n", req_len, req);
+		    	if(strstr(req, "switch1"))
+		    	{
+		    		myled1 = req[req_len-1] - 0x30;
+		    	}
+		    	else if(strstr(req, "switch2"))
+		    	{
+		    		myled2 = req[req_len-1] - 0x30;
+		    	}
+		    	
+		    }
+	    }
+    }
+}
+
+void socket_ping()
+{
+	EdpPacket* send_pack;
+	
+	while(1)
+	{
+		printf("ping onenet.\n");
+		send_pack = PacketPing();
+		socket.send(send_pack->_data,send_pack->_write_pos);   //send packge to OneNet Cloud
+		
+		wait(60);
+	}
+}
+
 
 // Socket demo
 int main() {
     int remaining;
-    int rcount;
-    char *p;
     char *buffer = new char[256];
+    EdpPacket* send_pack;
     nsapi_size_or_error_t result;
 
     // Bring up the ethernet interface
@@ -31,60 +101,73 @@
         return result;
     }
 
-    // Show the network address
-    const char *ip = net->get_ip_address();
-    const char *netmask = net->get_netmask();
-    const char *gateway = net->get_gateway();
-    printf("IP address: %s\n", ip ? ip : "None");
-    printf("Netmask: %s\n", netmask ? netmask : "None");
-    printf("Gateway: %s\n", gateway ? gateway : "None");
-
     // Open a socket on the network interface, and create a TCP connection to ifconfig.io
-    TCPSocket socket;
-    // Send a simple http request
-    char sbuffer[] = "GET / HTTP/1.1\r\nHost: ifconfig.io\r\nConnection: close\r\n\r\n";
-    nsapi_size_t size = strlen(sbuffer);
 
     result = socket.open(net);
     if (result != 0) {
         printf("Error! socket.open() returned: %d\n", result);
     }
-
-    result = socket.connect("ifconfig.io", 80);
+    
+    result = socket.connect(TCPADD, TCPPORT);
     if (result != 0) {
         printf("Error! socket.connect() returned: %d\n", result);
         goto DISCONNECT;
     }
 
-    // Loop until whole request sent
-    while(size) {
-        result = socket.send(sbuffer+result, size);
-        if (result < 0) {
-            printf("Error! socket.send() returned: %d\n", result);
-            goto DISCONNECT;
-        }
-        size -= result;
-        printf("sent %d [%.*s]\n", result, strstr(sbuffer, "\r\n")-sbuffer, sbuffer);
+	printf("now linking to OneNet...\r\n");
+    send_pack = PacketConnect1(DEVICE_ID, API_KEY);
+    result = socket.send(send_pack->_data, send_pack->_write_pos);
+	if (result < 0) {
+        printf("Error! socket.send() returned: %d\n", result);
+        goto DISCONNECT;
     }
-
+	
     // Receieve an HTTP response and print out the response line
     remaining = 256;
-    rcount = 0;
-    p = buffer;
-    while (remaining > 0 && 0 < (result = socket.recv(p, remaining))) {
-        p += result;
-        rcount += result;
-        remaining -= result;
-    }
+    result = socket.recv(buffer, remaining);
     if (result < 0) {
         printf("Error! socket.recv() returned: %d\n", result);
         goto DISCONNECT;
     }
-	// the HTTP response code
-    printf("recv %d [%.*s]\n", rcount, strstr(buffer, "\r\n")-buffer, buffer);
-
+    
+    if(buffer[3] != 0) {
+    	printf("Error!link to onenet failed!\n");
+    	goto DISCONNECT;
+    }
+    
+    thread_recv.start(socket_recv);
+    thread_ping.start(socket_ping);
+    
+    int number = 0;
+    
+    while(1) {
+    	cJSON *json_data = cJSON_CreateObject();  //create a new json data
+    	
+    	scanf("%d", &number);
+    	
+    	cJSON_AddNumberToObject(json_data, "number", number); //pack data into json package
+	    send_pack = PacketSavedataJson(DEVICE_ID, json_data, 3);  //pack send data into EDP package
+	    cJSON_Delete(json_data);   //delete json_data, unless may cause memory leak  
+	    
+	    printf("send data number = %d\r\n", number);
+	    
+	    /*
+	    for(i=0;i<send_pack->_write_pos;i++)
+	    	printf("0X%02X ", send_pack->_data[i]);
+	    printf("\n");
+	    */
+	    
+	    result = socket.send(send_pack->_data,send_pack->_write_pos);   //send packge to OneNet Cloud
+	    if (result < 0) {
+	        printf("Error! socket.send() returned: %d\n", result);
+	        goto DISCONNECT;
+	    }
+	    
+	    wait(3);
+    }
+    
     delete[] buffer;
-
+    
 DISCONNECT:
     // Close the socket to return its memory and bring down the network interface
     socket.close();