import machine
from machine import Pin, I2C
# from machine import Pin, SoftI2C
from time import sleep,sleep_ms
import sys
import sh1107
import urandom as random
# import dht
# SH1107 is vertically oriented 64x128
WIDTH = 128
HEIGHT = 64
def randint(min, max):
span = max - min + 1
div = 0x3FFFFFFF // span
offset = random.getrandbits(30) // div
val = min + offset
return val
currentBoard=""
if(sys.platform=="esp8266"):
currentBoard="esp8266"
I2C_SDA =4 # GPIO4(D2)
I2C_SCL =5 # GIPO5(D1)
i2c = I2C(scl=Pin(I2C_SCL), sda=Pin(I2C_SDA), freq=10000) #initializing the I2C method for ESP8266
elif(sys.platform=="esp32"):
currentBoard="esp32"
I2C_SDA =21 # GPIO21(I2C SDA)
I2C_SCL =22 # GPIO22(I2C SCL)
# sensor = dht.DHT22(Pin(15))
# Initialize I2C for SH1107 display on GP16 (SDA) and GP17 (SCL)
i2c = I2C(0, scl=Pin(I2C_SCL), sda=Pin(I2C_SDA), freq=400000) #initializing the I2C method for ESP32
# i2c = SoftI2C(scl=Pin(I2C_SCL), sda=Pin(I2C_SDA)) #initializing the I2C method for ESP32
elif(sys.platform=="rp2"):
currentBoard="Pi-Pico"
I2C_SDA =16 # GPIO16(I2C SDA)
I2C_SCL =17 # GPIO17(I2C SCL)
# sensor = dht.DHT22(Pin(15))
# Initialize I2C for SH1107 display on GP16 (SDA) and GP17 (SCL)
i2c = I2C(0, scl=Pin(I2C_SCL), sda=Pin(I2C_SDA))
print("Hello, " + sys.platform + "!")
oled = sh1107.SH1107_I2C(WIDTH, HEIGHT, i2c)
# Small delay to allow the OLED to initialize
sleep(1)
# Display a test message
oled.fill(0) # Clear the screen
oled.text("Hello, Wokwi!", 0, 0)
oled.show()
while True:
# sensor.measure() # read the parameters from the sensor
# t = sensor.temperature()
# h = sensor.humidity()
t=randint(1,50)
h=randint(1,100)
oled.fill(0)
oled.text(f"Temp: {t:.2f}C", 0, 10, 1)
oled.text(f"Hum: {h/100:.2%}", 0, 20, 1)
oled.show()
sleep_ms(500)
Loading
grove-oled-sh1107
grove-oled-sh1107