Posts: 5
Joined: Thu Apr 19, 2018 6:07 pm

how to control gpio & relay in raspberry pi 3 using website

Thu Apr 19, 2018 6:20 pm

hi i'm kurniawan
i want to make a project home automation (lights controls) use raspberry pi3 with website from internet (not LAN) this is my last job for
Graduated from college. :(

if anyone can help me?

Posts: 4765
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: how to control gpio & relay in raspberry pi 3 using website

Fri Apr 20, 2018 11:02 am

If you want your web site to be accessible from the internet then you need to open a port on your router and forward this port to your pi that is running the web server.

The port normally used for web sites is port 80 on the WAN side and the LAN side of your router.

Before doing this you need to make sure your pi/web site is secured against unauthorised access or you will find you pi hacked in no time at all.

if you are doing this on your own router then you need to look up the port forwarding instructions in its user manual.

if you are trying to do this on collage router I doubt you will be able to as you will not have access to the router setup.

Remember we want information.......................no information no help
The use of crystal balls & mind reading is not supported

User avatar
Posts: 5086
Joined: Tue Oct 09, 2012 11:46 am
Location: Trondheim, Norway

Re: how to control gpio & relay in raspberry pi 3 using website

Fri Apr 20, 2018 1:17 pm

kurniawan wrote:
Fri Apr 20, 2018 10:28 am
this is just work in LAN not work with internet :cry:
That is just a networking setup problem and doesn't require you to write your software differently or use different libraries.
How to do that depends on the environment the Pi should be deployed in, you can test it by adding port-forwarding in your home router, but when the unit is moved to a college campus/lab then the rules there apply. You have to decide/describe in what environment this should work.

You could also take a look at this for testing.

Posts: 38
Joined: Mon Mar 12, 2018 3:22 am

Re: how to control gpio & relay in raspberry pi 3 using website

Mon Apr 23, 2018 7:06 am

I have done this myself but probably using the long way around approach but it was an interesting and satisfying learning experience. I used basic Python as much as possible to be able to understand what was going on. I was prepared to experiment to increase my understanding.

I proceeded as follows:-

1) Using information on Tutorialspoint I created a simple HTML server that ran on my LAN ( note port number at end which is for port forwarding later).

https://www.tutorialspoint.com/python3/ ... orking.htm

2) In the simple server I grabbed and displayed (using print) the first 1024 (more than really needed) characters that arrive from the client (a browser) to see how a browser talks to a server.

3) From 2) I noticed that the browser sends a GET / string as follows:-

GET /hello (when I went to

4) I then had my simple server extract the text after the / and test with if statements to turn the leds on/off (I extract led1on after the GET / and turn the led on etc)

5) I then learned how to do Port Forwarding in my router so that I could use my WAN IP from outside the router. The router manual along with some
Google and YouTube helped here. I tested again and all was OK.

6) I then learned some HTML to be able to create a simple webpage with buttons that inserted the /led1on etc into the HTTP address line. I read up
on the "a href' and 'button' tags to be able to do this.

7) I modified my server to send the webpage HTML file to the client on connection with the client and extract the GET / from the client.

All in all it was quite a lot of work but taking it one step at a time I could see the finished product developing and I had an understanding of my code.

As other posters have shown, there are more sophisticated approaches using ready made web tools but I feel that a simpler approach with a deeper understanding and the ability to explain every step is more appropriate for a student project.

Posts: 352
Joined: Tue Oct 14, 2014 5:21 pm

Re: how to control gpio & relay in raspberry pi 3 using website

Tue Apr 24, 2018 8:51 pm

Another option is to set it up on the local network and not on the internet, then use a VPN to connect from outside. But, this will, of course, be more suitable for some use cases than others.

Return to “Advanced users”