Soohwan Kim
Published © GPL3+

[WIZwiki_W7500ECO] HTTPClient with JSON parser

This post shows how to use HTTPClient parse Json data in ARMmbed platform.

BeginnerProtip441
[WIZwiki_W7500ECO] HTTPClient with JSON parser

Things used in this project

Hardware components

WIZnet WIZwiki-W7500ECO
×1

Story

Read more

Schematics

WIZwiki-W7500ECO

WIZwiki-W7500ECO SCHEMEETIC

Code

Code

Plain text
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()

Code

Plain text
#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

Code

Plain text
  $>curl ip_address:port_number

Code

Plain text
 // 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)    printf("Fail - Ethernet Connecing");

Code

Plain text
 sock.connect("192.168.0.223", 8000); // "destination IP address", destination port number

Code

Plain text
 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");

Code

Plain text
 sock.send_all(http_tx_msg, http_tx_msg_sz-1); // tx_buf, tx_buf_size

Code

Plain text
 while ( (returnCode = sock.receive(http_rx_msg, http_rx_msg_sz-1)) > 0) {
         http_rx_msg[returnCode] = '\0';
         printf("Received %d chars from server:\n\r%s\n", returnCode, http_rx_msg);
     }

Code

Plain text
 parse(parser, http_rx_msg);

Code

Plain text
 // parsing "string" in string type
 printf("name =%s\r\n" , parser["name"].get<string>().c_str());
 // parsing "age" in integer type
 printf("age =%d\r\n" , parser["age"].get<int>());
 // parsing "gender" in string type
 printf("gender =%s\r\n" , parser["gender"].get<string>().c_str());

Code

Plain text
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()

Code

Plain text
#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

Code

Plain text
  $>curl ip_address:port_number

Code

Plain text
 // 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)    printf("Fail - Ethernet Connecing");

Code

Plain text
 sock.connect("192.168.0.223", 8000); // "destination IP address", destination port number

Code

Plain text
 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");

Code

Plain text
 sock.send_all(http_tx_msg, http_tx_msg_sz-1); // tx_buf, tx_buf_size

Code

Plain text
 while ( (returnCode = sock.receive(http_rx_msg, http_rx_msg_sz-1)) > 0) {
         http_rx_msg[returnCode] = '\0';
         printf("Received %d chars from server:\n\r%s\n", returnCode, http_rx_msg);
     }

Code

Plain text
 parse(parser, http_rx_msg);

Code

Plain text
 // parsing "string" in string type
 printf("name =%s\r\n" , parser["name"].get<string>().c_str());
 // parsing "age" in integer type
 printf("age =%d\r\n" , parser["age"].get<int>());
 // parsing "gender" in string type
 printf("gender =%s\r\n" , parser["gender"].get<string>().c_str());

Bitbucket

https://bitbucket.org/fraka6/mlboost/src/ce9e76218daa7bec96d61b98c9a9e90b10440b7c/mlboost/examples/server.py

Credits

Soohwan Kim

Soohwan Kim

6 projects • 7 followers
Embedded System, ARM mbed, Arduino, TCP/IP, SoC, Open Source Hardware

Comments