Как выполнить скрипт Python после события Arduino?Python

Программы на Python
Anonymous
Как выполнить скрипт Python после события Arduino?

Сообщение Anonymous »

Я хочу выполнить скрипт Python на своем ПК после обнаружения движения с помощью PIR-датчика, подключенного к Arduino UNO.
Я пытался написать скрипт Python, который прослушивает монитор Arduino serie через COM3 для обнаружения строки.
После обнаружения строки сценарий Python выполняет следующий сценарий.
Код Arduino

Код: Выделить всё

String receivedString = "";  // Variable to store the received word

void setup() {
// Initialize the serial port at 9600 baud
Serial.begin(9600);
Serial.println("Enter a word in the serial monitor:");
}

void loop() {
// If there is data available on the serial port
if (Serial.available()) {
// Read the incoming characters
char inChar = (char)Serial.read();

// If the user presses 'Enter' (newline character)
if (inChar == '\n') {
// Print the received word
Serial.println(receivedString);

// Reset the string for a new input
receivedString = "";

// Prompt the user to enter a new word
Serial.println("");
}
// Otherwise, add the character to the received word
else {
receivedString += inChar;
}
}
}
Код Python

Код: Выделить всё

import serial
import subprocess

ser = serial.Serial('COM3', 9600, timeout=1)

while True:
# read data send by the arduino
data = ser.readline().decode().strip()
if data == "camera":  # if the command camera is written on the monitor serie
print("python script execution")
else:
print(f"Donnée reçue : {data}")  # display data
Дело в том, что при запуске кода ARDUINO на COM3 коду Python запрещается доступ к COM3.
Следующая ошибка:
"serial. Serialutil.SerialException: не удалось открыть порт «COM3»: PermissionError(13, «Отказ в доступе.», Нет, 5)»
Я не знаю, хорошее ли мое решение ... Я подумал, что будет проще выполнить код Python из небольшого события Arduino в качестве обнаружения датчика PIR.

Подробнее здесь: https://stackoverflow.com/questions/790 ... uino-event

Вернуться в «Python»