Vous êtes sur la page 1sur 121

Copyright

Arduino T IAN Development Workshop

Agus Kurniawan

1st Edition, 2017

Copyright © 2017 Agus Kurniawan


Table of Contents
Copyright
Preface
1. Preparing Development Environment
1.1 Arduino TIAN
1.2 Electronic Components
1.2.1 Arduino Starter Kit
1.2.2 Fritzing
1.2.3 Cooking-Hacks: Arduino Starter Kit
1.2.4 Arduino Sidekick Basic kit
1.3 Unboxing
2. Getting Started with Arduino TIAN
2.1 Getting Started
2.2 Setting up Arduino TIAN for the first time
2.3 Exploring Arduino TIAN Web
2.4 Reseting Wifi Settings
2.5 SSH
2.6 SFTP
2.7 Restart and Shutdown
2.8 Updating Linino for Arduino TIAN
2.9 Expanding Storage
3. Arduino TIAN Sketch Programming
3.1 Getting Started
3.2 Arduino Software and Driver
3.3 Connecting Arduino IDE Sketch to Arduino TIAN
3.3.1 Serial Port
3.3.2 Remote via WiFi and Ethernet
3.4 Hello World: Blinking LED
3.4.1 Wiring
3.4.2 Writing Blinking LED Program
3.4.3 Deploying and Testing via Serial Port
3.4.4 Deploying and Testing via Remote WiFi/Ethernet
3.5 Digital I/O
3.5.1 Wiring
3.5.2 Writing Program
3.5.3 Testing
3.6 Serial Communication
3.7 Analog I/O
3.7.1 Analog Input Demo: Potentiometer
3.7.1.1 Wiring
3.7.1.2 Writing Program
3.7.1.3 Testing
3.7.2 Analog Output/PWM: RGB LED
3.7.2.1 Wiring
3.7.2.2 Writing Program
3.7.2.3 Testing
3.8 SPI
3.9 I2C
4. Arduino TIAN Linux Programming
4.1 Getting Started
4.2 Arduino TIAN Package Manager
4.3 C/C++
4.4 Python
4.5 Node.js
4.6 Introduce Ciao Library
5. Working with Bluetooth on Arduino TIAN
5.1 Getting Started
5.2 Working with Bluetooth on Arduino TIAN
5.3 iBeacon
6. Building IoT Program for Arduino TIAN
6.1 Getting Started
6.2 Deploy REST Server for Arduino TIAN
6.3 Testing Digital I/O
6.4 Testing Analog I/O
7. Energy Saving and RTC Libraries
7.1 Getting Started
7.2 Energy Saving
7.3 RTC
Source Code
Contact
Preface

This book was written to help anyone want to get started with Arduino TIAN. It describes the basic elements of the
Arduino TIAN operations and development.

Agus Kurniawan

Berlin and Depok, February 2017


1. Preparing Development Environment

1.1 Arduino TIAN


Arduino TIAN is Arduino board, powered by Atmel’s SAMD21 MCU, featuring a 32-bit ARM Cortex® M0+ core and a
Qualcomm Atheros AR9342, which is an highly integrated MIPS processor operating at up to 533MHz and feature-rich
IEEE802.11n 2x2 2.4/5 GHz dual-band WiFi module. Qualcomm Atheros MIPS supports a Linux distribution, based on
OpenWRT named Linino.

The Arduino Tian has also a build in 4GB eMMC memory that will can be helpful to build your projects. It is possible to
Switch ON/OFF the Linux port from the MCU to reduce the power consumption.

Further information about Arduino TIAN, you can visit the official website on this
site, http://www.arduino.org/products/boards/arduino-tian.

The following is feature of Arduino Microprocessor

Processor: Atheros AR9342


Architecture: MIPS
Operating Voltage: 3.3 V
Flash Memory: 16 MB + 4 GB eMMC
RAM: 64MB DDR2
Clock Speed: 560 MHz
WiFi: 802.11 b/g/n 2.4 GHz dual-band
Ethernet: 802.3 10/100/1000 Mbit/s
USB: 2.0 Host

The following is feature of Arduino Microcontroller

Microcontroller: SAMD21G18
Architecture: ARM Cortex-M0+
Operating Voltage: 3.3 V
Flash memory: 256 KB
SRAM: 32 KB
Clock Speed: 48 MHz
Analog I/O Pins: 6
DC Current per I/O Pins: 7mA (I/O Pins)

The following is general features of Arduino TIAN

Input Voltage: 5 V
Digital I/O Pins: 20
PWM Output: 12
Power Consumption: 470 mA
PCB Size: 53 x 68.5 mm
Bluetooth: CSR8510, Bluetooth with EDR / BLE 4.0
Weight: 36 g
The following is a form of Arduino TIAN.

The diagram of Arduino TIAN can be seen in Figure below.

You can buy this product on your local electronic store. You also can order it by online. Find it
on http://world.arduino.org/en. You can also buy this board on your favourite online store.
1.2 Electronic Components
We need electronic components to build our testing, for instance, Resistor, LED, sensor devices and etc. I recommend you
can buy electronic component kit.

