Getting start with Intel Galileo: calling APIs using Python

galileo2_wire

We’ll continue reviewing Galileo from previous post. In this article, we’ll give you instruction to setup and a quick example with Galileo (we assume that you are already familiar with Arduino tools and framework).

Arduino Galileo IDE

First, you need to download the Arduino IDE built specifically for Intel Galileo (get it from Intel site or Arduino site). If you’re on a OSX, it’s better to rename app to “Arduino Galileo” before moving it to the Applications folder to avoid overwriting the stock Arduino IDE.

Unlike ordinary Arduino board, Intel recommends not to power Galileo from USB port. It should be powered through barrel jack using power adapter that comes with the box. You should remember this important rule or you might ruin the board: Always connect the Galileo power BEFORE connecting client USB to computer and you must disconnect the client USB from computer before disconnecting the Galileo power, vice versa, your Galileo could be damaged.. We don’t know exactly why but it is already quite a hassle.

 

galileo2_wire

 

After powering Galileo through barrel jack, connect your Galileo to computer through USB client port (next to Ethernet port on the board). Open the IDE, select Tools – Boards – Intel ® Galileo. You should make sure that the Serial Port should be strictly /dev/cuXXX, and not usual /dev/ttyXXX ones. We’ve mistaken this many times when we could not upload sketches to Galileo.

 

galileo2_board
galileo2_serial

Access to the shell

Before we get to the shell, conceptually, you can think of Galileo as having Arduino component and Linux component. We said conceptually because Galileo is actually a Linux device that emulates Arduino environment; sketches aren’t uploaded to AVR the way existing Arduinos work.

As fas as we know, there are 2 ways to access the Linux shell. The first way is to use RS-232 cable and serial port terminal application (CoolTerm or Minicom on Mac OS, Realterm or PuTTy on Windows). The second method is to use Ethernet cable (or WiFi card) and connect via telnet connection. The first one requires some simple soldering to connect 3.5” stereo jack with 9-pin RS-232 cable since the serial port onboard is a female 3.5″ connector. Given how unpopular serial port is on modern machine, you might also need to get a USB-to-Serial adapter for your computer.

The second approach is much be less intricate. Simply copy the following sketch to your Arduino IDE and upload it to Intel ® Galileo. It will help you start telnet service on Galileo.

After uploading, open Terminal and type telnet <your IP address>.

 

gelileo2_telnet

Booting from the microSD card

Starting telnet service by uploading above Arduino sketch is required every time you reboot Galileo if you want to access Galileo shell remotely. Booting Galileo from microSD card instead of built-in memory could overcome this inconvenience. It allows saving of all settings, all uploaded sketches, provides bigger space for sketches and enable ability to use scripting languages like Python, Node.js…

First, you need download this Linux SD-Card Image from Intel site. Unzip it and copy to microSD card at root level. This Linux image is Poky distribution built using Yocto Project embedded Linux build system. One more thing you have to do is to edit grub.config file in boot/grub folder similar to the code snippet below so that Galileo can choose microSD card for booting (changing SPI kernel to MassStorage).

Insert microSD card into slot, then power up Galileo, then plug in USB cable. It should be in this order as recommended by Intel. Again, you should see /dev/cuXXX in Serial Port. Upload telnet sketch one more time, and access to the shell. Try to type python in Galileo shell.

Then you might want to set static IP for Galileo so that you can telnet to it every time you reboot Galileo. Use vi to edit /etc/networks/interfaces file (“nano” is not available).

Match the following lines, you can read what these IP addresses mean here:

Try to reboot the Galileo, you won’t need to upload telnet sketch again, just go to Terminal and telnet directly to Galileo.

Example: Call API with Galileo

In the following example, we will show you how to receive JSON-formatted data from API calls with Python and display it to LCD. We picked this particular example since it’s quite simple and can show you how to associate existing Arduino code with the power of Python on Linux.

JSON data from this URL is the current weather in Ho Chi Minh city: http://api.openweathermap.org/data/2.5/weather?id=1566083&units=metric.

Open your favourite text editor, copy this Python code that parses temperature of Ho Chi Minh City:

On Arduino IDE, upload this code below and open Serial Monitor to see result. You can output this result to wherever you want.

You won’t have system() like in normal Arduino sketches. With Galileo, you need to run system(command) in sketches’ code to interact with underlying Linux, it will emulate typing a command in Terminal.

Conclusion

After a few days experience with Galileo, we found that it’s nice to use but didn’t get us quite excited as we thought. Limitations of Linux distro and several compulsory work-arounds are two concerns that we had. Nevertheless, Galileo board is still under heavy development and hopefully it will become stable enough for the mainstream.

SSS Full-stack Engineer

Love Silicon Straits and want to know more about our company culture, working environment or job vacancies?
Find out more at careers.siliconstraits.vn.

Silicon Straits
Be Challenged. Be Inspired. Be Different.




  • Chào Anh Phạm Bá Thọ !!!
    Cho em hỏi xíu !!!
    Đoạn code sau để nạp vào board nhằm gọi python script cho board fai ko ạ …

    void setup() {
    Serial.begin(9600);
    }
    void loop() {
    Serial.println(temperature());
    delay(1000);
    }
    char* temperature() {
    char result[2];
    system(“python /home/root/weather.py > /result.txt”);
    FILE *tempFile;
    tempFile = fopen(“result.txt”, “r”);
    fgets(result, 2, tempFile);
    fclose(tempFile);
    return (result);
    }

    Em thắc mắc là em muốn viết 1 đoạn code đơn giản nhất . chỉ để mở file ” *.py ” thi viết ntn ạ ?

Posted by

on August 27, 2014

in , ,

Comments

Follow us for more later

or subscribe with