from machine import Pin, I2C
import ssd1306
import time
import ntptime
import network
import utime
# Connect to WiFi (REPLACE with your actual WiFi info)
def connect_wifi():
import network
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect('Wokwi-GUEST', 'your_wifipassword')
while not wlan.isconnected():
pass
print("Connected to WiFi")
# Setup
connect_wifi()
ntptime.settime() # Sync time from internet
i2c = I2C(0, scl=Pin(22), sda=Pin(21))
oled = ssd1306.SSD1306_I2C(128, 64, i2c)
while True:
oled.fill(0)
# Get current local time
t = utime.localtime()
time_str = "{:02}:{:02}:{:02}".format(t[3], t[4], t[5])
oled.text("Time:", 0, 0)
oled.text(time_str, 0, 12)
oled.show()
time.sleep(1)
"""
from machine import Pin, I2C
import ssd1306
import time
print("Starting OLED test...")
# I2C pins for ESP32: SDA=21, SCL=22
i2c = I2C(0, scl=Pin(22), sda=Pin(21))
# Create OLED object
oled = ssd1306.SSD1306_I2C(128, 64, i2c)
# Clear and show text
oled.fill(0)
oled.text("Hello from Wokwi!", 0, 0)
oled.show()
print("OLED should display message")
while True:
time.sleep(1)
'''"""'''
import network
import urequests
import ujson
from machine import Pin, I2C
from time import sleep, time
from mpu6050 import MPU6050
from oled_display import OLEDDisplay
from weather_api import get_weather
from button_handler import ButtonHandler
import ntptime
# ==== Configuration ====
WIFI_SSID = 'Wokwi-GUEST'
WIFI_PASSWORD = ''
OPENWEATHER_API_KEY = '4ba73f0865cfcc26ca11333782899a57'
CITY = 'Kuala Lumpur'
# ==== Global State ====
step_count = 0
water_intake = 0
last_water_time = time()
# ==== Initialize WiFi ====
def connect_wifi():
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(WIFI_SSID, WIFI_PASSWORD)
print("Connecting to WiFi...", end='')
while not wlan.isconnected():
print(".", end='')
sleep(1)
print("\nConnected:", wlan.ifconfig())
# ==== Sync Time ====
def sync_time():
try:
ntptime.settime()
print("Time synchronized")
except:
print("Failed to sync time")
# ==== Initialize ====
connect_wifi()
sync_time()
i2c = I2C(scl=Pin(22), sda=Pin(21))
mpu = MPU6050(i2c)
oled = OLEDDisplay(i2c)
buttons = ButtonHandler()
weather_data = get_weather(OPENWEATHER_API_KEY, CITY)
# ==== Main Loop ====
screen_index = 0
screen_count = 3
while True:
# Check for steps
if mpu.detect_step():
step_count += 1
# Update screen
if screen_index == 0:
oled.show_time_step(step_count)
elif screen_index == 1:
oled.show_weather(weather_data)
elif screen_index == 2:
oled.show_water(water_intake)
# Handle buttons
action = buttons.check_buttons()
if action == 'next':
screen_index = (screen_index + 1) % screen_count
elif action == 'water':
water_intake += 1
last_water_time = time()
# Drink reminder every 2 hours
if time() - last_water_time > 7200:
oled.show_reminder("Drink Water!")
sleep(0.5)
"""