Beruflich Dokumente
Kultur Dokumente
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
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
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 .
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.
Then, you open it. You will find Intel Edison board and its shield.
Now you can plug the Intel Edison on shield. If success, you can see green light on the board, shown in
Figure below.
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.
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.
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.
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
This chapter explains how to work with Yocto Linux on Intel Edison board.
Check serial port which Intel Edison is be attached. After that, open PuTTY (select Serial) for Windows
or Screen for Ubuntu.
$ 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 add a new HTML file, called test.html. Navigate to public
/usr/lib/edison_config_tools/public. Type the following command.
$ vi test.html
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.
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
GCC has already installed in Yocto Linux. You can check GCC version using the following command.
$ gcc -v
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.
You can install the new package using the following command.
$ opkg install <repo_name>
This chapter explains how to work Intel Edison and Arduino IDE software.
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.
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.
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.
3.2.3 Testing
To run application to Intel Edison board, you must deploy program. You can do it by clicking icon rightarrow (upload).
The following is a hardware implementation for connecting 7 segment display to Intel Edison.
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);
}
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.
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.
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.
void loop() {
val = analogRead(potentiometer);
Serial.print("Analog value: ");
Serial.println(val,DEC);
delay(1000);
}
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.
see the board, PWM pins are idenitified by 2 sign. You can see PWM pins on the following Figure
below.
Note:
Pin 1: Red
Pin 2: Common pin
Pin 3: Green
Pin 4: Blue
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);
}
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.
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 <SPI.h>
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.
This chapter explains how to work and write program using Python for accessing Intel Edison I/O from
Linux.
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.
$ 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()
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)
You can execute this program to Intel Edison. Type the following command.
$ python hello.py
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*
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.
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.
If you are working on Linux, you can use miniterm. You can see a sample output in Figure below.
4.5 SPI
We can verify installed SPI on Intel Edison by the following command.
$ ls /dev/spi*
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
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 IC. The name TWI was introduced by Atmel and
other companies to avoid conflicts with trademark issues related to IC.
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
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
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 <Wire.h>
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 <= 4; i++) {
t /= 10;
r *= ((t % 10) + 1);
r %= 11;
}
return (r - 1);
}
void sendData(){
byte data = randomDigit();
Wire.write(data);
Serial.print("send: ");
Serial.println(data,DEC);
}
void setup() {
pinMode(led, OUTPUT);
Serial.begin(9600);
Wire.begin(SLAVE_ADDRESS);
Wire.onRequest(sendData);
}
void loop() {
delay(100);
}
i2c = mraa.I2c(1)
i2c.address(0x15) # change it based on I2C on Arduinop
counter = 0
while True:
try:
data = i2c.read()
print ">>> " + str(data)
time.sleep(1)
counter += 1
if counter > 10:
break
except (KeyboardInterrupt, SystemExit):
raise
print "done"
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.
This chapter explains how to work and write program using C/C++ to access Intel Edison I/O from
Linux.
Let's start working with mraa library. We will check mraa library version using mraa_get_version().
Write the following code.
#include <stdio.h>
#include <syslog.h>
#include <mraa.h>
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;
}
Now you can compile this file by typing the following command.
$ gcc -lmraa -o testversion testversion.c
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 <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <mraa/gpio.h>
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);
If done, you can execute this program by typing the following command.
$ ./gpiodemo
Now you can write the program by creating a file, called analog_read.c, and write this code.
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <mraa/aio.h>
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;
}
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 <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
#include <string.h>
#include <errno.h>
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;
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
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 <unistd.h>
#include <stdint.h>
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
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 <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <mraa/i2c.h>
#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()
If success, you can get response on Terminal. The following is a sample output.
This chapter explains how to work and write program using Node.js to access Intel Edison I/O from
Linux.
Save this code into a file, called testversion.js. Now you can execute it.
$ node testversion.js
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()
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);
}
});
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;
}
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
To verify the result on Arduino, you can use Serial Monitor from Arduino software.
This chapter explains how to work with Bluetooth Low Energy on Intel Edison.
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.
We can use hcitool to check Bluetooth LE devices. Type the following command.
$ hcitool lescan
After paired and connected, you can communicate among BLE devices.
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
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.
This chapter explains how to work with XBee IEEE 802.15.4 on Intel Edison and write program using
Python.
I suggest you to find local electronics store which sells XBee IEEE 802.15.4.
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.
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
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.
We explore several commands to illustrate how to work with XBee through AT command. See section
9.6.
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/ .
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.
Python program will receive this message. A sample output is shown in Figure below.
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.
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()
Source Code
Contact
If you have question related to this book, please contact me at aguskur@hotmail.com . My blog:
http://blog.aguskurniawan.net.