Go Down

Topic: Parsing JSON on Arduino Yún (Read 3519 times) previous topic - next topic

PapyDanone

Hi all,

I have migrated code from an old Arduino Uno + Ethernet shield project that was polling a webservice and then parsing a JSON string using the aJSON library - which this forum is familiar with.

While polling the webservice is now way easier thanks to the new HttpClient lib, what about the JSON parsing? Can I delegate this to Linino as I assume it would do a better job and potentially reduce processing time?

On Linino, /usr/lib/python2.7/bridge/ already contains json.py and json.pyc scripts, how would I go about using them from the 32U4?

Can you think of a more elegant way?

Thanks for your contributions.


federicofissore

You can use json module from python
http://docs.python.org/2/library/json.html
Write a short script that you will call from your sketch
There is also a command line tool, jshn, but I can't find documentation about it

PapyDanone

#2
Oct 10, 2013, 06:19 pm Last Edit: Oct 10, 2013, 06:23 pm by PapyDanone Reason: 1
Thanks for your answer.

Yes, I did find the jshn command. The syntax is:

Code: [Select]
jshn -r "{your JSON string}"

but its output is hardly usable...

So how can I call a python script from the sketch? 

Also, any idea what the json.py and json.pyc scripts in /usr/lib/python2.7/bridge/ are supposed to do?

federicofissore

Take a look at the Bridge examples. Process and ShellCommands in particular

PapyDanone

#4
Oct 16, 2013, 05:58 pm Last Edit: Oct 16, 2013, 06:01 pm by PapyDanone Reason: 1
For the record, this is what I ended up doing, using the json.py script from /usr/lib/python2.7/bridge/

Linino side:

Code: [Select]
#!/usr/bin/python

import sys
import json
sys.path.insert(0, '/usr/lib/python2.7/bridge/')

from bridgeclient import BridgeClient as bridgeclient

client = bridgeclient()
values = client.get('json')

jsonreader = json.JsonReader()
parsed = jsonreader.read(values)
client.put('data', parsed[0]['data'])


Sketch:

Code: [Select]
#include <Bridge.h>
#include <HttpClient.h>

HttpClient client;

char data[10];
String response;

void setup()
{
  Serial.begin(9600);
  Bridge.begin();
}

void loop()
{
  client.get("http://webservice/json");
  delay(500);
 
  response = "";
 
  while (client.available()) {
    char c = client.read();
    response += c;
  }
 
  Bridge.put("json", response); // send response in JSON format
 
  Bridge.get("data", data, 10); //get latest parsed data
  Serial.print("data: ");
  Serial.println(data);
 
  delay(500);
}


I hope this helps!

KenNET

Hi,,

I'm new to Python so I have to ask.

- Where do you put the py code that you posted, since there is a json.py in /usr/lib/python2.7/bridge/
- How do I call the code? HttpClient is just fetching my json resault.


rkguy

Is this generally understood as the most efficient was to get and parse JSON?
I am working with wunderground and would like to do all of the webservices stuff on the linino side, creating an array there and spitting the array to the arduino side. TIPS, tricks, suggestions, examples for someone unfamiliar...unfortunately there is no choreo for Temboo.

Go Up