Gettiing Started with IoT using Raspberry Pi and Python

Post on 22-Jan-2017

317 views 8 download

Transcript of Gettiing Started with IoT using Raspberry Pi and Python

Martin Christen

FHNW – University of Applied Sciences and Arts Northwestern Switzerland

School of Architecture, Civil Engineering and Geomatics

Institute of Geomatics Engineering

martin.christen@fhnw.ch

@MartinChristen

Getting Started with IoT using a Raspberry Pi and Python

Quelle: http://cloudtimes.org/2013/09/09/introducing-web-3-0-internet-of-things/Quelle: https://www.hifiberry.com/2016/02/the-new-raspberry-pi-3-is-out/

10 April 2016Insitute of Geomatics Engineering 2

Quelle: https://www.ncta.com/platform/industry-news/infographic-the-growth-of-the-internet-of-things/

10 April 2016Insitute of Geomatics Engineering 3

MQTT (Message Queuing Telemetry Transport)

MQTT provides a lightweight method of carrying out messaging using a

publish/subscribe model. This makes it suitable for “machine to machine”

messaging such as with low power sensors or mobile devices such as phones or

the Raspberry Pi.

MQTT dates back to 1999.

MQTT is:

Open (ISO/IEC PRF 20922)

Lightweight (2 bytes header)

Reliable (QoS/patterns to avoid packet loss)

Simple (TCP based, async, publish/subscribe, payload agnostic)

10 April 2016Insitute of Geomatics Engineering 4

How MQTT works

Quelle: https://zoetrope.io/tech-blog/brief-practical-introduction-mqtt-protocol-and-its-application-iot

10 April 2016Insitute of Geomatics Engineering 5

XMPP Implementation: Eclipse Mosquitto

• Lightweight server implementation of MQTT written in C

• About 3 MB RAM with 1000 clients connected…

• http://eclipse.org/mosquitto

• Client support for Python 2.x and Python 3.x

10 April 2016Insitute of Geomatics Engineering 6

Dockerfile

FROM ubuntu:14.04

ENV DEBIAN_FRONTEND noninteractive

RUN apt-get update

RUN apt-get upgrade -y

RUN apt-get install wget build-essential libwrap0-dev libssl-dev -y

RUN apt-get install python-distutils-extra libc-ares-dev uuid-dev -y

RUN mkdir -p /usr/local/src

WORKDIR /usr/local/src

RUN wget http://mosquitto.org/files/source/mosquitto-1.4.8.tar.gz

RUN tar xvzf ./mosquitto-1.4.8.tar.gz

WORKDIR /usr/local/src/mosquitto-1.4.8

RUN make

RUN make install

RUN adduser --system --disabled-password --disabled-login mosquitto

USER mosquitto

EXPOSE 1883

CMD ["/usr/local/sbin/mosquitto"]

based on: https://hub.docker.com/r/ansi/mosquitto/~/dockerfile

docker build -t test-mosquitto .

docker run -p 1883:1883 test-mosquitto

10 April 2016Insitute of Geomatics Engineering 7

Python Client

pip3 install paho-mqtt

Documentation: https://pypi.python.org/pypi/paho-mqtt/

Source: https://github.com/eclipse/paho.mqtt.python

10 April 2016Insitute of Geomatics Engineering 8

MQTT: Subscribe

import paho.mqtt.client as mqtt

def on_connect(client, userdata, flags, rc):

print("Connected with result code " + str(rc))

# Subscribing in on_connect() means that if we lose the connection and

# reconnect then subscriptions will be renewed.

client.subscribe("SensorXY/#")

# The callback for when a PUBLISH message is received from the server.

def on_message(client, userdata, msg):

print(msg.topic + " " + str(msg.payload))

client = mqtt.Client()

client.on_connect = on_connect

client.on_message = on_message

host = "192.168.99.100"

print("Connecting to " + host)

client.connect(host, port=1883, keepalive=60)

client.loop_forever()

10 April 2016Insitute of Geomatics Engineering 9

MQTT: Publish

import paho.mqtt.client as mqtt

def on_connect(client, userdata, flags, rc):

print("Connected with result code "+str(rc))

# The callback for when a PUBLISH message is received from the server.

# unused for this demo

def on_publish(client, userdata, mid):

pass

client = mqtt.Client()

client.on_connect = on_connect

client.on_publish = on_publish

host = "192.168.99.100"

client.connect(host, port=1883, keepalive=60)

client.loop_start()

topic = "SensorXY"

s = ""

while s != "exit":

s = input("payload >")

client.publish(topic, s)

client.loop_stop()

10 April 2016Insitute of Geomatics Engineering 10

Simple example: Remote-control a light from anywhere in the world

In the first example we turn on/off a LED using MQTT. The LED is connected on

GPIO Pin 11 (GPIO 17)

https://ms-iot.github.io/content/en-US/win10/samples/PinMappingsRPi2.htm

10 April 2016Insitute of Geomatics Engineering 11

How it works:

Raspberri PiMQTTBroker

Subscribe and wait for command«light on» or «light off»

Controller Interface

Send command «light on», «light off», or «get status»

DBsave state(optional)

10 April 2016Insitute of Geomatics Engineering 12

Raspberry Pi Client

import RPi.GPIO as GPIO

import paho.mqtt.client as mqtt

def on_connect(client, userdata, flags, rc):

print("Connected with result code " + str(rc))

# Subscribing in on_connect() means that if we lose the connection and

# reconnect then subscriptions will be renewed.

client.subscribe("Raspberry/#")

# The callback for when a PUBLISH message is received from the server.

def on_message(client, userdata, msg):

s = str(msg.payload, encoding="ascii")

print("retrieved message: " + s)

if s == "lighton":

GPIO.output(LedPin, GPIO.LOW)

elif s == "lightoff":

GPIO.output(LedPin, GPIO.HIGH)

# Initialize GPIO

LedPin = 11 # pin GPIO 17, change if you connect to other pin!

GPIO.setmode(GPIO.BOARD)

GPIO.setup(LedPin, GPIO.OUT)

GPIO.output(LedPin, GPIO.HIGH) # turn off led

# Initialize MQTT

client = mqtt.Client()

client.on_connect = on_connect

client.on_message = on_message

host = "192.168.99.100"

print("Connecting to " + host)

client.connect(host, port=1883, keepalive=60)

client.loop_forever()

sudo pip3 install paho-mqtt

10 April 2016Insitute of Geomatics Engineering 13

Control Raspberry Pi

import paho.mqtt.client as mqtt

client = mqtt.Client()

host = "192.168.99.100"

client.connect(host, port=1883,

keepalive=60)

client.loop_start()

topic = "Raspberry"

print("COMMANDS:")

print("0: turn light off")

print("1: turn light on")

print("3: quit application")

s=0

while s!=3:

s = int(input("command >"))

if s == 0:

client.publish(topic, "lightoff")

elif s == 1:

client.publish(topic, "lighton")

elif s == 3:

print("bye")

else:

print("unknown command")

client.loop_stop()

10 April 2016Insitute of Geomatics Engineering 14

Questions ?