本文已参与「新人创作礼」活动,一起开启掘金创作之路。
一、简介
因为项目需要将单片机采集到的数据无线收发给树莓派,故想通过433Mhz射频无线收发设备进行操作,但由于缺乏相应的编程经验,以失败告终,仅根据开源教程实现了树莓派两个终端之间的收发,后续如果有进展,还会继续补充。
二、硬件准备
1、元器件清单
名称 | 数量 |
---|---|
树莓派4B | 1 |
433Mhz无线发射模块 | 1 |
433Mhz超再生模块 | 1 |
433MHZ超再生无线收发模块:
长的为超再生模块,即接收模块,方的为发射模块,除了433Mhz的,还有一种315Mhz的。
2、接线说明
树莓派 | 无线发射模块 | 超再生模块 |
---|---|---|
5V(PIN2) | VCC | * |
5V (PIN4) | * | VCC |
GND(PIN9) | GND | * |
GND (PIN6) | * | GND |
GPIO17(PIN11) | DATA | * |
GPIO27(PIN13) | * | DATA |
三、软件准备
1、安装rpi-rf库
sudo apt install python3-pip git
pip3 install rpi-rf
git clone https://github.com/mrpjevans/rfchat.git
2、运行rf程序
分别打开两个终端,一个运行receive.py
,一个运行send.py
。
cd ~/rfchat
python3 receive.py
cd ~/rfchat
python3 send.py 1234
因为没有错误纠正,所以看到丢失或损坏的字符属于正常现象,只要可以接收到发送的数据就算成功了。
3、rfchat.py
在两个终端同时使用该程序,在一终端上输入的数据可以实时在另一终端上显示,但经实际测试,数据显示感觉会比较卡顿。
import sys
import tty
import termios
import threading
import time
from rpi_rf import RFDevice
# Elegant shutdown
def exithandler():
termios.tcsetattr(sys.stdin, termios.TCSADRAIN, old_settings)
try:
rx.cleanup()
tx.cleanup()
except:
pass
sys.exit(0)
# Activate our transmitter and received
tx = RFDevice(17)
tx.enable_tx()
rx = RFDevice(27)
rx.enable_rx()
# Receiving loop
def rec(rx):
print(“Receiving”)
lastTime = None
while True:
currentTime = rx.rx_code_timestamp
if (
currentTime != lastTime and
(lastTime is None or currentTime - lastTime > 350000)
):
lastTime = rx.rx_code_timestamp
try:
if (rx.rx_code == 13):
# Enter/Return Pressed
sys.stdout.write(‘\r\n’)
else:
sys.stdout.write(chr
(rx.rx_code))
sys.stdout.flush()
except:
pass
time.sleep(0.01)
# Start receiving thread
t = threading.Thread(target=rec,
args=(rx,), daemon=True)
t.start()
print(“Ready to transmit”)
# Remember how the shell was set up so we can reset on exit
old_settings = termios.tcgetattr(sys.stdin)
tty.setraw(sys.stdin)
while True:
# Wait for a keypress
char = sys.stdin.read(1)
# If CTRL-C, shutdown
if ord(char) == 3:
exithandler()
else:
# Transmit character
tx.tx_code(ord(char))
time.sleep(0.01)
四、遇到的问题
1、Failed to establish a new connection: [Errno -3] Temporary failure in name resolution‘,)‘
该问题是由于我之前设置了固定的静态ip地址,导致DNS也自动固定到设置的网段,无法连接上网 ,我这里把它改回来就好了。 以下是网上流传的更改DNS的方法,但经实际测试,一般DNS是根据你的ip设置自动更改的,这个方法对我来说没什么用。
sudo gedit /etc/resolv.conf
# 最后一行添加
nameserver 8.8.8.8
nameserver 114.114.114.114
2、无法连接github
解决方案:
- 通过码云转存github代码
- 使用github的代下载网站下载
- 使用cnpmjs镜像进行加速
- 使用国外服务器进行搭桥
我个人是比较推荐第三种,适合于中小型程序的下载。
git clone https://github.com.cnpmjs.org/mrpjevans/rfchat.git