MQTT Publish for Raspberry Pi AIY Projects
|I have a lot of things in my house connected using a Raspberry Pi mosquitto broker and Node-RED so I wanted to add simple publish commands to my AIY Project.
The code is hosted at Github here: https://github.com/ktinkerer/aiyprojects-raspbian/tree/mqtt
For this you will just need to copy the action.py file to your ~voice-recognizer-raspi/src/ folder.
For it to work you need to install paho-mqtt:
sudo pip install paho-mqtt
I’ve set this up as an example to swich lights on and off so you will need to tweak it to suit your own setup.
If you are using the new version of the AIY Projects kit (from version aiyprojects-2017-09-11.img) and don’t have an action.py file then you need to use the mqtt_mod.py from my updated version (voicekit branch) copy the my_config.py file as well and fill it in with your MQTT config details.
Excellent, thanks.
Hi is it where i have to change to set my own mqtt ? and like this ?
publish.single(topic, payload=message,
hostname=”m23.cloudmqtt.com”,
port=”10174″,
auth={username:”cevevbbwy”,
password:”0gf_lV9BVz45M”})
You can do, I use a config file, copy the file called my_config.py and fill your details in there. It should work the same either way. Be careful that the formatting remains the same (same number of tabs and no extra spaces or anything) as that is important in python. Are those your actual mqtt details? Do you want me to delete them?
No dont worry i already changed them 🙂 thanks
Thanks i did it 🙂 the light of my room is worcking with it if i whant to use it with some rgb light i just have to add the same line as switch on and off but instead of thaht i add mqtt command for the rgb light right ?
Great! You need to copy this part:
def lights_on():
and replace the topic and payload to the right ones to change your RGB light also change lights_on to something else then add to themosquitto('livingroom/lights', 'on')
elif event.type == EventType.ON_RECOGNIZING_SPEECH_FINISHED and event.args:
part further down and add another like this:elif text == 'lights on':
but replace ‘lights_on’ with whatever name you used and change ‘lights on’ to whatever command you want to say to trigger it.assistant.stop_conversation()
lights_on()
Hope that makes sense!
Ok thanks à lot i will try thank you 🙂
If you need anything about à logo or design le whatever let me know 😉
when i try to run it like this
publish.single(topic, payload=message,
hostname=m23.cloudmqtt.com,
port=10174,
auth={‘username’:cbbebrgwy,
‘password’:0gf_lV9tjhgevbM})
i get :
env/bin/python3 -u src/mqtt_mod.py
File “src/mqtt_mod.py”, line 67
‘password’:0gf_lV9BVg5M})
^
SyntaxError: invalid syntax