0

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?

1
  • Web frameworks allow you to do whatever the interface language (in this case python) can do on the server.
    – goldilocks
    Commented Sep 22, 2015 at 12:28

2 Answers 2

1

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)

1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.