User Rating: 5 / 5

Star ActiveStar ActiveStar ActiveStar ActiveStar Active
 
From my previous post what you got was a functioning relay control, but not very sexy looking. Found this simple and easy to configure package online.

https://github.com/ThisIsQasim/WebGPIO

Little bit of tweaking and away you go.

The server initially runs on 0.0.0.0:8000 which is great if you are local to the box or have teamviewer installed. I wanted the server to run at boot and use the IP address of the Pi (eth0 or WLAN) Problem is that if you use the IP address of the Pi  you need to set the Pi to wait for network on boot (just use sudo raspi-config and select boot options if you want to do that) or it just doesn't work. Also limits you to one interface that you can access it from. So after a bit of work this is what I came up with (googled, copied and pasted - I'm not that clever!)

Firstly I decided to leave the server on 0.0.0.0 as this does not need to be active on boot as do the physical interfaces. What I wanted to do then was forward anything coming in on port 8000 to 0.0.0.0:8000

First check if ip-forward was set

cat /proc/sys/net/ipv4/ip_forward hit enter and you will probably see a 0 - this needs to be a 1

sudo nano /etc/sysctl.conf
# Uncomment the next line to enable packet forwarding for IPv4
net.ipv4.ip_forward=1

CTRL O to save and CTRL X to exit. Then reboot

cat /proc/sys/net/ipv4/ip_forward this time you should get a 1

Paste
sudo iptables -t nat -A PREROUTING -p tcp --dport 8000 -j DNAT --to-destination 0.0.0.0:8000 and hit enter.

If you don't get any errors that should be it.

To set the server running at boot I just added a line to rc.local as follows

sudo nano /etc/rc.local

Paste this line toward the end above where it says exit
/usr/bin/python3 /home/pi/WebGPIO/backend.py &

Save exit and reboot and all should be working - browse to your Pi IP address from another device (don't forget to add :8000 on the end of the IP address)