Hello folks,
I don't program very much but can usually figure out code issues but no luck with this.
So, I have a raspberry pi and the humidity/temp DHT11 sensor. I can run a script that outputs the temp in Fahrenheit but when I try to output to a webpage, its in Celsius.
Working terminal code:
Thanks for any help anyone can provide!!
I don't program very much but can usually figure out code issues but no luck with this.
So, I have a raspberry pi and the humidity/temp DHT11 sensor. I can run a script that outputs the temp in Fahrenheit but when I try to output to a webpage, its in Celsius.
Working terminal code:
import RPi.GPIO as GPIO import dht11 import time import datetime # initialize GPIO GPIO.setwarnings(True) GPIO.setmode(GPIO.BCM) # read data using pin 14 instance = dht11.DHT11(pin=17) try: while True: result = instance.read() if result.is_valid(): print("Last valid input: " + str(datetime.datetime.now())) print("Humidity: %-3.1f %%" % result.humidity) print("Temperature: %d F" % ((result.temperature * 9/5) +32)) time.sleep(6) except KeyboardInterrupt: print("Cleanup") GPIO.cleanup()Different script that works, just cant get it to show on the webpage as Fahrenheit :
import RPi.GPIO as GPIO import dht11 import time import datetime # initialize GPIO GPIO.setwarnings(True) GPIO.setmode(GPIO.BCM) # read data using pin 14 instance = dht11.DHT11(pin=17) try: while True: result = instance.read() if result.is_valid(): print("Last valid input: " + str(datetime.datetime.now())) print("Humidity: %-3.1f %%" % result.humidity) print("Temperature: %d F" % ((result.temperature * 9/5) +32)) time.sleep(6) except KeyboardInterrupt: print("Cleanup") GPIO.cleanup()
Output:pi@raspberrypi:~/DHT11_Python $ cd ..
pi@raspberrypi:~ $ cd webserver/
pi@raspberrypi:~/webserver $ ls
my-website.py templates
pi@raspberrypi:~/webserver $ cat my-website.py
from flask import Flask, render_template import RPi.GPIO as GPIO import Adafruit_DHT as dht app = Flask(__name__) GPIO.setmode(GPIO.BCM) led1 = 21 led2 = 20 DHT11_pin = 17 # Set each pin as an output and make it low: GPIO.setup(led1, GPIO.OUT) GPIO.setup(led2, GPIO.OUT) @app.route("/") def main(): return render_template('main.html') # The function below is executed when someone requests a URL with the pin number and action in it: @app.route("/<pin>/<action>") def action(pin, action): temperature = '' humidity = '' # if pin == "pin1" and action == "on": # GPIO.output(led1, GPIO.HIGH) # # # if pin == "pin1" and action == "off": # GPIO.output(led1, GPIO.LOW) # # if pin == "pin2" and action == "on": # GPIO.output(led2, GPIO.HIGH) # # if pin == "pin2" and action == "off": # GPIO.output(led2, GPIO.LOW) if pin == "dhtpin" and action == "get": humi, temp = dht.read_retry(dht.DHT11, DHT11_pin) # Reading humidity and temperature humi = '{0:0.1f}' .format(humi) temp = '{0:0.1f}' .format(temp) # temp = int (temp * 9/5.0 + 32) # temperature = 'Temperature ' + (temp * 9/5.0 + 32) # temperature = 'Temperature:' + (temp * 9/5) +32 temperature = 'Temperature: ' + temp humidity = 'Humidity: ' + humi templateData = { 'temperature' : temperature, 'humidity' : humidity } return render_template('main.html', **templateData) if __name__ == "__main__": app.run(host='0.0.0.0', port=80, debug=True)The # are my attempts, plus a lot more.
Thanks for any help anyone can provide!!