Use Arduino Uno to trigger the execution of python in computer

Hi All,

I'm a begineer of using arduino UNO.

I have a python program that activate the webcam and save the recorded file in a specific path, the python code is also stored in a specific path on the computer.

I'm hoping to use arduino to trigger the execution of the python program (the triggering event could be pressed a button, inputing a singal.....), only triggering is enough, I'm not trying to run the python on Arduino UNO. Is there anyone who have the experience of such communication using arduino code?

Thanks a lot for your help.

The easiest way is probably to have the Python program read the serial port, waiting for an indication from the Arduino that it's time to trigger the webcam.

Search the forum for Robin2, python, serial. IIRC, he's written some useful stuff on the topic.

Thanks a lot, the thread you mentioned is useful and I will try your method.

However, during my research on how to receive a feedback from Arduino using python, I try the code on:

But it didn't work the code is simple, but I cannot establish the connection to Arduino using Python.

when running this line of code:

board = Arduino('COM6')

there is an error:


TypeError Traceback (most recent call last)
in
7 import time
8
----> 9 board = Arduino('COM6')
10 iterator = util.Iterator(board)
11 iterator.start()

~\Anaconda3\lib\site-packages\pyfirmata_init_.py in init(self, *args, **kwargs)
17 args = list(args)
18 args.append(BOARDS['arduino'])
---> 19 super(Arduino, self).init(*args, **kwargs)
20
21 def str(self):

~\Anaconda3\lib\site-packages\pyfirmata\pyfirmata.py in init(self, port, layout, baudrate, name, timeout)
105 # Iterate over the first messages to get firmware data
106 while self.bytes_available():
--> 107 self.iterate()
108 # TODO Test whether we got a firmware name and version, otherwise there
109 # probably isn't any Firmata installed

~\Anaconda3\lib\site-packages\pyfirmata\pyfirmata.py in iterate(self)
280 received_data.append(data & 0x0F)
281 while len(received_data) < handler.bytes_needed:
--> 282 received_data.append(ord(self.sp.read()))
283 elif data == START_SYSEX:
284 data = ord(self.sp.read())

TypeError: ord() expected a character, but string of length 0 found

I have checked the Arduino board is connected to COM6 and no other devices are using the same port.

Thanks a lot for your help.

And I found out that if I press the "RESET" button on the UNO board when the code is running:
board = Arduino('COM6')

IT WORKS!!!!…......for once

it gave me the reading from the Arduino

But when I run the code again directly, the error now changed to:

SerialException: could not open port 'COM6': PermissionError(13, 'permission denied。', None, 5)

I have to unplug the Arduino and reconnect in order to work again, the above repeated.....

Does anyone have the similar experience?

kit0925:
But when I run the code again directly, the error now changed to:

SerialException: could not open port 'COM6': PermissionError(13, 'permission denied。', None, 5)

My guess is that when you reset the Arduino it caused Windows to open a different serial port because it would have appeared to Windows that the Python program was still using COM6

...R

Thanks a lot for your information about the serial communication, I found it really useful.

About the point you have mentioned, I checked the port didn't change when I press the reset button on UNO.

Based on the idea above, my project will look like this:

  1. Run python to monitor the arduino board

  2. An Arduino event will trigger the python to activate the webcam and start recording. (maybe if there is a 5V signal)

  3. Another arduino event will trigger the python to deactivate the webcam and save the video file at a specific location with a time specific name (to avoid overwriting the previous video file)

  4. Python code monitor the arduino again and wait for another triggering from arduino.

The idea is to minimize the human effort, so pressing reset and unplugging and reconnecting the arduino is not good enough for this project.

Is there anyway to access the port properly from your experience?

kit0925:
Is there anyway to access the port properly from your experience?

It's not clear to me what your present problem is - but I'm not a Windows expert.

Have a look at this Simple Python - Arduino demo. You will have to change the serial port references in the Python code but then it should work with Windows.

Are you getting COM port problems with my simple program?

...R