树莓派-Arduino通信

时间:2020-02-24 09:25:34

标签: arduino raspberry-pi iot serial-communication

我已经使用arduino设置设计了转速表,并获得了输出值(rpm),但是由于我没有wifi模块,因此我已经使用usb连接了arduino和raspberry pi 4。我可以在pi终端中读取rpm值。但是现在我需要将这些数据发送到adafruit io页面。如何编写代码以实时从pi的usb端口读取数据?我编写了一个脚本,可以将其打印在网页上,但是每次必须编写一个值时。如果我能得到答案,那将真的很有帮助。我是编码的新手,只是探索这些。

from Adafruit_IO import*

ADAFRUIT_IO_USERNAME = '******'
ADAFRUIT_IO_KEY = '**********************' 

aio = Client(ADAFRUIT_IO_USERNAME,ADAFRUIT_IO_KEY)

try:
    test = aio.feeds('test')
except RequestError:
    test_feed = Feed(name='test')
    test_feed = aio.create_feed(test_feed)
val = 4   
aio.send('test',val) 

1 个答案:

答案 0 :(得分:0)

下面的代码是一个示例,该示例说明了在USB连接到/dev/tty.usbmodem14201的情况下在Python中的工作方式:

import serial
ser = serial.Serial('/dev/tty.usbmodem14201', baudrate=9600) # NB set your baudrate to the one you are using!
ser.flushInput()

while True: #constant loop to get readings in real time
    ser_bytes = ser.readline() #read the incoming message
    decoded_bytes = ser_bytes[0:len(ser_bytes)-2].decode("utf-8") #decode it
    print(decoded_bytes) # print out what you got or, alternatively, make a web call to Adrafruit