Galileo Project: Create a Display to Track the Days Until MAKE hits Newsstands

Connected Home Other Boards Technology
Galileo Project: Create a Display to Track the Days Until MAKE hits Newsstands

The following is an adapted excerpt from the upcoming book, Getting Started with Intel Galileo.

fig04

Using the Intel Galileo for networked projects in the home is a natural fit. With Arduino pin compatibility, networking, and Linux, it’s a powerful yet flexible board. Since we’re exploring the connected home this week, I’d like to share a Galileo project that fits within that theme. I think the connected home isn’t just about flipping switches and getting the temperature, but it’s also about displaying information to the occupant.

I’ve created a simple website called How Many Days Until MAKE Comes Out?
which serves one simple purpose: it tells you how many days until the
next issue of MAKE Magazine is expected to hit newsstands. The data is
entered into a database by me based on the production calendars I
receive as a MAKE Contributing Editor. It’s served by a free tier of
Google App Engine, and the source code for the site is available on Github if you want to take a look at how I made it.connectedhome_banner

When you go to http://nextmakemagazine.appspot.com/
in your web browser, you’ll see the information is formatted to be
viewed and understood by a human, but the server is also configured to
speak directly to simple microcontrollers by stripping away all the
extra style and language and only returning the number of hours until
the next issue is released. You can see this if you go to the URL http://nextmakemagazine.appspot.com/simple

Your Galileo can use its Internet connection via Ethernet to connect to this URL, take the data it receives and evaluate how to display it. First, let’s make sure it can connect to the server.

NOTE: For this project, you’ll need to boot off of Galileo’s SD card image, which you can download here.

Testing the Connection

  1. Connect your Galileo via an Ethernet cable to your network. You can either
    plug it directly into an available port on your router, or if you are
    wired for Ethernet, plug it into an active jack.
  2. Connect your Galileo to power.
  3. Connect your computer to Galileo via the USB client port.
  4. Within the Arduino IDE, select File→Examples→Ethernet→WebClient.
  5. Click Upload.
  6. Open the serial monitor.

If your board was successful in connecting to a server, you’ll see text start to
appear in the serial monitor (as shown below). This example programs your Galileo
to do a Google search for the term “Arduino.” As the HTML response from
Google’s server is received, it sends those characters to the Serial Monitor.

fig00

Now that you’re sure the network connection is working, get your Galileo to connect to the How Many Days Until MAKE Comes Out? server:

  1. Create a new sketch and enter the code from below
  2. Upload the code and then open the serial monitor.

If it worked, you should see the number of hours until MAKE comes out printed every five seconds in the serial monitor.

void setup() {
}
void loop() {
Serial.println(getHours());
delay(5000);
}
int getHours() {
char output[5];
system("curl http://nextmakemagazine.appspot.com/simple > response.txt");
FILE *fp;
fp = fopen("response.txt", "r");
fgets(output, 5, fp);
fclose(fp);
return atoi(output);
}

The first thing you might notice in the code above is that the loop function has only two lines of code. The delay(5000) is what ensure that each iteration of the loop only happens every five seconds. But what about Serial.println(getHours());? The innermost function, getHours(), is actually defined right below the loop function.

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 in a file and then read the file and return an integer value representing the number of hours you’ll need to wait until a new issue of MAKE is available.

Having the Linux command write the data to a file and then having the Arduino sketch read that file is just one way that you can get data into your sketch. I was sure to show this unique Galileo feature because it’s a powerful way to connect different parts of a project together since there are so many ways to read and write files.

There’s another interesting thing going on in that code. 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 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 above 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. Since 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.

To preview JSON data, visit http://nextmakemagazine.appspot.com/json in your web browser.

{
  totalHours: 1473,
  volumeNumber: "38",
  daysAway: 61
}

The response will likely be together on one line, but if you were to add line breaks and indentation, it would look like the example above. There are three key/value pairs: the number of hours until the next issue, the next volume number, and the number of days until the next issue.

The code below uses the Python programming language to connect to the server’s JSON feed at http://nextmakemagazine.appspot.com/json and parses the volume number and number of hours.

import json 
import urllib2 

httpResponse = urllib2.urlopen('http://nextmakemagazine.appspot.com/json') 
jsonString = httpResponse.read() 

jsonData = json.loads(jsonString) 

print "Volume", jsonData['volumeNumber'], "will be released in", jsonData['totalHours'], "hours."
  1. Connect to Galileo’s command line using SSH, Telnet, or serial
  2. Change to root’s home directory
