intel-south-africa-galileo-2014-jun (basic io)

28
1 Galileo Tutorial – I/Os, Sensing and Actuation WHAT WILL YOU MAKE? Galileo Tutorial Using Python on Galileo Senzations 2014 Biograd na Moru 1. September 2013 Alex Gluhak Intel Labs Europe

Upload: lamtuyen

Post on 14-Feb-2017

226 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Intel-South-Africa-Galileo-2014-Jun (Basic IO)

1 Galileo Tutorial – I/Os, Sensing and Actuation WHAT WILL YOU MAKE?

Galileo Tutorial Using Python on GalileoSenzations 2014

Biograd na Moru1. September 2013

Alex GluhakIntel Labs Europe

Page 2: Intel-South-Africa-Galileo-2014-Jun (Basic IO)

2 Galileo Tutorial – I/Os, Sensing and Actuation WHAT WILL YOU MAKE?

Getting started with Python on Galileo

• Running Python interpreter and scripts

• Importing and using modules

I/O basics in Python

• Serial I/O

• Analog and digital I/O

Network communication using MQTT

Learning goals

Page 3: Intel-South-Africa-Galileo-2014-Jun (Basic IO)

3 Galileo Tutorial – I/Os, Sensing and Actuation WHAT WILL YOU MAKE?

3

WHAT WILL YOU MAKE?

Getting started with Python on Galileo

Page 4: Intel-South-Africa-Galileo-2014-Jun (Basic IO)

4 Galileo Tutorial – I/Os, Sensing and Actuation WHAT WILL YOU MAKE?

• Connect to your Galileo via Ethernet

• Restore sketch for port configuration if it does not work any more

• Launch: python or python2

• Leave: Ctrl+D or exit()

Python interpreter

Page 5: Intel-South-Africa-Galileo-2014-Jun (Basic IO)

5 Galileo Tutorial – I/Os, Sensing and Actuation WHAT WILL YOU MAKE?

• Creating a simple “hello world” script

• Launching “Hello world” script

Launching the Python interpreter

root@clanton:~# vi hello.py // test internet connectivity

Press “i” // insertType print “hello world”Press “ESC” // exit insert modePress “:w” // write to filePress “:q” // quit

root@clanton:~# python hello.py // “hello world”

Page 6: Intel-South-Africa-Galileo-2014-Jun (Basic IO)

6 Galileo Tutorial – I/Os, Sensing and Actuation WHAT WILL YOU MAKE?

• A module is a file containing Python definitions and statements.

• Modules is a way of sharing code efficiently across different scripts

• Python comes with a library of useful standard modules

• See library reference: https://docs.python.org/2/library/index.html

• Modules can be imported in other modules and accessed inside

• Syntax: import module_name

• Modules can contain functions, variables, etc –

• Syntax: dir(module_name) // list the

• New modules can be installed from repositories using easy_install

• Syntax: easy_install module_name

Python modules

Page 7: Intel-South-Africa-Galileo-2014-Jun (Basic IO)

7 Galileo Tutorial – I/Os, Sensing and Actuation WHAT WILL YOU MAKE?

Importing and using modules

Page 8: Intel-South-Africa-Galileo-2014-Jun (Basic IO)

8 Galileo Tutorial – I/Os, Sensing and Actuation WHAT WILL YOU MAKE?

• Extend your hello world script by printing the current system time after a delay of 5 seconds

Exercise: using modules

Hint: look into the time module for a function that can be used for delay

Page 9: Intel-South-Africa-Galileo-2014-Jun (Basic IO)

9 Galileo Tutorial – I/Os, Sensing and Actuation WHAT WILL YOU MAKE?

Solution

Page 10: Intel-South-Africa-Galileo-2014-Jun (Basic IO)

10 Galileo Tutorial – I/Os, Sensing and Actuation WHAT WILL YOU MAKE?

10

WHAT WILL YOU MAKE?

Analog and Digital I/O in Python

Page 11: Intel-South-Africa-Galileo-2014-Jun (Basic IO)

11 Galileo Tutorial – I/Os, Sensing and Actuation WHAT WILL YOU MAKE?

• Module with Arduino style syntax pre-installed

• Name: pyGalileo

• Source: https://github.com/galileo-chofrock/pyGalileo

