我有一个连接到运行 Arch 的 Raspberry Pi 的 Arduino。
arduino没什么可做的,只是不断打印一些传感器数据:
#include<Arduino.h>
void setup() {
Serial.begin(9600);
}
float temp = .0;
int lumen = 0;
void loop() {
if(Serial.available()) {
temp = analogRead(4) * .4882812 - 273.15;
lumen = analogRead(0);
Serial.print(temp);
Serial.print("|");
Serial.print(lumen);
Serial.println();
}
delay(10);
}
在 Raspberry 上,将运行以下脚本:
# run.py
#!/usr/bin/env python2
from arduino import Arduino
from poster import Poster
import time
import subprocess
import json
if __name__ == '__main__':
with open("config.json") as config_fh:
config = json.load(config_fh)
print(config)
base_url = config["URL"]
security_token = config["KEY"]
port = config["TTY"]
arduino = Arduino(port=port)
arduino.start()
time.sleep(1)
while True:
is_door_open = arduino.is_door_open()
temperature = arduino.get_temperature()
print arduino.is_door_open()
print arduino.get_temperature()
# nmap = subprocess.Popen("./networkClientsInNetwork.sh", stdout = subprocess.PIPE, stderr = subprocess.PIPE)
# network_clients_count = int(nmap.stdout.readlines()[0])
# print network_clients_count
# poster = Poster()
# poster.post_door_state(base_url, arduino.is_door_open(), security_token)
# poster.post_temperature(base_url, str(arduino.get_temperature()), security_token)
# poster.post_clients(base_url, str(network_clients_count), security_token)
time.sleep(3)
from threading import Thread
import serial
import random
import time
这是不断从串口获取值的线程:
class Arduino(Thread):
def __init__(self, port):
Thread.__init__(self, target=self.recieve)
self.daemon = True
self.last_recieved = None
self.serial = self.configure_port(port)
self.serial.open()
def configure_port(self, port_id):
ser = serial.Serial(port=port_id, timeout=1)
ser.rtscts = True
ser.dsrdtr = True
return ser
def recieve(self):
while True:
self.serial.flushInput()
self.serial.flushOutput()
if self.serial.isOpen():
self.last_recieved = self.serial.readline().replace("\r\n", "").split("|")
def getLastRecieved(self):
return self.last_recieved
def is_door_open(self):
try:
lumen = int(self.getLastRecieved()[1])
return_val = lumen > 150
except Exception:
return_val = None
pass
return return_val
def get_temperature(self):
try:
temperature = int(float(self.getLastRecieved()[0]))
temperature_offset = -5
return_val = int(temperature) + temperature_offset
except Exception:
return_val = None
pass
return return_val
现在,当我第一次启动
run.py
时,它永远不会从串口获取数据。他们永远都是None
。
当我跑步时
ino serial
(开始picocom
)
并点击 Enter,Arduino 将发送流中的所有数据。如果我通过Q退出picocom,然后启动
run.py
,python脚本可以从串口读取所有数据。
所以我想知道:我在初始化中缺少什么?如何通过
picocom
使用 python
自动进行串行端口初始化?
我通过提出问题来解决这个问题。我将 Arduino
loop
方法更改为:
void loop() {
temp = analogRead(4) * .4882812 - 273.15;
lumen = analogRead(0);
Serial.print(temp);
Serial.print("|");
Serial.print(lumen);
Serial.println();
delay(10);
}
这将使
run.py
无需通过picocom
进行magic初始化。虽然我仍然想知道
Serial.available()
期望什么作为输入。
import serial
ser=serial.Serial('/dev/ttyUSB0', 9600, timeout=3)
我使用以下代码使用 python 连接到我的 arduino 并接收数据:
import serial
class Arduino():
def __init__(self):
try:
self.arduino = serial.Serial('/dev/ttyUSB0', 9600)
self.receivethread = threading.Thread(target=self.receive)
#self.receivethread.start()
except serial.error as message:
pass
except threading.ThreadError as message:
pass
except Exception as message:
pass
def receive(self):
while True:
if self.arduino.open():
try:
datapacket = self.arduino.readline(30, chr(0))
except Exception as message:
pass
except serial.error as message:
pass
else:
pass
return datapacket