def publish_thingsboard(token, UNIQUE_ID, data, password=''): from mqtt import MQTTClient import gc import json import machine import utime client = MQTTClient(UNIQUE_ID, "iot.ier.unam.mx", port=1883, user=token, password=password) client.settimeout = settimeout client.connect() print(json.dumps(data)) client.publish('v1/devices/me/telemetry', json.dumps(data)) client.disconnect()
def main(): print("starting") time.sleep(1.0) # Since open-drain on() means relays OFF and off() means Relays ON RELAY1.on() RELAY2.on() client = MQTTClient("relays1", "192.168.1.145", port=1883) client.settimeout = settimeout client.connect() client.set_callback(switch) client.subscribe("switching/#") while True: client.check_msg() print("waiting") time.sleep(1.0)
def thinx_mqtt(): restore_device_info() if not THINX_API_KEY: print("* THiNX: MQTT init failed...") return print("* THiNX: Initializing MQTT client " + THINX_UDID + " / " + THINX_API_KEY) mqtt_client = MQTTClient(thinx_device_mac(), THINX_MQTT_URL, THINX_MQTT_PORT, THINX_DEVICE_OWNER, THINX_API_KEY, keepalive=0, ssl=False, ssl_params={}) mqtt_client.settimeout = thinx_mqtt_timeout mqtt_client.set_callback = thinx_mqtt_callback mqtt_client.set_last_will(mqtt_status_channel(), thx_disconnected_response, retain=True, qos=0) if mqtt_client.connect(): mqtt_connected = True mqtt_client.subscribe(mqtt_device_channel(), MQTT_DEVICE_QOS) mqtt_client.subscribe(mqtt_status_channel(), MQTT_QOS) mqtt_client.publish(mqtt_status_channel(), thx_connected_response, MQTT_RETAIN, MQTT_QOS) if mqtt_connected == False: print("* THiNX: Re/connecting MQTT to " + THINX_MQTT_URL + "...") if mqtt_client.connect(): mqtt_connected = True mqtt_client.subscribe(mqtt_device_channel(), MQTT_DEVICE_QOS) mqtt_client.subscribe(mqtt_status_channel(), MQTT_QOS) mqtt_client.publish(mqtt_status_channel(), thx_connected_response, MQTT_RETAIN, MQTT_QOS)
pass button_pin = machine.Pin(21, machine.Pin.IN) button_state = 0 wlan = network.WLAN(network.STA_IF) wlan.active(True) wlan.connect(essid, password) while not wlan.isconnected(): machine.idle() print("Connected to Wifi\n") client = MQTTClient("publisher", broker_host, port=1883) client.settimeout = settimeout client.connect() print("MQTT client connected") time.sleep(1) while True: new_button_state = button_pin.value() if new_button_state != button_state: button_state = new_button_state if button_state == 1: msg = 'ON' else: msg = 'OFF' client.publish('button', msg)
from network import WLAN from mqtt import MQTTClient gc.enable() # enable auto garbage collection wlan = WLAN() # we call the constructor without params uart = UART(0, 115200) os.dupterm(uart) wlan = WLAN(mode=WLAN.STA) wlan.scan() wlan.connect(ssid='YOURWiFi', auth=(WLAN.WPA2, 'YOURWiFi_pass')) while not wlan.isconnected(): pass print(wlan.ifconfig()) ####### MQTT setup def settimeout(duration): pass mqtt = MQTTClient(client_id="wipy_client", server='192.168.0.30', port=1883) mqtt.settimeout = settimeout mqtt.connect()
from network import WLAN from mqtt import MQTTClient import machine import time def settimeout(duration): pass wlan = WLAN(mode=WLAN.STA) wlan.antenna(WLAN.EXT_ANT) wlan.connect("yourwifinetwork", auth=(WLAN.WPA2, "wifipassword"), timeout=5000) while not wlan.isconnected(): machine.idle() print("Connected to Wifi\n") client = MQTTClient("demo", "broker.hivemq.com", port=1883) client.settimeout = settimeout client.connect() while True: print("Sending ON") client.publish("/lights", "ON") time.sleep(1) print("Sending OFF") client.publish("/lights", "OFF") time.sleep(1)