from machine import Pin, SoftI2C, PWM
from utime import sleep, sleep_us, ticks_us, sleep_ms
from i2c_lcd import I2cLcd
from servo import Servo
import network
import utelegram
from dht import DHT22
import ssd1306
i2c_oled = SoftI2C(sda=Pin(18), scl=Pin(5)) # SDA=18, SCL=5
oled = ssd1306.SSD1306_I2C(128, 64, i2c_oled)
i2c = SoftI2C(sda=Pin(21), scl=Pin(22))
lcd = I2cLcd(i2c, 0x27, 4, 20)
servo1 = Servo(19)
servo1.write_angle(0)
trigger1 = Pin(32, Pin.OUT)
echo1 = Pin(33, Pin.IN)
led = Pin(12, Pin.OUT)
weather = DHT22(Pin(25))
door = 'Close'
def door_open(message):
global door
servo1.write_angle(180)
door = 'Open'
bot.send(message['message']['chat']['id'], f'Door is {door}!')
def status(message):
global door
bot.send(message['message']['chat']['id'], f'Door is {door}')
def show_text(message):
lcd.clear()
lcd.putstr(message['message']['text'])
bot.send(message['message']['chat']['id'], 'Ok!')
def ultra(trigger, echo):
trigger.off()
sleep_us(2)
trigger.on()
sleep_us(5)
trigger.off()
while echo.value() == 0:
signal_off = ticks_us()
while echo.value() == 1:
signal_on = ticks_us()
try:
time_pass = signal_on - signal_off
except NameError:
return 0
return time_pass/58
def check_distance(message):
distance = ultra(trigger1, echo1)
bot.send(message['message']['chat']['id'], f'Distance to object: {distance:.1f}cm')
def kalk(message):
a = input("Введіть математичний вираз")
b = eval(a)
bot.send(message['message']['chat']['id'], b)
def led_on(message):
led.on()
bot.send(message['message']['chat']['id'], 'Led is ON!')
def led_off(message):
led.off()
bot.send(message['message']['chat']['id'], 'Led is OFF!')
def show_weather(message):
weather.measure()
temp = weather.temperature()
hum = weather.humidity()
bot.send(message['message']['chat']['id'], f'Temperature: {temp}C\nHumidity: {hum}%')
def find(message):
bot.send(message['message']['chat']['id'], 'Buzzer making sound')
for i in range(15):
buzzer = PWM(Pin(26), 1000, duty=512)
sleep_ms(300)
buzzer.deinit()
sleep_ms(201)
def found(message):
bot.send(message['message']['chat']['id'], 'Congratulations')
buzzer = PWM(Pin(26), 500, duty=512)
sleep_ms(250)
buzzer.deinit()
def paint_snake(message):
oled.fill(0)
for i in range(0, 128, 4):
oled.pixel(i, 63, 1)
oled.pixel(i+1, 62, 1)
oled.pixel(i+2, 63, 1)
oled.fill_rect(60, 15, 20, 12, 1)
oled.fill_rect(78, 17, 4, 8, 1)
oled.pixel(80, 18, 1)
oled.pixel(80, 20, 1)
oled.pixel(80, 22, 1)
oled.fill_rect(68, 17, 3, 3, 0)
oled.pixel(70, 18, 1)
oled.fill_rect(74, 17, 3, 3, 0)
oled.pixel(76, 18, 1)
oled.pixel(70, 22, 1)
oled.pixel(71, 23, 1)
oled.pixel(72, 23, 1)
oled.pixel(73, 23, 1)
oled.pixel(74, 22, 1)
oled.pixel(80, 21, 1)
oled.pixel(81, 20, 1)
oled.pixel(81, 22, 1)
oled.fill_rect(52, 27, 16, 8, 1)
oled.fill_rect(44, 35, 16, 8, 1)
oled.fill_rect(36, 43, 16, 8, 1)
oled.fill_rect(28, 51, 16, 8, 1)
oled.fill_rect(20, 55, 12, 6, 1)
oled.fill_rect(12, 57, 10, 4, 1)
oled.fill_rect(6, 59, 8, 2, 1)
oled.pixel(4, 60, 1)
oled.pixel(5, 60, 1)
oled.pixel(64, 16, 0)
oled.pixel(66, 16, 0)
body_pattern = [
(48, 28), (50, 30), (52, 32),
(40, 36), (42, 38), (44, 40),
(32, 44), (34, 46), (36, 48),
(24, 52), (26, 54), (28, 56)
]
for x, y in body_pattern:
oled.pixel(x, y, 0)
oled.fill_rect(100, 35, 10, 10, 1)
oled.fill_rect(98, 37, 14, 6, 1)
oled.fill_rect(105, 30, 4, 2, 1)
oled.pixel(108, 31, 1)
oled.pixel(101, 36, 0)
oled.pixel(102, 37, 0)
oled.text("Snake", 90, 5, 1)
oled.text("OLED Art", 85, 55, 1)
oled.show()
bot.send(message['message']['chat']['id'], '🐍 Beautiful snake painted on OLED!')
def off_painting(message):
oled.fill(0)
oled.show()
bot.send(message['message']['chat']['id'], '🖥️ OLED display turned OFF!')
wifi = network.WLAN(network.STA_IF)
wifi.active(True)
wifi.scan()
wifi.connect('Wokwi-GUEST', '')
print('Connecting.', end='')
while not wifi.isconnected():
sleep(0.1)
print('.', end='')
print('\nConnected!')
token = '8573907152:AAH_0hrO04cpR8CsVIwbGjmJn-0ALgtACSc'
print('Bot starting...')
bot = utelegram.ubot(token)
bot.register('/on', led_on)
bot.register('/of', led_off)
bot.register('/check_door', check_distance)
bot.register('/check_weather', show_weather)
bot.register('/find', find)
bot.register('/found', found)
bot.register('/k', kalk)
bot.register('/paint', paint_snake)
bot.register('/off_painting', off_painting)
bot.set_default_handler(show_text)
print('\nGO-GO-GO!!!!!')
bot.listen()