# from machine import Pin, SPI,I2C
# import max7219
# import time
# from ssd1306 import SSD1306_I2C
# # Configure SPI
# spi = SPI(1, baudrate=10000000, polarity=0, phase=0, sck=Pin(2), mosi=Pin(16))
# # Define Chip Select (CS) pin
# cs = Pin(18, Pin.OUT)
# # Create display object for 4 matrices
# num_matrices = 4
# display = max7219.Matrix8x8(spi, cs, num_matrices)
# # Initialize the display
# display.brightness(5) # Set brightness (0-15)
# display.fill(0) # Clear the display
# display.show()
# # Example: Display numbers 0 to 3 on each matrix
# import network
# import time
# import urequests
# print("Connecting to WiFi", end="")
# sta_if = network.WLAN(network.STA_IF)
# sta_if.active(True)
# sta_if.connect('Wokwi-GUEST', '')
# while not sta_if.isconnected():
# print(".", end="")
# time.sleep(0.1)
# print(" Connected!")
# # def __init__(self, width, height, i2c, addr=0x3C, external_vcc=False):
# # Create I2C object (adjust for your board and pins)
# i2c = I2C(0, scl=Pin(12), sda=Pin(14)) # For ESP32
# #i2c adress ['0x3c', '0x3d', '0x3e', '0x3f']
# oled1=SSD1306_I2C(128, 64, i2c,0x3c)
# oled2=SSD1306_I2C(128, 64, i2c,0x3d)
# oled3=SSD1306_I2C(128, 64, i2c,0x3e)
# oled4=SSD1306_I2C(128, 64, i2c,0x3f)
# oled1.text("hello",40, 30)
# oled2.text("hello",0,0)
# oled3.text("hello",0,0)
# oled4.text("hello",0,0)
# oled1.show()
# oled2.show()
# oled3.show()
# oled4.show()
# time.sleep(1)
# oled1.fill(0)
# oled2.fill(0)
# oled3.fill(0)
# oled4.fill(0)
# while True:
# display.fill(0)
# city = "London"
# response = urequests.get(f"http://worldtimeapi.org/api/timezone/Europe/{city}")
# time_data = response.json()
# print(time_data)
# response.close()
# # Extract time (HH:MM)
# datetime_str = time_data["datetime"]
# time_part = datetime_str[11:16] # Get HH:MM
# print("Time:", time_part)
# display.fill(0)
# # number=input("what number to display: ")
# first_digit = int(time_part[0])
# second_digit = int(time_part[1])
# third_digit = int(time_part[3])
# fourth_digit = int(time_part[4])
# display.text(str(first_digit)+str(second_digit)+str(third_digit)+str(fourth_digit),0,0,1)
# display.show()
# oled1.text(str(first_digit),0,0)
# oled2.text(str(second_digit),0,0)
# oled3.text(str(third_digit),0,0)
# oled4.text(str(fourth_digit),0,0)
# oled1.show()
# oled2.show()
# oled3.show()
# oled4.show()
# display.fill(0)
# time.sleep(0.5)
from machine import Pin, SPI, I2C
import max7219
import time
from ssd1306 import SSD1306_I2C
import network
import urequests
from mpu6050 import MPU6050
# ==== Setup WiFi ====
print("Connecting to WiFi", end="")
sta_if = network.WLAN(network.STA_IF)
sta_if.active(True)
sta_if.connect('Wokwi-GUEST', '')
while not sta_if.isconnected():
print(".", end="")
time.sleep(0.1)
print(" Connected!")
# ==== Setup MAX7219 SPI LED Matrix ====
spi = SPI(1, baudrate=10000000, polarity=0, phase=0, sck=Pin(2), mosi=Pin(16))
cs = Pin(18, Pin.OUT)
num_matrices = 4
display = max7219.Matrix8x8(spi, cs, num_matrices)
display.brightness(5)
display.fill(0)
display.show()
# ==== Setup I2C OLED Displays ====
i2c = I2C(0, scl=Pin(12), sda=Pin(14)) # Adjust pins as needed
print("I2C devices:", [hex(addr) for addr in i2c.scan()])
oled1 = SSD1306_I2C(128, 64, i2c, addr=0x3c)
oled2 = SSD1306_I2C(128, 64, i2c, addr=0x3d)
oled3 = SSD1306_I2C(128, 64, i2c, addr=0x3e)
oled4 = SSD1306_I2C(128, 64, i2c, addr=0x3f)
oled5 = SSD1306_I2C(128, 64, i2c, addr=0x40)
mpu=MPU6050(i2c,addr=0x68)
# Initial clear
for oled in [oled1, oled2, oled3, oled4]:
oled.fill(0)
oled.text("Waiting...", 0, 0)
oled.show()
# ==== Main Loop ====
while True:
data=mpu.get_all_data()
print(data)
try:
city = "London"
url = f"http://worldtimeapi.org/api/timezone/Europe/{city}"
response = urequests.get(url)
time_data = response.json()
response.close()
# Extract time (HH:MM)
datetime_str = time_data["datetime"]
time_part = datetime_str[11:16] # Get "HH:MM"
print("Time:", time_part)
# Split digits
digits = [int(time_part[0]), int(time_part[1]), int(time_part[3]), int(time_part[4])]
# === MAX7219 display (combined)
display.fill(0)
display.text(time_part, 0, 0, 1) # Show full time like 14:36
display.show()
# === OLEDs display (each digit)
for i, oled in enumerate([oled1, oled2, oled3, oled4]):
oled.fill(0)
oled.text("Time:", 0, 0)
oled.text(str(digits[i]), 50, 30)
oled.show()
except Exception as e:
print("Error fetching time:", e)
display.fill(0)
display.text("ERR", 0, 0, 1)
display.show()
time.sleep(1) # Refresh every 5 seconds