Sonali
Posts: 7
Joined: Sun Mar 26, 2017 6:44 am

Data transfer between python and html

Fri Mar 31, 2017 4:28 am

Hi,

I need to take input from the webpage, process it in pi and send the updates to the webpage. Please help me with editing the code.
A number should be entered by the user and then ir sensor should make that many counts and simultaneously display updates on the webpage. 'Target' should have value of 'NEW TARGET' and 'Production' should keep getting updated.

Python code:

Code: Select all

import RPi.GPIO as GPIO
import time
import requests
import urllib2, urllib
import cgi

GPIO.setmode(GPIO.BCM)
GPIO.setup(2,GPIO.IN)


x=1
form = cgi.FieldStorage()

target= form.getvalue('NEW TARGET')

production=0

while x<=target:
       i=GPIO.input(2)
       if i==0:
          print 'interrupt',i
          time.sleep(2)
          production += 1
          x += 1
       elif i==1:
          time.sleep(2)

path='http://192.168.5.104/index.php'
req=urllib2.Request(path, production)  
req.add_header("Content-type", "application/x-www-form-urlencoded")
page=urllib2.urlopen(req).read()
       
GPIO.cleanup()



HTML code:

Code: Select all

<html>
    <form action="/home/pi/ir_sensor1.py"method='POST'>
    NEW TARGET: 
    <input type='text' name='fname'><br>
    <input type='submit' value='Submit'>
    </form>

  <head>
      <title>PRODUCTION COUNTER</title>
    <style>
      body { background-color: #cccccc; font-family: Arial, Helvetica, Sans-   Serif; Color: #000088; }
    </style>
  </head>

  <body>
    <h1>CURRENT STATUS</h1>

    <p>Target: %02d</p>

    <p>Production: %02d</p>
        
  </body>

  <meta http-equiv='refresh' content='5'/>

</html>

Please help.
Thank you.

ghans
Posts: 7871
Joined: Mon Dec 12, 2011 8:30 pm
Location: Germany

Re: Data transfer between python and html

Fri Mar 31, 2017 6:24 am

You need to learn JavaScript , a Python web framework and use AJAX/Websockets.

Or simply use this library , which does it all for you :

https://github.com/dddomodossola/remi

You don't even need to understand HTML to use it.

ghans
• Don't like the board ? Missing features ? Change to the prosilver theme ! You can find it in your settings.
• Don't like to search the forum BEFORE posting 'cos it's useless ? Try googling : yoursearchtermshere site:raspberrypi.org

MarkyV
Posts: 97
Joined: Wed Sep 21, 2016 1:52 pm
Location: Buckingham, UK
Contact: Website

Re: Data transfer between python and html

Fri Mar 31, 2017 9:03 am

Hi,

It looks like you are getting there - however when you redirect your CGI back to your HTML I'm not sure how your production value will get into your web page - if it's php then you will need a <?php echo($production); ?> where $production is your data - however you are not passing your production value correctly - it needs to be an key->value array : https://docs.python.org/2/howto/urllib2.html

You can also use print from your CGI script to display your updated values if that is easier?

Mark
My RaspberryPi Blog: http://thepimaker.online

Sonali
Posts: 7
Joined: Sun Mar 26, 2017 6:44 am

Re: Data transfer between python and html

Sat Apr 01, 2017 4:21 am

Thanks a lot. I'll try it.

Sonali
Posts: 7
Joined: Sun Mar 26, 2017 6:44 am

Re: Data transfer between python and html

Sat Apr 01, 2017 10:49 am

I have tried using json, but now I keep getting 'ERROR' in the terminal after i run the python file. I have following two doubts:

1. What is wrong in the code?
2. How to ensure that the ir sensor program will work when I open the webpage and give input and the pi is powered on and connected to ir sensor (i.e there should be no need to open the terminal and give any commands)?


Python code:

Code: Select all

import RPi.GPIO as GPIO
import time
import sys, json
GPIO.setmode(GPIO.BCM)
GPIO.setup(2,GPIO.IN)
# Load the data that PHP index.php sent us
try:
    data = json.loads(sys.argv[1])
except:
    print "ERROR"
    sys.exit(1)
x=1
production=0
while x<=data:
       i=GPIO.input(2)
       if i==0:
          print 'interrupt',i
          time.sleep(2)
          production += 1
          x += 1
       elif i==1:
          time.sleep(2)
# Generate some data to send to PHP
result = {'result': 'production'}
# Send it to PHP
print json.dumps(result)   
GPIO.cleanup()
HTML code:

Code: Select all

<html>
  <head>
    <title>INDUSTRIAL AUTOMATION USING IOT</title>
    <style>
      body { background-color: #cccccc; font-family: Arial, Helvetica, Sans-Serif; Color: #000088; }
    </style>
  </head>
<body>
    <h1>INDUSTRIAL AUTOMATION USING IOT</h1>
    <form action="/index.php/submit">
    NEW TARGET: <input type='text' name='data'><br>
    <input type='submit' value='Submit'>
    </form>
</body>
<?php
// Call to the python script irtest.py with the JSON data
$result = shell_exec('python irtest.py ' . escapeshellarg(json_encode($data)));
// get data back from python, decode it and display it
$resultData = json_decode($result, true);
//var_dump($resultData);
echo $result;
?>
<p>Target: %02d</p>
<p>Production: %02d</p>
</html>
Thanks in advance.

ghans
Posts: 7871
Joined: Mon Dec 12, 2011 8:30 pm
Location: Germany

Re: Data transfer between python and html

Sun Apr 02, 2017 5:47 pm

Your architecture is wrong. Obviously the GPIO script is a long-running background
process by its very nature. It needs to asyncronously communicate with stuff running inside
a browser and vice versa.

Look into my link again , it solves all architectural problems while nicely
hiing the messy details i hinted at in my first post.

ghans
• Don't like the board ? Missing features ? Change to the prosilver theme ! You can find it in your settings.
• Don't like to search the forum BEFORE posting 'cos it's useless ? Try googling : yoursearchtermshere site:raspberrypi.org

MarkyV
Posts: 97
Joined: Wed Sep 21, 2016 1:52 pm
Location: Buckingham, UK
Contact: Website

Re: Data transfer between python and html

Mon Apr 03, 2017 3:08 pm

Hi,

I would need to go through the code with a Pi (which I don't have with me) - but I don't think the shell_exec route is a good one to go down as it isn't always allowed by the server depending on what you are doing so it's probably best avoided - I'm also not sure that using the json functions are going to work like that (I would have to test that myself) as the the posted variable is an array not a json object (not sure if python will do the conversion like that).

The handling of your data between the web page and the server may be better handled by PHP instead of passing it directly to python - then all you need to do is get your production values - that can be done in two ways :

1 - you have a continuous process in the background running (python) that does your GPIO work and saves the values to a file. Then the php code just reads that file and returns however many values were requested - I think this method is what Ghans is talking about.
2 - you get php to query the GPIO directly - apparently this is possible by using this library - https://github.com/calcinai/phpi

The way you are doing it at the moment although theoretically you should be able to pass the data backwards and forwards will cause the server to pause while it reads the values - meanwhile nothing else will happen on that thread so you have the possibility of very long pauses before it returns (which will also be a problem for option 2 above too).

I'll see if I can have a look at the code when I get home to see if I can clarify why your code is not doing what you think it should.

Mark
My RaspberryPi Blog: http://thepimaker.online

Sonali
Posts: 7
Joined: Sun Mar 26, 2017 6:44 am

Re: Data transfer between python and html

Sat Apr 22, 2017 2:16 pm

Hiee,
I am trying to use cgi script for my code. I have written the following code but it gives error as shown below:
index.html

Code: Select all

<html>
  <head>
    <title>INDUSTRIAL AUTOMATION USING IOT</title>
    <style>
      body { background-color: #cccccc; font-family: Arial, Helvetica, Sans-Serif; Color: #000088; }
    </style>
  </head>
<body>
    <h1>INDUSTRIAL AUTOMATION USING IOT</h1>
    <form action="/cgi-bin/ircgi.py" method="POST">
    NEW TARGET: <input type='text' name='data'><br>
    <input type='submit' value='Submit'>
    </form>
</body>
<p>Target: %02d</p>
<p>Production: %02d</p>
</html>
usr/lib/cgi-lib/ircgi.py

Code: Select all

#!/usr/bin/env python
import RPi.GPIO as GPIO
import time
import cgi
import cgitb
cgitb.enable()
print "Content-type: text/html\n\n"
GPIO.setmode(GPIO.BCM)
GPIO.setup(2,GPIO.IN)
form=cgi.FieldStorage()               # standard cgi script lines to here!
number=form["data"].value
target=int(number)
x=1
production=0
while x<=target:
       i=GPIO.input(2)
       if i==0:
          print 'interrupt',i
          time.sleep(2)
          production += 1
          x += 1
       elif i==1:
          time.sleep(2)
final=str(production)
print cgi.escape(final)
The error is
<type 'exceptions.RuntimeError'> Python 2.7.9: /usr/bin/python
Sat Apr 22 21:02:24 2017
A problem occurred in a Python script. Here is the sequence of function calls leading up to the error, in the order they occurred.

/usr/lib/cgi-bin/ircgi.py in ()
11
12 GPIO.setmode(GPIO.BCM)
=> 13 GPIO.setup(2,GPIO.IN)
14
15 form=cgi.FieldStorage() # standard cgi script lines to here!
GPIO = <module 'RPi.GPIO' from '/usr/lib/python2.7/dist-packages/RPi/GPIO/__init__.pyc'>, GPIO.setup = <built-in function setup>, GPIO.IN = 1
<type 'exceptions.RuntimeError'>: No access to /dev/mem. Try running as root!
args = ('No access to /dev/mem. Try running as root!',)
message = 'No access to /dev/mem. Try running as root!'
I have used the following commands but it still gives the same error:
sudo rpi-update
sudo apt-get update
sudo apt-get upgrade
sudo adduser pi gpio
sudo chown root.gpio /dev/gpiomem
sudo chmod g+rw /dev/gpiomem
Please help.
Thanks in advance.

asavah
Posts: 362
Joined: Thu Aug 14, 2014 12:49 am

Re: Data transfer between python and html

Sat Apr 22, 2017 3:54 pm

You need to add the user under which your webserver/cgi runs to the gpio group. Usually it's www-data.
Not the "pi" user.

Sonali
Posts: 7
Joined: Sun Mar 26, 2017 6:44 am

Re: Data transfer between python and html

Sun Apr 23, 2017 12:52 pm

I have added the following in /etc/sudoers
www-data ALL = NOPASSWD: ALL
and also tried
www-data ALL = NOPASSWD: /usr/lib/cgi-bin/ircgi.py

Still i am getting the same error as above. :(

User avatar
DougieLawson
Posts: 35813
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Data transfer between python and html

Sun Apr 23, 2017 2:26 pm

Sonali wrote:I have added the following in /etc/sudoers
www-data ALL = NOPASSWD: ALL
and also tried
www-data ALL = NOPASSWD: /usr/lib/cgi-bin/ircgi.py

Still i am getting the same error as above. :(
Oh good, you've just opened a hackers paradise. Your web server now has the ability to destroy or fatally compromise your system. That compromise could allow hackers to take complete control of every machine in your house.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

Sonali
Posts: 7
Joined: Sun Mar 26, 2017 6:44 am

Re: Data transfer between python and html

Mon Apr 24, 2017 1:50 pm

Okay, I just removed that line. But please tell me why such a a simple program is not working?? What is wrong??

User avatar
DougieLawson
Posts: 35813
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Data transfer between python and html

Mon Apr 24, 2017 6:20 pm

Have you added the www-data userid to the gpio group?
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

Return to “Beginners”