我想通过串行方式从arduino向raspberry pi发送一些参数。我必须每隔0.5秒同时发送这三个参数,它们是树莓派的数字。
Arduino代码:
#include "DHT.h"
#define echo 7
#define triger 8
#define photoresistorPin A0
int lux; //Variable for photoresistor reading
DHT dht;
void setup() {
Serial.begin(9600);
dht.setup(2);
pinMode(7,INPUT);
pinMode(8,OUTPUT);
}
void loop() {
lux = analogRead(photoresistorPin);
float temperature = dht.getTemperature();
long duration, distance;
digitalWrite(triger, LOW);
delayMicroseconds(2);
digitalWrite(triger, HIGH);
delayMicroseconds(10);
digitalWrite(triger, LOW);
duration = pulseIn(echo, HIGH);
distance = (duration / 2) / 29.1;
Serial.println(temperature);
Serial.println(distance);
Serial.println(lux);
delay(500);
}
我收到了python代码:
import serial
ser= serial.Serial('/dev/ttyUSB0',9600)
while True:
line = ser.readline()
print(line.decode("utf-8"))
结果是:
24 #temp
112 #distance
524 #lux
问题是:
如何读取它们并将它们分别放在某个变量中?示例:我想从序列中读取这三个参数,并将它们分别放入某个变量中,例如arduino的温度变量temp =24,0.5秒25后,然后是24,25,23,25。然后取其平均值。24+25+24+25+23+25/6并打印结果。因此,距离和勒克斯也是如此。
https://stackoverflow.com/questions/44334751
复制