Remote Raspberry Pi 3 GPIO

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?

Thanks for your help.

1 Like

I have been using node-red on a remote RPi to send MQTT payloads to Hassio Mosquito addon.

You should be able to send both ways, to and from a remote RPi, Arduino, or ESP8266.

Node-red has about anything you want as far as control of gpio on a Pi.

1 Like

I used to use just a php files on the pi and run from the web. You can also just install HA and get one HA to control the other by API.

the HA switch

   tvpower:
     command_on: 'curl -k "http://localhost/action/tvpoweron.php"'
     command_off: 'curl -k "http://localhost/action/tvpoweroff.php"'

The php file

<?php
//set pins mode to output
    system ( "gpio -g mode 16 out" );
//activate and print
     system ( "gpio -g write 16 0" );
?>
1 Like

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.

1 Like

My understanding was that MQTT requires access to a cloud platform. Am I misunderstanding its implementation?

I’ll give those a shot later today.

You only use a cloud platform if you want. You can use the Mosquitto broker locally.

1 Like

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.

Ok. I got it to work via php.

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.)

2 Likes

Running remote Python code as a command_line switch in Home Assistant:
(My remote RPi was using Raspbian Stretch Lite)

  1. Install python onto your remote RPi (works with both python 2.7 and python3):
    a. sudo apt-get install python2.7-dev python-dev python-pip
  2. Install flask on remote RPi:
    a. sudo pip install Flask ( although I technically did “sudo pip install Flask flask-assistant” for other reasons)
  3. Write gpio_test.py code (see below) and save to pi user folder (/home/pi/gpio_test.py)
  4. Run (and do not close): sudo python gpio_test.py
  5. Create switch in configuration.yaml file
  6. Restart Home Assistant. switch.py_test will be under Developer tools > States (< >)

gpio_test.py:

from flask import Flask
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)
GPIO.setup(21, GPIO.OUT)
pin = 21

app = Flask(__name__)
	
@app.route('/GPIO/<state>', methods={'GET', 'POST'})
def red_light(state):
	if state == 'on':
		GPIO.output(pin, GPIO.HIGH)
	elif state == 'off':
		GPIO.output(pin, GPIO.LOW)
	
if __name__ == '__main__':
	app.run(host='0.0.0.0')

Add to configuration.yaml at end of file:

switch:
  - platform: command_line
    switches:
        py_test:
            command_on: 'curl -k "http://192.168.1.93:5000/GPIO/on"'
            command_off: 'curl -k "http://192.168.1.93:5000/GPIO/off"'

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?

4 Likes

Thank you so much. It’s a serious start. I’ll just have to rework it to run as a sensor for the GPIO pin, rathe than a switch.

1 Like

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.

1 Like

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)

1 Like

Good suggestion. I’ll look into that further, as well as Node-Red that @oakbrad suggested.

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. :expressionless:

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.

1 Like

I’ll have to look up the state stream thing and template switches at a later point.

I’ll look into DietPi. I have a spare Pi Zero laying around.

Here’s a few examples I wrote

Motion detector, about as simple as it gets

Publish Citybikes info to MQTT

1 Like