francescovalle84
Posts: 5
Joined: Thu Oct 25, 2018 9:38 am

Comunication between Raspberry and Arduino

Fri Oct 26, 2018 3:37 pm

Hi all,

I am pretty new in the Raspberry world, and I need your help for my next project. :roll:
I already have create an arcade cabinet using Raspberry and RetroPie, and it was nice time with good results!

Now I would like to use the same Raspberry for my second project, involving Arduino and my table soccer. Arduino should detect goals, while Raspberry should show the results, and manager the front end logic. I already implemented Arduino logic to catch goals, and I need now to make it comunicate with Raspberry over wifi network.

In particular I would like to send simple characters (R for red and B for blue) when a goal is detected. I would like to manage on the Raspberry all the logic (new match, tournament, new player, ...)

Do you have any advise about programming language and framework to use? I can program in Java/Javascript, but I am also interested in learning something new, if it will help me having better results!!

Moreover, it would be possible to have it installed on same Raspberry? It would be possible to have a dual boot on Raspberry, or better to make it working in RetroPie?

Thank you,
Francesco

Andyroo
Posts: 795
Joined: Sat Jun 16, 2018 12:49 am

Re: Comunication between Raspberry and Arduino

Fri Oct 26, 2018 6:48 pm

Have a look at MQTT to communicate between the two processors so you can use a WiFi link between them and libraries are available for both machines. If this is overkill, you could use a serial connection between them and a simple handshake to make sure all goals are reported.

As for development languages, you are currently looking at a version of C or Python. C is the default for Arduino but has a version of Python for some units while you can get lots of support for Python on the Pi :lol: I would go for Python as it’s reasonably simple to learn and debug.

As for dual boot, have a look at PINN as the start up code.

No idea on the RetroPi code though...
Need Pi spray - these things are breeding in my house...

francescovalle84
Posts: 5
Joined: Thu Oct 25, 2018 9:38 am

Re: Comunication between Raspberry and Arduino

Mon Oct 29, 2018 3:06 pm

Hi @Andyroo,

I realized the first part with MQTT (now Raspberry receives "R" or "B" when a goal is scored). I also used PINN to manage several OS, passing from one to another with some scripts. Very nice solution in my opinion.

Now I need to create an application/web app/something able to read the input and to update automatically the result. I would be happy to learn Python, but I need some good links/samples/tutorial to start. Do you have any to share?

My dream would be something like this: https://youtu.be/vZXRub7AwhA

Thank you,
Francesco

Andyroo
Posts: 795
Joined: Sat Jun 16, 2018 12:49 am

Re: Comunication between Raspberry and Arduino

Mon Oct 29, 2018 6:45 pm

Eclipse Paho has a library for Python https://www.eclipse.org/paho/clients/python/

It acts on a callback process so when a message comes in the code is called and that’s where you could put score display.

Paho has plenty of examples on the web as it’s a very popular library and available for lots of languages :D
Need Pi spray - these things are breeding in my house...

francescovalle84
Posts: 5
Joined: Thu Oct 25, 2018 9:38 am

Re: Comunication between Raspberry and Arduino

Mon Nov 12, 2018 6:17 pm

Hi,

I am currently going on, but I need help with Python programming... probably it wil be very simple, but I can't get it!! :oops:
Looking at materials from courses and on the forums, I realized this simple UI with PyQt5:

Code: Select all

#!/usr/bin/python3
# -*- coding: utf-8 -*-

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QMessageBox, QLabel

class Example(QWidget):
    
    def __init__(self):
        super().__init__()
        self.initUI()
        
    def initUI(self):
        qbtn = QPushButton('Quit', self)
        qbtn.clicked.connect(QApplication.instance().quit)
        qbtn.resize(qbtn.sizeHint())
        qbtn.move(50, 50)
        
        # Should increment this when MQTT client receives something
        counter = 0
        label = QLabel(str(counter), self)
        label.move(100,100)
        
        self.setGeometry(50, 50, 300, 300)
        self.setWindowTitle('Sample window')
        self.show()

if __name__ == '__main__':
    
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())
It works properly, if launched from Thonny.

I already have this paho client script, that works properly if launched from the shell:

Code: Select all

# import
import time
import paho.mqtt.client as mqtt

# broker IP address
Broker = "192.168.178.52"

# publish topic
topic = "test/topic"

# on connect function
def on_connect(client, userdata, flags, rc) :
	print("Receiving client connected with code: " + str(rc))
	client.subscribe(topic)

# on message function
def on_message(client, userdata, msg) :
	print(msg.topic + " " + str(msg.payload) + " " + str(client))

# instantiate paho MQTT client
client = mqtt.Client()

# add on_connect and on_message functions to client events
client.on_connect = on_connect
client.on_message = on_message

# connect paho client to mosquitto broker (IP, port, timeout)
client.connect(Broker, 1883, 60)

# client loops forever
client.loop_forever()
Now... how can I update the "counter" value that is displayed on the UI when a new message arrive? I tried to put all in a single file, creating dedicated functions, but it doesn't work... it always gives me "ImportError: No module named 'paho'"

Can you please explain me how should I link the files? Have I to create two distinct classes? How can I do?

Thank you,
Francesco

francescovalle84
Posts: 5
Joined: Thu Oct 25, 2018 9:38 am

Re: Comunication between Raspberry and Arduino

Tue Nov 13, 2018 4:13 pm

I think I solved a first problem... paho was installed for Python 2.7, and not under Python 3.

I also merged the files, and obtained this code:

Code: Select all

#!/usr/bin/python3
# -*- coding: utf-8 -*-

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QMessageBox, QLabel
import time
import paho.mqtt.client as mqtt

class Example(QWidget):
    
    def __init__(self):
        super().__init__()
        self.initUI()
        
    def initUI(self):
        print("Button created")
        qbtn = QPushButton('Quit', self)
        qbtn.clicked.connect(QApplication.instance().quit)
        qbtn.resize(qbtn.sizeHint())
        qbtn.move(50, 50)
        
        print("Label created")
        # Should increment this when MQTT client receives something
        counter = 0
        label = QLabel(str(counter), self)
        label.move(100,100)
        
        self.setGeometry(50, 50, 300, 300)
        self.setWindowTitle('Sample window')
        
        Broker = "192.168.178.52"

        print("Creating client")
        client = mqtt.Client()
        client.on_connect = on_connect
        client.on_message = on_message
        print("Step 1")
        client.connect(Broker, 1883, 60)
        print("Step 2")
        client.loop_start()
        print("Step 3")
        
        self.show()
    
# on connect function
def on_connect(self, client, userdata, rc):
    print("Receiving client connected with code: " + str(rc))
    topic = "test/topic"
    client.subscribe(topic)

# on message function
def on_message(self, client, userdata, msg):
    print(msg.topic + " " + str(msg.payload) + " " + str(client))
        
if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())
The problem is that if I run it, I receive this output, but nothing when an MQTT message arrives on the topic...

Code: Select all

Button created
Label created
Creating client
Step 1
Step 2
Step 3
Receiving client connected with code: 0
Instead, if I run the initial script, I receive an output like this:

Code: Select all

Receiving client connected with code: 0
test/topic b'Message' <paho.mqtt.client.Client object at 0x767dcdd0>
test/topic b'Message' <paho.mqtt.client.Client object at 0x767dcdd0>
The only difference is that I use client.loop_forever() in the script that works as expected, while client.loop_start() in the complete file, because otherwise the UI remains hidden (maybe because are on the same thread?)

There should be something wrong in my code... can you please help me?

Thank you,
Francesco

Return to “General programming discussion”