Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: cJSON_lib Common_lib EdpKit_lib
main.cpp
00001 #include "mbed.h" 00002 #include "EdpKit.h" 00003 00004 #define TCPADD "jjfaedp.hedevice.com" 00005 #define TCPPORT 876 00006 #define DEVICE_ID "10443217" 00007 #define API_KEY "s9H5cODRLi8xjfTp2Nw9mZaKFeU=" 00008 00009 // Network interface 00010 NetworkInterface *net; 00011 TCPSocket socket; 00012 Thread thread_recv, thread_ping; 00013 DigitalOut myled1(LED1); 00014 DigitalOut myled2(LED2); 00015 00016 00017 void socket_recv() 00018 { 00019 nsapi_size_or_error_t result; 00020 char *cmdid; 00021 char *req; 00022 uint16 cmdid_len; 00023 uint32 req_len; 00024 uint8 type; 00025 RecvBuffer *recv_buf; 00026 00027 00028 printf("start recv thread.\n"); 00029 00030 while (true) { 00031 recv_buf = NewBuffer(); 00032 result = socket.recv(recv_buf->_data, 256); 00033 if (result < 0) { 00034 printf("Error! socket.recv() returned: %d\n", result); 00035 continue; 00036 } 00037 00038 recv_buf->_write_pos = result; 00039 ReadByte(recv_buf, &type); 00040 00041 if(type == 0xA0) 00042 { 00043 result = UnpackCmdReq(recv_buf, &cmdid, &cmdid_len, &req, &req_len); 00044 00045 if(result == 0) 00046 { 00047 printf("[%.*s]\n", req_len, req); 00048 if(strstr(req, "switch1")) 00049 { 00050 myled1 = req[req_len-1] - 0x30; 00051 } 00052 else if(strstr(req, "switch2")) 00053 { 00054 myled2 = req[req_len-1] - 0x30; 00055 } 00056 00057 } 00058 } 00059 } 00060 } 00061 00062 void socket_ping() 00063 { 00064 EdpPacket* send_pack; 00065 00066 while(1) 00067 { 00068 printf("ping onenet.\n"); 00069 send_pack = PacketPing(); 00070 socket.send(send_pack->_data,send_pack->_write_pos); //send packge to OneNet Cloud 00071 00072 wait(60); 00073 } 00074 } 00075 00076 00077 // Socket demo 00078 int main() { 00079 int remaining; 00080 char *buffer = new char[256]; 00081 EdpPacket* send_pack; 00082 nsapi_size_or_error_t result; 00083 00084 // Bring up the ethernet interface 00085 printf("Mbed OS Socket example\n"); 00086 00087 #ifdef MBED_MAJOR_VERSION 00088 printf("Mbed OS version: %d.%d.%d\n\n", MBED_MAJOR_VERSION, MBED_MINOR_VERSION, MBED_PATCH_VERSION); 00089 #endif 00090 00091 net = NetworkInterface::get_default_instance(); 00092 00093 if (!net) { 00094 printf("Error! No network inteface found.\n"); 00095 return 0; 00096 } 00097 00098 result = net->connect(); 00099 if (result != 0) { 00100 printf("Error! net->connect() returned: %d\n", result); 00101 return result; 00102 } 00103 00104 // Open a socket on the network interface, and create a TCP connection to ifconfig.io 00105 00106 result = socket.open(net); 00107 if (result != 0) { 00108 printf("Error! socket.open() returned: %d\n", result); 00109 } 00110 00111 result = socket.connect(TCPADD, TCPPORT); 00112 if (result != 0) { 00113 printf("Error! socket.connect() returned: %d\n", result); 00114 goto DISCONNECT; 00115 } 00116 00117 printf("now linking to OneNet...\r\n"); 00118 send_pack = PacketConnect1(DEVICE_ID, API_KEY); 00119 result = socket.send(send_pack->_data, send_pack->_write_pos); 00120 if (result < 0) { 00121 printf("Error! socket.send() returned: %d\n", result); 00122 goto DISCONNECT; 00123 } 00124 00125 // Receieve an HTTP response and print out the response line 00126 remaining = 256; 00127 result = socket.recv(buffer, remaining); 00128 if (result < 0) { 00129 printf("Error! socket.recv() returned: %d\n", result); 00130 goto DISCONNECT; 00131 } 00132 00133 if(buffer[3] != 0) { 00134 printf("Error!link to onenet failed!\n"); 00135 goto DISCONNECT; 00136 } 00137 00138 thread_recv.start(socket_recv); 00139 thread_ping.start(socket_ping); 00140 00141 int number = 0; 00142 00143 while(1) { 00144 cJSON *json_data = cJSON_CreateObject(); //create a new json data 00145 00146 scanf("%d", &number); 00147 00148 cJSON_AddNumberToObject(json_data, "number", number); //pack data into json package 00149 send_pack = PacketSavedataJson(DEVICE_ID, json_data, 3); //pack send data into EDP package 00150 cJSON_Delete(json_data); //delete json_data, unless may cause memory leak 00151 00152 printf("send data number = %d\r\n", number); 00153 00154 /* 00155 for(i=0;i<send_pack->_write_pos;i++) 00156 printf("0X%02X ", send_pack->_data[i]); 00157 printf("\n"); 00158 */ 00159 00160 result = socket.send(send_pack->_data,send_pack->_write_pos); //send packge to OneNet Cloud 00161 if (result < 0) { 00162 printf("Error! socket.send() returned: %d\n", result); 00163 goto DISCONNECT; 00164 } 00165 00166 wait(3); 00167 } 00168 00169 delete[] buffer; 00170 00171 DISCONNECT: 00172 // Close the socket to return its memory and bring down the network interface 00173 socket.close(); 00174 00175 // Bring down the ethernet interface 00176 net->disconnect(); 00177 printf("Done\n"); 00178 }
Generated on Fri Jul 15 2022 20:27:30 by
1.7.2