Trevader2413
Posts: 1
Joined: Mon Sep 07, 2015 8:32 pm

posting GPIO status on webpage

Mon Sep 07, 2015 8:38 pm

I am doing my best to learn all I can about php, python, raspberry pis, how they could be used etc. It's just really fun! however, I hit a wall when I wanted to post gpio status on a webpage (high or low). I found a post similar to this on these forums, but it didn't answer all of my questions. here is my webpage:

Code: Select all

<!doctype html>
<html>
<head>
	<link rel="stylesheet" type="text/css" href="Global.css">
	<div id="travelBar">
		<center>
			<a href="index.html">Home</a><b> | </b>
			<a href="remoteGPIO.html">remote GPIO</a><b> | </b>
		</center>
	</div>
	<div id="headbox">
		<center>
			<font size="6">
				Trevor's RPi!
			</font>
		</br>
			<font size="4">
				Where I test all my online controls!
			</font>
		</center>
	</div>
</head>
<body bgcolor="339900">
	<div id="bodyBox">
		<div id="swiss"><a href="javascript:GPIOzero()">Click to display CPU Temp</a></div>
		<script>
			function GPIOzero(){
				$.ajax(
				{
					type: "POST",
					url: "/var/www/GPIOzero.py",
					dataType: "html",
					success: function(msg)
					{
					document.getElementById('swiss').innerHTML = msg;
					},
				});
			}
		</script>
	</div>
</body>
</html>
and here is my python script I am trying to call:

Code: Select all

#!/usr/bin/python
import cgi;
import cgitb;
import time
cgitb.enable()
import commands
import sys
import string
print "Content-type: text/html\n\n";
GPIOzero = commands.getoutput('gpio read 0')
output = "Pi CPU Temp is: " + GPIOzero
print output
I tried to modify the code from that forum question to make this work to output one or zero depending on the input status of GPIO pin 0 (according to wiring pi). anyone know whats wrong?

maurice1
Posts: 37
Joined: Tue Mar 05, 2013 8:55 am
Location: Dublin

Re: posting GPIO status on webpage

Thu Sep 17, 2015 8:17 am

didn't really understand your code, but what I do:

1_on.py

Code: Select all

#!/usr/bin/env python
import time
import os 
os.system("/home/pi/mcp23017/mcp23017.py -b b -o 0 -s high")

f = open('/var/www/1_status.html', 'r+')
f.write ("<HTML><H2><BODY><FONT SIZE=15><FONT COLOR=ff0000>" +"\n")
f.write ("ON ")
f.close()
have the php webpage run this python script above, which energises the GPIO pin and writes the word "ON" to "1_status.html"


When I press off on the webpage
1_off.py

Code: Select all

#!/usr/bin/env python
import time
import os 
os.system("/home/pi/mcp23017/mcp23017.py -b b -o 0 -s low")

f = open('/var/www/1_status.html', 'r+')
f.write ("<HTML><H2><BODY><FONT SIZE=15><FONT COLOR=ff0000>" +"\n")
f.write ("OFF ")
f.close()
it de-energises the GPIO pin and writes the word "OFF" to "1_status.html"

on my webpage I then import "1_status.html" into my webpage eg. <object data="1_status.html"></object></td>

I use this for the following 8 outputs, eg "1_status.html" to "8_status.html"

It has worked well however can take 2 secs to render and really you are better to have the php webpage refresh by itself every 3 or 4 seconds

on android it seems to work better on opera rather than chrome

Hope the above gives you inspiration

Massi
Posts: 1691
Joined: Fri May 02, 2014 1:52 pm
Location: Italy

Re: posting GPIO status on webpage

Thu Sep 17, 2015 8:30 am

there can be a lot of errors/problems in what you are doing (i.e., the webserver is not serving python files, the user that is executing the web server can't read gpio status, the py script can have errors, you have missed to load jquery and so on) but the first thing i can see is that you are calling from javascript in the html page (so full CLIENT) a local path (/var/www/something). This is not going to work for a ton of reasons, first because the http client has no access to your server local file system (it doesn't matter if you are opening the web page on localhost..)

Start again from the beginning and start from easier steps :)

1) if you want to use python from webpages, learn how to get py files server by your webserver.
Until you get a "hello world" in your http://localhost/test.py don't waste your time going on
2) get your py script to read gpio status and print it.
Until you get a "1" or "0" in your http://localhost/test.py don't waste your time going on (python has a lot of modules to read gpios directly, not calling system commands)
3) learn a little of jquery if you want asyncronous updates

4) put all together :)

Return to “Troubleshooting”