[WIZwiki_W7500ECO] HTTPClient with JSON parser

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.
IMG_20151029_165139//embedr.flickr.com/assets/client-code.js

Preparation materials

  1. 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
IMG_20151029_164448//embedr.flickr.com/assets/client-code.js

Simplest python JSON server on PC

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

Make main.cc

// 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

  1. Network config.
  2. Confirm Received JSON data
  3. Print-out the pasing data
    2015-10-29_16-42-55//embedr.flickr.com/assets/client-code.js

Packet Capture

  1. TCP-Connection
  2. Target board sends GET Form to simple python JSON server
  3. simple python JSON server sends JSON data to Target board
    2015-10-29_16-22-56//embedr.flickr.com/assets/client-code.js

WIZnetInterface for ARMmbed

This is WIZnet Ethernet Interface using Hardware TCP/IP chip, W5500 and TCP/IP Offload Engine, W7500.
Users » embeddist » Code » WIZnetInterface
-> WIZnetInterface Lib will be released on Team WIZnet

This is an Ethernet Interface library port-based on EthernetInterface. This is where the driver using TCP/IP offload(W5500/W7500), which is a market-proven hardwired TCP/IP stack, is implemented. Therefore, this library does not need lwip-eth.library.

  • The Socket folder contains files that implement the SocketAPI and Protocols as like DHCP and DNS.
  • The arch folder contains files that implement the driver for W5500 and W7500x_TOE.
  • The EthernetInterface.c/.h implement the functions from SocketAPI/EthernetInterface.h
  • The eth_arch.h implement to select TCP/IP TOE depending on platform.

What is new?

  • eth_arch.h
    The eth_arch.h file is added to select arch depending to Target platform, we used define of TARGET_platform.
#if defined(TARGET_WIZwiki_W7500)
#include "W7500x_toe.h"
#define __DEF_USED_IC101AG__  //For using IC+101AG@WIZwiki-W7500
#else
#include "W5500.h"            // W5500 Ethernet Shield 
//#define USE_WIZ550IO_MAC    // WIZ550io; using the MAC address
#endif
  • link()
    The link function is added to check Ethernet link (PHY) up or not.
    * Check if an ethernet link is pressent or not.
    *
    * @returns true if successful
    */
    bool link(int wait_time_ms= 3*1000);
  • link_set()
    The set_link function is added to check Ethernet link (PHY) up or not.
   /*
    * Sets the speed and duplex parameters of an ethernet link.
    *
    * @returns true if successful
    */
    void set_link(PHYMode phymode);
  • Included DHCP and DNS lib
    DHCP and DNS lib moved in Socket folder.
    Included DHCP&DNS

How to import

  • import and update
    • Right Click and click ‘From Import Wizard’ to import WIZnetInterface Library
      import library
    • In import Wizard, input ‘WIZnetInterfae” in search box and click ‘Search’ button. Click ‘WIZnetInterface’ in search result window and click ‘Import’ button.
      Search WIZnetInterface
    • Set ‘Import name’ and ‘Target path’, check ‘update’
      import and update WIZnetInterface

Where is Clone repository

hg clone https://embeddist@developer.mbed.org/users/embeddist/code/WIZnetInterface/

How to use

  • make main.cpp
    • WIZwiki_W7500
    #define _DHCP_
    EthernetInterface eth;  /*1. Creat eth object from EthernetInteface class*/
    
    main()
    {
        uint8_t mac_addr[6] = {0x00, 0x08, 0xDC, 0x01, 0x02, 0x03};  
    
        /*2. Set MAC, IP, Gatway address and Subnet Mask*/
    #ifdef _DHCP_
        /*2.1 Set  MAC address, Initialize the interface with DHCP*/
        eth.init(mac_addr); 
    #else   
        /*2.2 Set  MAC address and Set MAC, IP, Gatway address and Subnet Mask*/
        eth.init(mac_addr, "192.168.77.34", "255.255.255.0", "192.168.77.1"); 
    #endif
    
        /*3. Check Ethernet Link-Done */
        printf("Check Ethernet Link\r\n");
        if(eth.link() == true) { printf("- Ethernet PHY Link-Done \r\n"); }
        else {printf("- Ethernet PHY Link- Fail\r\n");}
    
        /*4. Set IP addresses ,start DHCP if needed  */
        eth.connect();
        printf("Connected, IP: %s\n\r", eth.getIPAddress());
        printf("MASK: %s\n\r", eth.getNetworkMask());
        printf("GW: %s\n\r",eth.getGateway());
        ...
    
        /* Your application 
           Visit for examples - https://developer.mbed.org/teams/WIZnet/
        */
    
    }
    
    • W5500 Ethernet Shield
    #define _DHCP_
    /* 0. Set SPI Interface with SPI API*/
    SPI spi(D11, D12, D13);                  // mosi, miso, sclk
    /*1. Creat eth object from EthernetInteface class*/
    EthernetInterface eth(&spi, D10, D9);    // spi, cs, reset
    
    main()
    {
        uint8_t mac_addr[6] = {0x00, 0x08, 0xDC, 0x1D, 0x62, 0x11}; 
        /*2. Set MAC, IP, Gatway address and Subnet Mask*/
    #ifdef _DHCP_
        /*2.1 Set  MAC address, Initialize the interface with DHCP*/
        eth.init(mac_addr); 
    #else
        /*2.2 Set  MAC address and Set MAC, IP, Gatway address and Subnet Mask */
        eth.init(mac_addr, "192.168.77.34", "255.255.255.0", "192.168.77.1"); 
    #endif
    
        /*3. Check Ethernet Link-Done */
        printf("Check Ethernet Link\r\n");
        if(eth.link() == true) { printf("- Ethernet PHY Link-Done \r\n"); }
        else {printf("- Ethernet PHY Link- Fail\r\n");}
    
        /*4. Set IP addresses ,start DHCP if needed  */
        eth.connect();
        printf("Connected, IP: %s\n\r", eth.getIPAddress());
        printf("MASK: %s\n\r", eth.getNetworkMask());
        printf("GW: %s\n\r",eth.getGateway());
        ...
    
        /* Your application 
           Visit for examples - https://developer.mbed.org/teams/WIZnet/
        */
    
    }
    

