print('New message on topic {0}: {1}'.format(topic, message)) # Connect to WiFi wifi.connect() # Set up a MiniMQTT Client mqtt_client = MQTT(socket, broker = secrets['broker'], username = secrets['user'], password = secrets['pass'], network_manager = wifi) # Setup the callback methods above mqtt_client.on_connect = connected mqtt_client.on_disconnect = disconnected mqtt_client.on_message = message # Connect the client to the MQTT broker. mqtt_client.connect() photocell_val = 0 while True: # Poll the message queue mqtt_client.loop() # Send a new message print('Sending photocell value: %d'%photocell_val) mqtt_client.publish(default_topic, photocell_val) photocell_val += 1 time.sleep(0.5)
wifi.connect() # Set up a MiniMQTT Client client = MQTT(socket, broker = secrets['broker'], port = 1883, username = secrets['user'], password = secrets['pass'], network_manager = wifi) # Connect callback handlers to client client.on_connect = connect client.on_disconnect = disconnected client.on_subscribe = subscribe client.on_publish = publish client.on_message = message print('Attempting to connect to %s' % client.broker) client.connect() print('Subscribing to %s, %s, %s, and %s' % (mqtt_feed1, mqtt_feed2, mqtt_button1, mqtt_button2)) client.subscribe(mqtt_feed1) client.subscribe(mqtt_feed2) client.subscribe(mqtt_button1) client.subscribe(mqtt_button2) # ------------- Code Loop ------------- # while True: # Poll the message queue client.loop()