I have a python script running on my raspberry pi that outputs data from sensors connected to the pi's GPIO input. I need a simple way to have this live data on a server accessible from anywhere. I've looked at django framework but it seems like overkill for what I need to do.
3 Answers
You mean something like this?
You can create a static webpage with some JS like so:
<html>
<head>
<title>My Pi</title>
<script
src="https://code.jquery.com/jquery-3.2.1.min.js"
integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="
crossorigin="anonymous"></script>
</head>
<body>
<div id="sensor1"></div>
<a href="javascript:getSensor1()">Get data from sensor 1</a>
<script type="text/javascript">
function getSensor1() {
$.ajax({
type: "POST",
url: "cgi-bin/cputemp.py",
dataType: "html",
success: function(msg) {
document.getElementById('sensor1').innerHTML = msg;
},
});
}
</script>
</body>
And something like this in a cgi-bin/cputemp.py directory relative to your html page.
#!/usr/bin/python
import cgi;
import cgitb;
import time
cgitb.enable()
import commands
import sys
import string
print "Content-type: text/html\n\n";
mytemp1 = commands.getoutput('/opt/vc/bin/vcgencmd measure_temp | cut -d "=" -f2 | cut -f1')
output = "Pi CPU Temp is: " + mytemp1
print output
1 Comment
Mark Setchell
Shouldn't
getSensor1() be cputemp2()?You can use Flask instead of Django.
And if you don't want to create server you can use third party Application Like ThingSpeak where you can just post data and you can view your data from anywhere.