JSON is easy for machines to parse and generate which is based on a subset of the JavaScript Programming Language. Currently many Web Services allow to access data in JSON format. However, JSON parser is too big for low-end device as like a ARMmbed platform which has limited-resource. This post shows how to use HTTPClient parse Json data in ARMmbed platform.
//embedr.flickr.com/assets/client-code.js
Preparation materials
- Software
* JSON Parser: MbedJSONValue libs
* Ethernet Networking : WIZnetInterface
* HTTP Server with JSON : Fraka6 Blog – No Free Lunch: The simplest python server example 😉
2. Hardware
* WIZwiki-W7500ECONET: WIZwiki-W7500 + ECO Shield Ethernet
//embedr.flickr.com/assets/client-code.js
-
ARMmbed Board: WIZwiki-W7500ECO
//embedr.flickr.com/assets/client-code.js -
Ethernet Shield: ECO Shield Ethernet
//embedr.flickr.com/assets/client-code.js
Simplest python JSON server on PC
-
Reference : fraka6.blogsopot.kr: The simplest python server example
-
Modify Server IP address & port on ron_handler
def run(port=8000): #set port print('http server is starting...') #ip and port of server #server_address = ('127.0.0.1', port) server_address = ('192.168.0.223', port)#set port httpd = HTTPServer(server_address, Handler) print('http server is running...listening on port %s' %port) httpd.serve_forever()
- Modify JSON form in do_GET handler
#handle GET command def do_GET(self): if format == 'html': self.send_response(200) self.send_header("Content-type", "text/plain") self.send_header('Content-type','text-html') self.end_headers() self.wfile.write("body") elif format == 'json': #self.request.sendall(json.dumps({'path':self.path})) #self.request.sendall(json.dumps({'pi':3.14})) self.request.sendall(json.dumps({'name':'John snow', 'age': 30, 'gender':'male'})) else: self.request.sendall("%s\t%s" %('path', self.path)) return
-
Excute json_server
//embedr.flickr.com/assets/client-code.js -
Test by using curl
$>curl ip_address:port_number
//embedr.flickr.com/assets/client-code.js
Make main.cc
-
Source Code@https://developer.mbed.org/users/embeddist/code/Simple_HTTPClient_JSON/wiki/Homepage
-
Network Configration for WIZwiki-W7500ECO
// Enter a MAC address for your controller below. uint8_t mac_addr[6] = {0x00, 0x08, 0xDC, 0x00, 0x01, 0x02}; printf("initializing Ethernet\r\n"); // initializing MAC address eth.init(mac_addr, "192.168.0.34", "255.255.255.0", "192.168.0.1"); // Check Ethenret Link if(eth.link() == true) printf("- Ethernet PHY Link-Done \r\n"); else printf("- Ethernet PHY Link- Fail\r\n"); // Start Ethernet connecting: Trying to get an IP address using DHCP if (eth.connect() 0) { http_rx_msg[returnCode] = '\0'; printf("Received %d chars from server:\n\r%s\n", returnCode, http_rx_msg); }
- Do JSON parse
parse(parser, http_rx_msg);
- Output the parsed data
// parsing "string" in string type printf("name =%s\r\n" , parser["name"].get().c_str()); // parsing "age" in integer type printf("age =%d\r\n" , parser["age"].get()); // parsing "gender" in string type printf("gender =%s\r\n" , parser["gender"].get().c_str());
Demo. HTTPClient with JSON parser
- Network config.
- Confirm Received JSON data
- Print-out the pasing data
//embedr.flickr.com/assets/client-code.js
Packet Capture
- TCP-Connection
- Target board sends GET Form to simple python JSON server
- simple python JSON server sends JSON data to Target board
//embedr.flickr.com/assets/client-code.js