Download The Hands on Intel Edison Manual Lab...
Copyright
The Hands-on Intel Edison Manual Lab Agus Kurniawan ISBN: 978-1-312-73697-9 1st Edition, 2014 Copyright © 2014 Agus Kurniawan
Table of Contents Copyright Preface 1. Preparing Development Environment 1.1 Intel Edison 1.2 Getting Hardware 1.3 Development Tools 1.4 Electronics Devices 1.5 Intel Edison Boxing 1.6 Connecting Intel Edison to Computer 1.6.1 Windows Platform 1.6.2 Linux Platform 1.7 Updating The Latest Intel Edison Firmware 2. Yocto Embedded Linux-based OS 2.1 Getting Started 2.2 Configuring Intel Edison: Hostname and Password 2.2.1 Change Hostname 2.2.2 Change root Password 2.3 Connecting to Wi-Fi Hotspot 2.4 SSH 2.5 Change Date/Timezone 2.6 Web Server 2.6.1 Adding HTML File 2.7 SFTP 2.8 Development on Yocto Linux 2.8.1 Python 2.8.2 Node.js 2.8.3 C/C++ with GCC 2.9 Reboot and Shutdown 2.10 Updating Software
2.11 Installing Nano - Text Editor 2.12 Intel Edison Unofficial Repository 3. Working with Arduino IDE Software 3.1 Intel Edison and Arduino IDE Software 3.2 Hello Intel Edison and Arduino 3.2.1 Hardware Configuration 3.2.2 Building Application 3.2.3 Testing 3.3 Demo: Seven-Segment Display 3.3.1 Getting Started with 7 Segment Display 3.3.2 Deploying Hardware Wiring 3.3.3 Building Application 3.3.4 Testing 3.4 Working with Serial Communication (UART) 3.5 Reading Analog Input 3.5.1 Hardware Configuration 3.5.2 Writing Application 3.5.3 Testing 3.6 Working with Analog Output 3.6.1 RGB LED 3.6.2 Hardware Configuration 3.6.3 Writing Application 3.6.4 Testing 3.7 Working with SPI 4. Intel Edison I/O Programming using Python 4.1 Getting Started 4.2 GPIO 4.3 Analog I/O - Reading Analog Input 4.4 UART 4.5 SPI 4.6 I2C/TWI 5. Intel Edison Development using C/C++
5.1 Getting Started 5.2 GPIO 5.3 Analog I/O - Reading Analog Input 5.4 UART 5.5 SPI 5.6 I2C/TWI 6. Intel Edison Development using Node.js 6.1 Getting Started 6.2 GPIO 6.3 Analog I/O - Reading Analog Input 6.4 UART 6.5 SPI 6.6 I2C/TWI 7. Bluetooth Low Energy (BLE) and iBeacon 7.1 Bluetooth Low Energy (BLE) 7.2 Pairing BLE Devices 7.3 BLE Development and iBeacon 7.4 Further Reading 8. Working with XBee IEEE 802.15.4 8.1 XBee IEEE 802.15.4 8.2 Getting Hardware 8.3 Connecting XBee IEEE 802.15.4 to Intel Edison 8.4 XBee with AT Command 8.5 Demo Chatting Program 8.5.1 Hardware Configuration 8.5.2 XBee Configuration 8.5.3 Writing Python Program for Intel Edison 8.5.4 Testing 8.6 XBee with API Mode 8.7 Further Reading Source Code Contact
Preface This book was written to help anyone wants to get started in Intel IoT and Intel Edison. It describes all the basic elements of the Intel IoT and Intel Edison with step-by-step approach. Several code samples are be provided to illustrate how to work with Intel Edison. Agus Kurniawan Berlin, December 2014
1. Preparing Development Environment
1.1 Intel Edison The Intel Edison development platform is designed to lower the barriers to entry for a range of inventors, entrepreneurs, and consumer product designers to rapidly prototype and produce Internet of Things (IoT) and wearable computing products. You can read a brief description about Intel Edison on https://communities.intel.com/docs/DOC-23139 .
For evaluation, we use Intel Edison kit for Arduino expansion board. It is designed to be hardware and software pin-compatible with Arduino shields designed for the Uno R3. Please read about this breakout board on https://communities.intel.com/docs/DOC-23161 .
1.2 Getting Hardware How to get Intel Edison? Officially you can buy it from the official distributor Amazon, http://www.amazon.com/Intel-Edison-Arduino-Components-EDI1ARDUIN-ALK/dp/B00ND1KH42/ Sparkfun, https://www.sparkfun.com/products/13097 Maker shed, http://www.makershed.com/products/intel-edison-kit-for-arduino Exp-Tech, http://www.exp-tech.de/Mainboards/Arduino/intel-edison-arduino-breakout-kit.html Mouser, http://www.mouser.com/Intel/_/N-1yy6lwu?Keyword=edison&FS=True You can buy Intel Edison kit for Arduino expansion board on your local electronics store. In addition, you also buy Intel Edison peripheral devices for instance, power adapter, micro USB and micro disk.
1.3 Development Tools In this book, you use any development tool to write program with Intel Edison target. We will explore these development tools on next chapters.
1.4 Electronics Devices We need electronic components to build our testing, for instance, Resistor, LED, sensor devices and etc.
I recommend you can buy electronic component kit. You find them on your local electronics shops
You can buy Intel IoT edition kit, for instance starter kit from Seeedstudio http://www.seeedstudio.com/depot/intel-iot-c-143/ .
You also can use additional sensor and actuator devices to evaluate Intel Edison board.
1.5 Intel Edison Boxing After you bought Intel Edison kit for Arduino, you got the following box.
Then, you open it. You will find Intel Edison board and its shield.
In this case, we are missing: Power adapter Micro USB cable USB cable
Now you can plug the Intel Edison on shield. If success, you can see green light on the board, shown in Figure below.
1.6 Connecting Intel Edison to Computer Firstly, your Intel Edison is be connected to power and then it's be connected to computer via micro USB.
1.6.1 Windows Platform To connect your Intel Edison kit for Arduino to computer, based on this site https://communities.intel.com/docs/DOC-23147 , you can do the following steps: Install FTDI driver, http://www.ftdichip.com/Drivers/VCP.htm . Download this driver based your Windows platform edition. Run setup file as Administrator level Intel Edison driver, https://communities.intel.com/docs/DOC-23242
Just follow installation instructions.
Now you can connect the power adapter and then micro USB. See Figure below.
After connected to computer, open Device Manager. You will see on Intel Edison on Ports, shown in Figure below.
Sometime you may get a problem especially for x64 edition about digital signature of Intel Edison driver. Configure to ignore the digital signature settings on your Windows. Normally you can do it by pressin F8 key on restarting Windows. Then choose Disable Driver signature Enforcement. For Windows 8, you can do by clicking Restart menu and pressing SHIFT key. Select Troubleshoot. Then select Advanced options. On Advanced options display, select Startup settings, shown in Figure below.
Click Restart button. Then Windows 8 will restart. After that, select Disable driver signature enforcement.
1.6.2 Linux Platform For Ubuntu Linux, please the following document on https://communities.intel.com/docs/DOC-23149 .
1.7 Updating The Latest Intel Edison Firmware Firstly, you can download Intel Edison image on https://communities.intel.com/docs/DOC-23242 . You can see Edison Yocto Complete Image. Download it.
For instance, you have download Intel Edison Yocto Image, edison-image-rel1-maint-rel1-ww4214.zip . Now you can plug the power adapter to board and micro USB to computer, shown in Figure below.
Then, you can see Edison flashdisk on File Manager. For Instance, file explorer on Windows. My Intel Edison board shows as flashdisk on drive F.
Format Edison drive with FAT32 format. Default format is FAT from Intel so you should reformat to FAT32. Click Start button to format Edison flashdisk.
Extract Edison image file, edison-image-rel1-maint-rel1-ww42-14.zip, to this to Edison flashdisk. The following is a sample output.
Now change your micro USB cable to another micro USB, see Figure below.
On Windows platform you can see Intel Edison board which attached to computer. See it using Device Manager.
On Linux, it will show as /dev/ttyUSB0 . For Windows, you can use PuTTY on http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html to access Intel Edison board. Fill serial port with speed 115200 and select Serial for Connection type.
Click Open button if done. Then, you get PuTTY dialog. If you get a blank screen, please press Enter key until show messages on screen.
Enter edison logon: root . Then, click Enter key. No password on Yocto.
For Ubuntu Linux, you can use Screen program. You can install it by typing the following command. $ sudo apt-get install screen
Normally Intel Edison kit for Arduino is be recognized as /dev/ttyUSB0. Now you can connect it using the following command. $ sudo screen /dev/ttyUSB0 115200
After connected, you can type this command to update Intel Edison firmware. $ reboot ota
The following is a sample output.
Now your Intel Edison has the latest firmware. Note: After update your the latest firmware by this approach, our current configuration will be deleted. We must reconfigure our Edison board. Please read section 2.2 to configure Edison board. Please logon using root user. Then, verify Edison firmware version using the following command. $ cat /etc/version $ configure_edison --version
You will get the current firmware version.
2. Yocto Embedded Linux-based OS
This chapter explains how to work with Yocto Linux on Intel Edison board.
2.1 Getting Started On the previous chapters we setup Intel Edison and operate it. In this chapter, we use Yocto Linux which already installed for basic operations.
2.2 Configuring Intel Edison: Hostname and Password Before doing the lab, please upgrade your Edison firmware. Read section 1.7. Firstly, we connect Intel Edison for Arduino to computer via micro USB, see Figure below.
Check serial port which Intel Edison is be attached. After that, open PuTTY (select Serial) for Windows or Screen for Ubuntu.
Logon using root user without password. We can configure our Edison OS using configure_edison command. Type this following command to get information about Edison command. $ configure_edison --help
2.2.1 Change Hostname Type the following command. $ configure_edison --name
Enter your device name. It will be hostname for Edison device.
2.2.2 Change root Password Furthermore, we can change our password for root user. Type this command. $ configure_edison --password
Enter password for root user.
2.3 Connecting to Wi-Fi Hotspot After changed password, we can setup Wi-Fi. Type the following command to setup our Wi-Fi.
$ configure_edison --wifi
It will scan Wi-Fi hotspot automatically. Select which hotspot you want to connect.
If selected, you will be asked to entry password if Wi-Fi needs password (WPA). If success, you will get information about your IP Address.
To verify it, you can open a browser and navigate to your Edison's IP Address.
You also can use ping to Edison's IP Address from your local computer.
2.4 SSH
Intel Edison firmware has been installed SSH server so you can access Intel Edison via SSH client. For illustration, I use PuTTY tool to access SSH protocol. Firstly, fill Intel Edison IP address and select SSH on Connection type.
If finished, click Open button. If success, you obtain a terminal. By default, username is root and current password.
Now you can do activities normally .
2.5 Change Date/Timezone If your Wifi has Internet connection, Intel Edison will synch Date configuration. You can read the current date and time on Yocto Linux using command date. Then, if you want to change time, for instance, we change time to 12 October 2014 07:18 AM. $ date $ date 1012071814
2.6 Web Server Intel Edison doesn't installed web server such as Lighttpd, http://www.lighttpd.net/ , Apache, Nginx. When you type http://ip_address_edison/ you will redirect to node.js server. You can test using browser. Open your browser and navigate to IP address of Intel Edison. A sample output is shown in Figure below.
2.6.1 Adding HTML File For demo, we can add new HTML file to web server. The default web folder is /usr/lib/edison_config_tools/public.
If you open edison-config-server.js, you get the following script.
Now you can add a new HTML file, called test.html. Navigate to public /usr/lib/edison_config_tools/public. Type the following command. $ vi test.html
Write the following script like this Figure below.
Save this script and then quit from vi application. Now you can open browser on computer. Navigate to test.html. If success, you can obtain a response from Linux.
2.7 SFTP How to transfer file from local computer to Intel Edison? Yocto Linux on Intel Edison provides SFTP (Secure FTP). By default, it has already installed on Intel Edison image. I use WinSCP tool, http://winscp.net/ , to access SFTP on Intel Edison. Entry all authentication information on WinSCP app.
After that, click Login button. If success, you can see the following dialog.
Now you can transfer your file to Yocto Linux.
2.8 Development on Yocto Linux Yocto Linux provides several development compilers and tools. For text editor, Yocto provides vi editor. In this section, we explore various development tools in Yocto Linux.
2.8.1 Python We can use Python on Yocto Linux. It has already installed. You can type the following script for demo. $ python --version $ python >> 5*7 >> exit()
2.8.2 Node.js Node.js has already installed in Yocto Linux. You can type the following script for illustration. $ node -v $ node > 4*9 > .exit
2.8.3 C/C++ with GCC
GCC has already installed in Yocto Linux. You can check GCC version using the following command. $ gcc -v
Try to create a file, called hello.c, and write this code. #include main() { printf("Hello World\n"); }
Save and try to compile and run. $ gcc -o hello hello.c $ ./hello
2.9 Reboot and Shutdown You can reboot your Intel Edison using the following command. $ reboot
You can turn on your Intel Edison using shutdown command. $ shutdown -h -P now
Wait until all lights are turned off on Intel Edison board. Normally it takes a minute to turn all system. After that, you can unplug the power from the board.
2.10 Updating Software To update and upgrade the latest library, you can type the following command.
$ echo "src intel-iotdk http://iotdk.intel.com/repos/1.1/intelgalactic" > /etc/op
Then, try to update and upgrade. $ opkg update $ opkg upgrade
Make sure your board is be connected to Internet network.
2.11 Installing Nano - Text Editor If you want to install nano, text editor, on your Intel Edison. You can type the following command. $ wget http://www.nano-editor.org/dist/v2.2/nano-2.2.6.tar.gz $ tar xvf nano-2.2.6.tar.gz $ cd nano-2.2.6 $ ./configure $ make $ make install
After installed, you can use it, for instance, $ nano test.txt
You will get your nano editor.
2.12 Intel Edison Unofficial Repository If we add additional repository to Intel Edison image. Firstly, edit /etc/opkg/base-feeds.conf using nano or vi. $ nano /etc/opkg/base-feeds.conf
Then, add the following repository. 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
Save this file. Now you can update our repository. $ opkg update
You can install the new package using the following command. $ opkg install
Note: is package name.
3. Working with Arduino IDE Software
This chapter explains how to work Intel Edison and Arduino IDE software.
3.1 Intel Edison and Arduino IDE Software In this chapter, we explorer Arduino software to develop application for Intel Edison target. FYI, Intel Edison has pin-compatible with a wide range of Arduino Uno R3 shields. To download Arduino software for Intel Edison, you can visit on https://communities.intel.com/docs/DOC-23242 . After you downloaded Arduino software for Intel, for instance, arduino-windows-1.0.4.7z, extract this file into a specific folder. A sample of extracted file for Arduino Intel is shown in Figure below (Windows platform).
Now you can install Arduino Intel on Windows platform. Then, you can run Arduino Intel. The following is a sample of Arduino Intel app.
For Ubuntu Linux, you can execute Arduino Intel by typing the following command. $ ./arduino
You may run as Administrator (sudo) due to device security issue, for instance, accessing USB driver.
3.2 Hello Intel Edison and Arduino In this section, we learn how to get started to write a program for Intel Edison. We can use 3 LEDs to illustrate our case.
3.2.1 Hardware Configuration Connected your 3 LEDs to Intel Edison digital pins: 10, 9, 8. Another pin is connected to GND pin. The following is hardware schema.
Please connect your Intel Edison from Micro USB (J16) to computer.
Then, check which COM is used by Intel Edison. In Windows platform, you can find it via Device Manager. Otherwise, it presents as /dev/ttyACM0 in Linux.
My Intel Edison shows as COM16. It will be used to Arduino software.
3.2.2 Building Application We can turn on/off for LEDS using digitalWrite(), http://arduino.cc/en/Reference/DigitalWrite, which sends HIGH or LOW value to digital pin of Intel Edison. Create new application on Arduino software and write the following code. int led1 = 8; int led2 = 9; int led3 = 10;
void setup() { pinMode(led1, OUTPUT); pinMode(led2, OUTPUT); pinMode(led3, OUTPUT); } void offAll(){ digitalWrite(led1, LOW); digitalWrite(led2, LOW); digitalWrite(led3, LOW); } void loop() { digitalWrite(led1, HIGH); delay(1000); offAll(); delay(100); digitalWrite(led2, HIGH); delay(1000); offAll(); delay(100); digitalWrite(led3, HIGH); delay(1000); offAll(); delay(100); }
Make sure your Arduino software has configured to Intel Edison target. You can do it by clicking menu Tools->Board and then select your board.
You must configure board COM too. You can click menu Tools->Serial Port and then select your board COM.
Save this code.
3.2.3 Testing To run application to Intel Edison board, you must deploy program. You can do it by clicking icon rightarrow (upload).
If success, you can see 3 LEDs are blinking.
3.3 Demo: Seven-Segment Display In this section, we're going to build an application to display number 0 to 9 on a 7 segment display device. This device will be connected to Intel Edison via digital output pins and then we develop application to illustrate how to write data to digital output pins. Let's start.
3.3.1 Getting Started with 7 Segment Display To understand 7 segment display, I refer to a datasheet from SA56-11GWA and SC56-11GWA , http://www.kitronik.co.uk/pdf/7_segment_display_datasheet.pdf . The following is device schema.
The following is a sample physical of 7 segment display.
3.3.2 Deploying Hardware Wiring The next step, we must connect our 7 segment to Intel Edison. Here is pin configuration from 7 segment display to Intel Edison. Digital 2 --> pin DP Digital 3 --> pin c Digital 4 --> pin d Digital 5 --> pin e Digital 6 --> pin b Digital 7 --> pin a Digital 8 --> pin f Digital 9 --> pin g GND --> pin GND (center pin of 7 segment / Unlabeled pin)
The following is a hardware implementation for connecting 7 segment display to Intel Edison.
3.3.3 Building Application Algorithm to display number on 7 segment display is easy. We just combine value on 7 segment display pins. For instance, to display number 0, we set value LOW on the g pin. Otherwise, the pins are set to HIGH value. The following is implementation to display value 0,1...9 on 7 segement display. int pin_a = 7; int pin_b = 6; int pin_c = 3; int pin_d = 4; int pin_e = 5;
int pin_f = 8; int pin_g = 9; int pin_dp = 2; void setup() { pinMode(pin_a, OUTPUT); pinMode(pin_b, OUTPUT); pinMode(pin_c, OUTPUT); pinMode(pin_d, OUTPUT); pinMode(pin_e, OUTPUT); pinMode(pin_f, OUTPUT); pinMode(pin_g, OUTPUT); pinMode(pin_dp, OUTPUT); } void displaynull(){ digitalWrite(pin_a, LOW); digitalWrite(pin_b, LOW); digitalWrite(pin_c, LOW); digitalWrite(pin_d, LOW); digitalWrite(pin_e, LOW); digitalWrite(pin_f, LOW); digitalWrite(pin_g, LOW); digitalWrite(pin_dp, LOW); } void display(int num){ switch(num){ case 0: digitalWrite(pin_a, HIGH); digitalWrite(pin_b, HIGH); digitalWrite(pin_c, HIGH); digitalWrite(pin_d, HIGH); digitalWrite(pin_e, HIGH); digitalWrite(pin_f, HIGH); digitalWrite(pin_g, LOW); digitalWrite(pin_dp, HIGH); break; case 1: digitalWrite(pin_a, LOW); digitalWrite(pin_b, HIGH); digitalWrite(pin_c, HIGH); digitalWrite(pin_d, LOW); digitalWrite(pin_e, LOW); digitalWrite(pin_f, LOW); digitalWrite(pin_g, LOW); digitalWrite(pin_dp, HIGH); break; case 2: digitalWrite(pin_a, HIGH); digitalWrite(pin_b, HIGH); digitalWrite(pin_c, LOW); digitalWrite(pin_d, HIGH);
digitalWrite(pin_e, HIGH); digitalWrite(pin_f, LOW); digitalWrite(pin_g, HIGH); digitalWrite(pin_dp, HIGH); break; case 3: digitalWrite(pin_a, HIGH); digitalWrite(pin_b, HIGH); digitalWrite(pin_c, HIGH); digitalWrite(pin_d, HIGH); digitalWrite(pin_e, LOW); digitalWrite(pin_f, LOW); digitalWrite(pin_g, HIGH); digitalWrite(pin_dp, HIGH); break; case 4: digitalWrite(pin_a, LOW); digitalWrite(pin_b, HIGH); digitalWrite(pin_c, HIGH); digitalWrite(pin_d, LOW); digitalWrite(pin_e, LOW); digitalWrite(pin_f, HIGH); digitalWrite(pin_g, HIGH); digitalWrite(pin_dp, HIGH); break; case 5: digitalWrite(pin_a, HIGH); digitalWrite(pin_b, LOW); digitalWrite(pin_c, HIGH); digitalWrite(pin_d, HIGH); digitalWrite(pin_e, LOW); digitalWrite(pin_f, HIGH); digitalWrite(pin_g, HIGH); digitalWrite(pin_dp, HIGH); break; case 6: digitalWrite(pin_a, HIGH); digitalWrite(pin_b, LOW); digitalWrite(pin_c, HIGH); digitalWrite(pin_d, HIGH); digitalWrite(pin_e, HIGH); digitalWrite(pin_f, HIGH); digitalWrite(pin_g, HIGH); digitalWrite(pin_dp, HIGH); break; case 7: digitalWrite(pin_a, HIGH); digitalWrite(pin_b, HIGH); digitalWrite(pin_c, HIGH); digitalWrite(pin_d, LOW); digitalWrite(pin_e, LOW); digitalWrite(pin_f, LOW); digitalWrite(pin_g, LOW); digitalWrite(pin_dp, HIGH);
break; case 8: digitalWrite(pin_a, HIGH); digitalWrite(pin_b, HIGH); digitalWrite(pin_c, HIGH); digitalWrite(pin_d, HIGH); digitalWrite(pin_e, HIGH); digitalWrite(pin_f, HIGH); digitalWrite(pin_g, HIGH); digitalWrite(pin_dp, HIGH); break; case 9: digitalWrite(pin_a, HIGH); digitalWrite(pin_b, HIGH); digitalWrite(pin_c, HIGH); digitalWrite(pin_d, HIGH); digitalWrite(pin_e, LOW); digitalWrite(pin_f, HIGH); digitalWrite(pin_g, HIGH); digitalWrite(pin_dp, HIGH); break; } } void loop() { displaynull(); delay(1000); display(0); delay(1000); displaynull(); delay(1000); display(1); delay(1000); displaynull(); delay(1000); display(2); delay(1000); displaynull(); delay(1000); display(3); delay(1000); displaynull(); delay(1000); display(4); delay(1000); displaynull(); delay(1000); display(5); delay(1000); displaynull(); delay(1000); display(6); delay(1000);
displaynull(); delay(1000); display(7); delay(1000); displaynull(); delay(1000); display(8); delay(1000); displaynull(); delay(1000); display(9); delay(1000); }
Save this code as 7segment file.
3.3.4 Testing Now you can run this application by uploading program to Intel Edison board. The following is sample outputs for 7 segment on Intel Edison board.
3.4 Working with Serial Communication (UART) In this section we work with serial communication (UART) which we can use Serial object, http://arduino.cc/en/reference/serial . To illustrate our case, we access Intel Edison UART using Serial.write() to write and Serial.read() to read. Open Arduino software for Intel Edison and write this code. int led = 13; void setup() { pinMode(led, OUTPUT);
Serial.begin(9600); } void loop() { digitalWrite(led, HIGH); Serial.write("LEAD is HIGH\n"); delay(1000); digitalWrite(led, LOW); Serial.write("LEAD is LOW\n"); delay(1000); }
Save this code and then try to upload to the Intel Edison board. If success, you can see LED on pin 13 is blinking, shown in Figure below (red arrow).
You also can verify by opening Serial Monitor. You can do it by clicking menu Tools->Serial Monitor. A sample output can be seen in Figure below.
3.5 Reading Analog Input In this section, we learn how to read analog input on Intel Edison. For illustration, I use Potentiometer as analog input source. Let's start!.
3.5.1 Hardware Configuration To understand Potentiometer, you see its scheme in Figure below.
You can connect VCC to Intel Edison VCC 5V. Vout to Intel Edison Analog input A0. In addition, GND to Intel Edison GND. The following is hardware implementation.
3.5.2 Writing Application We can use analogRead(), http://arduino.cc/en/Reference/AnalogWrite , to read analog input in Intel Edison. Now open Arduino software and write this code. int led = 13; int potentiometer = 0; int val = 0; void setup() { pinMode(led, OUTPUT); Serial.begin(9600); }
void loop() { val = analogRead(potentiometer); Serial.print("Analog value: "); Serial.println(val,DEC); delay(1000); }
Save this code as potentiometer file.
3.5.3 Testing Compile and upload the program to Intel Edison board. Then, open Serial Monitor. You will see analog input value on this dialog. A sample output can be seen in Figure below.
3.6 Working with Analog Output The last section, we learn how to work with analog output on Intel Edison. We use RGB LED which sets colors by giving R, G and B values on its pins. We use Intel Edison Analog output (PWM). If you
see the board, PWM pins are idenitified by 2 sign. You can see PWM pins on the following Figure below.
3.6.1 RGB LED In this scenario we build an application to control RGB LED color using Intel Edison Analog output (PWM). RGB LED has 4 pins that you can see it in Figure below.
To understand these pins, you can see the following Figure.
Note: Pin 1: Red Pin 2: Common pin Pin 3: Green Pin 4: Blue
Now we can start to build an application and hardware implementation.
3.6.2 Hardware Configuration To connect RGB LED pins to Intel Edison, we can configure the following schema. RGB LED pin 1 (red) is connected to Intel Edison PWM pin 9 RGB LED pin 2 is connected to Intel Edison VCC 5V RGB LED pin 3 (green) is connected to Intel Edison PWM pin 6 RGB LED pin 4 (blue) is connected to Intel Edison PWM pin 5 Note: you can use resistor to handle voltage issues. Hardware implementation can be seen in Figure below.
3.6.3 Writing Application We can generate a color by combining R, G, and B values. This value can be 0 until 255. In this case, we will generata colors: red, green, blue, yellow, and purple. Write this code on Arduino software. int redPin = 5; int greenPin = 6; int bluePin = 9;
void setup() { pinMode(redPin, OUTPUT); pinMode(greenPin, OUTPUT); pinMode(bluePin, OUTPUT); Serial.begin(9600); } void loop() { setColor(255, 0, 0); // red Serial.println("red"); delay(1000); setColor(0, 255, 0); // green Serial.println("green"); delay(1000); setColor(0, 0, 255); // blue Serial.println("blue"); delay(1000); setColor(255, 255, 0); // yellow Serial.println("yellow"); delay(1000); setColor(80, 0, 80); // purple Serial.println("purple"); delay(1000); setColor(0, 255, 255); // aqua Serial.println("aqua"); delay(1000); } void setColor(int red, int green, int blue) { analogWrite(redPin, red); analogWrite(greenPin, green); analogWrite(bluePin, blue); }
Save this code as rgb file.
3.6.4 Testing Now you can compile and upload program to Intel Edison board. A sample output for RGB LED can be seen in Figure below.
You can also verify it using Serial Monitor.
3.7 Working with SPI The Serial Peripheral Interface (SPI) is a communication bus that is used to interface one or more slave peripheral integrated circuits (ICs) to a single master SPI device; usually a microcontroller or microprocessor of some sort. By defaults, Intel Edison has SPI with 25 MHz Master mode and 16.67 MHz slave mode. While Edison has a native SPI controller, it will act as a master and not as an SPI slave. We can use Intel Edison SPI pins for Arduino breakout. Pin 10 (SS) Pin 11 (MOSI) Pin 12 (MISO) Pin 13 (SCK).
For illustration, we test our SPI on Intel Edison using SPI loopback which we connect MOSI to MISO. We can use SPI library to access this I/O. The following is hardware implementation.
Now you open Arduino software for Intel Edison. Write the following code. #include int val = 50; void setup() { // initialize SPI: SPI.begin(); Serial.begin(9600); } void loop() { Serial.print("send: "); Serial.println(val); int recv = SPI.transfer(val); Serial.print("recv: "); Serial.println(recv); delay(1000); val = val + 1; if(val>60) val = 50; }
We verify it using Serial Monitor. You will send sent and received values.
4. Intel Edison I/O Programming using Python
This chapter explains how to work and write program using Python for accessing Intel Edison I/O from Linux.
4.1 Getting Started
Firstly, we must understand Intel Edison board schema. You can find information about the board on https://communities.intel.com/docs/DOC-23161 . The following is a board schema for Intel Edison.
We can verifiy GPIO on processor using the following command.
$ cat /sys/kernel/debug/gpio
In this section, we learn how to write Python program to access GPIO Intel Edison. We use libmraa library, http://iotdk.intel.com/docs/master/mraa/python/mraa.html . It has installed for you. To test libmraa library, you can check its version using Python console. $ python >> import mraa >> mraa.getVersion()
A sample output can be seen in Figure below.
4.2 GPIO In this section, we will work with digital I/O on Intel Edison and write Python program to access it. We use libmraa library which has installed on IoT OS. To write HIGH/LOW on GPIO, we use Gpio.write(). If you want to read digital input, you can use Gpio.read(). For illustration, we use demo from section 2.4. Connected your 3 LEDs to Intel Edison digital pins: 10, 9, 8. Another pin is connected to GND pin.
Now you can create a file, called hello.py, and write the following script. import time import mraa led1 = 10 led2 = 9 led3 = 8 pin_led1 = mraa.Gpio(led1) pin_led2 = mraa.Gpio(led2) pin_led3 = mraa.Gpio(led3) # set GPIO mode pin_led1.dir(mraa.DIR_OUT) pin_led2.dir(mraa.DIR_OUT) pin_led3.dir(mraa.DIR_OUT) try: while True: print 'LED 1 ON' pin_led1.write(1) pin_led2.write(0) pin_led3.write(0) time.sleep(2)
print 'LED 2 ON' pin_led1.write(0) pin_led2.write(1) pin_led3.write(0) time.sleep(2) print 'LED 3 ON' pin_led1.write(0) pin_led2.write(0) pin_led3.write(1) time.sleep(2) except KeyboardInterrupt: pin_led1.write(0) pin_led2.write(0) pin_led3.write(0)
You can execute this program to Intel Edison. Type the following command. $ python hello.py
If success, you will obtain responses as below.
You also see LEDs are blinking on Intel Edison.
4.3 Analog I/O - Reading Analog Input We can read analog input using mraa library. For illustration, we use the same scenario from section 3.5 which uses Potentiometer as analog input source. Connect Potentiometer pins to VCC and Vout to Analog input A0 on Intel Edison board.
The following is hardware implementation.
Mraa library provide a class, Aio, to manipulate analog processing. To read analog input, we can use Aio.read() with specific analog pin. For illustration, we create a file, called analog_read.py, and write this code. import time import mraa a0 = mraa.Aio(0) a0.setBit(10) try: while True: val = a0.read() print "analog val: " + str(val) time.sleep(2)
except KeyboardInterrupt: print "done"
Save this code and try to execute by typing the following command. $ python analog_read.py
If success, you can see analog value in Terminal. Try to change Potentiometer value so you get a new analog value from Intel Edison board.
4.4 UART In this section we learn how to access UART on Intel Edison board. By default, Inte Edison has six UART on the board. You can check them by typing the following command. $ ls /dev/tty*
A sample output of command response can be seen in Figure below.
The following is a list of Intel Edison UART which we can use. /dev/ttyGS0 is be connected to Micro USB Client which is connected to PC /dev/ttyMFD1 is be connected to Arduino pins, digital pin 0 (Rx) and digital pin 1( Tx) /dev/ttyMFD2 is be connected to Micro USB Client which is connected to PC for debugging
For accessing UART on Arduino pins from Linux, I used pyserial library, http://pyserial.sourceforge.net . Furthermore, you can install it via easy_install. The following is a command for installing pyserial. $ easy_install -U pyserial
If you don't install easy_install yet, you can install by typing the folllowing command.
$ wget https://bootstrap.pypa.io/ez_setup.py --no-check-certificate -O - | python
A sample output of installation of pyserial can be seen in Figure below.
For testing, I use a UART on /dev/ttyMFD2. We need a micro USB cable which is connected to a computer. Next step is to write program using Python. Create a file, called uart.py, and write this code.
import serial import time serialport = '/dev/ttyMFD2' print 'open ' + serialport ser = serial.Serial(serialport, 115200) counter = 0 while True: try: print 'writing: ' + str(counter) ser.write(str(counter)) print(counter) counter += 1 if counter > 10: counter = 1 time.sleep(1) except (KeyboardInterrupt, SystemExit): ser.close() raise
print "done"
To write data to UART, we can use serial.write() from pyserial library. Otherwise, we can use serial.read() to read incoming data from UART. Save this code and try to execute it to Intel Edison terminal. $ python uart.py
This program will send data from 0 to 10 and back to 1. On local computer, we use PuTTY to read data on Serial. Firstly, check which a serial port is used via Device Manager if you are working on Windows platform, for instance, COM6.
Then, you put COM6 to PuTTY.
If you are working on Linux, you can use miniterm. You can see a sample output in Figure below.
On Intel Edison, the following is program output.
4.5 SPI We can verify installed SPI on Intel Edison by the following command. $ ls /dev/spi*
You can see my SPI on Intel Edison is installed on /dev/spidev1.5 .
For illustration, we build a program to write and read data to/from Intel Edison SPI. We build a SPI loopback which digital pin 11 (MOSI) is be connected to digial pin 12 (MISO). Hardware implementation can be seen in Figure below.
We will use mraa library to access SPI. To write data to SPI, we can use Spi.write(). Then, we receive incoming data from returning Spi.write(). Create a file, called spi.py, and write this code. import time import mraa spi = mraa.Spi(5) counter = 100 while True: try: n = len(str(counter)) recv = spi.write(str(counter)) print recv[:n] time.sleep(1) counter += 1 if counter > 110: break except (KeyboardInterrupt, SystemExit): raise print "done"
This code will send counter data which is started from 100. If it reaches counter 110+, it will be stopped. Now you can run it by typing the following command. $ python spi.py
A sample output can be seen in Figure below.
4.6 I2C/TWI The I2C (Inter-Integrated Circuit) bus was designed by Philips in the early '80s to allow easy communication between components which reside on the same circuit board. TWI stands for Two Wire Interface and for most marts this bus is identical to I²C. The name TWI was introduced by Atmel and other companies to avoid conflicts with trademark issues related to I²C. Intel Edison has two I2C interfaces which are connected via I2C1 on Analog A4 (SDA) and A5 (SCL) pins and I2C6 on Digital SDA and SCL pins. We can obtain a list of installed I2C on Intel Edison using i2cdetect with passing -l parameter. Typing the following command. $ i2cdetect -l
If success, you see a list of installed I2C as below.
Analog A4 (SDA) and A5 (SCL) pins are represented as i2c-1. For illustration, we will build I2C application. We need an Arduino board as I2C source. I use Arduino Uno. For wiring scenario, we implement the following hardware schema: Arduino A4 (SDA) to Intel Edison A4 (SDA) Arduino A5 (SCL) to Intel Edison A5 (SCL) Arduino GND to Intel Edison GND
You can see the hardware implementation in Figure below.
The first step is to build a program for Arduino Uno. We define I2C address 0x15. Arduino will send a random number to requester on I2C channel. We create a program for Arduino, called arduino_i2c_write, and write this code. #include const byte SLAVE_ADDRESS = 0x15; int led = 13; byte x; // source: // http://forum.arduino.cc/index.php?topic=197633.0 byte randomDigit() { unsigned long t = micros(); byte r = (t % 10) + 1; for (byte i = 1; i 10: break except (KeyboardInterrupt, SystemExit): raise print "done"
Save this code and try to run on Intel Edison Terminal. $ python i2c.py
If success, you can see the data from I2C channel.
You also can verify on Arduino using Serial Monitor. Make sure your Arduino is connected to PC so you can see data using Serial Monitor tool.
5. Intel Edison Development using C/C++
This chapter explains how to work and write program using C/C++ to access Intel Edison I/O from Linux.
5.1 Getting Started Intel IoT distro provides mraa library which can be used for C/C++ development. In this chapter, we will focus on C development with GCC compiler. If you use C++, you can read this document, http://iotdk.intel.com/docs/master/mraa/ . We will explore several scenario to illustrate how C work with Intel Edison board. The following is our lab scenario: GPIO Analog I/O UART SP I2C/TWI
Let's start working with mraa library. We will check mraa library version using mraa_get_version(). Write the following code. #include #include #include int main(int argc, char **argv) { mraa_result_t ret; ret = mraa_set_log_level(LOG_DEBUG); fprintf(stdout, "Mraa version: %s\n", mraa_get_version()); mraa_deinit(); return ret; }
Save this code into a file, called testversion.c.
Now you can compile this file by typing the following command. $ gcc -lmraa -o testversion testversion.c
Then, try to execute. $ ./testversion
If success, you can obtain mraa library version.
5.2 GPIO In this section, we learn how to access GPIO on Intel Edison using C. For illustration, I use 3 LEDs which are connected to digital input on the board. Please read section 4.2 for hardware configuration. To access GPIO using C, we must declare header file, gpio.h. The following is a list of steps to access GPIO. Initialize GPIO by calling mraa_init() Getting mraa_gpio_context object by calling mraa_gpio_init() with passing GPIO pin. This parameter is similiar as digital I/O on Intel Edison. For instance, we connect to digital I/O 10. Then, we pass 10 value to mraa_gpio_init(10) We decide I/O direction which is possible as input or output. You can do it by calling mraa_gpio_dir() To write value to digital Intel Edison, we can use mraa_gpio_write(). Otherwise, we can use mraa_gpio_read() If you finish working with GPIO, you can close it by calling mraa_gpio_close()
Now we can start to write a program. Create a file, called gpiodemo.c, and write this code. #include #include #include #include sig_atomic_t volatile isrunning = 1; void sig_handler(int signum); int main () { signal(SIGINT, &sig_handler); mraa_init(); mraa_gpio_context led1; mraa_gpio_context led2; mraa_gpio_context led3; led1 = mraa_gpio_init(10); led2 = mraa_gpio_init(9); led3 = mraa_gpio_init(8); mraa_gpio_dir(led1, MRAA_GPIO_OUT); mraa_gpio_dir(led2, MRAA_GPIO_OUT); mraa_gpio_dir(led3, MRAA_GPIO_OUT); while (isrunning){ fprintf(stdout, "LED 1\n"); mraa_gpio_write(led1,1); mraa_gpio_write(led2,0); mraa_gpio_write(led3,0); sleep(1); fprintf(stdout, "LED 2\n"); mraa_gpio_write(led1,0); mraa_gpio_write(led2,1); mraa_gpio_write(led3,0); sleep(1); fprintf(stdout, "LED 3\n"); mraa_gpio_write(led1,0); mraa_gpio_write(led2,0); mraa_gpio_write(led3,1); sleep(1); } // reset all LEDS to 0 mraa_gpio_write(led1,0); mraa_gpio_write(led2,0); mraa_gpio_write(led3,0);
// close all gpio pins mraa_gpio_close(led1); mraa_gpio_close(led2); mraa_gpio_close(led3); fprintf(stdout, "\nDone\n"); return MRAA_SUCCESS; } void sig_handler(int signum) { if (signum == SIGINT) isrunning = 0; }
Save this code and try to compile. $ gcc -lmraa -o gpiodemo gpiodemo.c
If done, you can execute this program by typing the following command. $ ./gpiodemo
If success, you can see the output as below.
You also can see 3 LEDs are blinking.
5.3 Analog I/O - Reading Analog Input In this section, we learn how to read analog input on Intel Edison. We use the same problem on section 4.3. To read analog input we can use mraa_aio_read(). This function is a part of aio.h file. The following is the algorithm to read analog input: Initialize aio mraa library by calling mraa_aio_init() Set bit value for reading by calling mraa_aio_set_bit() Read analog input using mraa_aio_read() Call mraa_aio_close() to release aio object usage
Now you can write the program by creating a file, called analog_read.c, and write this code. #include #include #include #include sig_atomic_t volatile isrunning = 1; void sig_handler(int signum);
int main () { signal(SIGINT, &sig_handler); mraa_aio_context adc_a0; uint16_t adc_value = 0;
adc_a0 = mraa_aio_init(0); if (adc_a0 == NULL) { return 1; } mraa_aio_set_bit(adc_a0,10); while (isrunning){ adc_value = mraa_aio_read(adc_a0); fprintf(stdout, "ADC A0 value: %X - %d\n", adc_value, adc_value); sleep(1); } mraa_aio_close(adc_a0); fprintf(stdout, "\nDone\n"); return MRAA_SUCCESS; } void sig_handler(int signum) { if (signum == SIGINT) isrunning = 0; }
Save this code and try to compile by the following command. $ gcc -lmraa -o analog_read analog_read.c
Run the program. $ ./analog_read
A sample output can be seen in Figure below.
5.4 UART In this section, we will access Intel Edison UART using C. To access UART, we can implement termios, http://pubs.opengroup.org/onlinepubs/007908799/xsh/termios.h.html . For illustration, we use the same scenario on section 4.4. Create a file, called uart.c, and write this code. #include #include #include #include #include #include #include #include
void* handling_uart(void *arg) { int length, value; unsigned char buffer[256]; int *uart0 = (int *)arg; value = 70; errno = 0;
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL); while(1) { pthread_testcancel(); sprintf(buffer,"%d",value); printf("Write to UART: %d\n",value); fflush(stdout); // write to UART length = write((*uart0), buffer, 2); if (length < 0) { printf("\n write() failed with error [%s]\n",strerror(errno)); break; } memset(buffer,0,256); sleep(3); value++; if(value>80) value = 70; } printf("\r\n"); } int main(void) { int uart0 = -1; pthread_t thread; int ret, status; char serial_port[] = "/dev/ttyMFD2"; errno = 0; uart0 = open(serial_port, O_RDWR | O_NOCTTY ); if (uart0 == -1) { printf("\n open() failed with error [%s]\n",strerror(errno)); return -1; } // configure UART // check http://pubs.opengroup.org/onlinepubs/007908799/xsh/termios.h.html struct termios options; tcgetattr(uart0, &options); // set 115200 baud both ways cfsetispeed(&options, B115200); cfsetospeed(&options, B115200); // 8 bits, no parity, no stop bits options.c_cflag &= ~PARENB; options.c_cflag &= ~CSTOPB; options.c_cflag &= ~CSIZE; options.c_cflag |= CS8;
// inter-character timer unused options.c_cc[VTIME] = 0; // blocking read until x chars received options.c_cc[VMIN] = 1; // Canonical mode options.c_lflag |= ICANON; // save the serial port settings tcflush(uart0, TCIFLUSH); tcsetattr(uart0, TCSANOW, &options); // create a thread for handling data on UART ret = pthread_create (&thread, NULL, handling_uart, &uart0); if (ret) { printf("\n pthread_create() failed with error [%s]\n",strerror(errno)); return -1; } char c = getchar(); status = pthread_cancel(thread); if (status) { printf("\n pthread_cancel() failed with error [%s]\n",strerror(errno)); return -1; } close(uart0); return 0; }
This code will write data to UART /dev/ttyMFD2 . We use a thread for asynchronous writing. Now you can compile this file as follows. $ gcc -pthread -o uart uart.c
You can run the program. $ ./uart
If success, you get program output, shown in Figure below.
I use PuTTY to connect /dev/ttyMFD2 from computer. The following is output of serial data using PuTTY.
5.5 SPI We can implement the solution for problem on section 3.7 and 4.5 using C. Basically we can use mraa_spi_write() to write data and get a response on SPI channel. Now you can write the program by creating a file, called spi.c, and write this code.
#include "mraa.h" #include #include int main(int argc, char **argv) { mraa_init(); mraa_spi_context spi; spi = mraa_spi_init(5); unsigned int response = 0; uint8_t data[] = {0x00, 000}; uint8_t *recv; printf("Reading and Writing SPI\n"); int i; for (i = 10; i < 20; i++) { data[1] = i; recv = mraa_spi_write_buf(spi, data, 2); printf("Sent: %i\n",i); printf("Received: %i-%i\n",recv[0],recv[1]); usleep(100000); } }
This program will write data from 10 to 19 using mraa_spi_write(). This function will return a response from SPI channel. Save and compile the program. $ gcc -lmraa -o spi spi.c
Then, you can execute the program. $ ./spi
If success, you can get response as Figure below.
5.6 I2C/TWI In this section we build a program as explained on section 4.6. We use Arduino as I2C source. To read data on I2C channel, we can use mraa_i2c_read(). Now you can start by creating a file, called i2c.c, and write this code. #include #include #include #include #define I2C_ADDR 0x15 // you can change it sig_atomic_t volatile isrunning = 1; void sig_handler(int signum); int main () { signal(SIGINT, &sig_handler); uint8_t * buff; mraa_init();
mraa_i2c_context i2c; i2c = mraa_i2c_init(1); int n = 0; mraa_i2c_address(i2c, I2C_ADDR); while (isrunning){ int len = mraa_i2c_read(i2c,buff,1); if(len>0){ fprintf(stdout, "Received: %x\n", buff[0]); }else{ fprintf(stdout, "no data\n"); } n++; if(n>=10){ isrunning = 0; } sleep(1); } mraa_i2c_stop (i2c); fprintf(stdout, "\nDone\n"); return MRAA_SUCCESS; } void sig_handler(int signum) { if (signum == SIGINT) isrunning = 0; }
Explanation: Initialize mraa library by calling mraa_init() Access I2C using mraa_i2c_init() Set I2C address by calling mraa_i2c_address() Read I2C data using mraa_i2c_read() Release all resource usage using mraa_i2c_stop()
Save this code and try to compile. $ gcc -lmraa -o i2c i2c.c
Then, you can execute this program. $ ./i2c
If success, you can get response on Terminal. The following is a sample output.
You can verify this output on Arduino via Serial Monitor.
6. Intel Edison Development using Node.js
This chapter explains how to work and write program using Node.js to access Intel Edison I/O from Linux.
6.1 Getting Started Intel IoT distro provides mraa library which can be used for Node.js development. In this chapter, we will focus on Node.js development to access Intel Edison from Linux. We will use mraa library in our implementation. You can read mraa source code on https://github.com/intel-iot-devkit/mraa . We will explore several scenario to illustrate how Node.js work with Intel Edison board. The following is our lab scenario: GPIO Analog I/O UART SPI I2C/TWI Intel also provides a tool, called Intel XDK IoT edition, https://software.intel.com/en-us/html5/xdk-iot . You can use this tool to write JavaScript. To access Intel Edison from Node.js, we can use mraa library which has already installed for you. You can test mraa version by writing the following code. var m = require('mraa'); console.log('MRAA Version: ' + m.getVersion());
Save this code into a file, called testversion.js. Now you can execute it. $ node testversion.js
If success, you can see mraa version.
Next section we explore several scenario to access Intel Edison using mraa library.
6.2 GPIO In this section, we learn how to access GPIO on Intel Edison using Node.js. For illustration, we use the same problem on section 5.2. To start to write a program, you can create a file, called gpiodemo.js, and write this code. var m = require('mraa'); var led1 = new m.Gpio(10); var led2 = new m.Gpio(9); var led3 = new m.Gpio(8); led1.dir(m.DIR_OUT); led2.dir(m.DIR_OUT); led3.dir(m.DIR_OUT); var ledon = 1; setInterval(function(){ blinkLED(); ledon++; if(ledon>3) ledon = 1; },1000); function blinkLED() { switch(ledon){ case 1: led1.write(1); led2.write(0); led3.write(0); break;
case 2: led1.write(0); led2.write(1); led3.write(0); break; case 3: led1.write(0); led2.write(0); led3.write(1); break; } console.log("LED " + String(ledon) + " ON"); } process.on('SIGINT', function() { led1.write(0); led2.write(0); led3.write(0); console.log("\nDone"); process.exit(); });
Explanation: Open GPIO pin using mraa.Gpio() Set I/O direction via mraa.dir() To write data to GPIO, we can use mraa.write()
Save this code and try to run. $ node gpiodemo.js
A sample output is shown in Figure below.
6.3 Analog I/O - Reading Analog Input In this section, we solve a problem on section 5.3 using Node.js. We can use mraa.Aio() to read analog input from Intel Edison board. Call Aio.read() to read incoming data on analog input pin. For illustration, create a file, called analog_input.js, and write this code. var m = require('mraa'); var a0 = new m.Aio(0); a0.setBit(10); console.log("reading analog input>>>"); setInterval(function(){ readAnalog() }, 1000); function readAnalog() { var val = a0.read(); console.log(">> Analog A0 value: " + String(val)); }
Then, run this program. $ node analog_input.js
If success, you will get analog input value on Terminal.
6.4 UART To access UART, we can use node-serialport library, https://github.com/voodootikigod/node-serialport . Type the following command to install node-serialport. $ npm install serialport
We use the same scenario on section 5.4. The program will write data to serial port, /dev/ttyMFD2, every a second. Create a file, called uart.js, and write this code. var SerialPort = require("serialport").SerialPort; var port = "/dev/ttyMFD2"; var serialPort = new SerialPort(port, { baudrate: 115200 },false); console.log("Open port: " + port); serialPort.open(function (error) { if (error) { console.log('failed to open: '+error); } else { // write data to serial port every a second var counter = 90; setInterval(function () { serialPort.write(String(counter) + "\r\n", function(err) { if(err){ console.log('err ' + err); }else{ console.log('Write data ' + counter); } }); counter++; if(counter>100) counter = 90;
}, 1000); } });
Save this code and try to run. $ node uart.js
Program output can be seen in Figure below.
Open serial monitor, for instance PuTTY. Navigate to serial port which be connected to /dev/ttyMFD2 (micro USB).
6.5 SPI mraa library provides Node.js interface to access SPI on Intel Edison. We can use mraa.Spi() to get mraa object. Then, we can use Spi.read() to read data on SPI channel. For testing, we implement a problem on section 3.7, 4.5 and 5.5 using Node.js. Create a file, called spi.js, and write this code. var m = require('mraa'); var spi = new m.Spi(5); var counter = 0; console.log("Writing and Reading SPI.."); setInterval(function(){ readAnalog() }, 1000); function readAnalog() { var recv = spi.write(String(counter)); console.log(">> SPI sent value: " + String(counter)); console.log(">> SPI received value: " + String(recv));
counter++; if(counter>=10) counter = 0; }
Run the program. $ node spi.js
A sample output is shown in Figure below.
6.6 I2C/TWI The last demo is to write a program to access I2C on Intel Edison. This is problem based on section 4.6. Plug in your Arduino I2C to Intel Edison I2C. Then, write the following code.
var m = require('mraa'); var i2c = new m.I2c(1); i2c.address(0x15); // change it based on I2C on Arduinop
console.log("Reading I2C.."); setInterval(function(){ readAnalog() }, 1000); function readAnalog() { var val = i2c.read(); console.log(">> I2C value: " + String(val)); }
Explanation: Open GPIO pin using mraa.I2c() Set I2C address using i2c.address(0x15). Assume I2C address is 0x15 To write data to GPIO, we can use mraa.write()
Save this code into a file, called i2c.js, and run it. $ node i2c.js
You can see the sample output in Figure below.
To verify the result on Arduino, you can use Serial Monitor from Arduino software.
7. Bluetooth Low Energy (BLE) and iBeacon
This chapter explains how to work with Bluetooth Low Energy on Intel Edison.
7.1 Bluetooth Low Energy (BLE) Bluetooth low energy or Bluetooth LE is a wireless personal area network technology designed and marketed by the Bluetooth Special Interest Group aimed at novel applications in the healthcare, fitness, beacons, security, and home entertainment industries. In this chapter, we explore how to work with Bluetooth low energy on Intel Edison. By default, Intel Edison disables this device. To enable BLE, you can type the following command on Terminal. $ rfkill unblock bluetooth
7.2 Pairing BLE Devices For the first demo, we try to connect our Intel Device to another device via BLE. In this case, I use my Nexus 7 2013, running Android 5, which will be connected to Intel Edison. We use bluetoothctl to pair BLE devices. Type the following command. $ bluetoothctl
Then, you will get a prompt, shown in Figure below.
To get a list of command, you type help.
If you want to know BLE controller in the board, you can use show command.
To connect to BLE device, firstly we scan to find BLE devices using command scan on.
After you get BLE device name, you can use pair command. pair D8:50:E6:7E:CB:87
If success, you will get notification on BLE device. A sample notification from Nexus 7 2013. Tap PAIR.
After that, Intel Edison will get notification.
A sample output on Android Nexus 7 2013.
We can use hcitool to check Bluetooth LE devices. Type the following command. $ hcitool lescan
If there is BLE device, hcitool will detect it.
After paired and connected, you can communicate among BLE devices.
7.3 BLE Development and iBeacon To develop BLE program in Intel Edison, you must install bluez25-dev. Firstly, configure your repo. See section 2.12. Then, type these commands. $ opkg update $ opkg install bluez5-dev
After that, we test our Intel Edison bluetooth for iBeacon using bleno, https://github.com/sandeepmistry/bleno , and noble, https://github.com/sandeepmistry/noble libraries. $ npm install -g async $ npm install noble $ npm install bleno
We can run test file, for instance, test-ibeacon.js, from bleno. $ node node_modules/bleno/test-ibeacon.js
Now you can test it using iBeacon app, for instance, I use iBeaconDetector from Google Play store. It runs on my Nexus 7 2013 and detects Intel Edison iBeacon. A sample output is shown in Figure below.
I also test it using iBeacon Scanner on Android app.
7.4 Further Reading To learn more BLE programming, you can read BLE resources on books and websites. I'm finishing my new book about Bluetooth Low Energy (BLE) Programming by Example. I hope it will release on year 2015.
8. Working with XBee IEEE 802.15.4
This chapter explains how to work with XBee IEEE 802.15.4 on Intel Edison and write program using Python.
8.1 XBee IEEE 802.15.4 Digi International provides XBee products with specific features. You can see product comparison for XBee on this document, http://www.digi.com/pdf/chart_xbee_rf_features.pdf . In this book, we will focus on XBee IEEE 802.15.4. XBee-PRO 802.15.4 modules are embedded solutions providing wireless end-point connectivity to devices. These modules use the IEEE 802.15.4 networking protocol for fast point-to-multipoint or peer-to-peer networking. They are designed for highthroughput applications requiring low latency and predictable communication timing. You can read XBee product with IEEE 802.15.4 protocol on this website, http://www.digi.com/products/wireless-wired-embedded-solutions/zigbee-rf-modules/point-multipointrfmodules/xbee-series1-module . The following is a sample picture of XBee 802.15.4.
8.2 Getting Hardware How to get XBee IEEE 802.15.4 device? Officially you can buy it from Digi International, http://www.digi.com/howtobuy/ . You can buy this product from official distributor, http://www.digi.com/howtobuy/find-a-distributor . You also can buy these products on the following electronics online store: Sparkfun, https://www.sparkfun.com/ . You can see a list of XBee IEEE 802.15.4 product on https://www.sparkfun.com/categories/223 Amazon, http://www.amazon.com/ or your local Amazon website Cooking-hacks, http://www.cooking-hacks.com/ Watterott electronic, http://www.watterott.com/index.php?page=manufacturers&mnf=22 Exp-tech, http://www.exp-tech.de/index.php?manu=m35_Digi.html Ebay, http://www.ebay.com/
I suggest you to find local electronics store which sells XBee IEEE 802.15.4.
8.3 Connecting XBee IEEE 802.15.4 to Intel Edison By default, you can't connect your XBee module to computer and breadboard directly. You need shield in order to connect to computer or breadboard. The following is a list of sample of XBee shield which we can connect it via USB. XBee Explorer USB, https://www.sparkfun.com/products/8687 XBee Explorer Dongle, https://www.sparkfun.com/products/9819 UartSBee V4, http://www.seeedstudio.com/depot/UartSBee-V4-p-688.html Foca, http://imall.iteadstudio.com/im120525005.html
The following is a sample of XBee shield with USB.
You also can connect it directly from XBee pins so you need XBee regular shield, https://www.sparkfun.com/products/11373, or you add additional pins on your shield. Another approach, you can use XBee shield for Ardunio, http://www.cookinghacks.com/communication-shield-xb-bt-rfid . You can buy it in your local store. A sample shield can be seen in Figure below.
In this chapter, I connect my XBee to USB so I need an adapter for Micro USB to USB, http://www.amazon.com/SANOXY-Micro-USB-OTG-Adapter/dp/B005QX7KYU/ , and a cable for USB to mini USB, http://www.amazon.com/AmazonBasics-USB-Male-Mini-Cable/dp/B00E9IF9RI/ . You can find them on your local/online electronics store.
Now you can connect these items to Intel Edison via USB Host. The following is hardware implementation, shown in Figure below.
Don't forget to select Host USB on switch SW1
After connected, you can verify it and find /dev/ttyUSB* on Terminal of Intel Edison
$ ls /dev/ttyUSB*
You will see Intel Edison doesn't detect because FTDI driver doesn't install yet. Firstly, you update your repository with unofficial repo, see section 2.12. Now you can install FTDI driver for Intel Edison. $ opkg update $ opkg install kernel-module-ftdi-sio
Try to check your USB now. $ ls /dev/ttyUSB*
You will see XBee on USB, for instance, my XBee shows on /dev/ttyUSB0.
Now your XBee Explorer USB is be connected to Intel Edison for Arduino breakout and ready for writing program.
8.4 XBee with AT Command XBee IEEE 802.15.4 provides a communication mechanism through AT command. It can be achieved by opening serial communication to XBee. In this section, we explore how to access XBee IEEE 802.15.4 using Python from Linux on Intel Edison. There are many AT commands for XBee. You can read these commands on the following document, http://examples.digi.com/wpcontent/uploads/2012/07/XBee_802.15.4_AT_Commands.pdf . Each command begins with "AT". X-CTU software is useful tool to manage XBee device. Unfortunately, this tool is be provided only for Windows Platform. You can emulate it on Linux using Wine,http://www.winehq.org/ . You can download X-CTU software on http://www.digi.com/support/productdetail?pid=3352 . After installed, you can run it. The following is a sample of X-CTU application.
We explore several commands to illustrate how to work with XBee through AT command. See section 9.6.
8.5 Demo Chatting Program The objective of this demo is to illustrate how to work XBee through AT command. In this scenario we need: Two XBee IEEE 802.15.4 Two XBee shield (XBee Explorer) with USB connector
Let's start to execute our demo.
8.5.1 Hardware Configuration To configure hardware for this scenario, we connect XBee 1 to computer and XBee 2 to Intel Edison.
XBee 2 adapter is connected to Intel Edison via USB Host.
8.5.2 XBee Configuration Firstly, we must configure two XBee so they can communicate each other. The following is XBee configuration. XBee 1: PAN ID (ID): 3001 Address (MY): 1 Destination address high (DH): 0 Destination address low (DL): 3 API Enable (AP): 0 XBee 2: PAN ID (ID): 3001 Address (MY): 3 Destination address high (DH): 0 Destination address low (DL): 1 API Enable (AP): 0
Note: (XX) is AT command name. To configure these settings, we execute using AT command or via X-CTU. Don't forget to save these configurations to XBee firmware.
8.5.3 Writing Python Program for Intel Edison On Intel Edison board, we create a program to read incoming data from XBee. Create a file, called xbee_reader.py, and write this code. import serial import time port = '/dev/ttyUSB0' baudrate = 9600
print 'open ' + port xbee = serial.Serial(port, baudrate) print 'waiting incoming message...' while True: try: out = '' while xbee.inWaiting() > 0: out += xbee.read(1) if out != '': print out time.sleep(1) except (KeyboardInterrupt, SystemExit): xbee.close() raise
Save this code.
8.5.4 Testing Now you can execute Python program on Intel Edison Terminal. $ python xbee_reader.py
On computer, we use a serial application. For serial application, I used CoolTerm. You can download CoolTerm application on http://freeware.the-meiers.org/ .
How to use and connect to XBee? Firstly, we connect XBee to computer via USB. On CoolTerm, click menu Connection -> Options. Then you obtain this dialog.
Fill Port of your XBee, Baudrate 9600, Data Bits 8, Parity none and Stop Bits 1. If you can't find your XBee port, you can re-scan by clicking Re-Scan Serial Ports button. After that, click Terminal on left menu side. Checked Local Echo.
If done, click OK button. Now you can write message on CoolTerm.
Python program will receive this message. A sample output is shown in Figure below.
8.6 XBee with API Mode XBee can be programmed via API Mode. To achieve this, you must configure our XBee with API Mode using X-CTU.
One of Python implementation for XBee with API Mode is Python XBee, https://pypi.python.org/pypi/XBee . You can find Python XBee source code on Source: https://code.google.com/p/python-xbee/ . Download and install this library into your computer. Download this library and store to Intel Edison. After that, you can install it.
$ wget --no-check-certificate https://pypi.python.org/packages/source/X/XBee/XBee $ tar -xvzf XBee-2.1.0.tar.gz $ cd XBee-2.1.0 $ python setup.py install
For illustration, we configure our previous demo, section 9.4, using API Mode. Please configure them with API enable W/PPP. Now on Intel Edison board side, we write a program to access XBee. Create a file, called at_command_demo.py, and write this code. from xbee import XBee import serial
port = '/dev/ttyUSB0' baudrate = 9600 print 'open ' + port ser = serial.Serial(port, baudrate) xbee = XBee(ser, escaped=True) # VR command xbee.send('at', frame_id='A', command='VR') response = xbee.wait_read_frame() print response # ID command xbee.send('at', frame_id='B', command='ID') response = xbee.wait_read_frame() print response # MY command xbee.send('at', frame_id='C', command='MY') response = xbee.wait_read_frame() print response
xbee.halt() ser.close()
Save this code. Try to execute this code. $ python at_command_demo.py
If success, you can see response from XBee.
8.7 Further Reading I write a book about XBee IEEE 802.15.4 Programming. This book helps you how to get started with IEEE 802.15.4 programming through XBee device. All sample codes were written in Python to illustrate how to work with IEEE 802.15.4. If you're interest with this book, you can visit on my blog, http://blog.aguskurniawan.net/post/XBee-IEEE-802154-Programming.aspx .
Source Code
Source code can be downloaded on http://www.aguskurniawan.net/book/edisoncode.zip.
Contact
If you have question related to this book, please contact me at
[email protected] . My blog: http://blog.aguskurniawan.net.