#все для MQTT
from machine import Pin
from umqtt.simple import MQTTClient
import ujson
import network
import utime as time
import dht
#https://www.hivemq.com/demos/websocket-client/
import machine
from machine import Pin, SoftI2C
from lcd_api import LcdApi
from i2c_lcd import I2cLcd
from time import sleep
from machine import Pin
from time import sleep
import dht
#Для Neopixel
from machine import Pin
from neopixel import NeoPixel
from time import sleep
pixels = NeoPixel(Pin(13), 16)
pixels.fill((0, 0, 0))
pixels.write()
def mqtt_message(topic, msg):
print("Incoming message:", msg)
try:
msg = ujson.loads(msg)
pixels.fill((msg[0], msg[1], msg[2]))
pixels.write()
except Exception as e:
print("Error:", e)
print("Connecting to WiFi...", end="")
import network
wifi = network.WLAN(network.STA_IF)
wifi.active(True)
wifi.connect("Wokwi-GUEST", "")
while not wifi.isconnected():
sleep(0.5)
print(".", end="")
print("Done")
print("Connecting to MQTT...")
client = MQTTClient("gusev", "broker.mqttdashboard.com")
client.set_callback(mqtt_message)
client.connect()
client.subscribe("gusev")
print("Connected!")
while True:
client.wait_msg()
I2C_ADDR = 0x27
totalRows = 2
totalColumns = 16
#18-25
#20-60%
led1 = machine.Pin(2, machine.Pin.OUT)
led2 = machine.Pin(4, machine.Pin.OUT)
i2c = SoftI2C(scl=Pin(22), sda=Pin(21), freq=10000) #initializing the I2C method for ESP32
#i2c = I2C(scl=Pin(5), sda=Pin(4), freq=10000) #initializing the I2C method for ESP8266
lcd = I2cLcd(i2c, I2C_ADDR, totalRows, totalColumns)
sensor = dht.DHT22(Pin(15))
#mqtt
# Device Setup
DEVICE_ID = "nikita"
# WiFi Setup
WIFI_SSID = "Wokwi-GUEST"
WIFI_PASSWORD = ""
# MQTT Setup
MQTT_BROKER = "broker.mqttdashboard.com"
MQTT_CLIENT = DEVICE_ID
MQTT_TELEMETRY_TOPIC = "remnev"
MQTT_CONTROL_TOPIC = "remnev"
# DHT Sensor Setup
DHT_PIN = Pin(15)
# LED/LAMP Setup
RED_LED = Pin(12, Pin.OUT)
BLUE_LED = Pin(13, Pin.OUT)
FLASH_LED = Pin(2, Pin.OUT)
RED_LED.on()
BLUE_LED.on()
# Methods
def did_recieve_callback(topic, message):
print('\n\nData Recieved! \ntopic = {0}, message = {1}'.format(topic, message))
# device_id/lamp/color/state
# device_id/lamp/state
# lamp/state
if topic == MQTT_CONTROL_TOPIC:
if message == ('{0}/lamp/red/on'.format(DEVICE_ID)):
RED_LED.on()
elif message == ('{0}/lamp/red/off'.format(DEVICE_ID)):
RED_LED.off()
elif message == ('{0}/lamp/blue/on'.format(DEVICE_ID)):
BLUE_LED.on()
elif message == ('{0}/lamp/blue/off'.format(DEVICE_ID)):
BLUE_LED.off()
elif message == ('{0}/lamp/on'.format(DEVICE_ID)) or message == b'lamp/on':
RED_LED.on()
BLUE_LED.on()
elif message == ('{0}/lamp/off'.format(DEVICE_ID)) or message == b'lamp/off':
RED_LED.off()
BLUE_LED.off()
elif message == ('{0}/status'.format(DEVICE_ID)) or message == ('status'):
global telemetry_data_old
mqtt_client_publish(MQTT_TELEMETRY_TOPIC, telemetry_data_old)
else:
return
def mqtt_connect():
print("Connecting to MQTT broker ...", end="")
mqtt_client = MQTTClient(MQTT_CLIENT, MQTT_BROKER, user="", password="")
mqtt_client.set_callback(did_recieve_callback)
mqtt_client.connect()
print("Connected.")
mqtt_client.subscribe(MQTT_CONTROL_TOPIC)
return mqtt_client
def create_json_data(temperature, humidity):
data = ujson.dumps({
"temp": temperature,
"humidity": humidity
})
return data
def mqtt_client_publish(topic, data):
print("\nUpdating MQTT Broker...")
mqtt_client.publish(topic, data)
print(data)
# Application Logic
# Connect to WiFi
wifi_client = network.WLAN(network.STA_IF)
wifi_client.active(True)
print("Connecting device to W