sending information to arduino

I have wrote a python skipt to send a command to my arduino throgh the serial port if a specific twitter post gets a like. The command is just a 1 and woks if i send it with the serial monitor but not if it´s send from the python skript.

Python Skript to send:

ser = serial.Serial('COM6', 9600)  # open serial port
print(ser.name)         # check which port was really used
ser.write(b'1')     # write a string
ser.close()

Arduino code(shortend):

int pinNumber = 1;
int delayTime = 300;
char newLike;

void setup() {

  while(pinNumber <= 12){
    pinNumber++;
    pinMode(pinNumber, OUTPUT);
    
  }
  Serial.begin(9600);
}

void loop() {

  if (Serial.available() > 0) {
    newLike = Serial.read();
    if (newLike == 1){
        blendIn();
        blendOut();
        movingLight();
        heartSplitIn();
        heartSplitOut();
        newLike = 0;
    }
  }
}
if (newLike == 1)

Did you perhaps mean

if (newLike == '1')

if your script keeps opening and closing the Serial port, your arduino will reboot every time - not sure if that matters in you design

How do you send a binary 1 with Serial Monitor?

I don't know enough Python to know if "ser.write(b'1')" is the right syntax to send a binary 1.

I would send commands in ASCII to make the serial line easier to log.

UKHeliBob:

if (newLike == 1)

Did you perhaps mean

if (newLike == '1')

corrected the code and still the same problem but it recives it (rx led blinks)

johnwasser:
How do you send a binary 1 with Serial Monitor?

I don't know enough Python to know if "ser.write(b'1')" is the right syntax to send a binary 1.

I would send commands in ASCII to make the serial line easier to log.

replacing the 1 with an a doesn´t change anything

johnwasser:
How do you send a binary 1 with Serial Monitor?

I don't know enough Python to know if "ser.write(b'1')" is the right syntax to send a binary 1.

I would send commands in ASCII to make the serial line easier to log.

that sends a string - ser.write(b'hello') would send Hello for example but then ASCII or another encoding is depending on your python version and how it's configured if I remember correctly

Woodpeckerfpv:
corrected the code and still the same problem but it recives it (rx led blinks)

replacing the 1 with an a doesn´t change anything

send something and on the arduino side print the HEX you receive then You'll know :slight_smile:

how can i print the Hex? I´m a newbe

Woodpeckerfpv:
how can i print the Hex? I´m a newbe

:o
RTFM... here is the doc for Serial.print()

so the answer if hidden into the following examples

Serial.print(78, BIN) gives "1001110"

Serial.print(78, OCT) gives "116"

Serial.print(78, DEC) gives "78"

Serial.print(78, HEX) gives "4E"

:slight_smile:

thank you

1 gives me 31 and a gives me 61 using the python skript I get nothing

Woodpeckerfpv:
1 gives me 31 and a gives me 61 using the python skript I get nothing

So the "ser.write(b'1')" isn't getting any data to your sketch. That sounds like a programming or hardware error somewhere. If Serial Monitor on COM6 works and Python on COM6 doesn't, I'd think the problem was in the Python program.

The Python code in the Original Post is not sufficient.

If it is sending data to an Uno, Mega or Nano it will cause the Arduino to reset when it opens the serial port and it does not wait for the Arduino to reset before trying to send data.

If you don't want the Arduino to reset then the Python program should keep the serial port open until it is completely finished with the Arduino.

Have a look at this Python - Arduino demo and at the examples in Serial Input Basics - simple reliable ways to receive data.

...R

added a 2 second sleep now it works. the original skript opens the port at the beginning. Thank you everyone have a nice day

Woodpeckerfpv:
added a 2 second sleep now it works. the original skript opens the port at the beginning. Thank you everyone have a nice day

yep that was mentioned in answer #2