def creat_data(): u=driver.read_u() if u: pack = ts.A.collect('volalite', str('%.1f'%u)) DataCache.append(pack) i=driver.read_i() if i: pack = ts.A.collect('current', str('%.1f'%i)) DataCache.append(pack) p=driver.read_p() if p: pack = ts.A.collect('power', str('%.1f'%p)) DataCache.append(pack) dht.measure() t=dht.temperature() h=dht.humidity() if h: pack = ts.A.collect('humi', str('%.1f'%h)) DataCache.append(pack) if t: pack = ts.A.collect('temp', str('%.1f'%t)) DataCache.append(pack) if Relay.value(): pack = ts.A.collect('switch', str(1)) else: pack = ts.A.collect('switch', str(0)) DataCache.append(pack)
def read_dht(): try: dht.measure() except Exception as e: common.log('Failed to get DHT measurements: %s' % e) return common.publish(TEMPTOPIC, "%.2f" % dht.temperature()) common.publish(HUMTOPIC, "%.2f" % dht.humidity())
def make_response(sta_if, request_content): try: dht.measure() dht_ready = True except OSError: dht_ready = False values = { 'wifi': format_network_status(sta_if.ifconfig()), 't': '{:.2f}'.format(dht.temperature()) if dht_ready else 'error', 'h': '{:.2f}'.format(dht.humidity()) if dht_ready else 'error', } return PAGE_TEMPLATE.format(**values)
def measure(mqtt, dht): # try: dht.measure() temp = dht.temperature() humi = dht.humidity() print("Temperature: %3.1f °C" % temp) print(" Humidity: %3.2f %% RH" % humi) json_data = {"location": "lala", "temperature": temp, "humidity": humi} json_str = json.dumps(json_data) mqtt.publish(MQTT_TOPIC_TEMP, str(temp)) mqtt.publish(MQTT_TOPIC_HUMI, str(humi))
def polldht(dht, ledpin=None): """Collect measurements from DHT and return in a dictionary. If ledpin is specified, turn it on before measurement, and off after. """ if ledpin: ledon(ledpin) dht.measure() results = {} results['temp'] = dht.temperature() results['humi'] = dht.humidity() if ledpin: ledoff(ledpin) return results
import dht, time, machine dht = dht.DHT11(machine.Pin(13)) while Thread[0]: dht.measure() print("temperature:", dht.temperature()) print("humidity:", dht.humidity()) #DHT22 can be read twice every second time.sleep(1) #DHT11 can only be read once every second
def read(): dht.measure() value_hum = dht.humidity() value_temp = dht.temperature() read_output(value_hum) return value_hum, value_temp