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
Clock and temp using TM1637
import machine
import dht
import time
import tm1637
# SETUP HARDWARE
# Display connected to GP26 (CLK) and GP27 (DIO)
display = tm1637.TM1637(clk=machine.Pin(26), dio=machine.Pin(27))
# Temperature sensor connected to GP18
sensor = dht.DHT11(machine.Pin(18))
# Set display brightness
display.brightness(3)
print("Clock started (using synced laptop time)...")
# MAIN LOOP
while True:
# Get current time.
# Returns a tuple: (year, month, mday, hour, minute, second, weekday, yearday)
t = time.localtime()
# Extract Hour (index 3) and Minute (index 4)
hh = t[3]
mm = t[4]
# Show the time for 10 seconds
display.numbers(hh, mm, True) # Colon ON
time.sleep(10)
# Measure and show temperature for 5 seconds
try:
sensor.measure()
temp = sensor.temperature()
display.temperature(temp) # Format example: "25 C"
time.sleep(5)
except:
print("Sensor error")
time.sleep(1)
Weather Station ThingSpeak
import network
import time
import dht
import urequests
from machine import Pin
import secret
# --- Configuration ---
ssid=secret.WIFI_SSID
password=secret.WIFI_PASS
api_key = secret.THINGSPEAK_API_KEY
DHT_PIN = 18
# --- LED Setup ---
led_pwr = Pin(15, Pin.OUT) # Red
led_wifi = Pin(14, Pin.OUT) # Green
led_data = Pin(13, Pin.OUT) # Blue
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)
# --- Main Loop ---
while True:
sensor.measure()
t = sensor.temperature()
h = sensor.humidity()
# Print values to Shell
print('Temp ', t)
print('Humidity ', h)
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)
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)
Secret credentials
ssid="xxxxx"
password="xxxxx"
WIFI_SSID = "xxxxx"
WIFI_PASS = "xxxxx"
Mobile_Hotspot_SSID = "xxxxx"
Mobile_Hotspot_pass = "xxxxx"
THINGSPEAK_API_KEY = "xxxxx"
To call these passwords, insert a line:
import secret
in the main program
Subscribe to:
Comments (Atom)