Can't send/receive command over serial to Arduino to control LED - python

I have been working on a little side project in which I want to control my Arduino pins via UI that was designed in Tkinter.
The UI is set to send serial command to my Arduino that will place that particular PIN high and turn on the LED for example.
The problem that I am facing is that when I check one of my pins, for instance, PIN4 and press 'SET', a routine is called which keep the PIN high for a small duration of time before putting the PIN to low again. I have notice that this happens when multiple USBs are connected to my laptop. If I remove all USB devices except Arduino and then run my sript then the program works flawlessly as expected.
My intention is to simply connect my arduino to my laptop while the scipt automatically connects the appropriate COM port.
Could someone help me understand where I am making a mistake in my script.
Thanks
Here is the link to the code: http://www.heypasteit.com/clip/28PJ

Related

Detect connection loss when working with Raspberry Pi's Remote GPIO feature (in Python)

I am controlling the GPIO pins of a Raspberry Pi over a WiFi network from a control device (in this case a Linux laptop), which is running a Python programme that uses the gpiozero library's Remote GPIO feature to control the pins.
In the event that the WiFi connection between the control device and the Raspberry Pi is severed, I want to be able to detect that this has happened so that I can take measures to ensure that the programme on the control device does not lock up.
To give an example of the most likely scenario, let's say that the robot the Raspberry Pi is installed in drives out of WiFi range.
The function that I am using to quit the control device programme is set up to automatically tell tell the robot to stop moving before quitting the programme. This is done by setting the values on the GPIO pins that connect to the motor driver.
Problem is, when I simulate a connection break by turning off the WiFi on the Raspberry Pi, if I then click on the quit button on the control device programme, the code execution just seems to stop at the point where it is trying to set the relevant GPIO pins to stop the robot's motors.
My suspicion is that the control device programme is just waiting forever for a socket response that will never come.
In order to address this problem, can anyone give me advice on doing any of the following:
Detecting when the connection between the control device is cut, and running a function in response.
Setting a timeout on the Remote GPIO connection, which I can then catch with a try statement.
Any other fix you think might work (please give details).
Thanks.

Pyserial cannot send all characters to arduino, but can do it on another PC terminal

I am working on an existing project.
Until now, a PC software controls an Arduino Due.
The PC software sends serial commands to the Arduino Due.
What I am trying to do, is to replace the PC software with a python script. Python 3.5.
So I am working with pyserial.
The problem seems to be that the python script does not send all the characters to the Arduino Due. It misses some final characters.
The difficult parts to understand are the following:
When I am sending the characters, from the python script, to another PC terminal instead of the Arduino, then I can successfully collect all the characters from the terminal, I am using Bray's terminal.
When I am sending the same string from my terminal to the Arduino Due, the Arduino Due successfully collects the data sent.
It seems as if only the Python to Arduino does not work, while
Python to PC termimal is working and
PC terminal to Arduino is working
I open the serial port like this:
my_port = serial.Serial('COM6', 115200)
while connected != True:
if my_port.is_open == 1:
connected = True
Can anyone provide any insight?
Thanks.
edit: I just noticed that when the python script sends the data, then the debug serial port I am using sends corrupted data.
Solved it.
I noticed that the debug serial was also sending less characters and I thought that there maybe a reset going on.
So I am now sending my arrays from a thread after each button press.
What I was doing, is that I was sending it directly after connection.

RosAria doesn't update after certain time?

I have a robot (Pioneer 3-AT) with distance sensors that I wan't to drive around autonomously.
What am I using?
Pioneer 3-AT (Robot with wheels and a bunch of sensors and stuff, see: http://www.mobilerobots.com/ResearchRobots/P3AT.aspx)
ROS (tool to let scripts interact over network by publishing and subscribing to "topics", see: http://www.ros.org/)
RosAria (this connects the ROS network and Pioneer, it takes data published to the /RosAria/cmd_vel topic and moves the robot accordingly, see: http://wiki.ros.org/ROSARIA)
This is the setup:
The sensors are connected to an Arduino Mega that is connected to the robot over serial port (/dev/ttyS0), after the Arduino gets the sensors data it sends them over the serial port to the robot, where a python scripts takes this data and uploads it to the ROS network, this upload automatically gets detected by RosAria which in turn moves the robot.
In short:
Sensors ---> Arduino ---> Robot (Python Script) --(ROS)-> RosAria
The python script also turns the sensor data into commands for the robot (left, right, ...)
This setup works ... for a certain amount of time
The robot automatically moves forward after the script is started.
If I hold something in front of the sensors it changes direction. NOTICE: This is whilst the robot is connected to my PC using Ethernet. However, after I disconnect the Ethernet cable it continues to work for a short amount of time.
This bug is strange
It stops working in a very strange way.
I found this out whilst debugging:
the robot continues to move forward but does not react to sensor input.
(both things are handled in the same script, at the same place)
the sensors output the data correctly
the script doesn't output any errors
the connection to RosAria is still running
RosAria receives the movement commands
According to what I found out, it should work perfectly (since it does so when connected to the ethernet), but weirdly enough, after a few meters it changes from avoiding obstacles very well to going full on berserk and just ramming everything out of its way.
What could be wrong?

How to use multiple Arduino boards with a single Raspberry Pi for sending and receiving data?

I am doing a project that involves using a Raspberry Pi to control all the switchboards in my house. I will start by wiring three switchboards first.
I am planning to use one 8-channel and one 2-channel relay for each of the switchboards, as each of these switchboards contain 10 switches. So, that amounts to 30 relay units.
However, I do not have as many as 30 free GPIO pins on my Raspberry Pi. I can use a port expander, but the Pi is installed with a camera and stationed near a door and fixed there. So, all the 30 wires from the switchboards need to be brought to the Pi, which will make the walls look messy. I was wondering if I could install an Arduino mini for each of the switchboards and control the relays using the Arduinos.
The Arduinos in that case need to the connected to the Raspberry Pi somehow such that when I want to switch on a specific light in my bedroom, I send a command to the Pi. The Pi sends the corresponding information to the Arduino in some well-defined format, like JSON, mentioning the switch number and the action to be performed. The Arduino switches on or off the switch and returns a message to the Pi.
Can this be done? I would be grateful if anyone could help me with this.
Thanks in advance.

Issue with raspberry pi serial port

I'm using raspberry pi 2 to control 2 types of printers. I found at this site how to send serial commands in hexadecimal format using python, like:
cmdTest=b"\x12\x02\x42"
ser.write(cmdTest)
This code works true for one printer but fail for another!
knowing that:
I test both devices using a normal computer and both works.
Both are from same manufacturer (ELTRASISTEMI).
I am using the appropriate commands for every printer
I am specifying the correct baud rate knowing that both are of the same baud rate.
My problem solved, Although something still not clear.
The commands were correct the problem is something related to the serial convertor I were using. Now I used a usb to serial convertor instead of a shield convertor and it works great with same code and same commands. Although it worked, I'm still confused why a printer worked with RS232 shield and other not.

Categories

Resources