How to send Date and time to Arduino via pyserial

Hello I want to send date and time to my Arduino, when I turn on the light i want it to say light is on or off an the date and time the light turned on. This is what I have now, i get an error saying TypeError: unicode strings are not supported, please encode to bytes: '20-Apr-2021 (10:39:20.125972)'
https://www.nomnomnow.one/
Python Code

import datetime as dt
import serial
import time
if __name__ == '__main__':
ser = serial.Serial('/dev/ttyACM0', 9600, timeout=1)
ser.flush()
while True:
curent_date = dt.datetime.now()
ser.write(curent_date.strftime("%d-%b-%Y (%H:%M:%S.%f)"))
line = ser.readline().decode('utf-8').rstrip()
print(line)
time.sleep(1)

Arduino code

#include <Servo.h>

Servo myservo;
int pos = 0;
int soundSensor=2;
int LED=4;
boolean LEDStatus=false;

void setup() {
Serial.begin(9600);
myservo.attach(9);
pinMode(soundSensor,INPUT);
pinMode(LED,OUTPUT);


}

void loop() {




int SensorData=digitalRead(soundSensor);
if(SensorData==1){
if(LEDStatus==false){
LEDStatus=true;

String data = Serial.readStringUntil('\n');
Serial.println("Light ON"+ data);
digitalWrite(LED,HIGH);
for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15);
// waits 15ms for the servo to reach the position
}
}
else{
Serial.println("Light OFF");
LEDStatus=false;
digitalWrite(LED,LOW);
for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15);
// waits 15ms for the servo to reach the position
}
}

}

}```
Last edited on
What are you trying to do?
Sending the date/time when to switch the light on?
Or getting the date/time when the light is switched on?

Your Arduino code does nothing with date/time.

i get an error saying TypeError: unicode strings are not supported, please encode to bytes: '20-Apr-2021 (10:39:20.125972)'
That's the message you get? When and from whom?

There is really no need to use utf-8. Why not just numbers. But again: What does it has to do with the Arduino?
Registered users can post here. Sign in or register to post.