I'm trying to make Arduino trigger a relay if the char "s" is read on serial port. That char "s" is sent by python based on an image read from screen.
My problem is that arduino seems not able to read from serial port as it never performs the if condition. My guess is that there is some kind of deadlock between the two (that's why I put ardu.close() in the function foo)
This is my arduino code:
char serial;
#define RELAY1 7
void setup()
{
Serial.begin(9600);
pinMode(RELAY1, OUTPUT);
}
void loop()
{
if(Serial.available() > 0){
serial = Serial.read();
//Serial.print(serial);
if(serial=='s'){
digitalWrite(RELAY1,0);
Serial.println("Light ON");
delay(2000);
digitalWrite(RELAY1,1);
Serial.println("Light OFF");
delay(2000);
}
}
}
This is my python code:
import time
import serial
#from serial import serial
import cv2
import mss
import numpy
import pytesseract
def foo():
print("sent")
ardu= serial.Serial('COM6',9600, timeout=.1)
time.sleep(1)
ardu.write('s'.encode())
time.sleep(1)
ardu.close()
foo()
sleep()after the write as well as before; that immediate close you're currently doing could potentially shut down the serial port before the character has been fully sent.