WIZnetInterface Implementations for mbed Ethenret Interface

For networking based on Ethernet network, Ethenret Interface library is provided and is composed TCP/IP Protocol layer, Ethernet, EthernetInterface and Socket. In other words, the EthernetInterface library includes the networking stack necessary for connect betwwen mbed platform and Internet.

Each layer in EthernetInterface provides APIs to connect to the internet.
* EthernetInterfaec : https://developer.mbed.org/handbook/Ethernet-Interface
* Socket : https://developer.mbed.org/handbook/Socket
* TCP/IP Protocols : https://developer.mbed.org/handbook/TCP-IP-protocols-and-APIs
* Ethernet : https://developer.mbed.org/handbook/Ethernet

WIZnetInterface Implementation base on mbed Ethernet Interface

  • EthernetInterface- EthernetInterface Class
    Type Func. Descriptions WIZnetInterface Support
    static int init () Initialize the interface with DHCP. O
    static int init (const char *ip, const char *mask, const char *gateway) Initialize the interface with a static IP address. O
    static int connect (unsigned int timeout_ms=15000) Connect Bring the interface up, start DHCP if needed. O
    static int disconnect () Disconnect Bring the interface down. X
    static char* getMACAddress () Get the MAC address of your Ethernet interface. O
    static char* getIPAddress () Get the IP address of your Ethernet interface. O
    static char* getGateway () Get the Gateway address of your Ethernet interface. O
    static char* getNetworkMask () Get the Network mask of your Ethernet interface. O
    void EthernetInterface (PinName mosi, PinName miso, PinName sclk, PinName cs, PinName reset) Initialize SPI SPI pins to user for SPI interface and Reset pin for W5500 0 (for W5500)
    void EthernetInterface (SPI* spi, PinName cs, PinName reset) Initialize SPI SPI pins to user for SPI interface and Reset pin for W5500 O (for W5500)
  • Socket – TCPSocketServer Class

    Type Func. Descriptions WIZnetInterface Support
    TCPSocketServer () Instantiate a TCP Server. O
    int bind (int port) Bind a socket to a specific port. O
    int listen (int backlog=1) Start listening for incoming connections. O
    int accept ( TCPSocketConnection &connection) Accept a new connection. O
    void set_blocking (bool blocking, unsigned int timeout=1500) Set blocking or non-blocking mode of the socket and a timeout on blocking socket operations. O
    int set_option (int level, int optname, const void *optval, socklen_t optlen) Set socket options. X
    int get_option (int level, int optname, void *optval, socklen_t *optlen) Get socket options. X
    int close (bool shutdown=true) Get socket options. O
  • Socket – TCPSocketConnection Class

    Type Func. Descriptions WIZnetInterface Support
    TCPSocketConnection () TCP socket connection. O
    int connect (const char *host, const int port) Connects this TCP socket to the server. O
    bool is_connected (void) Check if the socket is connected. O
    int send (char *data, int length) Send data to the remote host. O
    int send_all (char *data, int length) Send all the data to the remote host. O
    int receive (char *data, int length) Receive data from the remote host. O
    int receive_all (char *data, int length) Receive all the data from the remote host. O
    void set_blocking (bool blocking, unsigned int timeout=1500) Set blocking or non-blocking mode of the socket and a timeout on blocking socket operations. O
    int set_option (int level, int optname, const void *optval, socklen_t optlen) Set socket options. X
    int get_option (int level, int optname, void *optval, socklen_t *optlen) Get socket options. X
    int close (bool shutdown=true) Close the socket. O
    void reset_address (void) Reset the address of this endpoint. O
    int set_address (const char *host, const int port) Set the address of this endpoint. O
    char* get_address (void) Get the IP address of this endpoint. O
    int get_port (void) Get the port of this endpoint. O
  • etnerhet_api – ethernet_api Class

    Type Func. Descriptions WIZnetInterface Support
    Ethernet () Initialise the ethernet interface. X
    virtual ~Ethernet () Powers the hardware down. X
    int write (const char *data, int size) Writes into an outgoing ethernet packet. X
    int send () Send an outgoing ethernet packet. X
    int receive () Recevies an arrived ethernet packet. X
    int read (const char *data, int size) Read from an recevied ethernet packet. X
    void address (char *mac) Gives the ethernet address of the mbed. X
    int link() Returns if an ethernet link is pressent or not. O
    void set_link(Mode mode) Sets the speed and duplex parameters of an ethernet link. O

##Revision History
* Initial Release : 19 June. 2015