def publish(client, userdata, topic, pid): # This method is called when the client publishes data to a topic. print('Published to {0} with PID {1}'.format(topic, pid)) def message(client, topic, msg): # This method is called when the client receives data from a topic. print("Message from {}: {}".format(topic, msg)) # Set up a new MiniMQTT Client client = MQTT.MQTT(broker=secrets['broker'], client_id=secrets['client_id']) # Initialize AWS IoT MQTT API Client aws_iot = MQTT_CLIENT(client) # Connect callback handlers to AWS IoT MQTT Client aws_iot.on_connect = connect aws_iot.on_disconnect = disconnect aws_iot.on_subscribe = subscribe aws_iot.on_unsubscribe = unsubscribe aws_iot.on_publish = publish aws_iot.on_message = message print('Attempting to connect to %s' % client.broker) aws_iot.connect() # Time in seconds since power on initial = time.monotonic()
# Set AWS RSA Private Key esp.set_private_key(DEVICE_KEY) # Connect to WiFi print("Connecting to WiFi...") wifi.connect() print("Connected!") # Initialize MQTT interface with the esp interface MQTT.set_socket(socket, esp) # Set up a new MiniMQTT Client client = MQTT.MQTT(broker=secrets["broker"], client_id=secrets["client_id"]) # Initialize AWS IoT MQTT API Client aws_iot = MQTT_CLIENT(client) # Connect callback handlers to AWS IoT MQTT Client aws_iot.on_connect = connect aws_iot.on_disconnect = disconnect aws_iot.on_subscribe = subscribe aws_iot.on_unsubscribe = unsubscribe aws_iot.on_publish = publish aws_iot.on_message = message print("Attempting to connect to %s" % client.broker) aws_iot.connect() # Pump the message loop forever, all events # are handled in their callback handlers # while True: