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
}
}
}
}```