sj_Rpi
Posts: 12
Joined: Wed Jun 08, 2016 6:53 pm

MQTT -> RPi -> Arduino I/O's

Sat Sep 17, 2016 11:36 pm

I have an Android MQTT client app sending data to my Pi. I'd like to have the Pi -which is connected to an Arduino via USB- send that data to the Arduino to control it's I/O pins. Right now the Arduino is running the standard Firmata firmware.

Is this possible?

mattmiller
Posts: 2188
Joined: Thu Feb 05, 2015 11:25 pm

Re: MQTT -> RPi -> Arduino I/O's

Sat Sep 17, 2016 11:38 pm

Yes

mattmiller
Posts: 2188
Joined: Thu Feb 05, 2015 11:25 pm

Re: MQTT -> RPi -> Arduino I/O's

Sat Sep 17, 2016 11:40 pm

What Android app are you using to send MQTT data to Pi?

Why not just get the Pi to subscribe to the MQTT broker sending the data your interested in?

sj_Rpi
Posts: 12
Joined: Wed Jun 08, 2016 6:53 pm

Re: MQTT -> RPi -> Arduino I/O's

Sat Sep 17, 2016 11:59 pm

I am using 'MQTT Dashboard' for now until I get my App Inventor client app working correctly. Right now the broker is on the Pi. The goal is to control the IO pins on the Arduino through my Android app over MQTT as well as receive data back.

mattmiller
Posts: 2188
Joined: Thu Feb 05, 2015 11:25 pm

Re: MQTT -> RPi -> Arduino I/O's

Sun Sep 18, 2016 1:43 pm

Got it now - your just using a Pi as a MQTT WiFi bridge from a droid app to an Arduino.

So all you need do is get a small Python prog on the Pi to connect to the Pi broker using paho lib and then use Pyfirmata lib http://raspberrypi-aa.github.io/session3/firmata.html to communicate to your Arduino

It has to be suggested that you look into just using a HAT on the Pi to do whatever the arduino is doing for you to make a smaller neater project perhaps?

mattmiller
Posts: 2188
Joined: Thu Feb 05, 2015 11:25 pm

Re: MQTT -> RPi -> Arduino I/O's

Sun Sep 18, 2016 4:46 pm

Just did a quick test (using PC not Pi but makes no diff)

Code: Select all

# -*- coding: utf-8 -*-
import time
import paho.mqtt.client as mqtt
from pyfirmata import Arduino, util
board = Arduino('com5')

# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, 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("home/+")

# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
    print time.asctime(), " Topic: ", msg.topic+'\nMessage: '+str(msg.payload)
    board.digital[13].write(1)
    time.sleep(1)
    board.digital[13].write(0)   
    #print msg

client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message

client.connect("mypc.local", 1883)

# Blocking call that processes network traffic, dispatches callbacks and
# handles reconnecting.
# Other loop*() functions are available that give a threaded interface and a
# manual interface.
client.loop_forever()
And it flashes the on-board LED on pin 13 every time a message is received

User avatar
DougieLawson
Posts: 37726
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: MQTT -> RPi -> Arduino I/O's

Sun Sep 18, 2016 5:50 pm

