Controlling Raspberry Pi LED via internet – Part3

We are in part 4 of tutorial series of Building Raspberry Pi Line Follower Robot! If you want to jump back and forth or missed any of my previous tutorials – here is the consolidated list.

All Tutorials to Build IOT Raspberry Pi Robot

  1. Build Raspberry Pi Robot controlled by Internet – IOT Robot
  2. Setup Static IP Address on Raspberry Pi
  3. Installing Web Server on Raspberry Pi
  4. Controlling Raspberry Pi LED via internet
  5. Raspberry Pi LED controlled by Internet
  6. Build REST API using Python and Flask
  7. Raspberry Pi Robot Logitech Camera Server
  8. Create Web Dashboard for IOT Robot Control

Entire project Video Tutorial


 

We have 1 and 2 ready. We are now going to integrate everything and control LED via internet:

  1. Static Static IP Address on Raspberry Pi
  2. Installing Web Server on Raspberry Pi
  3. Controlling Raspberry Pi LED via internet

Port Forwarding

First of all, we already have static IP address assigned to Raspberry Pi i.e. 192.168.1.100. We can access this by http://192.168.1.100. Though this is going via internal router only. We want to access script via internet. So, here we are going to use the IP address provided by Internet provider and use port forwarding to pass the request to 192.168.1.100. Here is how I did port forwarding in my router. The process is same but it may differ for other routers:

Basically, port forwarding is a technique by which we can tell router to forward request to internal IP. I want to Advanced section of the router.

Then I went to NAT settings. Clicked Virtual Server. Here I created virtual server by using below configuration:

After this, I found IP address allocated by ISP

So, now when I did hit the IP address http://122.170.182.199/test.py it rendered the “Hello World” page which we created in previous tutorial.

IPs are hard to remember and will change when we restart router. Hence, we can use FREE services like https://www.noip.com/  to give name to IP. It will also keep pinging and change the name as required.

Creating Python Script for LED On and Off

Code is self explanatory:

LED ON:


#!/usr/bin/python

import cgi
import cgitb
import RPi.GPIO as GPIO
cgitb.enable()
GPIO.setmode(GPIO.BOARD)
print 'Content-type: text/html\n\n'
GPIO.setup(37, GPIO.OUT)
GPIO.output(37, 1)

LED OFF:


#!/usr/bin/python

import cgi
import cgitb
import RPi.GPIO as GPIO
cgitb.enable()
GPIO.setmode(GPIO.BOARD)
print 'Content-type: text/html\n\n'
GPIO.setup(37, GPIO.OUT)
GPIO.output(37, 0)

Applying execute Permissions

sudo chmod +x lighton.py
sudo chmod +x lightoff.py

Restart nginx server

sudo service nginx restart

We are all set now!

Testing Raspberry Pi LED via Internet

Now anyone in the world can control the LED at my home(provided Raspberry Pi and server are turned on) by urls:

Light On:
http://122.170.182.199/lighton.py

Light Off:
http://122.170.182.199/lightoff.py
Please follow and like us:

Leave a Reply

Your email address will not be published. Required fields are marked *