Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from machine import Pin, idle, reset
- from network import WLAN, STA_IF
- from time import time, sleep_ms
- # https://raw.githubusercontent.com/micropython/micropython-lib/mqtt/umqtt.simple/umqtt/simple.py
- from mqtt import MQTTClient
- # Enter the name of your Wi-Fi and its password here.
- # If you have an open Wi-Fi, simply remove the second item.
- WIFI_SSID_PASSWORD = '__SSID__', '__PWD__'
- WIFI_CONNECTION_TIMEOUT = 10 # seconds
- MQTT_BROKER_INFO = dict(client_id='ESP8266-%d' % time(),
- server='m10.cloudmqtt.com',
- port=13633,
- user='***REMOVED***',
- password='***REMOVED***')
- MQTT_TOPIC = b'esp'
- MQTT_PING_INTERVAL = 30 # seconds
- # --------------------------------------------------------------------------- #
- led = Pin(13, Pin.OUT)
- relay = Pin(12, Pin.OUT)
- button = Pin(0, Pin.IN)
- button_state_prev = button.value()
- button_state_now = button_state_prev
- btn_value = False
- def on_message(topic, msg):
- print('%s -> %s' % (str(topic), msg))
- if topic == MQTT_TOPIC:
- if msg == b'led on':
- print('Turning LED ON')
- led.off()
- elif msg == b'led off':
- print('Turning LED OFF')
- led.on()
- if msg == b'rel on':
- print('Turning Realy ON')
- relay.on()
- elif msg == b'rel off':
- print('Turning Realy OFF')
- relay.off()
- elif msg == b'controller online':
- print('Controller online')
- for i in range(4):
- led.off() # actually on
- sleep_ms(100)
- led.on() # actually off
- sleep_ms(100)
- STA = WLAN(STA_IF)
- STA.active(True)
- while not STA.isconnected():
- print("Connecting to Wi-Fi...")
- wifi_reconnect_time = time() + WIFI_CONNECTION_TIMEOUT
- STA.connect(*WIFI_SSID_PASSWORD)
- while not STA.isconnected() and time() < wifi_reconnect_time:
- sleep_ms(500)
- if not STA.isconnected():
- print("Connection FAILED!")
- continue
- print("Connected!")
- mq = MQTTClient(**MQTT_BROKER_INFO)
- mq.set_callback(on_message)
- def mqtt_connect():
- try:
- mq.connect()
- mq.subscribe(MQTT_TOPIC, qos=1)
- return True
- except Exception as e:
- print("[Exception] %s: %s" % (type(e).__name__, e))
- return False
- mqtt_connected = False
- while not mqtt_connected:
- if not STA.isconnected():
- print("Wi-Fi Connection failed!")
- break
- print("Connecting to MQTT...")
- mqtt_connected = mqtt_connect()
- if not mqtt_connected:
- continue
- print("Connected!")
- mq.publish(MQTT_TOPIC, 'device online', qos=1)
- print("Waiting for messages...")
- next_ping = time() + MQTT_PING_INTERVAL
- while True:
- try:
- mq.check_msg()
- button_state_prev = button_state_now
- button_state_now = button.value()
- # Falling edge detection
- if button_state_prev == 1 and button_state_now == 0:
- btn_value = not btn_value
- mq.publish(MQTT_TOPIC, 'btn pressed', qos=1)
- if time() >= next_ping:
- mq.ping()
- next_ping = time() + MQTT_PING_INTERVAL
- sleep_ms(100)
- except Exception as e:
- print("%s: %s" % (type(e).__name__, e))
- device = None
- break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement