from machine import Pin, PWM, SoftI2C
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
from neopixel import NeoPixel
np = NeoPixel(Pin(23), 16)
buz = Pin(18, Pin.OUT)
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
servo.write_angle(180)
door = 'Open'
bot.send(message['message']['chat']['id'], f'Door is {door}!')
def status(message):
global 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 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 disco(message):
def buzz(frequency, duration):
pwm = PWM(buz)
pwm.freq(frequency)
pwm.duty_u16(32768)
sleep_ms(duration)
pwm.duty_u16(0)
pwm.deinit()
frequencies = [200, 400, 500, 600, 700, 800, 900, 1000, 1100, 1200, 1300, 1400, 1500, 1600, 1700]
while True:
for i in range(16):
frequency = 200 + (i * 120)
if frequency > 2000:
frequency = 2000
np[i] = (55, 45, 55)
np.write()
sleep_ms(100)
np[i] = (0, 255, 30)
np.write()
sleep_ms(200)
np[i] = (20, 10, 255)
np.write()
sleep_ms(100)
buzz(frequency, 150)
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 k(message):
a = input("Введіть математичний вираз")
m=eval(a)
bot.send(message['message']['chat']['id'],f'Otvet: {m:.1f}')
def hello_1(message):
bot.send(message['message']['chat']['id'], 'Hellow!')
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 ='8260135025:AAE_Ko67KV_MZwMhbUD4ojy1qfmykVDjjIs'
print('Bot starting...')
bot = utelegram.ubot(token)
bot.register('/hello', hello_1)
bot.register('/k', k)
bot.register('/on', led_on)
bot.register('/off', led_off)
bot.register('/check_door', check_distance)
bot.register('/check_weather', show_weather)
bot.register('/disco', disco)
bot.set_default_handler(show_text)
print('\nGO-GO-GO!!!!!')
bot.listen()