I'm trying to send a float from arduino, and read it with python using serial.
I'm generating numbers on the arduino with the following code:
This is the arduino code, it works
long randNumber;
float avgMe;
float theTemp;
void setup() {
Serial.begin(9600); // initialize print
Serial.println("Start session");
}
void loop() {
avgMe = 0;
for (int i = 0; i < 10; i++) {
randNumber = random(0, 11);
delay(100);
avgMe = avgMe + randNumber;
}
theTemp = avgMe / 10;
Serial.print(theTemp);
}
It prints what i want to grab in python:
Start session
4.90
6.80
3.90
5.70
6.10
This is what i trid in python to grab it:
# %% Import
import serial
import time
import struct
# %% connect
try:
ser = serial.Serial('COM3', 115200, timeout=.1)
time.sleep(2)
print("Connection to " + 'port' + " established succesfully!\n")
except Exception as e:
print(e)
# %% loop for some time
t_end = time.time() + 10
while time.time() < t_end:
data = ser.read(4)
if data:
# test = struct.unpack(">fff",data)
print(data)
time.sleep(1)
ser.close()
Python prints:
b'\x00\x00\x00\x00'
b'\x00'
b'\x00\x00\x00\x00'
b'\x00\x00\x00\x00'
b'\x00\x00\x00\x00'
b'\x00\x00\x00\x00'
b'\x00'
But firstly i can't convert that, also it doesn't seem like the information is there. I would love your input on this
9600while python is collecting at115200. They need to be the same for data transfer to work correctly.