1.2.1 Arduino Starter Kit


Store website: http://arduino.cc/en/Main/ArduinoStarterKit

1.2.2 Fritzing
Store website: http://shop.fritzing.org/ .

You can buy Fritzing Starter Kit with Arduino UNO or Fritzing Starter Kit with Arduino Mega.
1.2.3 Cooking-Hacks: Arduino Starter Kit
Store website: http://www.cooking-hacks.com/index.php/shop/arduino/starter-kits/arduino-starter-kit.html
1.2.4 Arduino Sidekick Basic kit
Store website: http://www.seeedstudio.com/depot/arduino-sidekick-basic-kit-p-775.html

Alternative online store

http://www.amazon.com/Arduino-Sidekick-Basic-Kit-Version/dp/B007B14HM8/

http://www.exp-tech.de/Zubehoer/Arduino-Sidekick-Basic-Kit.html

1.3 Unboxing
If you buy Arduino TIAN, you should get this package.
After opened, you should see Arduino TIAN board, shown in Figure below.
This case we still the following items:

Power adapter with 5V


micro USB cable for power if you don't have power adapter
USB cable

For electronics component, I user Arduino Sidekick Basic kit and Linker kit, http://store.linksprite.com/linker-kit/, for
electronic components. Other additional electronic components are also welcomed.
2. Getting Started with Arduino TIAN

This chapter explains how to get started with Arduino TIAN.

2.1 Getting Started


In this chapter, we learn how to set up Arduino TIAN for the first time via WiFi and then connect to the existing WiFi. We
also also explore some basic operations of Arduino TIAN.

Let's start.

2.2 Setting up Arduino TIAN for the first time


In this section, firstly, we try to connect to Arduino TIAN via Wifi. This board create AP with SSID Arduino-Tian-
XXXXXX. XXXXXX is random characters. Plug in power adapter to micro USB Arduino TIAN. Note: Please use 5V
DC for Arduino TIAN power.

From your computer, open your Wifi manager, you can see the SSID Arduino-Tian-XXXXXX, for instance, you can see it
in Figure below.

If you see this SSID, try to connect to it.

After that, open a browser and navigate tp http://arduino.local . If success, you see this page.
By default, the board password is arduino. Click Login button to authenticate.

If success, you will get this page.


If you see the Figure above, your Arduino TIAN runs for the first time. Click Next menu.

Fill Arduino name, Timezone, the password of Arduino. If done, click Next button.
Now we configure our Arduino to connect the existing WiFi. Select your SSID included SSID key if it's available.
You also configure for REST API setting. You can set 1 to enable it.
Click Next button if finished. Your Arduino should be restarted.
Please wait for a minute. Arduino will connect to existing WiFi that you have configured it.

Now your computer should connect to existing WiFi in order to communicate with your Arduino.

Then, try to connect via browser by navigating to URL http://arduino.local.


2.3 Exploring Arduino TIAN Web
Arduino TIAN can be explored via a browser. You can see a list of menu for our Arduino.
You can see the following Figure for Arduino menu on a browser.
Settings dialog in Arduino browser.
You can open a Terminal on Website or External Window.
The following is a form of Arduino TIAN terminal.
2.4 Reseting Wifi Settings
We can reset our existing Wifi settings on Arduino TIAN. It's easy. You just press and hold (20-30 seconds) WiFi RESET
button on the board. See this button in Figure below.
If success, Arduino TIAN will back to original Wifi settings. You also see Arduino TIAN AP with SSID Arduino-Tian-
XXXX .

2.5 SSH
Arduino TIAN has installed SSH so we can connect to Arduino TIAN via SSH. Firstly, your computer already connect to
Arduino TIAN or both your computer and Arduino TIAN connect to the same Wifi or one IP segment (Wired).

You can use SSH client to connect Arduino TIAN. For Windows users, you can use PuTTY,
http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html .

Run your SSH client application. Fill hostname of Arduino TIAN is arduino.local or IP Address of Arduino TIAN and
port 22.
Now you get SSH Terminal. Entry user: root. Password is your Arduino password. By default is arduino.

2.6 SFTP
Arduino TIAN has instaled SSH and SFTP for you. Using SFTP client app, for instance, FileZilla, https://filezilla-
project.org, we can access SFTP on Arduino TIAN. Fill Arduino TIAN IP address, username and password.
After connected, you can see files and directories on Arduino TIAN.
2.7 Restart and Shutdown
We can restart Arduino TIAN using two methods. You can restart by press Linux reset, shown in Figure below.
You also can restart Arduino TIAN via SSH. You can type this command on your Terminal.

$ reboot

Arduino TIAN will restart. It takes several minutes to restart.

To turn off your Arduino TIAN, you can plug out power adapter from the board. You also can turn off Arduino TIAN via
SSH and type this command.

$ poweroff
2.8 Updating Linino for Arduino TIAN
In this section, we try to update Arduino TIAN firmware. When we buy Arduino TIAN, the board firmware may has old
version so we need to update. We can update Arduino TIAN easily while this board is connecting to Internet.

