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