Digital and analog I/O using python

pinMode(pin, direction) // pin number, INPUT/OUTPUT”

digitalWrite (pin, value) // pin number, HIGH/LOW

digitalRead (pin) // pin number

analogWrite (pin, value) // PWM pin number, value from 0..255

analogRead (pin) // analog PIN number (A0 …A5)

wait (time) // time in miliseconds

Page 12: Intel-South-Africa-Galileo-2014-Jun (Basic IO)

12 Galileo Tutorial – I/Os, Sensing and Actuation WHAT WILL YOU MAKE?

1. Connect Grove LED to D2

2. Write a python script to make it blink every second

Exercise: Digital write

Hints: • If you are not familiar with the vi editor you can use your favourite on

the PC and copy the file over via SFTP• You can use the python interpreter first to experiment with the module

Page 13: Intel-South-Africa-Galileo-2014-Jun (Basic IO)

13 Galileo Tutorial – I/Os, Sensing and Actuation WHAT WILL YOU MAKE?

Solution – pyblink.py

Page 14: Intel-South-Africa-Galileo-2014-Jun (Basic IO)

14 Galileo Tutorial – I/Os, Sensing and Actuation WHAT WILL YOU MAKE?

1. Connect Grove Light / Rotary angle sensor to A0

2. Write a python script to print out the reading every second

Exercise: Analog read

Hints: • If you are not familiar with the vi editor you can use your favourite on

the PC and copy the file over via SFTP• You can use the python interpreter first to experiment with the module

Page 15: Intel-South-Africa-Galileo-2014-Jun (Basic IO)

15 Galileo Tutorial – I/Os, Sensing and Actuation WHAT WILL YOU MAKE?

Solution – analog.py

Page 16: Intel-South-Africa-Galileo-2014-Jun (Basic IO)

16 Galileo Tutorial – I/Os, Sensing and Actuation WHAT WILL YOU MAKE?

16

WHAT WILL YOU MAKE?

Serial communications on Galileo using Python

Page 17: Intel-South-Africa-Galileo-2014-Jun (Basic IO)

17 Galileo Tutorial – I/Os, Sensing and Actuation WHAT WILL YOU MAKE?

• Python serial module pre-installed

• Name: pySerial

• Use: import serial

• Source: http://pyserial.sourceforge.net/

• Main principles

• Open a serial port on the Galileo

• Perform read/write operations

• Close port when you are done

• Serial Linux devices on Galileo

• /dev/ttyS0 – UART0 (Pins: 0/1)

• /dev/ttyS1- UART1 (3.5 mm jack)

• /dev/ttyGS0 – serial port to Arduino IDE console

Serial communications in Python

Page 18: Intel-South-Africa-Galileo-2014-Jun (Basic IO)

18 Galileo Tutorial – I/Os, Sensing and Actuation WHAT WILL YOU MAKE?

pySerial API overview

Serial (device, baudrate) // create serial port object and return it

inWaiting() // returns number of bytes received

read (number of bytes) // read number of bytes and return them

write (msg) // write msg buffer, e.g a string

close () // close created serial port

Detailed API: http://pyserial.sourceforge.net/pyserial_api.html

Page 19: Intel-South-Africa-Galileo-2014-Jun (Basic IO)

19 Galileo Tutorial – I/Os, Sensing and Actuation WHAT WILL YOU MAKE?

1. Write a basic echo server that connects to the serial monitor of the ArduinoDev UI

2. Send messages from the ArduinoDev UI to the echo server

Exercise: Serial echo server

Page 20: Intel-South-Africa-Galileo-2014-Jun (Basic IO)

20 Galileo Tutorial – I/Os, Sensing and Actuation WHAT WILL YOU MAKE?

Solution - echoserver.py

Page 21: Intel-South-Africa-Galileo-2014-Jun (Basic IO)

21 Galileo Tutorial – I/Os, Sensing and Actuation WHAT WILL YOU MAKE?

21

WHAT WILL YOU MAKE?

Network communications using MQTT

Page 22: Intel-South-Africa-Galileo-2014-Jun (Basic IO)

22 Galileo Tutorial – I/Os, Sensing and Actuation WHAT WILL YOU MAKE?

IoT/M2M connectivity protocol