The latest of Arduino TIAN firmware can be found on http://download.linino.org/linino_distro/linino-2.0/samd/. Make sure
your Arduino connected to Internet. Open Terminal and connect to Arudino TIAN via SSH. Then, type this command.

$ cd /tmp
$ wget http://download.linino.org/linino_distro/linino-2.0/samd/latest/lininoIO-generic-linino-tian-squash
$ sysupgrade -v lininoIO-generic-linino-tian-squashfs-sysupgrade.bin
This takes several minutes. After completed, your board will reboot. You can verify by checking the Linux version using
uname.
2.9 Expanding Storage
Arduino TIAN has internal storage, eMMC, about 4 GB. You should extend this storage to use the extra space. To work
this step, you should reset your configuration (default configuration). If you have configured any setting, you will lose all
settings. If not, you get error.

You can restore to default configuration by pressing and holding Linux Reset. You also perform to reset your Arduino
TIAN via web panel. Just open Arduino Luci setting. Then, select System -> Backup / Flash Firmaware. You should
Perform reset button which is shown in Figure below,
To verify your current disk size, you can type this command.

$ df -h

My Arduino TIAN shows my current disk size in Figure below.


Now you can expand your storage. You can type this command on Terminal of Arduino TIAN.

$ overlay-only -i

Press ENTER key to continue.


Select 1 to extend our storage.
If done, select 0 to exit.

Now your Arduino TIAN has additional storage. In my case, my Arduino TIAN has 2.5 GB free spaces on rootfs.
3. Arduino TIAN Sketch Programming

In this chapter I'm going to explain how to write program for Arduino TIAN using Arduino Sketch.

3.1 Getting Started


In this chapter, we learn how to develop program for Arduino TIAN using Arduino Sketch. We focus on I/O programming.
Several cases will be provided to show how to work with Arduino TIAN I/O. The following is our list of problem case:

Digital I/O
Serial Communication
Analog I/O
SPI
I2C/PWI

Let's go!.

3.2 Arduino Software and Driver


You need to install Arduino software in order to develop Sketch program. You can download it
on http://www.arduino.org/downloads. To recognize Arduino TIAN on your local computer, you should install Arduino
TIAN driver. You can download and install it on http://www.silabs.com/products/interface/Pages/CP2105EK.aspx.
After installed Arduino software and driver, you can run Arduino software. Click menu Tools -> Board -> Boards
Manager. Then, you should get a dialog. Type tian and then you should see Arduino SAMD Boards (32-bits ARM Cortex-
M0+). Install the latest version.
If done, restart Arduino application. Now you should see Arduino TIAN as target on Board menu.
If your Arduino TIAN is connected to computer or a network, you should see a serial port and network port for your
Arduino TIAN.

Now you're ready to develop,

3.3 Connecting Arduino IDE Sketch to Arduino TIAN


In this section, we try to connect Arduino TIAN via Serial Port, WiFi and Ethernet.

3.3.1 Serial Port


You can plug in Arduino TIAN board to computer directly through micro USB.

In Windows platform, you will see Arduino TIAN board on Device Manager. In Linux, you can see it as /dev/ttyACM* .
On OSX/macOS, you see it as /dev/cu* which is shown in Figure below.

Run Arduino Sketch software. You will see Arduino TIAN board connected. You can verify it by clicking menu Tools ->
Port -> COMx for Windows. On macOS, my Arduino TIAN is detected as /dev/tty.SLAB_USBtoUART* which is shown
in Figure below.
Now your Arduino Sketch is ready to develop app.

3.3.2 Remote via WiFi and Ethernet


If your computer already connect to the same WiFi (same SSID) or Ethernet, you will see Arduino with IP on Arduino
Sketch software, shown in Figure below.
If you don't see it, you can verify with this check list:

Upgrade Arduino TIAN with the latest firmware


Check if your computer block Arduino TIAN port. It uses UDP port 5353 for broadcasting.
Check your firewall. Make sure your firewall doesn't block the Arduino Sketch
Reinstall Bonjour if you use Windows platform. The latest Bonjour can be downloaded on
https://support.apple.com/kb/DL999?locale=en_US .

3.4 Hello World: Blinking LED


Now we start to write program for Arduino TIAN. We create a blinking LED app.

3.4.1 Wiring
There is no configuration on your board. We use a built-in LED, (Digital pin 13).

3.4.2 Writing Blinking LED Program


Run Arduino Sketch. Click menu File -> Examples -> 01.Basics -> Blink .
Then, you get a blink code sample.
3.4.3 Deploying and Testing via Serial Port
Now you can compile and deploy to Arduino TIAN. Firstly, we select board type on Arduino Sketch. Click menu Tools ->
Board -> Arduino TIAN .
The first scenario for deploying can be applied through Serial Port. Plug in your Arduino TIAN via micro USB to your
computer. After that, Arduino Sketch will detect your Arduino TIAN, for instance, COM8 for Windows platform. Then,
select this serial Port by clicking menu Tools -> Port -> <<Serial Port>>. Select Arduino TIAN Programming Port.
After that, you can compile and deploy by clicking those icons, see them in Figure below.
After deployed, you will see LED L13 is blinking.
3.4.4 Deploying and Testing via Remote WiFi/Ethernet
If you want to deploy Blink program to Arduino TIAN via remote WiFi/Ethernet, select remote port on Arduino Sketch.
Click menu Tools -> Board -> Arduino TIAN .
Then, select remote Arduino on Network port. Click menu Tools -> Port -> Arduino at xxxxx (Arduino Tian).

