Python 树莓皮;Arduino读取串行传感器数据
我有一个电压传感器连接到我的Arduino uno上,它又连接到我的Raspberry Pi 3上。我想用乒乓球的方式从Arduino到Raspberry Pi获取传感器信息。我将在cronjob上通过Python脚本发送一个字符来唤醒它,并捕获传感器值并将其放入mysql数据库 将来我想在Arduino上增加更多的传感器 我遇到的问题是Python方面,当我运行Python代码时,我只得到一条黑线 Raspberry Pi 3 Python代码:Python 树莓皮;Arduino读取串行传感器数据,python,python-2.7,arduino,raspberry-pi,raspberry-pi3,Python,Python 2.7,Arduino,Raspberry Pi,Raspberry Pi3,我有一个电压传感器连接到我的Arduino uno上,它又连接到我的Raspberry Pi 3上。我想用乒乓球的方式从Arduino到Raspberry Pi获取传感器信息。我将在cronjob上通过Python脚本发送一个字符来唤醒它,并捕获传感器值并将其放入mysql数据库 将来我想在Arduino上增加更多的传感器 我遇到的问题是Python方面,当我运行Python代码时,我只得到一条黑线 Raspberry Pi 3 Python代码: #!/usr/bin/python impo
#!/usr/bin/python
import serial
import MySQLdb
import time
db = MySQLdb.connect(host="localhost",
user="user",
passwd="password",
db="database")
cur = db.cursor()
port = serial.Serial("/dev/ttyACM0", baudrate = 9600, timeout=None)
port.flushInput()
sensor1 = 0;
sensor2 = 0;
sensor3 = 0;
vals = []
while (port.inWaiting()==0):
port.write("*")
time.sleep(1)
vals = (port.readline()).split(',')
print vals
sensor1 = int(vals[0])
sensor2 = int(vals[1])
sensor3 = int(vals[2])
cur.execute("insert into voltage(volts) values(" + str(Battout) + ")" )
cur.execute("SELECT * from voltage")
db.close()
Arduino代码:
const int BattVolt = A0;
int BattVal = 0;
float Battout;
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.flush();
while(!Serial.available()); //wait for character from raspi
delay(1000);
float Voltage;
BattVal = analogRead(BattVolt); //read analog pins
Voltage=BattVal/4.09;
Battout=(Voltage/10);
Serial.print(Battout);
Serial.print(",");
}
对您的实现的一些观察
Battout
在Python脚本中,您期望一行(即字符串以‘\n’结尾),但在ARDUINO C++代码中,使用“代码>打印< /代码>代替代码> PROTLNN<代码>或添加一行提要。
Raspberry Pi 3和uart0(蓝牙)、
uart1(串行)存在问题。
对于Pi 3uart1
,通常在/dev/ttyS0
和TX-GPIO 14、RX-GPIO 15上提供。
uart1的波特率取决于核心时钟。因此,如果核心时钟改变,波特率将改变
解决方法1:在/boot/config.txt
中,添加行core\u freq=250
。保存并重新启动!现在你的圆周率有一个恒定的核心频率
解决方案2:更改设备树,使用uart0
进行串行通信,使用uart1
进行蓝牙(蓝牙现在也有相同的问题)。为什么在python脚本中使用str(Battout)
?谢谢,只是一个简单的错误,没有在Arduino中添加新行就解决了所有问题。