0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Azure IoT Hub に paho で pub/sub

Last updated at Posted at 2021-01-15

次にあるプログラムに、.env を使って汎用性を持たせました。
Azure IoT Hub に paho で publish
Azure IoT Hub に paho で subscribe
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("*** 開始 ***\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("*** 終了 ***\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("*** 開始 ***\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("*** 終了 ***\n")
# ------------------------------------------------------------------
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?