Welcome to Hackster!
Hackster is a community dedicated to learning hardware, from beginner to pro. Join us, it's free!
AZ-Delivery
Published © GPL3+

Raspberry Pi Pico W with BME280 and OLED in Thonny and Micro

This article uses the WLAN interface to send temperature and relative humidity to the home network web server.

BeginnerFull instructions provided182
Raspberry Pi Pico W with BME280 and OLED in Thonny and Micro

Things used in this project

Hardware components

1.3 inch OLED I2C 128 x 64 Pixel Display
×1
Raspberry Pi Pico RP2040 Mikrocontroller-Board
×1
GY-BME280 barometric sensor for temperature, air humidity and air pressure
×1
DHT20 Digital temperature sensor and air humidity sensor
×1

Story

Read more

Code

Untitled file

HTML
 # Example Code for Web Server and DHT20 sensor
 # based on example of Raspberry Pi Pico documentation
 # import modules
 import network
 import socket
 import utime
 from machine import Pin, I2C
 from dht20 import DHT20
 import sh1106
 
 ssid = 'Xxxxxx' #Your network name
 password = 'Yyyyyyy' #Your WiFi password
 
 #initialize I2C
 sda = Pin(12)
 scl = Pin(13)
 i2c0=I2C(0,sda=sda, scl=scl, freq=400000)
 dht20 = DHT20(0x38, i2c0)
 WIDTH = 128
 HIGHT = 64
 oled = sh1106.SH1106_I2C(128, 64, i2c0, None, 0x3c,rotate=180)
 
 #Connect to WLAN
 wlan = network.WLAN(network.STA_IF)
 wlan.active(True)
 wlan.connect(ssid, password)
 
 
 # Wait for connect or fail
 max_wait = 10
 while max_wait > 0:
     if wlan.status() < 0 or wlan.status() >= 3:
         break
     max_wait -= 1
     print('waiting for connection...')
     utime.sleep(1)
 
 # handle connection error
 if wifi.status() != 3:
     raise RuntimeError('network connection failed')
 else:
     print('connected')
     status = wlan.ifconfig()
     print( 'ip = ' + status[0] )    
 
 # Open socket
 addr = socket.getaddrinfo('0.0.0.0', 80)[0][-1]
 connection = socket.socket()
 connection.bind(addr)
 connection.listen(1)
 
 def webpage(output):
     #template HTML
     html = f"""
 
 
 
 
 
 
 
 
{output}

 

 
            """
     return str(html)
     
 try:
     while True:
         measurements = dht20.measurements
         t = round(measurements['t'],1)
         rh = int(measurements['rh'])
         temp = "Temperature: " + str(t) + "*C"
         humid = "Rel.Humidity: " + str(rh) +"%"
         print(temp)
         print(humid)
         output = temp + humid
         #Write data to display
         oled.fill(0)
         oled.text("Pico W, DHT20 ",6,10)
         oled.text("Temp:" + str(t) + "*C",6,26)
         oled.text("Rel.Humid:" + str(rh) + "%",6,42)
         oled.show()
         client = connection.accept()[0]
         request = client.recv(1024)
         request = str(request)      
         html = webpage(output)
         client.send(html)
         client.close()
 
 except KeyboardInterrupt:
     machine.reset()

Credits

AZ-Delivery
27 projects • 3 followers
Projects that spark curiosity. Just DIY with us. Visit our shop with regularly new Blogs and free E-Books.
Contact

Comments

Please log in or sign up to comment.