sj_Rpi wrote:I am using 'MQTT Dashboard' for now until I get my App Inventor client app working correctly. Right now the broker is on the Pi. The goal is to control the IO pins on the Arduino through my Android app over MQTT as well as receive data back.
I got that working easily. Thanks for pointing out that Android app (it's going to be a useful tool). I've got a simple python server that subscribes to a "relay/xxx/nnn" topic and switches one of 16 pins on a MCP23017 when the appropriate message is received. I've got another python TkInter program that lets me publish to the right topics.

It would be relatively simple to amend that stuff to use Nick O'Leary's Arduino MQTT library on my Arduino with an ethernet shield.
https://github.com/knolleary/pubsubclient
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

sj_Rpi
Posts: 12
Joined: Wed Jun 08, 2016 6:53 pm

Re: MQTT -> RPi -> Arduino I/O's

Wed Sep 21, 2016 5:35 am

mattmiller wrote:Just did a quick test (using PC not Pi but makes no diff)

Code: Select all

# -*- coding: utf-8 -*-
import time
import paho.mqtt.client as mqtt
from pyfirmata import Arduino, util
board = Arduino('com5')

# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, 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("home/+")

# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
    print time.asctime(), " Topic: ", msg.topic+'\nMessage: '+str(msg.payload)
    board.digital[13].write(1)
    time.sleep(1)
    board.digital[13].write(0)   
    #print msg

client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message

client.connect("mypc.local", 1883)

# Blocking call that processes network traffic, dispatches callbacks and
# handles reconnecting.
# Other loop*() functions are available that give a threaded interface and a
# manual interface.
client.loop_forever()
And it flashes the on-board LED on pin 13 every time a message is received
Hi Matt, thanks for the info. I do plan on getting a hat in the future to keep a small footprint. I had a question about the code here, in the subscribe topic -("home/+"). What does the '+' actually do here?

Thinking about matt's first post, regarding my setup, I'm wondering if I'm going about this the right way. I'm wondering if having the broker on the Pi is the best way or not. Is there any time when the Pi would need to act as client in this setup? What if I were to have the broker on a home PC and have the Pi act as a client. Would there be any advantage of one over the other? Another question, would having multiple Pi's subscribed to the same topic be the 'MQTT equivalent' of multicasting?

sj_Rpi
Posts: 12
Joined: Wed Jun 08, 2016 6:53 pm

Re: MQTT -> RPi -> Arduino I/O's

Wed Sep 21, 2016 6:00 am

DougieLawson wrote: I got that working easily. Thanks for pointing out that Android app (it's going to be a useful tool). I've got a simple python server that subscribes to a "relay/xxx/nnn" topic and switches one of 16 pins on a MCP23017 when the appropriate message is received. I've got another python TkInter program that lets me publish to the right topics.

It would be relatively simple to amend that stuff to use Nick O'Leary's Arduino MQTT library on my Arduino with an ethernet shield.
https://github.com/knolleary/pubsubclient
The app is handy. This is actually my first time hearing about tkinter, gonna look into it more, thanks. Is the O'Leary library a better client than the paho? Or is it strictly for the micro's? Would connecting multiple arduino's via USB to a Pi(used as a wifi bridge) be a good idea for keeping costs low? A 10ft usb cable vs Ethernet/wifi shield.

mattmiller
Posts: 2188
Joined: Thu Feb 05, 2015 11:25 pm

Re: MQTT -> RPi -> Arduino I/O's

Wed Sep 21, 2016 9:25 am

. I had a question about the code here, in the subscribe topic -("home/+"). What does the '+' actually do here?
The + acts as a wildcard - just make that whatever topic you actually need - have a read up on mosquitto.org abotu wildcards
I'm wondering if having the broker on the Pi is the best way or not. Is there any time when the Pi would need to act as client in this setup? What if I were to have the broker on a home PC and have the Pi act as a client. Would there be any advantage of one over the other?
Its your choice - my home PC is ALWAYS on 24/7 so I'm running my house broker on it but it could be running on anything that's on all the time
Another question, would having multiple Pi's subscribed to the same topic be the 'MQTT equivalent' of multicasting?
Yep - that's one of its beauties :)

I have 2 sets of kitchen lights neopixels connected to 2x PiZero controlled by a single MQTT topic to keep them in sync (as they are on opposite ides of the kitchen and my wife wouldn't let me drill holes in the Granite worktops!)

sj_Rpi
Posts: 12
Joined: Wed Jun 08, 2016 6:53 pm

Re: MQTT -> RPi -> Arduino I/O's

Fri Sep 23, 2016 10:08 pm

mattmiller wrote: I have 2 sets of kitchen lights neopixels connected to 2x PiZero controlled by a single MQTT topic to keep them in sync (as they are on opposite ides of the kitchen and my wife wouldn't let me drill holes in the Granite worktops!)
This is where I'd like to take this project next. I actually have 2 neopixel sticks and another RPi1 to try this all out(reason for multicasting Q). Are you using the Adafruit library or some other library for your setup? Also, what kind of refresh rates are you achieving? Is MQTT's latency fast enough to produce some nice color effects smoothly?

mattmiller
Posts: 2188
Joined: Thu Feb 05, 2015 11:25 pm

Re: MQTT -> RPi -> Arduino I/O's

Fri Sep 23, 2016 10:38 pm

I install the Pimoroni UnicornHAT software to drive the Neopixels and just do some math to convert from x,y to linear for the strips :)
I simply just have them synced to the cheerlights colour - and switching to white when person goes into Kitchen - not tried any fancy effects.

MQTT doesn't give any guarantees with timing - neither does the underlying Linux OS :)

asandford
Posts: 1998
Joined: Mon Dec 31, 2012 12:54 pm
Location: Waterlooville

Re: MQTT -> RPi -> Arduino I/O's

Fri Sep 23, 2016 11:30 pm

Node-Red would be a good choice for MQTT<-->Arduino comms as it has Firmata support and (probably) better performance than python.

thomen
Posts: 31
Joined: Mon Sep 01, 2014 3:46 am

Re: MQTT -> RPi -> Arduino I/O's

Sun Mar 12, 2017 11:49 am

Hi just wondering how you ended up getting this working? I was hoping to get a pi and teensy talking either via mqtt or ethernet or the like?

Ideally I'd be sending json or delimited text from the pi to the teensy

asandford
Posts: 1998
Joined: Mon Dec 31, 2012 12:54 pm
Location: Waterlooville

Re: MQTT -> RPi -> Arduino I/O's

Tue Mar 14, 2017 1:46 am

thomen wrote:Hi just wondering how you ended up getting this working? I was hoping to get a pi and teensy talking either via mqtt or ethernet or the like?

Ideally I'd be sending json or delimited text from the pi to the teensy
The Pi with NR can do all of that. Whether the teensy can is a bit out of scope.

ashfaaaa
Posts: 1
Joined: Mon Jul 31, 2017 6:22 am

Re: MQTT -> RPi -> Arduino I/O's

Mon Jul 31, 2017 6:27 am

sj_Rpi wrote:
Sat Sep 17, 2016 11:59 pm
I am using 'MQTT Dashboard' for now until I get my App Inventor client app working correctly. Right now the broker is on the Pi. The goal is to control the IO pins on the Arduino through my Android app over MQTT as well as receive data back.
does Mit App Inventor support Mqtt With user Password?
im looking for an mqtt extension which supports username password.. will you please share it if you have got it?

Return to “Python”