This is the API I wrote to control my light switch from my iPhone, using a Raspberry Pi, a servo, and a 3D printed bracket.
- 3D print one of the above brackets, install an MG90S servo and a Raspberry Pi on it, and mount it to your light switch.
- Install the correct toolchain for cross compiling to your Raspberry Pi.
- Clone the repo. If necessary, change the constants in the
servo
module to match the datasheet of the servo you're using. - Build the crate, then transfer the executable and the
Rocket.toml
file to your Pi. Enable hardware PWM on your Pi. - Run the executable to generate an API key, found in the
config.toml
file. You can keep it running however you wish (I use systemd).
You can control the light switch by making requests to endpoints at the Raspberry Pi's ip address. See main.rs
for all the endpoints. Here are some iOS Shortcuts you can use to control it (make sure to modify them with your Pi's ip address and generated API key):
- Light On: Turns the light on. Change the
state
field toOff
to make it turn the light off instead. - Get Wake Up Time: Technically this isn't specific to this project. This returns the date/time of your Sleep Schedule alarm. The input should be on the day of the alarm you want to get, sometime between midnight and the time of the alarm (I use 1 AM).
- Schedule Light On: Schedules the switch to turn on at the inputted time. I use this with the previous shortcut to turn on my light 15 minutes before my alarm goes off.
- Bug: servo sometimes twitches slightly, not sure if I can stop that
- Create frontend