Very lightweight publish/subscribe messaging transport

Minimizes network bandwidth and device resource requirements whilst and provides some degree of reliability for assured delivery

MQTT Overview

Client app

MQTT protocol

TCP/IP stack

Client app

MQTT protocol

TCP/IP stack

Client app

MQTT protocol

TCP/IP stack

Broker app

MQTT protocol

TCP/IP stack

Subscribe (Topic A)

Subscribe (Topic A)

Publish(Topic A, MSG)

MSG

MSG Client app

MQTT protocol

TCP/IP stack

Subscribe (Topic B)

Publisher

Subscriber

Subscriber

Subscriber

Page 23: Intel-South-Africa-Galileo-2014-Jun (Basic IO)

23 Galileo Tutorial – I/Os, Sensing and Actuation WHAT WILL YOU MAKE?

2 Byte fixed header

14 Message types

3 QoS levels (at most once, at least once, exactly once)

Topic

Topic name is the key that identifies the information channel to which payload data is published

Topic names are UTF encoded and an upper length limit of 32,767 char

Payload can be of varying length

MQTT protocol basics

Bit 7 6 5 4 3 2 1 0

Byte 1 MESSAGE TYPE DUP flag

QOS LEVEL RETAIN

Byte 2 REMAINING LENGTH

Page 24: Intel-South-Africa-Galileo-2014-Jun (Basic IO)

24 Galileo Tutorial – I/Os, Sensing and Actuation WHAT WILL YOU MAKE?

• A topic name is a string of varying length

• To allow subscription to multiple topics at once, a tree hierarchy can be introduced with a topic level separator “/”

• Wild cards can be used to subscribe to a subset of the hierarchy tree

• Multilevel wild card “#”• “/BuildingA/Floor1/Room1/#” subscribes to topics (2) and (3)• “/BuildingA/Floor1/#” subscribes to topics (1)-(3)

• Single level wild card “+”• “/BuildingA/Floor1/+/Light” subscribe to light from all rooms on Floor1

Topic names

“/BuildingA/Floor1/Room1” (1)“/BuildingA/Floor1/Room1/Light” (2)“/BuildingA/Floor1/Room1/Temperature (3)

Page 25: Intel-South-Africa-Galileo-2014-Jun (Basic IO)

25 Galileo Tutorial – I/Os, Sensing and Actuation WHAT WILL YOU MAKE?

• mosquitto library (http://mosquitto.org/)

MQTT in Python

# create a mosquito client instancemqttc = mosquitto.Mosquitto()

# Connect call backs to functions implementing themmqttc.on_message = on_messagemqttc.on_connect = on_connectmqttc.on_publish = on_publishmqttc.on_subscribe = on_subscribe

# connect to a mosquitto brokermqttc.connect(broker_address, port)

# listen for messages in a loopmqttc.loop(1) or mqttc.loop_forever()

# publish a message mqttc.publish(topic, msgbody)

Page 26: Intel-South-Africa-Galileo-2014-Jun (Basic IO)

26 Galileo Tutorial – I/Os, Sensing and Actuation WHAT WILL YOU MAKE?

1. Look at the mqttGalileo example and run it

• Code demonstrates both subscriber and publisher part in one client

2. Adapt the code to exchange information or trigger some action with one or more neighbouring Galileos

Exercise

Client app

MQTT protocol

TCP/IP stack

Broker app

MQTT protocol

TCP/IP stack

Subscribe:button pressed event

Publish:Button pressed event

Button pressed event

Page 27: Intel-South-Africa-Galileo-2014-Jun (Basic IO)

27 Galileo Tutorial – I/Os, Sensing and Actuation WHAT WILL YOU MAKE?

Python: https://www.python.org/

Python2 reference: https://docs.python.org/2/

PySerial libray: http://pyserial.sourceforge.net/

PyGalileo library: https://github.com/galileo-chofrock/pyGalileo

MQTT website: http://mqtt.org/

MQTT Protocol specification v3.1:

http://public.dhe.ibm.com/software/dw/webservices/ws-mqtt/mqtt-v3r1.html

Mosquitto library: http://mosquitto.org/

Paho project: http://www.eclipse.org/paho/

Resources and further reading

Page 28: Intel-South-Africa-Galileo-2014-Jun (Basic IO)