简体   繁体   English

树莓派 - Arduino 通信

[英]Raspberry pi - Arduino communication

I've designed a tachometer using an arduino setup and I get the output values(rpm), but since I don't have an wifi module, I've connected my arduino and raspberry pi 4 using usb.我使用 arduino 设置设计了一个转速计,并获得了输出值(rpm),但是由于我没有 wifi 模块,我使用 USB 连接了我的 arduino 和 raspberry pi 4。 I can read the rpm value in the pi terminal.我可以在 pi 终端中读取 rpm 值。 But now I need to send these data to an adafruit io page.但是现在我需要将这些数据发送到 adafruit io 页面。 How do I write the code to read the data from the usb port of my pi in real-time?如何编写代码从我的pi的usb端口实时读取数据? I've written a script which can print it on the webpage but each time I've to write a value.我写了一个脚本,可以将它打印在网页上,但每次我都要写一个值。 It would be really helpful if i can get the answers.如果我能得到答案,那将非常有帮助。 I'm new to coding and just exploring these.我是编码新手,只是在探索这些。

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) 

The below code is an example of what will work in Python, assuming your USB is connected at /dev/tty.usbmodem14201:下面的代码是在 Python 中工作的示例,假设您的 USB 连接在 /dev/tty.usbmodem14201:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM