Python

Edison can also be programmed in Python. The code needs to be run on the device directly. We can either directly program the device, using any editor, such as the VI editor, or write the code in the PC first, and then transfer it using any FTP client, like FileZilla. Here we'll first write the code using Notepad++ and then transfer the script. Here also, we'll be executing a simple script which will blink the on-board LED. While dealing with Python and hardware, we need to use the MRAA library to interface with the GPIO pins. This is a low-level skeleton library for communication on GNU/Linux platforms. It supports almost all of the widely-used Linux-based boards. So, initially you need to install the library on the board.

Open up PuTTY and log in to your device. Once logged in, we'll add AlexT's unofficial opkg repository.

To do that, add the following lines to /etc/opkg/base-feeds.conf using the VI editor:

src/gz all http://repo.opkg.net/edison/repo/all
src/gz edison http://repo.opkg.net/edison/repo/edison
src/gz core2-32 http://repo.opkg.net/edison/repo/core2-32

Next, update the package manager and install git by executing the following commands:

opkg update
opkg install git

We'll clone Edison-scripts from GitHub to simplify certain things:

git clone https://github.com/drejkim/edison-scripts.git ~/edison-scripts

Next we'll add ~/edison-scripts to the path:

echo 'export PATH=$PATH:~/edison-scripts' >> ~/.profile
source ~/.profile

We'll now run the following scripts to complete the process. Please note that the previous steps will not only configure the device for MRAA, but will also set up the environment for later projects in this book.

Firstly, run the following script. Just type:

resizeBoot.sh
Then go for
installPip.sh

The previous package is the Python package manager. This will be used to install essential Python packages to be used in a later part of this book. Finally, we'll install Mraa by executing the following command:

installMraa.sh

MRAA is a low-level skeleton library for communication on GNU/Linux platforms. Libmraa is a C/C++ library with bindings to Java, Python, and JavaScript to interface with the IO on Galileo, Edison, and other platforms. In simple words, it allows us to operate on the IO pins.

Once the preceding steps have completed, we are good to go with the code for Python. For that, open up any code editor, such as Notepad++, and type in the following code:

import mraa 
import time


led = mraa.Gpio(13)
led.dir(mraa.DIR_OUT)

while True:
led.write(1)
time.sleep(0.2)
led.write(0)
time.sleep(0.2)

Please save the preceding code as a .py extension such as blink.py, and now, we'll explain it line by line.

Initially, using the import statements, we import two libraries: MRAA and time. MRAA is required for interfacing with the GPIO pins:

led = mraa.Gpio(13)
led.dir(mraa.DIR_OUT)

Here we initialize the LED pin and set it to the output mode:

while True:
led.write(1)
time.sleep(0.2)
led.write(0)
time.sleep(0.2)

In the preceding block, we put our main logic in an infinite loop block. Now, we will transfer this to our device. To do that again, go to the PuTTY console and type ifconfig. Under the wlan0 section, note down your IP address:

IP address to be used

Now open up FileZilla and enter your credentials. Make sure your device and your PC are on the same network:

  • Host: The IP address you got according to the preceding screenshot: 192.168.0.101
  • Username: root because you will be logging in to the root directory
  • Password: Your Edison password
  • Port: 22

Once entered, you will get the folder structure of the device. We'll now transfer the Python code from our PC to the device. To do that, just locate your .py file in Windows Explorer and drag and drop the file in the FileZilla console's Edison's folder. For now, just paste the file under root. Once you do that and if it's a success, the file should be visible in your Edison device by accessing the PuTTY console and executing the ls command.

Another alternative is to locate your file on the left-hand side of FileZilla; once located, just right-click on the file and click Upload. The following is the typical screenshot of the FileZilla windows:

FileZilla application

Once transferred and successfully listed using the ls command, we are going to run the script. To run the script, in the PuTTY console, go to your root directory and type in the following command:

python blink.py

If the file is present, then you should get the LED blinking on your device. Congrats! You have successfully written a Python script on your Edison board.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.224.59.192