# cd /home/root/
  1. Launch the text editor vi with the filename _json-parse.py_ to create that file.
# vi json-parse.py
  1. Along the left side of the screen you’ll see a column of tildes (~). Type the letter i to enter insert mode. An “I” will appear in the lower left hand corner of your screen.
  2. Enter the code from above into vi.
  3. Hit the escape key to switch from insert mode to command mode. The “I” in the lower left corner will disappear and you’ll see a dash instead.
  4. Type “:x” and press enter to save the file and exit vi.
  5. Test the script by executing the code from the command line.
# python json-parse.py

If you got everything right, you should see the following output on the command line:

Volume 38 will be released in 1473.0 hours.

As you can see from the example above, parsing the JSON response from a website isn’t very difficult when you have Python available to you on Galileo. Now you simply need to connect the response from Python to your Arduino code.

To try that now, first modify json-parse.py:

  1. On Galileo’s command line, be sure you’re still in root’s home directory:
# cd /home/root/
  1. Open the file for editing in vi:
# vi json-parse.py
  1. Type the letter i to enter insert mode. An “I” will appear in the lower left hand corner of your screen.
  2. Edit the file so that it reflects the code the code below.
import json
import urllib2

httpResponse = urllib2.urlopen('http://nextmakemagazine.appspot.com/json')
jsonString = httpResponse.read()

jsonData = json.loads(jsonString)

print jsonData['daysAway']
  1. In the Arduino IDE, create a new sketch with the code from the code below. You’ll see that it’s very similar to the Arduino code above. Instead of calling curl from the command line, it uses Python to run the script you wrote.


void setup() {
}
void loop() {
Serial.println(getDays());
delay(5000);
}
int getDays() {
char output[5];
system("python /home/root/json-parse.py > /response.txt");
FILE *fp;
fp = fopen("response.txt", "r");
fgets(output, 5, fp);
fclose(fp);
return atoi(output);
}

  1. Upload the code to the board and open the serial monitor.

Now you should see the response from the server as the number of days until the next issue of MAKE comes out.

Connecting an LCD Character Display

What good is this information if it can only be seen in the serial monitor? Let’s hook up an LCD display to print out this information.

In addition to the parts you’ve been using, you’ll also need:

  • A standard 16×2 LCD character display, available from Adafruit, Sparkfun, or Maker Shed.
  • Breadboard
  • Jumper wires
  • 10K or 2K potentiometer

fig03

To connect the LCD to Galileo:

  1. Disconnect your Galileo board from your computer’s USB port and from power.
  2. Insert the LCD into the breadboard (solder header pins onto it if necessary).
  3. Insert the potentiometer into the breadboard as well.
  4. Using your jumper wires, connect the potentiometer and LCD to Galileo as shown in the diagram below.

fig01

  1. Using your jumper wires, connect the LCD as shown in the diagram below.

fig02

  1. Connect power to Galileo.
  2. Connect Galileo to your computer via USB.
  3. From the Arduino IDE, upload the code below.


#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup() {
lcd.init(1,12,255,11,5,4,3,2,0,0,0,0);
lcd.begin(16, 2);
lcd.setCursor(3, 0);
lcd.print("days until");
lcd.setCursor(0, 1);
lcd.print("MAKE is here!");
}
void loop() {
lcd.setCursor(0, 0);
lcd.print(" ");
lcd.setCursor(0, 0);
lcd.print(getDays());
delay(30*60*1000);
}
int getDays() {
char output[5];
system("python /home/root/json-parse.py > /response.txt");
FILE *fp;
fp = fopen("response.txt", "r");
fgets(output, 5, fp);
fclose(fp);
return atoi(output);
}

Now you’ll easily be able to keep tabs on when the next issue of MAKE will hit newsstands! Keep an eye out for the release of Getting Started with Intel Galileo for more network-connected Galileo projects!

Discuss this article with the rest of the community on our Discord server!
Tagged

Matt Richardson is a San Francisco-based creative technologist and Contributing Editor at MAKE. He’s the co-author of Getting Started with Raspberry Pi and the author of Getting Started with BeagleBone.

View more articles by Matt Richardson

ADVERTISEMENT

Maker Faire Bay Area 2023 - Mare Island, CA

Escape to an island of imagination + innovation as Maker Faire Bay Area returns for its 15th iteration!

Buy Tickets today! SAVE 15% and lock-in your preferred date(s).

FEEDBACK