#15 Raspberry Pi: Initialise CGI server on RPi3 to make it Gateway (for Node-Gateway IoT Architecture)

IoT Tutorial #15 [ Raspberry Pi Tutorials #3 ]
If you want to use Raspberry Pi as a Gateway in Node-Gateway IoT Architecture, It mandatory to initialise CGI server on RPi3.

In this post, I will show you the steps to initialise CGI (Common Gateway Interface) Server on your Raspberry Pi (RPi3)

Following these steps sequentially as it is and you will be able to initialise CGI server on RPi3 and test it using your browser window:

Steps:

    1. Create a new directory using the terminal as follows:
      Open the terminal and enter following commands:
      cd ~
      mkdir cgi-bin

    2. Go to the following directory and copy the following file and paste it to /home/pi/ folder:
      cp /usr/lib/python2.7/CGIHTTPServer.py ~/

    3. Create a new file and paste given code as follow:
      cd ~/cgi-bin
      nano blink.py
      <Paste the given code>
      Press "Ctrl+x" and then hit "enter" to save the file.

    4. Change the executable access of the above file as follows:
      chmod +x ~/cgi-bin/blink.py

    5. Go to Home directory and Run the "CGIHTTPServer.py" as follows:
      cd ~/
      python -m CGIHTTPServer

      you will see the response on the terminal as follows:
      Serving HTTP on 0.0.0.0 port 8000 ...

    6. Open Browser on Laptop (Not RPi3) and type:
      <IP_of_RPi3>:8000
      Eg. 192.168.0.34:8000
      You will see the directories in the browser from your RPi3

    7. To run the blink.py program, Type following in browser:
      <IP_of_RPi3>:8000/cgi-bin/blink.py
      Eg. 192.168.0.34:8000/cgi-bin/blink.py

      your blink.py script will be executed on RPi3 and
      Response will be shown in the browser as follows:
      Executed the task...

    8. Similarly, values.py can be triggered from browser window
      which collect data sent through API and log it to a TEXT file on RPi3:
      Open a browser window and Type following:
      <IP_of_RPi3>:8000/cgi-bin/values.py?field1=<data_to_be_sent>
      Eg. 192.168.0.34:8000/cgi-bin/values.py?field1=20

      This will execute the python script values.py on RPi3 and store the value "20" in text file "TextFile_Out.txt" in home directory of RPi3.



For Better understanding you can watch demonstration video given below:

Downloads:
Download link is given in the Description of the YouTube video shown below.

Demonstration:




Programs:

blink.py:
#!/usr/bin/env python

import RPi.GPIO as GPIO             # Import GPIO Library
import time                         # Import 'time' library for delay / sleep

led_pin = 7                         # Connect LED + to Pin 7

GPIO.setmode(GPIO.BOARD)            # Use Board Pin Numbering
#GPIO.setmode(GPIO.BCM)             # Use BCM Pin Numbering

GPIO.setup(led_pin,GPIO.OUT)        # Set Pin 7 as Output

GPIO.output(led_pin,GPIO.HIGH)  # LED ON
time.sleep(1)                   # wait for 1sec
GPIO.output(led_pin,GPIO.LOW)   # LED OFF
time.sleep(1)                   # wait for 1sec
GPIO.output(led_pin,GPIO.HIGH)  # LED ON
time.sleep(1)                   # wait for 1sec
GPIO.output(led_pin,GPIO.LOW)   # LED OFF
time.sleep(1)                   # wait for 1sec

GPIO.cleanup()               # To Cleanup all the ports you've used


# Response to be printed on Browser 
print "Content-type : text/html\r\n\r\n"
print '<html>'
print '<head>'
print '<title> Raspberry Pi Server </title>'
print '</head>'
print '<body>'
print '<h2> Executed the task </h2>'
print '</body>'
print '</html>'




values.py:
#!/usr/bin/env python

import cgi     # import cgi libraries

form = cgi.FieldStorage()        # read cgi form
val = form.getvalue('field1')    # read field1 value from form
f = open('TextFile_Out.txt','a') # open new text file in append mode
f.write(str(val)+' \n')   # write field1 value in buffer
f.flush()     # write from buffer to text file
f.close()     # close text file

--------------------------------------------------------------------------------

Click here to see more codes for Raspberry Pi 3 and similar Family.
&
Click here to see more codes for NodeMCU ESP8266 and similar Family.
&
Click here to see more codes for Arduino Mega (ATMega 2560) and similar Family.

Feel free to ask doubts in the comment section. I will try my best to solve it.
If you find this helpful by any mean like, comment and share the post.
This is the simplest way to encourage me to keep doing such work.

Thanks and Regards,
-Akshay P. Daga
Post a Comment (0)
Previous Post Next Post