
Simple HTTP Client with JSON parser
Dependencies: MbedJSONValue WIZnetInterface mbed
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 example shows how to use HTTPClient parse Json data in ARMmbed platform.
main.cpp@0:e6cc33c4970b, 2015-10-29 (annotated)
- Committer:
- embeddist
- Date:
- Thu Oct 29 08:00:44 2015 +0000
- Revision:
- 0:e6cc33c4970b
Initial Commit.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
embeddist | 0:e6cc33c4970b | 1 | #include "mbed.h" |
embeddist | 0:e6cc33c4970b | 2 | #include "EthernetInterface.h" |
embeddist | 0:e6cc33c4970b | 3 | #include "MbedJSONValue.h" |
embeddist | 0:e6cc33c4970b | 4 | |
embeddist | 0:e6cc33c4970b | 5 | MbedJSONValue parser; |
embeddist | 0:e6cc33c4970b | 6 | EthernetInterface eth; |
embeddist | 0:e6cc33c4970b | 7 | TCPSocketConnection sock; |
embeddist | 0:e6cc33c4970b | 8 | |
embeddist | 0:e6cc33c4970b | 9 | int main() |
embeddist | 0:e6cc33c4970b | 10 | { |
embeddist | 0:e6cc33c4970b | 11 | int http_tx_msg_sz=800; |
embeddist | 0:e6cc33c4970b | 12 | char http_tx_msg[http_tx_msg_sz]; |
embeddist | 0:e6cc33c4970b | 13 | int http_rx_msg_sz=500; |
embeddist | 0:e6cc33c4970b | 14 | char http_rx_msg[http_rx_msg_sz]; |
embeddist | 0:e6cc33c4970b | 15 | int returnCode = 0; |
embeddist | 0:e6cc33c4970b | 16 | |
embeddist | 0:e6cc33c4970b | 17 | // Enter a MAC address for your controller below. |
embeddist | 0:e6cc33c4970b | 18 | uint8_t mac_addr[6] = {0x00, 0x08, 0xDC, 0x00, 0x01, 0x02}; |
embeddist | 0:e6cc33c4970b | 19 | |
embeddist | 0:e6cc33c4970b | 20 | printf("initializing Ethernet\r\n"); |
embeddist | 0:e6cc33c4970b | 21 | // initializing MAC address |
embeddist | 0:e6cc33c4970b | 22 | eth.init(mac_addr, "192.168.0.34", "255.255.255.0", "192.168.0.1"); |
embeddist | 0:e6cc33c4970b | 23 | |
embeddist | 0:e6cc33c4970b | 24 | // Check Ethenret Link |
embeddist | 0:e6cc33c4970b | 25 | if(eth.link() == true) printf("- Ethernet PHY Link-Done \r\n"); |
embeddist | 0:e6cc33c4970b | 26 | else printf("- Ethernet PHY Link- Fail\r\n"); |
embeddist | 0:e6cc33c4970b | 27 | |
embeddist | 0:e6cc33c4970b | 28 | // Start Ethernet connecting: Trying to get an IP address using DHCP |
embeddist | 0:e6cc33c4970b | 29 | if (eth.connect()<0) printf("Fail - Ethernet Connecing"); |
embeddist | 0:e6cc33c4970b | 30 | |
embeddist | 0:e6cc33c4970b | 31 | // Print your local IP address: |
embeddist | 0:e6cc33c4970b | 32 | printf("IP=%s\n\r",eth.getIPAddress()); |
embeddist | 0:e6cc33c4970b | 33 | printf("MASK=%s\n\r",eth.getNetworkMask()); |
embeddist | 0:e6cc33c4970b | 34 | printf("GW=%s\n\r",eth.getGateway()); |
embeddist | 0:e6cc33c4970b | 35 | |
embeddist | 0:e6cc33c4970b | 36 | while(1) { |
embeddist | 0:e6cc33c4970b | 37 | sock.connect("192.168.0.223", 8000); |
embeddist | 0:e6cc33c4970b | 38 | |
embeddist | 0:e6cc33c4970b | 39 | 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"); |
embeddist | 0:e6cc33c4970b | 40 | sock.send_all(http_tx_msg, http_tx_msg_sz-1); |
embeddist | 0:e6cc33c4970b | 41 | |
embeddist | 0:e6cc33c4970b | 42 | while ( (returnCode = sock.receive(http_rx_msg, http_rx_msg_sz-1)) > 0) { |
embeddist | 0:e6cc33c4970b | 43 | http_rx_msg[returnCode] = '\0'; |
embeddist | 0:e6cc33c4970b | 44 | printf("Received %d chars from server:\n\r%s\n", returnCode, http_rx_msg); |
embeddist | 0:e6cc33c4970b | 45 | } |
embeddist | 0:e6cc33c4970b | 46 | |
embeddist | 0:e6cc33c4970b | 47 | sock.close(); |
embeddist | 0:e6cc33c4970b | 48 | |
embeddist | 0:e6cc33c4970b | 49 | parse(parser, http_rx_msg); |
embeddist | 0:e6cc33c4970b | 50 | printf("name =%s\r\n" , parser["name"].get<string>().c_str()); |
embeddist | 0:e6cc33c4970b | 51 | printf("age =%d\r\n" , parser["age"].get<int>()); |
embeddist | 0:e6cc33c4970b | 52 | printf("gender =%s\r\n" , parser["gender"].get<string>().c_str()); |
embeddist | 0:e6cc33c4970b | 53 | |
embeddist | 0:e6cc33c4970b | 54 | wait(10); |
embeddist | 0:e6cc33c4970b | 55 | } |
embeddist | 0:e6cc33c4970b | 56 | |
embeddist | 0:e6cc33c4970b | 57 | } |