Raspberry Pi Tinkering: An RF Light Switch
A few days ago I found a Zhihu tutorial on building a smart switch from scratch. After asking the author a couple of questions, I wanted to try it myself. These notes record my setup and the problems I ran into. I recommend reading the original tutorial first and keeping these notes handy: some of its instructions had become outdated by the time I tried them.
Setting up the Raspberry Pi
There are already plenty of guides to installing the operating system and connecting to Wi-Fi, so I will start once those are done. I only needed a monitor for the first boot and had no need for a graphical desktop. For normal use, I planned to connect only power and GPIO, so I chose the Lite image.
Changing package mirrors
The Pi’s default mirror was painfully slow from my connection in China, where access to overseas sites can be restricted. I recommend switching mirrors early. Editing the mirror configuration without Vim was awkward, so I reluctantly installed Vim from the default mirror first.
I used the Tsinghua mirror; its Raspbian setup guide gives detailed instructions.
Here is the configuration I used for Debian 10 on a Raspberry Pi 3B:
1 | # 编辑 `/etc/apt/sources.list` 文件,删除原文件所有内容,用以下内容取代: |
Then run sudo apt-get update && sudo apt-get upgrade -y && sudo apt-get dist-upgrade -y for a full upgrade.
Creating a user with sudo privileges
I recommend creating your own account for experimenting. The default pi account works too, but I found its password awkward to type and preferred a setup of my own.
Run sudo adduser username to create the account, enter its details, and set its password.
Then use adduser username sudo to add the account to the sudo group.
Enabling SSH
The image already included openssh-server, but it was not enabled. I ran sudo systemctl enable ssh to start it automatically at boot and sudo systemctl start ssh to start it immediately.
From then on, I could connect to the Pi over SSH, which made the rest of the setup much more convenient.
Getting the RF module working
Installing wiringPi
Most of the guides I found installed wiringPi with these commands:
1 | git clone git://git.drogon.net/wiringPi (git我假设大家已经安了哦,没有的话搜一下安装很简单的哦) |
The author had since closed the repository, and the official site said the old source-download method no longer worked. Fortunately, the newer Raspberry Pi image I used already had wiringPi in its APT repositories, so I installed it with sudo apt-get install wiringpi -y.
After installation, I ran gpio -v to check GPIO and hit this error:
1 | Unable to determine hardware version. I see: Hardware : BCM2835, |
I spent a long time chasing the error. The official download was gone, although I eventually found a university repository with a copy of the deleted files:
https://github.com/mm1994uestc/WiringPi
Building that copy by hand did not help. A GitHub issue suggested updating Pi4J from 1.1 to 1.2, but that did not fix my setup either.
Eventually I found a wiringPi installation note that traced this error to an outdated wiring-python library. I followed its instructions and the error went away:
1 | sudo apt-get install swig2.0 python-dev git python-pip python3-pip -y |
After those steps, GPIO finally responded.
Installing 433Utils
My controller uses 433 MHz radio, so I searched GitHub for a suitable utility and found 433Utils:
1 | git clone --recursive git://github.com/ninjablocks/433Utils.git |
After the first build, I needed to change the pin numbers to match how I had wired the receiver and transmitter.
First, open RFSniffer.cpp and set the receiver pin:
1 | /* |
Then make the corresponding transmitter-pin changes in codesend.cpp and send.cpp.
codesend.cpp
1 | /* |
send.cpp
1 | /* |
One detail caught me out: the numbers in the code are wiringPi pin numbers, not physical positions on the header.
Here’s the pinout I used:

Choose GPIO pins to match your own receiver and transmitter wiring; you do not have to use the same pins as the tutorial. Once you have edited the files, run make again.
Reading and sending my switch’s signals
I ran sudo ./RFSniffer, pointed the remote at the receiver, and pressed its buttons. For each button, I wrote down the number shown and what the button controlled. The output looked like this:
1 | Received 3984646 |
I could then use sudo ./codesend 3984646 to transmit the same signal. I repeated this for the other buttons and kept a list of the code for each function.
Deploying with Flask
Flask was quick to install and felt much simpler to set up than the Java Spring MVC projects I had worked with:
pip3 install flask
Here is the Flask script I used:
1 | from flask import request, Flask, render_template |
Here is the template. I only had one device, and the tutorial’s version was not passing parameters through to my handler, so I modified it:
1 | <html> |
Please be kind to the HTML. Front-end work isn’t exactly my specialty. /(ㄒoㄒ)/~~
From the project directory on the Pi, I started the app with sudo python3 app.py and left it running in the background.
The switch worked on my local network, but typing 192.168.1.48:11451 every time was tedious. I wanted a domain name I could use instead.
Making the service reachable through a domain name
Port forwarding with frp
For port forwarding I used frp, a handy Linux tool. I already had a very cheap VPS that cost me ten dollars for a year, which was enough for this project. On the machine with the public IP, I downloaded frp:
1 | wget https://github.com/fatedier/frp/releases/download/v0.33.0/frp_0.33.0_linux_amd64.tar.gz |
On that public server, I edited frps.ini:
1 | [common] |
I started a screen or tmux session and left the server running with ./frps -c frps.ini.
Next, I downloaded frp on the Raspberry Pi to run the client:
1 | wget https://github.com/fatedier/frp/releases/download/v0.33.0/frp_0.33.0_linux_arm.tar.gz |
On the Pi, I edited frpc.ini:
1 | [common] |
I left ./frpc -c frpc.ini running in a screen or tmux session on the Pi.
The page was then reachable at the public IP address and forwarded port.
Setting up the domain
To put a domain name in front of the service, I used Nginx:
1 | server{ |
My earlier post about configuring HTTPS with Nginx explains how this configuration works.
Once I’d put the switch online, an uncomfortable question occurred to me: if somebody else knew the address, couldn’t they operate it too?
I added password authentication to address that problem, although Nginx’s password prompt was awkward to use on my phone:
1 | htpasswd -c /root/passwd username |
Then I updated the Nginx configuration to use it:
1 | server{ |
That finished the project. I was pleased with how the switch turned out.
