On Jessie, for python 2, first do a
Code: Select all
sudo apt-get install python-dev
Code: Select all
sudo pip2 install python-banyan
Code: Select all
sudo pip2 install python-banyan
Code: Select all
sudo apt-get install python-dev
Code: Select all
sudo pip2 install python-banyan
Code: Select all
sudo pip2 install python-banyan
Starting on a fresh stretch lite image:pumpkinpi wrote: ↑Mon Sep 04, 2017 3:48 amIn the meantime, I tried asandford's recommendation. I did this:
And these two things installed no probs.Code: Select all
sudo apt-get install mosquitto sudo pip install paho-mqtt
I did some reading here:
https://pypi.python.org/pypi/paho-mqtt/1.3.0
but quickly got lost. Have not yet found a tutorial for dummies on how to make this work on my raspberry pi's running simple python scripts. I am just looking to pass strings and simple numbers between them.
If anybody knows of guides, I would appreciate direction.
I think I just need to see a few examples. To late to keep googling...
PPi.
Code: Select all
sudo apt update; sudo apt upgrade -y
sudo apt install -y mosquitto mosquitto-clients
Code: Select all
pi@raspberrypi:~ $ mosquitto_sub -d -t hello/world
Client mosqsub/1389-raspberryp sending CONNECT
Client mosqsub/1389-raspberryp received CONNACK
Client mosqsub/1389-raspberryp sending SUBSCRIBE (Mid: 1, Topic: hello/world, QoS: 0)
Client mosqsub/1389-raspberryp received SUBACK
Subscribed (mid: 1): 0
Code: Select all
pi@raspberrypi:~ $ mosquitto_pub -d -t hello/world -m "Hello from Terminal window 2!"
Client mosqpub/1416-raspberryp sending CONNECT
Client mosqpub/1416-raspberryp received CONNACK
Client mosqpub/1416-raspberryp sending PUBLISH (d0, q0, r0, m1, 'hello/world', ... (29 bytes))
Client mosqpub/1416-raspberryp sending DISCONNECT
pi@raspberrypi:~ $
Code: Select all
Client mosqsub/1389-raspberryp sending PINGREQ
Client mosqsub/1389-raspberryp received PINGRESP
Client mosqsub/1389-raspberryp sending PINGREQ
Client mosqsub/1389-raspberryp received PINGRESP
Client mosqsub/1389-raspberryp received PUBLISH (d0, q0, r0, m0, 'hello/world', ... (29 bytes))
Hello from Terminal window 2!
Client mosqsub/1389-raspberryp sending PINGREQ
Client mosqsub/1389-raspberryp received PINGRESP
Code: Select all
pi@raspberrypi:~ $ mosquitto_sub -d -t ledStatus
Code: Select all
sudo apt install -y python-pip
pip install paho-mqtt
Collecting paho-mqtt
Downloading paho-mqtt-1.3.0.tar.gz (79kB)
100% |████████████████████████████████| 81kB 1.0MB/s
Building wheels for collected packages: paho-mqtt
Running setup.py bdist_wheel for paho-mqtt ... done
Stored in directory: /home/pi/.cache/pip/wheels/a8/fb/b8/25cf89a4ebec3846c24b8feec8469f8402e7706c2c962c84b4
Successfully built paho-mqtt
Installing collected packages: paho-mqtt
Successfully installed paho-mqtt-1.3.0
Code: Select all
pi@raspberrypi:~ $ vi testmq.py
pi@raspberrypi:~ $ cat testmq.py
import paho.mqtt.publish as publish
import time
print("Sending 0...")
publish.single("ledStatus", "0", hostname="localhost")
time.sleep(1)
print("Sending 1...")
publish.single("ledStatus", "1", hostname="localhost")
pi@raspberrypi:~ $ python testmq.py
Sending 0...
Sending 1...
pi@raspberrypi:~ $
Code: Select all
pi@raspberrypi:~ $ mosquitto_sub -d -t ledStatus
Client mosqsub/6104-raspberryp sending CONNECT
Client mosqsub/6104-raspberryp received CONNACK
Client mosqsub/6104-raspberryp sending SUBSCRIBE (Mid: 1, Topic: ledStatus, QoS: 0)
Client mosqsub/6104-raspberryp received SUBACK
Subscribed (mid: 1): 0
Client mosqsub/6104-raspberryp received PUBLISH (d0, q0, r0, m0, 'ledStatus', ... (1 bytes))
0
Client mosqsub/6104-raspberryp received PUBLISH (d0, q0, r0, m0, 'ledStatus', ... (1 bytes))
1
Glad you found it useful.
Code: Select all
import datetime
import time
import paho.mqtt.client as paho
def on_message(pihmi1, userdata, msg):
audiblealert()
print(msg.topic+" "+str(msg.qos)+" "+str(msg.payload))
pihmi1 = paho.Client()
pihmi1.on_message = on_message
pihmi1.connect("192.168.0.4", 1883)
pihmi1.subscribe("kitchenoccupancy")
pihmi1.loop_start()
Code: Select all
import time
import datetime
import RPi.GPIO as io
import paho.mqtt.client as paho
io.setmode(io.BCM)
io.setup(18, io.IN)
occupiedcount = 0
picontrol = paho.Client()
picontrol.connect("192.168.0.5", 1883)
picontrol.loop_start()
occupied = 'NO'
picontrol.publish("kitchenoccupancy", occupied)
while True:
oldmotion = io.input(18)
time.sleep(2)
occupiedcount += 1
if io.input(18):
if occupied == 'NO':
occupiedcount = 0
occupied = 'YES'
picontrol.publish("kitchenoccupancy", occupied)
if occupiedcount > 1350:
if occupied == 'YES':
occupied = 'NO'
picontrol.publish("kitchenoccupancy", occupied)
The address on all machines needs to be the address of the broker you are usingOh, that is interesting. I thought the addresses had to be different. Should they be the address of the publisher computer or the subscriber?
In the mini tutorial, the clients and server are all installed on one host. All the references to localhost should refer to the hostname of the device running the broker. The client IP addressess do not need to be used, or even known, for a successfull connection. Think of the process like this forum. You need to know the address to post a message, and I need to know it to read it. You don't need to know who will read your post, and I don't need to know the poster - hence the forum is a broker of sorts.pumpkinpi wrote: ↑Fri Sep 08, 2017 6:10 pmOh, that is interesting. I thought the addresses had to be different. Should they be the address of the publisher computer or the subscriber?
They are not the actual addresses, but are different in my code.
I did do the stuff above (command line stuff) so I know it is working fine - just not in my code.
Thanks,
PPi
Code: Select all
kitchenoccupancy 0 b'YES'
Code: Select all
if msg.payload.decode("utf-8") == "YES":
Yes. If one client publishes to a topic, all subscribers to that topic will receive it. It's worth pointing out that a client that is publishing may also be subscribing, so the flow of data can be complex.
I'm with youI just can't believe how easy this is in the end.
Now try Node Red and MQTT and you can have asynchronous, non-blocking, event driven code. I have a green house monitoring system reading sensors and controlling switches with it. It also has easy database integration and message transformation, native gpio control on a Pi and Arduino support with firmata. It's already built in to Raspbian, and is developed by IBM.pumpkinpi wrote: ↑Sat Sep 09, 2017 7:14 pmIT WORKS! IT WORKS!
This is the coolest thing I have done on my Pi's. I wish I had figured this out years ago. I have about 12 Pi's in my house doing lots of different jobs and the one thing I have struggled with the most is getting them to coordinate with each other. I have a lot of code to sort through now and rewrite.
This is so much more powerful than other solutions I have used.
Thanks a million for the assistance folks.
I just can't believe how easy this is in the end.
PPi