Article Index

 

Page 212

import dht
from machine import Pin
from time import sleep

dht = dht.DHT22(Pin(23))
while True:
    dht.measure()
    temp = dht.temperature()
    print(temp,"C")
    hum = dht.humidity()
    print(hum,"%")
    sleep(1)

Page 213

from machine import Pin
from utime import sleep_ms
DHT = Pin(23,mode=Pin.OUT,value=1)
sleep_ms(1)
DHT.off()
sleep_ms(1)
DHT.init(mode=Pin.IN)

Page 217

from machine import Pin,time_pulse_us
from utime import sleep_ms

class DHT22():
    def __init__(self,gpio):
        self.pin = Pin(gpio, mode=Pin.IN)
        self.checksum = 0
        self.temperature = 0
        self.humidity=0
        sleep_ms(1)
       
    @micropython.native
    def getReading(self):
        data = 0
        checksum = 0
        DHT=self.pin  
        DHT.init(mode=Pin.OUT,value=0)
        sleep_ms(1)
        DHT.init(mode=Pin.IN)
        t = time_pulse_us(DHT, 0, 1000)
        t = time_pulse_us(DHT, 1, 1000)        
             
        for i in range(32):
            t = time_pulse_us(DHT, 1, 1000)
            data = data << 1
            data = data | (t > 50)          
       
        for i in range(8):
            t = time_pulse_us(DHT, 1, 1000)
            checksum = checksum << 1
            checksum = checksum |(t > 50)
 
        byte1 = (data >> 24 & 0xFF)
        byte2 = (data >> 16 & 0xFF)
        byte3 = (data >> 8 & 0xFF)
        byte4 = (data & 0xFF)
        self.checksum =(checksum ==(byte1+byte2+byte3+byte4)&0xFF)
        self.humidity = ((byte1 <<8)| byte2) / 10.0
        neg = byte3 & 0x80
        byte3 = byte3 & 0x7F
        self.temperature = (byte3 << 8 | byte4) / 10.0
        if neg > 0:
            self.temperature = -self.temperature

dht = DHT22(23)
dht.getReading()
print("Checksum",dht.checksum)
print("Humidity= ", dht.humidity)
print("Temperature=", dht.temperature)

Page 223

from machine import Pin
import onewire,ds18x20

ow = onewire.OneWire(Pin(23))
presence=ow.reset()
if presence:
    print("Device present")
else:
    print("No device")
   
ds = ds18x20.DS18X20(ow)    
roms = ds.scan()
t = ds.read_temp(roms[0])
print(t)

Page 230

from machine import Pin
import onewire

class DS18B20:
    def __init__(self,pin):
        self.ow = onewire.OneWire(Pin(pin))    
       
    def __convert(self):
        self.ow.write([0xCC,0x44])
        for i in range(500):
            sleep_ms(10)
            if self.ow.readbit() == 1:
                j = i
                break
        return j
   
    def getTemp(self):
        if not self.ow.reset:
            return -1000
        if self.__convert()==500:
            return -3000
        self.ow.reset()
        self.ow.write([0xCC,0xBE])
        data = bytearray(9)
        self.ow.readinto(data)
        if self.ow.crc8(data)!=0:
            return -2000
        t1 = data[0]
        t2 = data[1]
        temp1 = (t2 << 8 | t1)
        if t2 & 0x80:
            temp1=temp1 | 0xFFFF0000
        return temp1/16

dS18B20=DS18B20(23)
print(dS18B20.getTemp())