Pub/sub on paho on Azure IoT Hub

I used .env to make the following program versatile. Publish to Azure IoT Hub with paho (https://qiita.com/ekzemplaro/items/8a2f8ac7bae35a9c91d4) Subscribe to Azure IoT Hub with paho There are no changes to generate_sas_token.py.

.env


IOTHUB='iot-aa'
DEVICE='pansy'
MAIN_KEY='2eQ2wF5OjZLP7tq6PG123456789RTfAnMJbZ8wtJPu4='

paho_publish.py


#!/usr/bin/python
# -*- coding: utf-8 -*-
#
#	paho_publish.py
#
#						Jan/15/2021
# ------------------------------------------------------------------
import	sys
import	os
from dotenv import load_dotenv
import	ssl
import	paho.mqtt.publish as publish
import	paho.mqtt.client as mqtt

from generate_sas_token import generate_sas_token
# ------------------------------------------------------------------
sys.stderr.write("***start***\n")
dotenv_path = '.env'
load_dotenv(dotenv_path)
IOTHUB = os.environ.get("IOTHUB")
DEVICE = os.environ.get("DEVICE")
MAIN_KEY = os.environ.get("MAIN_KEY")

sys.stderr.write(IOTHUB + "\n")
sys.stderr.write(DEVICE + "\n")
sys.stderr.write(MAIN_KEY + "\n")

uri = IOTHUB + ".azure-devices.net/devices/" + DEVICE 
#
key = MAIN_KEY
policy_name = None
iexpire = 3600
sas_token = generate_sas_token(uri, key, policy_name, iexpire)
username =  IOTHUB + ".azure-devices.net/" + DEVICE + "/api-version=2020-03-13"
sys.stderr.write(username + "\n")
auth = {
	'username': username,
	'password': sas_token
	}

tls = {
	'ca_certs': "/etc/ssl/certs/ca-certificates.crt",
	'tls_version': ssl.PROTOCOL_TLSv1_2
	}
#
msg="Hello from Uchida Jan/15/2021 PM 15:36"
try:
#	publish.single("devices/pansy/messages/events/",
	publish.single("devices/" + DEVICE + "/messages/events/",
		payload=msg,
		hostname= IOTHUB + ".azure-devices.net",
		client_id=DEVICE,
		auth=auth,
		tls=tls,
		port=8883,
		protocol=mqtt.MQTTv311)
except Exception as ee:
	sys.stderr.write("*** error *** in publish.single ***\n")
	sys.stderr.write(str(ee) + "\n")
#
sys.stderr.write("***End***\n")
# ------------------------------------------------------------------

azure_subscribe.py


#! /usr/bin/python
# -*- coding: utf-8 -*-
#
#	azure_subscribe.py
#
#						Jan/15/2021
#
# ------------------------------------------------------------------
import  sys
import	os
from dotenv import load_dotenv
import  ssl

from time import sleep
import paho.mqtt.client as mqtt

from generate_sas_token import generate_sas_token
# ------------------------------------------------------------------
sys.stderr.write("***start***\n")

dotenv_path = '.env'
load_dotenv(dotenv_path)
IOTHUB = os.environ.get("IOTHUB")
DEVICE = os.environ.get("DEVICE")
MAIN_KEY = os.environ.get("MAIN_KEY")

sys.stderr.write(IOTHUB + "\n")
sys.stderr.write(DEVICE + "\n")
sys.stderr.write(MAIN_KEY + "\n")

uri = IOTHUB + ".azure-devices.net/devices/" + DEVICE
#
key = MAIN_KEY

policy_name = None
iexpire = 3600
sas_token = generate_sas_token(uri, key, policy_name, iexpire)
#
path_to_root_cert = "/etc/ssl/certs/ca-certificates.crt"

host = IOTHUB + '.azure-devices.net'
port = 8883
topic = 'devices/' + DEVICE + '/messages/devicebound/#'

sys.stderr.write(uri + "\n")
sys.stderr.write(host + "\n")
sys.stderr.write(topic + "\n")
# ------------------------------------------------------------------
def on_connect(client, userdata, flags, respons_code):
	sys.stderr.write("***on_connect ***\n")
	print('status {0}'.format(respons_code))
	client.subscribe(topic)

# ------------------------------------------------------------------
def on_message(client, userdata, msg):
	sys.stderr.write("***on_message ***\n")
	print(msg.topic + ' ' + str(msg.payload,'utf-8'))

# ------------------------------------------------------------------
client = mqtt.Client(client_id=DEVICE,protocol=mqtt.MQTTv311)
username =  IOTHUB + ".azure-devices.net/" + DEVICE + "/api-version=2020-03-13"
# username =  IOTHUB + ".azure-devices.net/" + DEVICE
sys.stderr.write(username + "\n")
client.username_pw_set(username=username, password=sas_token)


client.tls_set(ca_certs=path_to_root_cert, certfile=None,
	keyfile=None, cert_reqs=ssl.CERT_REQUIRED,
	tls_version=ssl.PROTOCOL_TLSv1_2, ciphers=None)

client.tls_insecure_set(False)

client.on_connect = on_connect
client.on_message = on_message

try:
	client.connect(host, port=port, keepalive=60)
	client.loop_forever()
except Exception as ee:
	sys.stderr.write("*** error *** in client.connect ***\n")
	sys.stderr.write(str(ee) + "\n")
#
sys.stderr.write("***End***\n")
# ------------------------------------------------------------------

Recommended Posts

Pub/sub on paho on Azure IoT Hub
Pub/sub (CA certificate) with paho on Azure IoT Hub
[Note] The story of setting up the SDK for Python of Azure IoT Hub on Linux
How to create a SAS token for Azure IoT Hub
Rebuild kernel on Ubuntu 20.04 (on Azure)