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

MQTT/TCP data to Serial Port-- confused

Thu Oct 06, 2016 10:56 pm

Hi all, I'm kind of stuck with this problem. I built a proof of concept app with App Inventor. It's a TCP and MQTT client app. I have a Mosquitto Broker and a TCP Server( ... a-over-tcp) running on my RPi2. I can connect to both servers and send data to my RPi okay.

I also have Node Red setup along with an Arduino UNO attached via USB with the Firmata firmware running on it. Node Red is running fine, I have some servo's and led's I can control on the Arduino with no problems. I'd like to add the same kind of functionality within my app. I would like to continue using Firmata but if I have to write a simple sketch on Arduino to retrieve the serial data and use PySerial so be it.

So where I'm at right now. I'm trying to use PyFirmata to send TCP data from my app to the RPi ->Arduino to blink a LED. Using this documentation( the furthest I've gotten was turning the LED by adding the red to the script

Code: Select all

from pyfirmata import Arduino, util
board = Arduino('/dev/ttyACM0')
import socket

host = ''        # Symbolic name meaning all available interfaces
port = 12345     # Arbitrary non-privileged port
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((host, port))

print host , port
conn, addr = s.accept()
print('Connected by', addr)
while True:

        data = conn.recv(1024)

        if not data: break

        print "Client Says: "+data[11].write(1)
        conn.sendall("Server Says:hi")

    except socket.error:
        print "Error Occured."

I added this line:[11].write(1) to the script, which does turn the LED on the first time data is received, but that's about it. I tried replacing the (1) with (data) and some other variations but I can't seem to get the LED to turn ON/OFF. I'm confused at this point. I haven't tried PySerial yet so I dont know if I would have a easier time using that library instead. If you've read this far, thanks, Im open to any ideas.

User avatar
Posts: 6269
Joined: Tue Oct 09, 2012 11:46 am
Location: Trondheim, Norway

Re: MQTT/TCP data to Serial Port-- confused

Fri Oct 07, 2016 9:02 am

If "write(1)" turns ON the LED, then I assume a "write(0)" turns it OFF.

So you may have to start a separate thread to do blinking without slowing down your server, but for a test just take 1 second sleep/pause and then do a write(0)

Return to “Troubleshooting”