How to send a value from Arduino to Python and then use that value

I am creating a robot that is remotely controlled using Python to send control messages over the Internet via a simple graphical interface.

I got a part of my code that works very well, a graphical interface and a control system, but I'm stuck. I am trying to use the ping parallax sensor to get the distance to object information from an Arduino Mega and send this value to my Python script control for display on the remote GUI.

The main problem I am facing is how to integrate Python code that will use the already installed COM port with Arduino, and send a message to tell Arduino to poll the ping sensor, and then send it to a Python program that will get the value and then let me paste this value into my GUI.

I already have this code to control the Arduino, and it works with my simple graphical interface.

import serial ser = serial.Serial('/dev/ttyUSB0', 9600) from PythonCard import model class MainWindow(model.Background): def on_SpdBtn_mouseClick(self, event): spd = self.components.SpdSpin.value def on_FBtn_mouseClick(self, event): spd = self.components.SpdSpin.value ser.write('@') ser.write('F') ser.write(chr(spd)) def on_BBtn_mouseClick(self, event): spd = self.components.SpdSpin.value ser.write('@') ser.write('B') ser.write(chr(spd)) def on_LBtn_mouseClick(self, event): spd = self.components.SpdSpin.value ser.write('@') ser.write('L') ser.write(chr(spd)) def on_RBtn_mouseClick(self, event): spd = self.components.SpdSpin.value ser.write('@') ser.write('R') ser.write(chr(spd)) def on_SBtn_mouseClick(self, event): spd = self.components.SpdSpin.value ser.write('@') ser.write('S') ser.write('0') def on_PngDisBtn_mouseClick(self, event): ser.write('~') ser.write('P1') ser.write('p2') app = model.Application(MainWindow) app.MainLoop() 

What I really would like to do is improve the code above and add a button to click to tell Python to send a message to Arduino to check the ping sensor and return the value. I am very knowledgeable with Arduino code, but I just started playing with Python in the last two weeks.

+7
source share
3 answers

Basically, you just send a suitable command to Arduino, just like you, but wait until Arduino returns something; The end of python might look something like this.

 ser.write('foo') retval = ser.readline() # read a complete line (\r\n or \n terminated), #or you could use read(n) where n is the number of bytes you want (default=1) ping_data = retval.strip() # strip out the newline, if you read an entire line 

of course you get a string, you probably want to convert it to int or float to use it in calculations later (use int (ping_data) or float (ping_data) for strings or struct.unpack in case its byte sequence, which First it needs to be unpacked, but it all depends on how you present the sensor data).

+1
source

Perhaps check out the Pyduino project :

pyduino is a library that allows you to talk to Arduino boards loaded with Firmata protocol from Python. It currently supports version 2 of the Firmata protocol.

+1
source

First, let me say that the previous answers are good, helpful, and relevant. My comments are more general and apply to anyone who implements a bi-directional data stream to and from Arduino. The main idea is to design the data stream so that it is a person suitable for data related to the sketch of Arudino and a person readable for data coming from the sketch of Arduino. This is not always possible, but often it is.

The main idea is to run the Arduino sketch several times using the Serial Monitor. The serial monitor can be found in the Tools menu of the IDE menu. You can also enter Ctrl-Shift-M to call up the Serial Monitor.

The serial monitor displays what the Arduino sketch sends you. However, it also allows you to enter data that is sent to an Arduino sketch. In other words, you test and debug both sides of the serial data stream just by using a serial monitor.

Look what appears. This will often be useful if your sketch is trying to send data back through Serial.print (). A few notes. Make sure that the baud rate set in Serial Monitor exactly matches the baud rate in your sketch (9600 is a good choice in almost all cases).

The second note is crucial. Casting a Serial Monitor forces a reset on the Arduino board. Your sketch begins (always). This is good because it gives you a fresh turn every time. Please note that you can force reset by setting the baud rate to 9600 (even if it is already 9600). This allows you to run many tests inside the serial monitor without having to restart the serial monitor every time.

0
source

All Articles