Use an ESP8266-based arduino microcontroller (D1 mini by default) to remotely power on and off your Raspberry Pi from any device connected to your wireless network!
- Raspberry Pi and correct power supply (I used a 4B, but should work with any version)
- LOLIN/Wemos D1 Mini or other ESP8266 or ESP32 based device (guide assumes D1 Mini, but minor changes would be needed for most other devices)
- Dupont cables or other wires and solder for wiring
- Micro-usb cable to connect D1 Mini to computer
- Go to the command line on the RPi (ssh if on another system)
sudo nano /boot/config.txt
- Scroll to bottom and add the following line:
# Enable GPIO power trigger
dtoverlay=gpio-shutdown
crl+x
,Y
,enter
to save and exit the editorsudo reboot
to restart the system
- Download the latest Arduino IDE for your system if you don't already have it installed
- Install esp8266 with Boards Manager in the Arduino IDE
- Open the IDE and go to
Tools
>Boards
and selectWemos D1 R2 & mini
- Make sure that
Tools
>Upload Speed
is set to115200
in order to be able to read the serial output correctly
- Go to the GitHub repository and click the green
Clone or download
button and thenDownload ZIP
to download the needed files.
OPTIONAL: you can also fork the repository to your own GitHub account if you want to use source control on your own project.
- Unzip the files to a folder on your computer, and rename the folder to
wifi_power_button
so that the IDE will not give you an error when opening it. - In the Arduino IDE goto
File
>Open...
and select one of the files in the folder you unzipped (it should open the whole project no matter which you choose) - Go to
wifi_power_button.h
and change the following settings for your network:
#define USRSSID "your wifi ssid"
#define USRPSK "your wifi password"
Optional: Uncomment the appropriate lines in this file to enable mDNS and/or static IP if you would like.
- Plug the D1 mini into your computer's USB port and hit the
Upload
button in the top right of the IDE to compile the code and upload it to your device. - Go to
Tools
>Serial Monitor
and make sure it is set to115200
in the bottom right corner of the Serial Monitor. - Once the code is uploaded to the D1 Mini, you can hit the reset button on the device and you should see your network info come up on the Serial Monitor once connected.
- Type the IP address from the Serial Monitor into your web browser and you should see the webpage if everything is setup correctly
A word of caution here: Incorrectly wiring the Raspberry Pi and/or the Wemos D1 Mini could permanently damage either devices! It is safest to do your wiring while there is no power connected and be sure to double check your connections to make sure you are not sending power to a pin that cannot handle it. I accept no liability for any damage from incorrect wiring!
Also refer to a pinout diagram for Raspberry Pi and D1 Mini to make it easier to select the correct pins.
- Connect
pin 3
on the Raspberry Pi topin D3
on the D1 Mini - Connect one of the 5V pins and one of the ground pins on the Raspberry Pi to the
5V
pin andGND
on the D1 Mini. (I usepin 4
andpin 6
on the RPi) - Double check that your setup is correct (especially that no 5V pin is connected to ground or an IO pin anywhere!) and then you can plug in the power cable for your Raspberry Pi to let it boot up.
After everything is up and running, go to the IP for the D1 mini (or the .local
address if using mDNS) in a browser on any device connected to your home wifi. If you have not setup a static IP or mDNS, you may have to use a network analyser app to find the correct IP.
You should be able to see the hosted webpage at this point and you can test the LED button or the Power button to see how everything works!
If you run into any problems, feel free to open an issue in the GitHub repository or reach out to me and I'll do my best to help!