Hardware Reference
In-Depth Information
The first line of the function definition indicates that our function is called
getHours and will return an integer as a result. Whenever the function is called
from the setup or loop functions, the code inside the function is executed.
For getHours , it will request data from the server, store that response, and
then convert that response into an integer value representing the number of
hours you'll need to wait until a new issue of MAKE is available. That integer
is returned from the function as long as there are no errors. If there are any
errors, it will return the value -1.
Executing a Linux command and getting its response is just one way that you
can get data into your sketch. This is a very unique Galileo feature because
it's a powerful way to connect different technologies together. You can also
have data passed between the Linux system and your Arduino code by writing
and reading files. See “Another Approach to Passing Data” on page 117 for a
few hints on how to do that.
Converting ASCII Characters to Integers
There's another interesting thing going on in Example 6-3 . Let's say the server
sent a response of 45. It's actually sending two ASCII characters, a 4 and a
5. When the Arduino reads these characters, it's not going to understand
them as the integer 45, but rather as two bytes that represent the characters
4 and 5. This means if you need to do math with the value, you must convert
them to an integer. That's what the function atoi() is for. It will look at the
array of characters and output their value as an integer, which you can use
for arithmetic.
Parsing JSON with Python
The How Many Days Until MAKE Comes Out? server makes the preceding
example pretty easy because it supplies one simple piece of data. Other
services may provide a few different pieces of data structured in a format
called JSON , or JavaScript Object Notation.
JSON has become the standard format for transmitting structured data
through the web. If you want to be able to read data from a site that offers
JSON, you'll have to parse it. As this would be difficult to do with Arduino
code, you can use other languages to do this job and pass the appropriate
information into Arduino's code.
Another format for structuring data is called XML , or eXtensi-
ble Markup Language .
To preview JSON data, visit http://nextmakemagazine.appspot.com/json in
your web browser.
 
Search WWH ::




Custom Search