in aws-iot-core-raspberrypi-gpio/main.py [0:0]
def main():
event_loop_group = io.EventLoopGroup(1)
host_resolver = io.DefaultHostResolver(event_loop_group)
client_bootstrap = io.ClientBootstrap(event_loop_group, host_resolver)
mqtt_connection = mqtt_connection_builder.mtls_from_path(
endpoint="xxxxxxxxxxx-ats.iot.region.amazonaws.com",
cert_filepath="xxxxxxxxxx-certificate.pem.crt",
pri_key_filepath="xxxxxxxxxx-private.pem.key",
ca_filepath="AmazonRootCA1.pem",
client_id="raspi_switch",
client_bootstrap=client_bootstrap,
)
# Connect to AWS IoT
connect_future = mqtt_connection.connect()
connect_future.result()
print("Connected!")
# Read the switch state and publish a message
last_state = False
while True:
switch_pressed = read_switch()
if switch_pressed != last_state:
print(f"pressed: {switch_pressed}")
msg = {"pressed": switch_pressed}
mqtt_connection.publish(
topic="data/raspi_switch",
payload=json.dumps(msg),
qos=mqtt.QoS.AT_LEAST_ONCE)
last_state = switch_pressed
time.sleep(0.01)