Python Forum
DHT11 output to website problem
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
DHT11 output to website problem
#1
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:

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!!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  problem in output of a snippet code akbarza 2 357 Feb-28-2024, 07:15 PM
Last Post: deanhystad
  output shape problem with np.arange alan6690 5 668 Dec-26-2023, 05:44 PM
Last Post: deanhystad
  problem in output of a function akbarza 9 1,178 Sep-29-2023, 11:13 AM
Last Post: snippsat
  Python Pandas Syntax problem? Wrong Output, any ideas? Gbuoy 2 920 Jan-18-2023, 10:02 PM
Last Post: snippsat
  Facing problem with Pycharm - Not getting the expected output amortal03 1 855 Sep-09-2022, 05:44 PM
Last Post: Yoriz
  Wiring DHT11 to Raspberry pi hcccs 3 4,086 Feb-06-2021, 08:42 PM
Last Post: hcccs
  single input infinite output problem Chase91 2 1,935 Sep-23-2020, 10:01 PM
Last Post: Chase91
  Output to a json file problem Netcode 3 3,723 Nov-22-2019, 01:44 AM
Last Post: Skaperen
  Problem Table Output Phil 4 2,630 May-19-2019, 12:17 PM
Last Post: Phil
  Pass variable script return twice output problem Faruk 8 4,384 Dec-26-2018, 11:57 AM
Last Post: Faruk

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020