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: MbedJSONValue WIZnetInterface httpServer_with_Ethernt mbed
Fork of Simple_HTTPClient_JSON by
main.cpp
00001 //#include "mbed.h" 00002 //#include "EthernetInterface.h" 00003 //#include "MbedJSONValue.h" 00004 00005 /* 00006 MbedJSONValue parser; 00007 EthernetInterface eth; 00008 TCPSocketConnection sock; 00009 00010 int main() 00011 { 00012 int http_tx_msg_sz=800; 00013 char http_tx_msg[http_tx_msg_sz]; 00014 int http_rx_msg_sz=500; 00015 char http_rx_msg[http_rx_msg_sz]; 00016 int returnCode = 0; 00017 00018 // Enter a MAC address for your controller below. 00019 uint8_t mac_addr[6] = {0x00, 0x08, 0xDC, 0x00, 0x01, 0x02}; 00020 00021 printf("initializing Ethernet\r\n"); 00022 // initializing MAC address 00023 eth.init(mac_addr, "192.168.0.34", "255.255.255.0", "192.168.0.1"); 00024 00025 // Check Ethenret Link 00026 if(eth.link() == true) printf("- Ethernet PHY Link-Done \r\n"); 00027 else printf("- Ethernet PHY Link- Fail\r\n"); 00028 00029 // Start Ethernet connecting: Trying to get an IP address using DHCP 00030 if (eth.connect()<0) printf("Fail - Ethernet Connecing"); 00031 00032 // Print your local IP address: 00033 printf("IP=%s\n\r",eth.getIPAddress()); 00034 printf("MASK=%s\n\r",eth.getNetworkMask()); 00035 printf("GW=%s\n\r",eth.getGateway()); 00036 00037 while(1) { 00038 sock.connect("192.168.0.223", 8000); 00039 00040 snprintf(http_tx_msg, http_tx_msg_sz, "GET / HTTP/1.1\r\nHost: 192.168.0.223:8000\r\nUser-Agent: WIZwiki-W7500ECO\r\nConection: close\r\n\r\n"); 00041 sock.send_all(http_tx_msg, http_tx_msg_sz-1); 00042 00043 while ( (returnCode = sock.receive(http_rx_msg, http_rx_msg_sz-1)) > 0) { 00044 http_rx_msg[returnCode] = '\0'; 00045 printf("Received %d chars from server:\n\r%s\n", returnCode, http_rx_msg); 00046 } 00047 00048 sock.close(); 00049 00050 parse(parser, http_rx_msg); 00051 printf("name =%s\r\n" , parser["name"].get<string>().c_str()); 00052 printf("age =%d\r\n" , parser["age"].get<int>()); 00053 printf("gender =%s\r\n" , parser["gender"].get<string>().c_str()); 00054 00055 wait(10); 00056 } 00057 00058 } 00059 */ 00060 00061 00062 #include "mbed.h" 00063 #include "EthernetInterface.h" 00064 #include "MbedJSONValue.h" 00065 00066 00067 EthernetInterface eth; 00068 TCPSocketConnection sock; 00069 00070 MbedJSONValue WIZwikiREST; 00071 MbedJSONValue GPIOs; 00072 00073 char ip_addr[] = "192.168.0.100"; 00074 char subnet_mask[] = "255.255.255.0"; 00075 char gateway_addr[] = "192.168.0.1"; 00076 00077 int main() { 00078 00079 std::string s; 00080 00081 // Fill the object 00082 WIZwikiREST["Name"] = "WIZwiki-REST-01"; 00083 WIZwikiREST["Network"]["IP"] = ip_addr; 00084 WIZwikiREST["Network"]["SN"] = subnet_mask; 00085 WIZwikiREST["Network"]["GW"] = gateway_addr; 00086 WIZwikiREST["User"]["Name"] = "Lawrence"; 00087 WIZwikiREST["User"]["ID"] = "law"; 00088 WIZwikiREST["User"]["PSWD"] = "law1234"; 00089 GPIOs["P5"]["Mode"] = "DIO"; 00090 GPIOs["P6"]["Mode"] = "DIO"; 00091 WIZwikiREST["GPIOs"] = GPIOs; 00092 00093 00094 // Serialize it into a JSON string 00095 s = WIZwikiREST.serialize(); 00096 printf("json: %s\r\n", s.c_str()); 00097 string item = "Name"; 00098 printf("WIZwikiREST[item] =%s", WIZwikiREST[item].get<string>().c_str()); 00099 00100 00101 int http_tx_msg_sz=800; 00102 char http_tx_msg[http_tx_msg_sz]; 00103 int http_rx_msg_sz=500; 00104 char http_rx_msg[http_rx_msg_sz]; 00105 int returnCode = 0; 00106 00107 // Enter a MAC address for your controller below. 00108 uint8_t mac_addr[6] = {0x00, 0x08, 0xDC, 0x00, 0x01, 0xFE}; 00109 00110 // initializing MAC address 00111 printf("initializing Ethernet\r\n"); 00112 eth.init(mac_addr, ip_addr, subnet_mask, gateway_addr); 00113 00114 // Check Ethernet Link 00115 printf("Check Ethernet Link\r\n"); 00116 while(1) //Wait link up 00117 { 00118 if(eth.link() == true) 00119 break; 00120 } 00121 printf("Link up\r\n"); 00122 00123 // Connect 00124 eth.connect(); 00125 printf("Server IP Address is %s\r\n", eth.getIPAddress()); 00126 00127 if (!svr.start(80, ð)) { 00128 00129 error("Server not starting !"); 00130 exit(0); 00131 } 00132 00133 while(1) { 00134 svr.poll(); 00135 } 00136 00137 /* 00138 while(1) { 00139 sock.connect("192.168.0.223", 8000); 00140 00141 snprintf(http_tx_msg, http_tx_msg_sz, "GET / HTTP/1.1\r\nHost: 192.168.0.223:8000\r\nUser-Agent: WIZwiki-W7500ECO\r\nConection: close\r\n\r\n"); 00142 sock.send_all(http_tx_msg, http_tx_msg_sz-1); 00143 00144 while ( (returnCode = sock.receive(http_rx_msg, http_rx_msg_sz-1)) > 0) { 00145 http_rx_msg[returnCode] = '\0'; 00146 printf("Received %d chars from server:\n\r%s\n", returnCode, http_rx_msg); 00147 } 00148 00149 sock.close(); 00150 00151 parse(parser, http_rx_msg); 00152 printf("name =%s\r\n" , parser["name"].get<string>().c_str()); 00153 printf("age =%d\r\n" , parser["age"].get<int>()); 00154 printf("gender =%s\r\n" , parser["gender"].get<string>().c_str()); 00155 00156 wait(10); 00157 } 00158 */ 00159 00160 } 00161 00162 00163 00164 00165 /** MbedJSONValue class 00166 * 00167 * Example: 00168 * - creation of an MbedJSONValue of type TypeObject containing two others MbedJSONValue: 00169 * -one array of one string and one integer identified by "my_array" 00170 * -a boolean identified by "my_boolean" 00171 * - serialization in JSON format of this object 00172 * @code 00173 * #include "mbed.h" 00174 * #include "MbedJSONValue.h" 00175 * #include <string> 00176 * 00177 * int main() { 00178 * 00179 * MbedJSONValue demo; 00180 * std::string s; 00181 * 00182 * //fill the object 00183 * demo["my_array"][0] = "demo_string"; 00184 * demo["my_array"][1] = 10; 00185 * demo["my_boolean"] = false; 00186 * 00187 * //serialize it into a JSON string 00188 * s = demo.serialize(); 00189 * printf("json: %s\r\n", s.c_str()); 00190 * } 00191 * 00192 * @endcode 00193 * 00194 * Example: 00195 * - creation of an MbedJSONValue from a JSON string 00196 * - extraction of different values from this existing MbedJSONValue 00197 * @code 00198 * #include "mbed.h" 00199 * #include "MbedJSONValue.h" 00200 * #include <string> 00201 * 00202 * int main() { 00203 * MbedJSONValue demo; 00204 * 00205 * const char * json = "{\"my_array\": [\"demo_string\", 10], \"my_boolean\": true}"; 00206 * 00207 * //parse the previous string and fill the object demo 00208 * parse(demo, json); 00209 * 00210 * std::string my_str; 00211 * int my_int; 00212 * bool my_bool; 00213 * 00214 * my_str = demo["my_array"][0].get<std::string>(); 00215 * my_int = demo["my_array"][1].get<int>(); 00216 * my_bool = demo["my_boolean"].get<bool>(); 00217 * 00218 * printf("my_str: %s\r\n", my_str.c_str()); 00219 * printf("my_int: %d\r\n", my_int); 00220 * printf("my_bool: %s\r\n", my_bool ? "true" : "false"); 00221 * } 00222 * @endcode 00223 */
Generated on Tue Jul 12 2022 21:46:35 by
1.7.2
