fly037
fly037

Reputation: 23

Sending data from python to Arduino throught serial port

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()

Upvotes: 2

Views: 9899

Answers (1)

DrM
DrM

Reputation: 2515

To communicate with the Arduino board from a Windows machine, you have to install PySerial. See the instructions here for installing PySerial on your machine: PySerial website

And, make sure that you have installed the correct serial driver for your board. This should be installed with your board software. But, in case you need to do it manually, here are two links that may be helpful, Sparkfun driver instructions and Arduino driver instructions

Then, make sure that you are using the correct com port. Run your arduino IDE, upload your program to the arduino, and then under the Tool menu (in the IDE), set the com port and run the serial monitor. Then, in the serial monitor, enter an 's' and verify that you see the light on, light off messages.

Here are your arduino and python codes, stripped to the minimum set of instructions to demonstrate your example, plus a println() statement (in the arduino code) to echo the received characters in hex. That debugging statement will help you sort out line feeds and so forth as you develop your code.

The codes as listed here, work on my board and Linux machine after changing the pin number for the relay, and the device name for the port. The close() is commented-out only to show you that it works without that line.

On the arduino:

#include <stdlib.h>

char serial;
#define RELAY1  7                       
void setup()
{    
  Serial.begin(9600);
  pinMode(RELAY1, OUTPUT);       
}

void loop()
{
  if(Serial.available() > 0)
  {
      serial = Serial.read();
      Serial.println( serial, HEX);
      if (serial=='s')
      {
        digitalWrite(RELAY1,0);           
        Serial.println("Light ON");
        delay(2000);                                      
        digitalWrite(RELAY1,1);          
        Serial.println("Light OFF");
        delay(2000);
      }
   } 
}

The python code:

import time
import serial

def foo():
    print("sent")
    ardu= serial.Serial('/dev/ttyACM0',9600, timeout=.1)
    time.sleep(1)
    ardu.write('s'.encode())
    time.sleep(1)
    #ardu.close()


foo()

Upvotes: 2

Related Questions