Example #1
0
class MQTTDevice:
    def __init__(self,
                 topic_online,
                 true_message="true",
                 false_message="false"):
        self.true_message = true_message
        self.false_message = false_message
        self.client = MQTTClient(MQTT_CLIENT_ID_RANDOM,
                                 MQTT_BROKER_ADDRESS,
                                 port=MQTT_PORT,
                                 keepalive=KEEP_ALIVE_TIME_SEC)
        self.next_scheduled_ping_time = 0
        self._client_setup(topic_online)

    def _client_setup(self, topic_online):
        self.client.set_last_will(topic_online, self.false_message)
        self.client.connect()
        self.client.publish(topic_online, self.true_message)
        self.setup_subscriptions()

    # override this method in subclass
    # set callback and subscriptions
    def setup_subscriptions(self):
        pass

    def _ping(self):
        if (self.next_scheduled_ping_time < utime.time()):
            self.client.ping()
            self.next_scheduled_ping_time = utime.time() + PING_EVERY_SEC

    #run method has to be called every execution cycle
    def run(self):
        self._ping()
        self.client.check_msg()
class IotConnection:
    def __init__(self, client_id, server):
        self.client_id = client_id
        self.client = MQTTClient(client_id, server, port=1883, keepalive=60)
        self.client.set_callback(sub_cb)
        self.client.set_last_will('backend/1/1/status/heartbeat',
                                  'down',
                                  retain=False,
                                  qos=0)
        self.client.connect()
        self.client.publish(topic='backend/1/1/status/heartbeat',
                            msg='up',
                            qos=1)

    def send_temperature(self, topic, value, unique_id):
        data = {
            "temperature": {
                "value": str(value),
                "unique_id": str(unique_id)
            }
        }
        json_data = json.dumps(data)
        print(topic, json_data)
        self.client.publish(topic=topic, msg=json_data)

    def send_humidity(self, topic, value, unique_id):
        data = {"humidity": {"value": str(value), "unique_id": str(unique_id)}}
        json_data = json.dumps(data)
        print(topic, json_data)
        self.client.publish(topic=topic, msg=json_data)
Example #3
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)
Example #4
0

def button_cb(pin):
    global button_changed
    button_changed = True


pycom.heartbeat(False)

client = MQTTClient(id,
                    "io.adafruit.com",
                    user="******",
                    password="******",
                    port=1883,
                    keepalive=keepalive)
client.set_last_will(LOG_TOPIC, id + b' is down')
client.set_callback(sub_cb)
client.connect()
client.subscribe(topic=LED_STATE_TOPIC)

button = Pin('P14', mode=Pin.IN, pull=None)
button.callback(trigger=Pin.IRQ_FALLING | Pin.IRQ_RISING, handler=button_cb)

client.publish(topic=LOG_TOPIC, msg=id + b' is up')

last_message = time.time()

while True:
    client.check_msg()

    if button_changed == True:
Example #5
0
    wdt = WDT()
else:
    wdt = WDT(timeout=10000)
wdt.feed()

rts = Pin(RTS, mode=Pin.OUT)
rts.value(0)

debug("Reading config", 1)
read_config()

debug("Connecting to MQTT", 1)
try:
    mqtt_client = MQTTClient(bytes2hex(machine.unique_id()), MQTT_SERVER, keepalive=30)
    mqtt_client.set_callback(sub_cb)
    mqtt_client.set_last_will(topic=TOPIC_MASTER, msg="offline")
    mqtt_client.connect()
    mqtt_client.subscribe(topic=TOPIC % 'rate')
    mqtt_client.publish(topic=TOPIC_MASTER, msg="online")
except:
    debug('unable to connect to mqtt, reseting to reconnect to wifi', 1)
    machine.reset()

# Announce ourselves as the master and give slaves a chance to
# change id before talking to us

debug("Announcing master to slaves", 1)
for _ in range(0, 5):
    send_master_linkready1()
    time.sleep(.1)