I have been combing the web for days trying to figure out how to control the GPIO pins on a remote Raspberry Pi 3 from my Hass.io server with a couple of conditions:
Should be accessible over local intranet (no cloud)
Should not require specialized hardware (just standard RPi3)
I’ve tried WebIOPi, but it’s not working. Is there a way to implement GPIOZero as a solution?
I’ve seen many people suggest that connecting to remote Raspberry Pi’s is “overkill,” but I just want to play around with the hardware I have available at the moment.
I am specifically trying to turn on LED at the moment. I figure if I can get that to work, I might be able to find other uses. Any input? Or is there a good reason beyond “overkill” that I shouldn’t use a Raspberry Pi?
I’m interested to see what you come up with. This is the only thing preventing me from migrating to away from my rPi3 to a more substantial computer. I’m currently using my GPIO pins to read the status of the wired door/window sensors that were installed in my house before I bought it.
Tried the php scripts with lighttpd running on remote pi. The switch shows up with the command on and off buttons, but they do nothing. Tried accessing it from the web, and it says the connection is refused. Looking into it, but if you have a quick fix, let me know.
Newbie Instructions:
1. On new load of Raspbian:
a. Install phpmyadmin: sudo apt install apache2 phpmyadmin
b. I tried to go through the prompts until it errored out, then I selected "ignore".
The rest of these steps still worked after that.
2. Create directory for php files:
a. sudo mkdir /var/www
b. sudo mkdir /var/www/html
3. Create php files in /var/www/html
a. sudo nano /var/www/html/switchon.php
b. Input:
<?php
system ( "gpio -g mode 21 out" ); #for output mode on GPIO 21 pin
system ( "gpio -g write 21 1" ); #turns GPIO 21 on
?>
c. Save using these key combos: CTRL+X, then Y, then ENTER
d. sudo nano /var/www/html/switchoff.php
e. Input:
<?php
system ( "gpio -g mode 21 out" );
system ( "gpio -g write 21 0" ); #turns GPIO 21 off
?>
f. Save using key combo from 3(c)
4. Test by browsing to http://192.168.1.93/switchon.php and /switchoff.php
#Change IP address to match the local IP of your remote RPi
#Your LED should turn off and on if wired correctly
5. Add as switch to Home Assistant's configuration.yaml file:
a. Input (at bottom of file if you don't know where to put it):
switch:
- platform: command_line
switches:
gpio_test: #This is the switch identifier name
command_on: 'curl -k "http://192.168.1.93/switchon.php"'
#NOTE: Change the ip address to local ip address of your remote RPi,
#and don't forget extra ' after the closing "
command_off: 'curl -k "http://192.168.1.93/switchoff.php"'
6. Restart your Home Assistant; it should then show up under states in the HA GUI
Thanks, RobDYI, for the input. As you can probably tell, I’m fairly new to a lot of this, and I figured I’d write this out for others who are fairly new to both Raspberry Pi and Home Assistant. I may still play around with the other suggestions to see if I can get newbie instructions for those (assuming I can figure them out.)
I can now control an LED on a remote RPi through Home Assistant. I figure I can now try to build a lot of the interesting RPi projects out there and be able to link them to HA.
I also think I understand what @RobDYI was saying about installing HA on the remote RPi. It seems that you can set up all your sensors like normal on the second RPi, and your main HA server should be able to see them. I didn’t test it, but I did read a little more about it. You may want to try it on a separate Pi first before migrating your main HA server to your computer. Does any of this help?
I think it would be much easier just to leave your rpi3 as is and include the mqtt state stream component. You can then create a new HA with a different computer with a mqtt sensor that will read rpi3 sensor state. If you want to control a switch on your rpi3, you can create a template switch to control rpi3 by HA api.
I use a $10 Pi Zero W and run Node-Red on it. DietPi is a great alternative to Raspbian for the Zero. Also runs other python scripts, room assistant, etc. I just have Node-Red publish to MQTT because you can integrate with Hass directly (node-red-contrib-home-assistant package)
I was just looking for things to try with python. I did manage to get MQTT working with Python on the remote RPi, but it was a lot more work than the curl solution. Documentation was sparse.
I have a few ESP8266’s on the way along with some MOSFETs to set up some RGB LED strips, and I plan on using MQTT on those. From what I’ve seen, MQTT is much better documented for use with the ESP8266. It looks like I’m learning C and C++ only three weeks after learning Python.
This all ties into me being an engineering student and finally feeling like I have enough knowledge to put it into practice.
I did try out MQTT on your suggestion. Once you get it, it makes sense. I couldn’t ever get Node Red working on my RPi for some reason. It may have something to do with all the crap I’ve installed on it while testing different integration solutions for remote RPi GPIO control.
But, I get why MQTT is the preferred solution. I also looked up the ESP8266, and I ordered a few. Those seem like the best platform to base my DIY home automation projects. Thanks for your input.
I got the Mosquitto broker running on my HA server. It works with Python, but it was a painful process. Not enough documentation for HA + MQTT + Remote Python Scripts, but I eventually learned enough to piece it together.