Then, try to compile and deploy to board.


When you deploy to the board, you will be asked to entry password for Arduino TIAN.
If done, you can see LED L13 is blinking.
3.5 Digital I/O
In this section, we build a program to access Arduino Digital I/O. In this section, we need the following components:

LED
Push button
Cables

When a push button is pressed, LED will ON.

Let's start.

3.5.1 Wiring
The demo wiring is simple. Connect your LED to digital pin 8 and push button to digital pin 9. A sample of hardware
implementation is shown in Figure below.
3.5.2 Writing Program
Now we can write program. Run Arduino Sketch and write this code.

int led = 8;
int button = 9;
int val = 0;

void setup() {
pinMode(led, OUTPUT);
pinMode(button, INPUT);

void loop() {
val = digitalRead(button);
digitalWrite(led, val);
}

Save this code as DigitalIO.

3.5.3 Testing
After written program, you can compile and deploy the program to the board.

For testing, you press that push button. Then, you can see LED is ON.
3.6 Serial Communication
We can access Serial Port (UART) from Arduino Sketch using Serial
object, http://www.arduino.org/learning/reference/Serial.

Open software Arduino Sketch and write this code.

unsigned long counter;


int led = 13;

void setup()
{
counter = 0;
Serial.begin(9600);
}

void loop()
{
Serial.println(counter);
delay(1000);
counter++;
if(counter>=100)
counter = 0;
}
Save this code with name SerialDemo. Now you can compile and deploy this program to board.

To verify this program, you can use Serial Monitor. It can be called by clicking menu Tools -> Serial Monitor.

If success, you get a dialog. It shows message from Arduino UART.


3.7 Analog I/O
In this section, we develop Arduino Sketch to access Analog I/O. Arduino TIAN has 12 analog inputs: A0 - A5, A6 - A11
(on digital pins 4, 6, 8, 9, 10, and 12). inputs A6-A11 are on digital i/o pins 4, 6, 8, 9, 10, and 12 respectively. Each analog
input provide 10 bits of resolution (i.e. 1024 different values).

For Analog output, it's called PWM. You obtain PWM pins easily but if you have Arduino TIAN, it writes DIGITAL
(PWM ~). It means your PWM pins can be found on DIGITAL pins which pin with ~, for instance, ~3,~5,~6,~9, ~10, ~11.

3.7.1 Analog Input Demo: Potentiometer


We use a Potentiometer to illustrate how to read analog input.

3.7.1.1 Wiring

We use Potentiometer as analog input source. Connect Potentiometer pins to VCC and Vout to Analog input A0 on
Arduino TIAN.

For implementation, I use a slide Potentiometer, http://store.linksprite.com/linear-slide-potentiometer-of-linker-kit-for-


pcduino-arduino/ . The following is hardware implementation.
3.7.1.2 Writing Program

To read analog input, we use analogRead(), http://arduino.cc/en/Reference/AnalogRead . Open Arduino Sketch and write
this code.

int val = 0;
int pot = 0;

void setup()
{
Serial.begin(9600);
}

void loop()
{
val = analogRead(pot);
Serial.print("A0=");
Serial.println(val);
delay(500);
}
Save this code, called PotDemo.

3.7.1.3 Testing

Compile and deploy the program to the board. To see analog input value, open Serial Monitor. It can get it by clicking menu
Tools -> Serial Monitor.

Try to change value on Potentiometer and see it on Serial Monitor. A sample output can be seen in Figure below.

3.7.2 Analog Output/PWM: RGB LED


This chapter explains how to control RGB LED connected to Arduino board using Arduino Sketch. We explore how to
access PWM (Pulse Width Modulation) Arduino.

In this scenario we build Arduino Sketch application to control RGB LED color using Arduino Analog output (PWM).
RGB LED has 4 pins that you can see it on 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 Arduino application and hardware implementation.

3.7.2.1 Wiring

Firstly we implement RGB LED hardware. The following is a hardware schema with Arduino TIAN.

For our testing, we configure the following PWM pins.

RGB LED pin 1 (red) is connected to Arduino PWM pin 9


RGB LED pin 2 is connected to Arduino VCC 5V or GND (depend on RGB LED datasheet)
RGB LED pin 3 (green) is connected to Arduino PWM pin 10
RGB LED pin 4 (blue) is connected to Arduino PWM pin 11

Here is a sample implementation with Arduino TIAN.


3.7.2.2 Writing Program

Now we implement our RGB LED controller in Arduino. This is for testing. Open Firstly, we define our RGB LED pins.
The following is RGB LED pins for Arduino TIAN.

int redPin = 11;


int greenPin = 10;
int bluePin = 9;

Now we initialize pins on setup().

void setup()
{
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
Serial.begin(9600);
}
We define a function, called setColor(). This function aims to write RGB values on PWM pins. We can use
analogWrite(), http://www.arduino.org/learning/reference/AnalogWrite.

void setColor(int red, int green, int blue)


{
analogWrite(redPin, red);
analogWrite(greenPin, green);
analogWrite(bluePin, blue);
}

Now we control RGB values on RGB LED, for instance, Red, Green, Blue, Yellow, Purple, Aqua.

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);
}

