You are on page 1of 14

Getting Started with IoT using a Raspberry Pi and Python

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


Quelle: https://www.ncta.com/platform/industry-news/infographic-the-growth-of-the-internet-of-things/
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)
How MQTT works

Quelle: https://zoetrope.io/tech-blog/brief-practical-introduction-mqtt-protocol-and-its-application-iot
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
docker build -t test-mosquitto .
docker run -p 1883:1883 test-mosquitto
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


Python Client

pip3 install paho-mqtt

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

Source: https://github.com/eclipse/paho.mqtt.python
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()
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()
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
How it works:

Subscribe and wait for command MQTT save state


Raspberri Pi «light on» or «light off» (optional)
DB
Broker

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

Controller
Interface
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))
Raspberry Pi Client
# Subscribing in on_connect() means that if we lose the connection and
# reconnect then subscriptions will be renewed.
sudo pip3 install paho-mqtt 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()
import paho.mqtt.client as mqtt

client = mqtt.Client()
Control Raspberry Pi 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()
Dudas ?

You might also like