Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import dht
- import machine
- import time
- import network
- from simple import MQTTClient
- from machine import Timer
- # from umqtt_simple import MQTTClient
- def blink():
- led.off()
- time.sleep(0.1)
- led.on()
- time.sleep(0.1)
- led.off()
- time.sleep(0.1)
- led.on()
- isLedOn = False
- # Modify below section as required
- CONFIG = {
- # Configuration details of the MQTT broker
- "MQTT_BROKER": "192.168.1.6",
- "USER": "",
- "PASSWORD": "",
- "PORT": 1883,
- "TOPIC1": "/room/light",
- "TOPIC2": "/dnevna/led",
- # unique identifier of the chip
- "CLIENT_ID": b"esp8266_lazar"
- }
- #Create an instannt(CONFIG['CLIENT_ID'], CONFIG['MQTT_BROKER'], user=CONFIG['USER'], password=CONFIG['PASSWORD'], port=CONFIG['PORT'])
- def onMessage(topic, msg):
- print("msg:"+msg.decode('UTF-8'))
- message = str(msg.decode('UTF-8'))
- topicStr=str(topic.decode('UTF-8'))
- if(topicStr == "/dnevna/led"):
- if(message == "on"):
- led.off()
- global isLedOn = True
- print("LED ON")
- client.publish("/dnevna/ledStatus", str(isLedOn))
- elif(message == "off"):
- print("LED OFF")
- led.on()
- global isLedOn = False
- client.publish("/dnevna/ledStatus", str(isLedOn))
- else:
- print("command not found")
- client.publish("/dnevna/status","command not found...")
- def listen():
- # Attach call back handler to be called on receiving messages
- client.set_callback(onMessage)
- client.connect()
- client.subscribe(CONFIG['TOPIC1'])
- client.subscribe(CONFIG['TOPIC2'])
- # client.subscribe("/dnevna/led")
- print("ESP8266 is Connected to %s and subscribed to %s topic" % (CONFIG['MQTT_BROKER'], CONFIG['TOPIC1']))
- print("ESP8266 is Connected to %s and subscribed to %s topic" % (CONFIG['MQTT_BROKER'], CONFIG['TOPIC2']))
- blink()
- REBOOT_TIMEOUT = 86400000
- tim = Timer(-1)
- #import webrepl_setup
- sta_if = network.WLAN(network.STA_IF)
- print("connecting to network, please wait...")
- sta_if.active(True)
- sta_if.connect('mw-net', 'Kravataokovrata')
- time.sleep(1)
- led = machine.Pin(2, machine.Pin.OUT)
- sensor = dht.DHT22(machine.Pin(16))
- led.off()
- time.sleep(1)
- led.on()
- time.sleep(1)
- try:
- listen()
- except:
- tim.init(period=1000, mode=Timer.PERIODIC, callback=lambda t:machine.reset())
- tim.init(period=REBOOT_TIMEOUT, mode=Timer.PERIODIC, callback=lambda t:machine.reset())
- if(sta_if.isconnected()):
- print("connected!")
- print(sta_if.ifconfig())
- while True:
- try:
- sensor.measure()
- print("TEMP: "+ str(sensor.temperature()))
- print("HUM: "+ str(sensor.humidity()))
- client.check_msg()
- time.sleep(0.5)
- client.publish("/dnevna/temp",str(sensor.temperature()))
- client.publish("/dnevna/hum",str(sensor.humidity()))
- time.sleep(2)
- #message1=0;
- except Exception as e:
- print(e)
- else:
- while (not sta_if.isconnected()):
- print("connection failure, trying to reconnect...")
- sta_if.active(True)
- sta_if.connect('mw-net', 'Kravataokovrata')
- time.sleep(1)ce of MQTTClient
- client = MQTTClient(CONFIG['CLIENT_ID'], CONFIG['MQTT_BROKER'], user=CONFIG['USER'], password=CONFIG['PASSWORD'], port=CONFIG['PORT'])
- def onMessage(topic, msg):
- print("msg:"+msg.decode('UTF-8'))
- message = str(msg.decode('UTF-8'))
- topicStr=str(topic.decode('UTF-8'))
- if(topicStr == "/dnevna/led"):
- if(message == "on"):
- led.off()
- isLedOn = True
- print("LED ON")
- client.publish("/dnevna/ledStatus", str(isLedOn))
- elif(message == "off"):
- print("LED OFF")
- led.on()
- isLedOn = False
- client.publish("/dnevna/ledStatus", str(isLedOn))
- else:
- print("command not found")
- client.publish("/dnevna/status","command not found...")
- def listen():
- # Attach call back handler to be called on receiving messages
- client.set_callback(onMessage)
- client.connect()
- client.subscribe(CONFIG['TOPIC1'])
- client.subscribe(CONFIG['TOPIC2'])
- # client.subscribe("/dnevna/led")
- print("ESP8266 is Connected to %s and subscribed to %s topic" % (CONFIG['MQTT_BROKER'], CONFIG['TOPIC1']))
- print("ESP8266 is Connected to %s and subscribed to %s topic" % (CONFIG['MQTT_BROKER'], CONFIG['TOPIC2']))
- blink()
- REBOOT_TIMEOUT = 86400000
- tim = Timer(-1)
- #import webrepl_setup
- sta_if = network.WLAN(network.STA_IF)
- print("connecting to network, please wait...")
- sta_if.active(True)
- sta_if.connect('mw-net', 'Kravataokovrata')
- time.sleep(1)
- led = machine.Pin(2, machine.Pin.OUT)
- sensor = dht.DHT22(machine.Pin(16))
- led.off()
- time.sleep(1)
- led.on()
- time.sleep(1)
- try:
- listen()
- except:
- tim.init(period=1000, mode=Timer.PERIODIC, callback=lambda t:machine.reset())
- tim.init(period=REBOOT_TIMEOUT, mode=Timer.PERIODIC, callback=lambda t:machine.reset())
- if(sta_if.isconnected()):
- print("connected!")
- print(sta_if.ifconfig())
- while True:
- try:
- sensor.measure()
- print("TEMP: "+ str(sensor.temperature()))
- print("HUM: "+ str(sensor.humidity()))
- client.check_msg()
- time.sleep(0.5)
- client.publish("/dnevna/temp",str(sensor.temperature()))
- client.publish("/dnevna/hum",str(sensor.humidity()))
- time.sleep(2)
- #message1=0;
- except Exception as e:
- print(e)
- else:
- while (not sta_if.isconnected()):
- print("connection failure, trying to reconnect...")
- sta_if.active(True)
- sta_if.connect('mw-net', 'Kravataokovrata')
- time.sleep(1)
Advertisement
Add Comment
Please, Sign In to add comment