Save this code, called rgbdemo.ino.

Compile and verify this code. If success, you can upload it to Arduino board.

3.7.2.3 Testing

If success, you can see RGB LED blinking with different colors. Here is a sample output of RGB LED with Arduino
TIAN.
If you open Serial Monitor, you will get the following output.
3.8 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. Note that
the SPI pins are not connected to any of the digital I/O pins as they are on the Uno, They are only available on the ICSP
connector.

You can see ICSP pins for Arduino TIAN in Figure below.
The following is a schema of ICSP connector.

(source: http://arduino.cc/en/Reference/SPI)

Further information about SPI on Arduino TIAN, you can read it on http://www.arduino.org/learning/reference/SPI.

For illustration, we build a program to write and read data to/from Arduino TIAN SPI. We build a SPI loopback which
ICSP MOSI pin is be connected to ICSP MISO. Hardware implementation can be seen in Figure below.
Now we can write program. Open Arduino Sketch and write this code.

#include "SPI.h"
int val = 0;

void setup() {
SPI.begin();
Serial.begin(9600);
val = 0;
}

void loop() {
int ret = SPI.transfer(val);
Serial.print("send=");
Serial.print(val);
Serial.print(", recv=");
Serial.println(ret);

delay(1000);
val++;
if(val>=100)
val = 0;
}
This code send a counter number using SPI.Transfer() and return reading value from SPI. Because we connect MOSI to
MISO, we get the same data from sending and receiving.

Save this code as SPIDemo.ino.

Compile and deploy this program to the board. Open Serial Monitor to see SPI communication.

3.9 I2C
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.

I2C bus consists of two wires, SDA (Serial Data Line) and SCL (Serial Clock Line). Arduino TIAN has I2C/TWI on SDA
and SCL pins directly.
For testing, I used PCF8591 AD/DA Converter module with sensor and actuator devices. You can find it on the following
online store:

Amazon, http://www.amazon.com/PCF8591-Converter-Module-Digital-Conversion/dp/B00BXX4UWC/
eBay, http://www.ebay.com
Dealextreme, http://www.dx.com/p/pcf8591-ad-da-analog-to-digital-digital-to-analog-converter-module-w-dupont-
cable-deep-blue-336384
Aliexpress, http://www.aliexpress.com/

In addition, you can find this device on your local electronics store/online store.
This module has mini form model too, for instance, you can find it on Amazon, http://www.amazon.com/WaveShare-
PCF8591T-Converter-Evaluation-Development/dp/B00KM6X2OI/ .
This module use PCF8591 IC and you can read the datasheet on the following URLs.

http://www.electrodragon.com/w/images/e/ed/PCF8591.pdf
http://www.nxp.com/documents/data_sheet/PCF8591.pdf

The following is our wiring lab:

PCF8591 AD/DA SDA --> Arduino TIAN SDA


PCF8591 AD/DA SCL --> Arduino TIAN SCL
PCF8591 AD/DA GND --> Arduino GND
PCF8591 AD/DA VCC --> Arduino VCC +3.3V

Hardware implementation can be shown in Figure below.

Now you can write the program. We use Wire library to access I2C. You can read Wire library
on http://www.arduino.org/learning/reference/Wire.

Open Arduino Sketch and write this code.


#include <Wire.h>

#define PCF8591 (0x90 >> 1) // Device address


#define PCF8591_ADC_CH0 0x40 // thermistor
#define PCF8591_ADC_CH1 0x41 // photo-voltaic cell
#define PCF8591_ADC_CH2 0x42
#define PCF8591_ADC_CH3 0x43 // potentiometer

byte adc_value;
byte getADC(byte config)
{
Wire.beginTransmission(PCF8591);
Wire.write(config);
Wire.endTransmission();
Wire.requestFrom((int) PCF8591,2);
while (Wire.available())
{
adc_value = Wire.read(); //This needs two reads to get the value.
adc_value = Wire.read();
}
return adc_value;
}
void setup()
{
Serial.begin(9600);
Wire.begin();
Serial.println("ADC Test");
}
void loop()
{
adc_value = getADC(PCF8591_ADC_CH0);
Serial.println("thermistor: ");
Serial.println(adc_value);
delay(500);

adc_value = getADC(PCF8591_ADC_CH1);
Serial.println("photo-voltaic cell: ");
Serial.println(adc_value);
delay(500);

adc_value = getADC(PCF8591_ADC_CH3);
Serial.println("potentiometer: ");
Serial.println(adc_value);
delay(500);
}

Save this code, called I2CDemo.uno.

Compile and deploy this program to the board. To see I2C output, you can open Serial Monitor, shown in Figure below.
4. Arduino TIAN Linux Programming

This chapter explains how to work with Arduino TIAN Linux and then write programs for Arduino TIAN.

4.1 Getting Started


In this chapter, we explore Arduino TIAN Linux. We will build several programs for targerting Arduino TIAN

Let's explore!

4.2 Arduino TIAN Package Manager


To update Arduino TIAN package, we can use opkg command. Firstly, Arduino TIAN must be connected to Internet
network. You can do this by connecting Arduino TIAN WiFi to existing WiFi which has Internet access. You can also
connect your Arduino TIAN to Internet network via Ethernet cable.

After Arduino TIAN connected to Internet network, you can update Arduino TIAN package by typing this command on
SSH Terminal.

$ opkg update
Further information about opkg, you can read it on this site, http://wiki.openwrt.org/doc/techref/opkg .

4.3 C/C++
By default, Arduino TIAN Linux doesn't install gcc yet. You should install it manually.
Before installed gcc, you should expand your 4GB eMMC storage on Arduino TIAN. You can install gcc by typing
commands.

$ opkg update
$ opkg install gcc
If finished, you can verify its GCC version. Type this command.

$ gcc -v

You get a GCC version.


Now we can start to write a simple program, hello world. Type these commands.

$ touch hello.c
$ vi hello.c

You can use any text editor to write program. Then, we write this program.

#include<stdio.h>

main()
{
printf("Hello World");

}
Save this and quit from vi.

Now you can compile and run the program.

$ gcc -o hello hello.c


$ ./hello
4.4 Python
Arduino TIAN Linux has installed Python for you. You can test it via SSH Terminal. Type these commands.

$ python
>>> a = 3
>>> b = 5
>>> c = a*b
>>> c
>>> quit()
If you want to install Python pip, you can type these commands.

$ opkg update
$ opkg install distribute
$ opkg install python-openssl
$ easy_install pip

4.5 Node.js
We can develop Node.js program. Further information about Node.js, I recommend to read the official
website, https://nodejs.org/.

You can install Node.js on Arduino TIAN by typing these commands.

$ opkg update
$ opkg install node

If done, you can check Node.js version.

$ node -v
Now we can write the program using vi.

$ touch hello.js
$ vi hello.js

We write words "hello world".

console.log("hello world");
Save and quit from vi.

You can run the program by typing this command.

$ node hello.js
4.6 Introduce Ciao Library
Arduino Ciao is a easy-to-use and powerful technology that enables Arduino sketches to communicate intuitively with the
"outside World". It aims to simplify interaction between microcontroller and Linino OS allowing a variety of connections
with most common protocols, social networks, third-party services and applications.

You can see Ciao architecture in Figure below.


To work with Ciao on Arduino TIAN, you should install Ciao on Arduino TIAN.

$ opkg update
$ rm /etc/udev/udev.conf
$ opkg upgrade arduinoos udev python-base python-light
$ opkg install ciao
$ reboot

Ciao communicates between Sketch and Linux via connector. You should enable one connector at least.

You can see all Ciao connector files on /usr/lib/python2.7/ciao/conf folder. You can see them in Figure below.
For instance, we enable a file connector.

$ cd /usr/lib/python2.7/ciao/conf
$ vi file.ciao.json.conf

Please make sure enabled value is true which is shown Figure below.
If completed, please save all changes. Then, you should reboot your Arduino board.

The next step is to create hello.txt on /root folder.

$ touch hello.txt
$ vi hello.txt

Type "hello ciaao" message.


Save this content into /root/hello.txt file.

To work Ciao library in Arduino Sketch, you should install Arduino Ciao library. Download and install it
on https://github.com/arduino-org/arduino-library-ciao/releases.

Then, you can write this Sketch program.

#include <Ciao.h>

void setup() {
Ciao.begin();
Serial.println("Ciao started");
Serial.begin(9600);
}

void loop() {
delay(500);
Serial.println("reading..");

CiaoData data = Ciao.read("file","/root/hello.txt");


if(!data.isEmpty()){
String message = data.get(2);
Serial.println(message);
}
Serial.println("done");
delay(500);
}
Save this program as CiaoDemo.

Compile and upload this program to Arduino TIAN. Open Serial Monitor to see program.
5. Working with Bluetooth on Arduino TIAN

This chapter explains how to work with Bluetooth on Arduino TIAN.

5.1 Getting Started


Arduino TIAN has built-in Bluetooth with CSR8510, Bluetooth with EDR / BLE 4.0. In this chapter, we learn how to
access this Bluetooth.

Let's explore!

5.2 Working with Bluetooth on Arduino TIAN


To use Bluetooth on Arduino TIAN, we should install required libraries. Open Terminal on Arduino TIAN and type these
commands.

$ opkg update
$ opkg install kmod-bluetooth bluez-libs bluez-utils kmod-usb-core kmod-usb-uhci kmod-usb2 usbutils

To test Bluetooth state, you can type this command.

$ hciconfig

If you see hci0 down, you can turn on by typing this command.

$ hciconfig hci0 up

Then, check again its state.

$ hciconfig
To scan all Bluetooth beacons, you can type this command.

$ hcitool scan

If you have devices with installed Bluetooth, that command will show all Bluetooth around the device.
5.3 iBeacon
In this section, we learn to work with iBeacon on Arduino TIAN.

$ hciconfig hci0 up
$ hciconfig hci0 leadv 3
$ hciconfig hci0 noscan

Then, you can check to verify this Bluetooth.

$ hciconfig
To send iBeacon message, you can use this command.

$ hcitool -i hci0 cmd 0x08 0x0008 1E 02 01 1A 1A FF 4C 00 02 15 <UUID> <MAJOR> <MINOR> <POWER> 00

To test your iBeacon message, you can use iBeacon client app such as Beacon Scan. You can also find another iBeacon
client apps on mobile platform.
For testing, we send two iBeacon messages.

$ hcitool -i hci0 cmd 0x08 0x0008 1E 02 01 1A 1A FF 4C 00 02 15 63 6F 3F 8F 64 91 4B EE 95 F7 D8 CC 64 A8

$ hcitool -i hci0 cmd 0x08 0x0008 1E 02 01 1A 1A FF 4C 00 02 15 63 6F 3F 8F 64 91 4B EE 95 F7 D8 CC 64 A8


On iBeacon client app, you should see iBeacon messages with your own UUID.
6. Building IoT Program for Arduino TIAN

In this chapter We access Arduino TIAN from REST.

6.1 Getting Started


We can access Arduino TIAN using REST. In this chapter, we try to implement this feature.

6.2 Deploy REST Server for Arduino TIAN


To enable Arduino Yún to be accessed using REST, we must install Ciao library. Please read section 4.6 to deploy Ciao
library on Arduino TIAN.

The first step is to setup REST server connector. You should see restserver.json.conf file on
/usr/lib/python2.7/connectors/restserver/ folder.

If you open restserver.json.conf file, you should see it in Figure below.


The next step is to enable REST server connector on restserver.ciao.json.conf file on /usr/lib/python2.7/ciao/conf folder.
Make sure you set enabled is true.
Save these changes.

After finished, please reboot your Arduino TIAN by typing this command.

$ reboot

Now we deploy CiaoRestServer sketch program into Arduino TIAN so the board can serve REST request.

Open Arduino Sketch. Click menu File -> Examples -> Ciao -> CiaoRestServer. You will get a CiaoRestServer code.
Compile and deploy to Arduino TIAN.

6.3 Testing Digital I/O


After deployed Ciao library and CiaoRestServer to Arduino TIAN, we can use a browser.

In this scenario, we turn on/off on LED 13. Firstly, we send value 1 to digital output 13. Navigate this URL:

http://arduino.local/arduino/digital/13/1

You can change arduino.local domain if you already change it.


If you activate authentication mode, you will get the authentication dialog. Entry username and password.

You should see a lighting LED on digital pin 13. To turn off LED 13, you can type this URL.
http://arduino.local/arduino/digital/13/0

6.4 Testing Analog I/O


We also can work with analog I/O. We don't need to set mode for analog I/O. For instance, we want to read analog input
A1. Navigate this URL on your browser.

http://arduino.local/arduino/analog/1
7. Energy Saving and RTC Libraries

In this chapter I'm going to explain how to work with Energy saving and RTC libraries on Arduino TIAN.

7.1 Getting Started


Arduino TIAN provides features to save energy by sleeping MIPS and MCU. The board can wake up after we give GND
on specific pin. Other feature, we will explore RTC on Arduino TIAN.

Let's start!.

7.2 Energy Saving


Energy Saving library provides you to manage power on Samd boards. This library can standby microcontroller and wake-
up it by external interrupt or alarm match interrupt. During standby, the board drawing very low current from power supply,
about 2 mA. Further information about energy saving library, you can read it
on http://www.arduino.org/learning/reference/Energy-Saving.

For demo, we try to sleep MIPS and MCU. They will wake up after triggered by external power. We use energy saving
library, EnergySaving. You use two constants WAKE_EXT_INTERRUPT and WAKE_RTC_ALARM to wake up our
board.

Open Arduino sketch and write this program.

#include <EnergySaving.h>

EnergySaving nrgSave;
#define MIPS_PIN 32 //PA28 PIN 32
#define WAKE_UP_PIN 8 // wake up pin
int led = 13;

void setup() {
pinMode(MIPS_PIN,OUTPUT);
digitalWrite(MIPS_PIN,HIGH);

nrgSave.begin(WAKE_EXT_INTERRUPT, WAKE_UP_PIN, wakeUp); //standby setup for external interrupts


}

void loop() {
perform();// perform something
sleep_system();// sleep MIPS and MCU
}

void perform() {
//blink for 10 seconds
for(int i=0; i<10; i++)
{
digitalWrite(led,HIGH);
delay(500);
digitalWrite(led,LOW);
delay(500);
}
}

void sleep_system(void){
// MCU and MIPS go in standby mode
digitalWrite(MIPS_PIN, LOW);
nrgSave.standby(); //now mcu goes in standby mode
}

void wakeUp(void) {
//mcu is waked-up by the interrupt
//wake up mips
digitalWrite(MIPS_PIN,HIGH);
}

Save this program as EnergySaving. Compile and upload the program to Arduino TIAN.

You should see blinking LED for 10 seconds. Then, the board is sleep.

To wake up the board, you should connect digital pin 8 to GND using a jumper cable. If the board is already wake up, you
should release a jumper cable from Arduino TIAN.
7.3 RTC
In this section, we work with RTC on Arduino TIAN. We use RTCInt library. You can the source of RTCInt library
on https://github.com/arduino-org/brackets-arduino/tree/master/libraries/RTC. This library has already installed. You can
read RTC API on this site, http://www.arduino.org/learning/reference/rtc-alarmmatch.

For demo, we set a specific date and time on Arduino. Then, we retrieve current date and time.

Open Arduino software and write this sketch program.

#include <RTCInt.h>
RTCInt rtc;

void setup() {
while (!Serial);
Serial.begin(9600);

rtc.begin(TIME_H24); //init RTC in 24 hour mode


Serial.println("RTC set time");
// set the current time 15 Feb 2017 7:23 AM
rtc.setDate(15,2,17);
rtc.setTime(7,TIME_H24,23,0);
delay(1000);

Serial.println("RTC Read time");


Serial.println("-------------------");
}

void loop() {
rtc.getDate();
rtc.getTime();

Serial.print("RTC, Time = ");


print2digits(rtc.getHour());
Serial.write(':');
print2digits(rtc.getMinute());
Serial.write(':');
print2digits(rtc.getSecond());
Serial.print(", Date (D/M/Y) = ");
Serial.print(rtc.getDay());
Serial.write('/');
Serial.print(rtc.getMonth());
Serial.write('/20');
Serial.print(rtc.getYear());
Serial.println();
delay(1000);
}

void print2digits(int number) {


if (number >= 0 && number < 10) {
Serial.write('0');
}
Serial.print(number);
}
Save the program as RTCDemo. Compile and upload sketch program to Arduino TIAN.

To see the program output, run Serial Monitor tool. A sample of program output can be seen in Figure below.
Source Code

You can download source code on http://www.aguskurniawan.net/book/tian1532.zip .


Contact

If you have question related to this book, please contact me at aguskur@hotmail.com . My blog:
http://blog.aguskurniawan.net
Table of Contents
Copyright 2
Preface 6
1. Preparing Development Environment 7
1.1 Arduino TIAN 7
1.2 Electronic Components 9
1.2.1 Arduino Starter Kit 9
1.2.2 Fritzing 9
1.2.3 Cooking-Hacks: Arduino Starter Kit 10
1.2.4 Arduino Sidekick Basic kit 11
1.3 Unboxing 11
2. Getting Started with Arduino TIAN 15
2.1 Getting Started 15
2.2 Setting up Arduino TIAN for the first time 15
2.3 Exploring Arduino TIAN Web 22
2.4 Reseting Wifi Settings 27
2.5 SSH 28
2.6 SFTP 29
2.7 Restart and Shutdown 31
2.8 Updating Linino for Arduino TIAN 33
2.9 Expanding Storage 36
3. Arduino TIAN Sketch Programming 42
3.1 Getting Started 42
3.2 Arduino Software and Driver 42
3.3 Connecting Arduino IDE Sketch to Arduino TIAN 45
3.3.1 Serial Port 46
3.3.2 Remote via WiFi and Ethernet 47
3.4 Hello World: Blinking LED 48
3.4.1 Wiring 48
3.4.2 Writing Blinking LED Program 48
3.4.3 Deploying and Testing via Serial Port 50
3.4.4 Deploying and Testing via Remote WiFi/Ethernet 54
3.5 Digital I/O 58
3.5.1 Wiring 58
3.5.2 Writing Program 59
3.5.3 Testing 59
3.6 Serial Communication 60
3.7 Analog I/O 62
3.7.1 Analog Input Demo: Potentiometer 62
3.7.1.1 Wiring 62
3.7.1.2 Writing Program 63
3.7.1.3 Testing 64
3.7.2 Analog Output/PWM: RGB LED 64
3.7.2.1 Wiring 66
3.7.2.2 Writing Program 67
3.7.2.3 Testing 68
3.8 SPI 72
3.9 I2C 75
4. Arduino TIAN Linux Programming 81
4.1 Getting Started 81
4.2 Arduino TIAN Package Manager 81
4.3 C/C++ 82
4.4 Python 87
4.5 Node.js 88
4.6 Introduce Ciao Library 91
5. Working with Bluetooth on Arduino TIAN 97
5.1 Getting Started 97
5.2 Working with Bluetooth on Arduino TIAN 97
5.3 iBeacon 99
6. Building IoT Program for Arduino TIAN 104
6.1 Getting Started 104
6.2 Deploy REST Server for Arduino TIAN 104
6.3 Testing Digital I/O 107
6.4 Testing Analog I/O 109
7. Energy Saving and RTC Libraries 111
7.1 Getting Started 111
7.2 Energy Saving 111
7.3 RTC 113
Source Code 117
Contact 118

Vous aimerez peut-être aussi