This blog is a record of what I do, what I think and what I read. If you are interested to know about my professional experience, you can refer to the other blog of mine. http://admi2camac.blogspot.com/
Sunday, 29 March 2026
Weather station using ThingSpeak abd SSD1306
import network
import time
import dht
import urequests
from machine import Pin,I2C
import ssd1306
import secret
# --- Configuration ---
ssid=secret.WIFI_SSID
password=secret.WIFI_PASS
api_key = secret.THINGSPEAK_API_KEY
DHT_PIN = 18
# --- SSD1306 setup ---
# Screen dimensions
WIDTH = 128
HEIGHT = 64
# Initialize OLED
i2c = I2C(0, scl=Pin(17), sda=Pin(16)) # GP17=SCL, GP16=SDA
oled = ssd1306.SSD1306_I2C(WIDTH, HEIGHT, i2c)
# Clear the screen
oled.fill(0)
# --- LED Setup ---
led_pwr = Pin(15, Pin.OUT) # Red
led_wifi = Pin(14, Pin.OUT) # yellow
led_data = Pin(13, Pin.OUT) # Green
led_pwr.value(1)
# --- Setup Sensor ---
sensor = dht.DHT11(Pin(DHT_PIN))
# --- WiFi ---
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(ssid, password)
while not wlan.isconnected():
led_wifi.toggle()
time.sleep(0.5)
led_wifi.value(1)
print('Connected to ' , wlan.ifconfig()[0])
# --- Main Loop ---
while True:
sensor.measure()
t = sensor.temperature()
h = sensor.humidity()
# Print values to Shell
print('Temperature ', t, '°C')
print('Humidity ', h, '%')
# Write to the display
# Syntax: text(string, x, y)
oled.text("Temp: {} degC".format(t), 0, 0)
oled.text("Humidity: {} %".format(h), 0, 35)
# Draw temperature bar (max width = 100 pixels)
bar_width = int((t / 100) * 100)
oled.rect(0, 15, 100, 10, 1) # outline
oled.fill_rect(0, 15, bar_width, 10, 1) # filled bar
# Draw humidity bar (max width = 100 pixels)
bar_width = int((h / 100) * 100)
oled.rect(0, 50, 100, 10, 1) # outline
oled.fill_rect(0, 50, bar_width, 10, 1) # filled bar
# Show on OLED
oled.show()
url = f"https://api.thingspeak.com/update?api_key={api_key}&field1={t}&field2={h}"
led_data.value(1)
response = urequests.get(url)
response.close()
led_data.value(0)
time.sleep(20)
# Clear the screen
oled.fill(0)
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment