gpt4 book ai didi

python - 使用串行端口和 MySQL 从 arduino 在 Rpi 上收集数据

转载 作者:行者123 更新时间:2023-11-30 22:06:17 25 4
gpt4 key购买 nike

我正在制作一个 arduino 程序,通过 USB 将一些传感器数据发送到 Raspberry pi model 3

我还制作了一个 python 脚本来将这些数据记录到 Raspberry 和 SQL Base

arduino 发送一个 1 来知道何时获取第一个值,rPi 脚本将模式从 0 更改为 1,然后开始读取 4 个值,这就是问题来的时候,因为 rPi 没有注意到 1 和从不改变模式 1

这是 python 代码:

while True:
while mode==0:
x = ser.readline()
if x==1:
//never gets here
print("b")
mode = 1
print("Starting data Gathering at rPi from arduino")

while mode==1:
print("Gathering data")
text1 = "CFL"
ignore1 = ser.readline()
x1 = ser.readline()
x1 = double(x1)
x2 = ser.readline()
x2 = double(x2)
y = ser.readline()
y = int(y)
z = ser.readline()
z = int(z)
time1 = time.time()
date1 = time.strftime('%d-%m-%d')
print("Saving sql data")
sql = "INSERT INTO tempdat(tdate, ttime, zone, DHTtemp, DHThum, SoilHum, Light) VALUES (%s, %s, %s, %d, %d, %d, %d)" %(date1,time1,text1,x1,x2,y,z)
cursor.execute(sql)
db.commit()

还有 arduino 的:

void loop() {

Serial.println(1);

Serial.println(read_temp());
delay(1000);

Serial.println(read_humidity_DHT());
delay(1000);

Serial.println(read_humidity_soil());
delay(1000);

Serial.println(read_light());
delay(10000);
}

也很想问问是否有人知道,在 rPi 上标记我发送的信息以了解传感器来自的最佳方式。提前致谢

*编辑一些代码在创建问题时被严重复制

最佳答案

Serial.println 发送 ASCII 码后跟 \r\n。您正在将 ASCII 字符与整数值进行比较。试试这个:

    x = ''
while ser.inWaiting():
c=ser.read()
if c not in "\r\n":
x += c
if x=='1':
. . .

关于python - 使用串行端口和 MySQL 从 arduino 在 Rpi 上收集数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41641855/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com