In this post we will be talking about setting up our Main hub (Raspberry Pi) with Eclipse Mosquitto and Paho for MQTT and communicating with our main hub controller (nanoESP).
We are using a Raspberry Pi Model B+ with the current Raspbian (Jessie) and a 32 GB SD card. As we are still waiting to receive our GSM/UMTS interface we are using the eth0 interface instead for the communication towards the cloud.
For the communication between Arduino and the Raspberry Pi we don't have to really dive in to that. As we are printing the data on serial (see prior blog post) we will briefly mention our setup and dive more in to local data aggregation, retention and other details details.
Setting up Eclipse Mosquitto
Setting up Eclipse Mosquitto on Raspbian was really easy as its available in the Raspbian main repository. On the command line:
sudo apt-get install mosquitto
Check if the service is running by using:
sudo service mosquitto status
We found this tutorial and discovered FHEM on the way. FHEM is an interesting Perl based open source platform which acts a message broker and visualisation aid. However we decided to go with another platform. More about that in our next post.
You can test if your MQTT broker ist getting anything by executing (on CLI):
Connecting Raspberry Pi to Arduino
At first we found this tutorial to be very helpful. We did this connection both over the Raspberry Pi GPIO and later on over USB (see prior blog post). Creating the following Python script to write the values to a file:
from __future__ import print_function
import serial
import time
s = serial.Serial('/dev/ttyUSB0', 19200) # Namen ggf. anpassen
s.isOpen()
time.sleep(5) # der Arduino resettet nach einer Seriellen Verbindung, daher muss kurz gewartet werden
#s.write("test")
try:
while True:
response = s.readline()
print(response)
f = open('workfile', 'a')
line = response + '\n'
f.write(line) # python will convert \n to os.linesep
f.close() # you can omit in most cases as the destructor will call it
except KeyboardInterrupt:
s.close()
The "workfile" contains following entries:
#!/usr/bin/env python
import serial
import time
import paho.mqtt.client as mqtt
def on_connect(client, userdata, flags, rc):
print("Connected with result code " + str(rc))
client = mqtt.Client()
client.on_connect = on_connect
client.connect("iot.eclipse.org", 1883, 60)
client.loop_start()
s = serial.Serial('/dev/ttyUSB0', 19200) # Namen ggf. anpassen
s.isOpen()
time.sleep(5) # der Arduino resettet nach einer Seriellen Verbindung, daher muss kurz gewartet werden
s.write("test")
try:
while True:
response = s.readline()
print(response)
time.sleep(2)
client.publish("moisture", response)
except KeyboardInterrupt:
s.close()
Setting up a wifi access point
We bought a wifi USB stick for about 4 €. The stick is compatible to Linux and works right out of the box and can be in AP mode. The wireless access point is used by the sensor nodes. They wake up once every hour and connect to it. We used the following tutorial to install hostapd and dnsmasq on the Raspberry Pi and set the needed configuration.
Links:
https://tutorials-raspberrypi.de/arduino-raspberry-pi-miteinander-kommunizieren-lassen/
https://www.elektronik-kompendium.de/sites/raspberry-pi/2002171.htm
https://pypi.python.org/pypi/paho-mqtt/1.1
https://tutorials-raspberrypi.de/arduino-raspberry-pi-miteinander-kommunizieren-lassen/
https://www.elektronik-kompendium.de/sites/raspberry-pi/2002171.htm
https://pypi.python.org/pypi/paho-mqtt/1.1
No comments:
Post a Comment