• Skip to main content
  • Skip to primary sidebar

RNTLab.com

The Ultimate Shortcut to Learn Electronics and Programming with Open Source Hardware and Software

  • Courses
  • Forum
    • Forum
    • Ask Question
  • Shop
  • Account
  • Blog
  • Login

DHT11

Q&A Forum › Category: ESP32 › DHT11
0 Vote Up Vote Down
self.raymond asked 4 years ago

I am trying to convert Celsius to Fahrenheit in the following code.  As you can see I have tried various ways.  Get one working and the other wont work.  What am I missing in the print statement.  Thanks for the help.

import dht
import machine
import time
print('Starting DHT11.')
d = dht.DHT11(machine.Pin(4))
#temp = (d.temperature()*(9/5)) + 32 # eg. 23 (°C)
#humid = d.humidity() # eg. 41 (% RH)
while True:
print("Measuring.")
retry = 0
while retry < 3:
try:
d.measure()
break
except:
retry + retry + 1
print(".", end="")
print("")
if retry < 3:
#print('Temperature: ' + str(temp) + '°F, Humidity: ' + str(humid) + '%')
#print((d.temperature()-32)*(5/9))
print(" Temperature: %3.1f %% Fahrenheit" % d.temperature())
print(" Humidity: %3.1f %% Relative Humidity" % d.humidity())
time.sleep(5)

#temp = (d.temperature()*(9/5)) + 32
3 Answers
0 Vote Up Vote Down
Rui Santos Staff answered 4 years ago

Hello, you can use the example that we have in the “MicroPython eBook” for the ESP DHT Web Server example: https://github.com/RuiSantosdotme/ESP-MicroPython/blob/master/code/WiFi/Web_Server_DHT/main.py

Basically, you just need to do this:

sensor.measure()
temp = sensor.temperature()
#converts temperature to Fahrenheit
temp = temp * (9/5) + 32.0

I know this code works to convert it from Celsius to Fahrenheit

0 Vote Up Vote Down
self.raymond answered 4 years ago

Thank You… Should have saw that in the book..

0 Vote Up Vote Down
Rui Santos Staff answered 4 years ago

Let me know if it works for you!

Primary Sidebar

Login to Ask or Answer Questions

This Forum is private and it’s only available for members enrolled in our Courses.

Login »

Latest Course Updates

  • [eBook Updated] SMART HOME with RPi, ESP32, and ESP8266 (version 1.2) March 8, 2023
  • [eBook Updated] ESP32-CAM Projects (version 1.3) January 7, 2023

You must be logged in to view this content.

Contact Support - Refunds - Privacy - Terms - MakerAdvisor.com - Member Login

Copyright © 2013-2023 · RandomNerdTutorials.com · All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.