Skip to content

Instantly share code, notes, and snippets.

@KaSroka
Created August 20, 2021 21:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KaSroka/7266cfb5ed1363c1114718aade9674a2 to your computer and use it in GitHub Desktop.
Save KaSroka/7266cfb5ed1363c1114718aade9674a2 to your computer and use it in GitHub Desktop.
Toshiba Home AC AMQP connection
import os
import asyncio
from six.moves import input
import threading
from azure.iot.device.aio import IoTHubDeviceClient
HOST_NAME = 'toshibasmaciothubprod.azure-devices.net'
DEVICE_ID = '<YOUR_DEVICE_ID>'
SHARED_ACCESS_KEY = '<YOUR_SHARED_ACCESS_KEY>'
CONNECTION_STRING = f'HostName={HOST_NAME};DeviceId={DEVICE_ID};SharedAccessKey={SHARED_ACCESS_KEY}'
async def main():
conn_str = CONNECTION_STRING
# The client object is used to interact with your Azure IoT hub.
device_client = IoTHubDeviceClient.create_from_connection_string(conn_str)
# connect the client.
await device_client.connect()
print('Connected!!!')
def method_received(method):
print("Method handler")
print(method.name)
print(method.request_id)
print(method.payload)
device_client.on_method_request_received = method_received
# define behavior for halting the application
def stdin_listener():
while True:
selection = input("Press Q to quit\n")
if selection == "Q" or selection == "q":
print("Quitting...")
break
# Run the stdin listener in the event loop
loop = asyncio.get_running_loop()
user_finished = loop.run_in_executor(None, stdin_listener)
# Wait for user to indicate they are done listening for messages
await user_finished
# Finally, shut down the client
await device_client.shutdown()
if __name__ == "__main__":
asyncio.run(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment