【发布时间】:2018-08-27 13:26:52
【问题描述】:
我使用 Arduino Uno 将来自光传感器的模拟数据转换为数字数据,然后通过 USB 数据线将这些数据发送到树莓派。但是,当我运行代码时,我会从 10 位传感器读取像 1923 这样的值。
这是arduino代码
int a = A0;
int meas = 0;
void setup() {
Serial.begin(9600);
}
void loop() {
meas = analogRead(a);
if(Serial.readString() == "1"){ //Check that Raspberry wants data or not
Serial.println(meas);
}
}
这是树莓派中的 Python 代码
import serial
from datetime import datetime
now = datetime.now()
ser = serial.Serial('/dev/ttyACM0', 9600)
ser.write("1".encode())
s = ser.readline()
file = open("dataset.txt", "a")
file.write(now.strftime("%Y-%m-%d %H:%M") + " Sensor Value:" + str(s)+ "\n")
file.close()
这是每 5 分钟运行一次代码后的示例输出
14:08 Sensor Value:6
14:10 Sensor Value:8
14:15 Sensor Value:8
14:20 Sensor Value:10
14:25 Sensor Value:6
14:30 Sensor Value:9
14:35 Sensor Value:6
14:40 Sensor Value:7
14:45 Sensor Value:5
14:50 Sensor Value:5
14:55 Sensor Value:12
15:00 Sensor Value:1
15:05 Sensor Value:1
15:10 Sensor Value:10
15:15 Sensor Value:12
15:20 Sensor Value:14
15:25 Sensor Value:1922
15:30 Sensor Value:2211
15:35 Sensor Value:11
15:39 Sensor Value:6
15:40 Sensor Value:7
15:45 Sensor Value:8
15:50 Sensor Value:10
15:55 Sensor Value:1
16:00 Sensor Value:
16:05 Sensor Value:11
我想去掉这些 1 和 1922 之类的东西,它们肯定是毫无意义的数据。
PS:传感器在山顶,我正在使用远程连接检查数据和操作代码。
我该怎么做?感谢您的宝贵时间。
【问题讨论】:
-
在 Arduino 上阅读之前尝试测试
serial.available()并在 Pi 上的readline()之前等待 200 毫秒。
标签: python arduino raspberry-pi arduino-uno pyserial