Now this issue isn't particular to the Raspberry Pi, but I'm really lost on how all this works. I've written a Python script that successfully pulls the temperature and humidity from a number of DHT22 modules, averages them, then prints them. From here, how do I port this information to my webpage to be displayed? I have absolutely no idea how I would connect the return values of my Python script to an HTML file, or how to make it update every time the Python script computes the temperature/humidity, which is done every two seconds.
I've seen frameworks like Django, but does it allow me to access the GPIO pins on the RPi in the script?
-
Web frameworks allow you to do whatever the interface language (in this case python) can do on the server.goldilocks– goldilocks2015年09月22日 12:28:21 +00:00Commented Sep 22, 2015 at 12:28
2 Answers 2
You can run your Python script as CGI from the web server, all outputs from the script has to be formatted as HTML. Or can save fetched values in a text file or in MySQL or SQLite database and grab the values from script in the web server (could be also Python or Php or something else)
I really enjoy using websockets for my realtime data in the Pi. I would convert your DHT22 into what this blog considers a "module" http://simplyautomationized.blogspot.com/2015/09/raspberry-pi-create-websocket-api.html
Put your DHT code into a class that is on another "thread":
class DHT_Poller(threading.Thread):
cmd = None
def __init__(self):
threading.Thread.__init__(self)
self.dht_temp_change_callback = None
self.loop = True
self.dht_temp = -1
def run(self):
while(self.loop):
dht = get_temp_dht() # your python method
if(dht!=self.dht_temp): #means your temperature has changed
self.dht_temp=dht
if(self.dht_temp_change_callback):
self.dht_temp_change_callback(json.dumps({"Temp":dht}))
def stop(self):
self.loop=False
def get_temp(self):
return self.dht_temp
- Save that into a file and import it into your websocket server.
- Follow the step on the blog to build the websocket/website server.
- Build a simple webpage that connects to the websocket server. Update values on the webpage using jquery. the github example of the blog is here: https://github.com/SimplyAutomationized/PiFacePythonWebSocket/blob/master/websocket.py