树莓派玩具怎么优化?

2020-08-17 18:33:52 +08:00
 Breadykid

其实树莓派自带 GPIO 接口,想把我目前的实现变成舍弃 Arduino,让树莓派直接接元器件,有没有好的建议? readme如下:

基于 Rasperry Pi 3B and Arduino 选型如下:


Arduino

#include "DHT.h"
DHT dht(2, DHT22);

void setup()
{
  Serial.begin(9600);
  dht.begin();
}

void loop()
{
  float humidity = dht.readHumidity();
  float temperature = dht.readTemperature();
  Serial.print("Humidity: ");
  Serial.print(humidity);
  Serial.print(" ");
  Serial.print("Temperature: ");
  Serial.println(temperature);
  delay(2000);
}

RasberryPi 3B

安装系统

镜像:raspbian-stretch-lite
microSD 卡:16G,推荐 8G 以上

以上,RasberryPi 的系统安装完成,可以将 SD 卡插入开发版上电开机了

登录系统

SSH
ssh pi@ip
default password: raspberry

通过路由器查看树莓派的当前 ip:

关机sudo shutdown -h now,重新上电后会自动开机


RaspberryPi 读取 Arduino 数据

安装 Python3

sudo aptitude install python-dev

安装 Python 的 GPIO 模块

用于控制 LED 、电机等

sudo apt-get install rpi.gpio

安装 Serial

用于串口通信及 USB 通信sudo apt-get install python-serial

安装串口调试程序

minicom 是 linux 平台串口调试工具,可以用它来读取 Arduino 通过 USB 串口发送的传感器数值。

Python 打印数据

show.py

import serial
ser = serial.Serial('/dev/ttyACM0',9600)
info = ser.readline()
while info:
    print(info)
    info = ser.readline()

Python 调用 http API 安装 requests sudo pip install requests

后台运行 py 脚本
nohup python api.py &

1479 次点击
所在节点    程序员
2 条回复
tankren
2020-08-18 00:30:55 +08:00
包保鲜膜防尘
Breadykid
2020-08-20 17:29:10 +08:00
@tankren 会发烫

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/699065

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX