Hardware Reference
In-Depth Information
return - 1 ;
}
return atoi ( output );
}
Use Python to execute your script, writing the output to fp .
Now, open the Serial Monitor in Arduino. You should see the response from
the server as the number of days until the next issue of MAKE comes out.
Another Approach to Passing Data
It's important to know that every time you use the system() or popen()
functions to run Python code, it can take time for Galileo to launch the Python
interpreter and run the code. This also means that nothing else in your Ar-
duino code will execute until that process is completely finished and Python
has exited.
In this project, it'll work fine. But as your projects grow in complexity, you may
not want to wait for Python to relaunch each time you need to fetch data.
To solve this, you can also have your Python code run constantly in the back-
ground, updating a file (or files) that the Arduino code will read. In order to
get the Arduino code to run your Python script and then move on to the rest
of the Arduino code, simply append an ampersand to the end of the system
call so that the Python script runs in the background and your Arduino code
continues to execute. For instance:
system("python /home/root/json-loop.py &");
For more information about having Python read and write files, I recommend
exercises 15 through 17 of the free online resource, Learn Python the Hard
Way .
As an example of reading files within Arduino code, Example 6-8 shows a
modified version of the getHours() function in Example 6-3 . It has been
rewritten so that the response from the server is piped into a file called re-
sponse.txt and then read by the Arduino code.
Example 6-8. Writing and reading a file with
Arduino code
int getHours () {
char output [ 5 ];
system ( "curl http://nextmakemagazine.appspot.com/simple > re
sponse.txt" );
FILE * fp ;
fp = fopen ( "response.txt" , "r" );
fgets ( output , 5 , fp );
 
Search WWH ::




Custom Search