質問編集履歴

2

発生しているエラーの内容を更新しました。

2020/01/21 02:15

投稿

raspypy
raspypy

スコア247

test CHANGED
File without changes
test CHANGED
@@ -54,15 +54,15 @@
54
54
 
55
55
  pi@raspberrypi:~/dev/aws/pub $ python alpn_mqtt.py
56
56
 
57
- 2020-01-21 09:40:36,252 - root - INFO - open ssl version:OpenSSL 1.1.1d 10 Sep 2019
57
+ 2020-01-21 11:11:30,045 - root - INFO - open ssl version:OpenSSL 1.1.1d 10 Sep 2019
58
-
58
+
59
- 2020-01-21 09:40:36,259 - root - INFO - start connect
59
+ 2020-01-21 11:11:30,051 - root - INFO - start connect
60
-
60
+
61
- 2020-01-21 09:40:36,268 - root - ERROR - exception main()
61
+ 2020-01-21 11:14:30,351 - root - ERROR - exception main()
62
-
62
+
63
- 2020-01-21 09:40:36,269 - root - ERROR - e obj:{}
63
+ 2020-01-21 11:14:30,351 - root - ERROR - e obj:{}
64
-
64
+
65
- 2020-01-21 09:40:36,269 - root - ERROR - message:
65
+ 2020-01-21 11:14:30,352 - root - ERROR - message:
66
66
 
67
67
  Traceback (most recent call last):
68
68
 
@@ -82,11 +82,11 @@
82
82
 
83
83
  return socket.create_connection(addr, source_address=source, timeout=self._keepalive)
84
84
 
85
- File "/usr/lib/python2.7/socket.py", line 557, in create_connection
85
+ File "/usr/lib/python2.7/socket.py", line 575, in create_connection
86
-
86
+
87
- for res in getaddrinfo(host, port, 0, SOCK_STREAM):
87
+ raise err
88
-
88
+
89
- gaierror: [Errno -2] Name or service not known
89
+ error: [Errno 101] Network is unreachable
90
90
 
91
91
  ```
92
92
 
@@ -114,7 +114,7 @@
114
114
 
115
115
  IoT_protocol_name = "x-amzn-mqtt-ca"
116
116
 
117
- aws_iot_endpoint = "AWS_IoT_ENDPOINT_HERE" # <random>.iot.<region>.amazonaws.com
117
+ aws_iot_endpoint = "hogehoge" # <random>.iot.<region>.amazonaws.com
118
118
 
119
119
  url = "https://{}".format(aws_iot_endpoint)
120
120
 

1

プログラムコードを追加しました。

2020/01/21 02:15

投稿

raspypy
raspypy

スコア247

test CHANGED
@@ -1 +1 @@
1
- AWS IoTでPub/Sub
1
+ AWS MQTTの実装について
test CHANGED
@@ -2,7 +2,13 @@
2
2
 
3
3
 
4
4
 
5
+ AWSに公開されている、次の記事の内容を試しています。
6
+
7
+ https://aws.amazon.com/jp/blogs/news/how-to-implement-mqtt-with-tls-client-authentication-on-port-443-from-client-devices-python/
8
+
9
+
10
+
5
- AWS IoT Device SDK for Pythonのサンプルプログラムを使って、MQTT Pub/Subのテストを行いたいです。
11
+ サンプルプログラムを使って、MQTT Publishテストを行いたいです。
6
12
 
7
13
 
8
14
 
@@ -10,35 +16,35 @@
10
16
 
11
17
 
12
18
 
19
+ ①前提条件のチェック
20
+
13
- ①AWSマネジメントコンソールから「IoTデバイスに接続」をクリック
21
+ OpenSSL 1.1.1d 10 Sep 2019
14
-
22
+
15
- ②「IoTデバイスに接続」の手順を完了
23
+ Python version 2.7.16
16
-
17
- 接続キット(Linux/Python)をダウンロード
24
+
18
-
25
+
26
+
19
- ③セキュリティ証明書作成
27
+ 証明書作成
28
+
20
-
29
+ rootCA.pem
30
+
21
-
31
+ hogehoge-certificate.pem.crt
32
+
22
-
33
+ hogehoge-private.pem.key
34
+
23
- 証明書を次とおり配置。
35
+ 証明書の有効化
24
-
25
- pi@raspberrypi:~/dev/aws/aws-iot-device-sdk-python/samples $ ls cert
36
+
26
-
27
- f2b08833e3-certificate.pem.crt f2b08833e3-private.pem.key rootCA.pem
28
-
29
- **ポリシーアタッチも行いました。**
37
+ ポリシーアタッチ
30
-
31
-
32
-
38
+
39
+
40
+
33
- ④動作確認(MQTTのPub/Sub)
41
+ ④動作確認(MQTTのPublish)
34
-
35
- 動作確認の状況は次のとおりです。
42
+
36
-
37
-
38
-
39
- サンルプログラムを動作させているのです、↓の状況からずっと変化しません
43
+ プログラムを実行すと、次エラー発生てしいます
44
+
40
-
45
+ **connect success**が表示されないので、接続エラーが発生していると思うのですが、
46
+
41
- 何が原因で、↓の状況から変化しないのかが分からず困っております。
47
+ 原因が分からず困っております。
42
48
 
43
49
 
44
50
 
@@ -46,50 +52,166 @@
46
52
 
47
53
  ```text
48
54
 
49
- pi@raspberrypi:~/dev/aws/aws-iot-device-sdk-python/samples/basicPubSub $ python basicPubSub.py --endpoint hogehoge.amazonaws.com --rootCA ../cert/rootCA.pem --cert ../cert/hoge-certificate.pem.crt --key ../cert/hoge-private.pem.key
50
-
51
- 2020-01-20 15:11:49,230 - AWSIoTPythonSDK.core.protocol.internal.clients - DEBUG - Initializing MQTT layer...
52
-
53
- 2020-01-20 15:11:49,232 - AWSIoTPythonSDK.core.protocol.internal.clients - DEBUG - Registering internal event callbacks to MQTT layer...
54
-
55
- 2020-01-20 15:11:49,233 - AWSIoTPythonSDK.core.protocol.mqtt_core - INFO - MqttCore initialized
56
-
57
- 2020-01-20 15:11:49,233 - AWSIoTPythonSDK.core.protocol.mqtt_core - INFO - Client id: basicPubSub
58
-
59
- 2020-01-20 15:11:49,234 - AWSIoTPythonSDK.core.protocol.mqtt_core - INFO - Protocol version: MQTTv3.1.1
60
-
61
- 2020-01-20 15:11:49,234 - AWSIoTPythonSDK.core.protocol.mqtt_core - INFO - Authentication type: TLSv1.2 certificate based Mutual Auth.
62
-
63
- 2020-01-20 15:11:49,235 - AWSIoTPythonSDK.core.protocol.mqtt_core - INFO - Configuring endpoint...
64
-
65
- 2020-01-20 15:11:49,235 - AWSIoTPythonSDK.core.protocol.mqtt_core - INFO - Configuring certificates...
66
-
67
- 2020-01-20 15:11:49,236 - AWSIoTPythonSDK.core.protocol.mqtt_core - INFO - Configuring reconnect back off timing...
68
-
69
- 2020-01-20 15:11:49,236 - AWSIoTPythonSDK.core.protocol.mqtt_core - INFO - Base quiet time: 1.000000 sec
70
-
71
- 2020-01-20 15:11:49,237 - AWSIoTPythonSDK.core.protocol.mqtt_core - INFO - Max quiet time: 32.000000 sec
72
-
73
- 2020-01-20 15:11:49,237 - AWSIoTPythonSDK.core.protocol.mqtt_core - INFO - Stable connection time: 20.000000 sec
74
-
75
- 2020-01-20 15:11:49,238 - AWSIoTPythonSDK.core.protocol.mqtt_core - INFO - Configuring offline requests queueing: max queue size: -1
76
-
77
- 2020-01-20 15:11:49,240 - AWSIoTPythonSDK.core.protocol.mqtt_core - INFO - Configuring offline requests queue draining interval: 0.500000 sec
78
-
79
- 2020-01-20 15:11:49,241 - AWSIoTPythonSDK.core.protocol.mqtt_core - INFO - Configuring connect/disconnect time out: 10.000000 sec
80
-
81
- 2020-01-20 15:11:49,241 - AWSIoTPythonSDK.core.protocol.mqtt_core - INFO - Configuring MQTT operation time out: 5.000000 sec
82
-
83
- 2020-01-20 15:11:49,242 - AWSIoTPythonSDK.core.protocol.mqtt_core - INFO - Performing sync connect...
84
-
85
- 2020-01-20 15:11:49,243 - AWSIoTPythonSDK.core.protocol.mqtt_core - INFO - Performing async connect...
86
-
87
- 2020-01-20 15:11:49,243 - AWSIoTPythonSDK.core.protocol.mqtt_core - INFO - Keep-alive: 600.000000 sec
88
-
89
- 2020-01-20 15:11:49,247 - AWSIoTPythonSDK.core.protocol.internal.workers - DEBUG - Event consuming thread started
90
-
91
- 2020-01-20 15:11:49,248 - AWSIoTPythonSDK.core.protocol.mqtt_core - DEBUG - Passing in general notification callbacks to internal client...
92
-
93
- 2020-01-20 15:11:49,248 - AWSIoTPythonSDK.core.protocol.internal.clients - DEBUG - Filling in fixed event callbacks: CONNACK, DISCONNECT, MESSAGE
55
+ pi@raspberrypi:~/dev/aws/pub $ python alpn_mqtt.py
56
+
57
+ 2020-01-21 09:40:36,252 - root - INFO - open ssl version:OpenSSL 1.1.1d 10 Sep 2019
58
+
59
+ 2020-01-21 09:40:36,259 - root - INFO - start connect
60
+
61
+ 2020-01-21 09:40:36,268 - root - ERROR - exception main()
62
+
63
+ 2020-01-21 09:40:36,269 - root - ERROR - e obj:{}
64
+
65
+ 2020-01-21 09:40:36,269 - root - ERROR - message:
66
+
67
+ Traceback (most recent call last):
68
+
69
+ File "alpn_mqtt.py", line 45, in <module>
70
+
71
+ mqttc.connect(aws_iot_endpoint, port=443)
72
+
73
+ File "/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py", line 937, in connect
74
+
75
+ return self.reconnect()
76
+
77
+ File "/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py", line 1071, in reconnect
78
+
79
+ sock = self._create_socket_connection()
80
+
81
+ File "/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py", line 3522, in _create_socket_connection
82
+
83
+ return socket.create_connection(addr, source_address=source, timeout=self._keepalive)
84
+
85
+ File "/usr/lib/python2.7/socket.py", line 557, in create_connection
86
+
87
+ for res in getaddrinfo(host, port, 0, SOCK_STREAM):
88
+
89
+ gaierror: [Errno -2] Name or service not known
94
90
 
95
91
  ```
92
+
93
+
94
+
95
+ ##プログラムコード
96
+
97
+ ```python
98
+
99
+ from __future__ import print_function
100
+
101
+ import sys
102
+
103
+ import ssl
104
+
105
+ import time
106
+
107
+ import datetime
108
+
109
+ import logging, traceback
110
+
111
+ import paho.mqtt.client as mqtt
112
+
113
+
114
+
115
+ IoT_protocol_name = "x-amzn-mqtt-ca"
116
+
117
+ aws_iot_endpoint = "AWS_IoT_ENDPOINT_HERE" # <random>.iot.<region>.amazonaws.com
118
+
119
+ url = "https://{}".format(aws_iot_endpoint)
120
+
121
+
122
+
123
+ ca = "YOUR/ROOT/CA/PATH"
124
+
125
+ cert = "YOUR/DEVICE/CERT/PATH"
126
+
127
+ private = "YOUR/DEVICE/KEY/PATH"
128
+
129
+
130
+
131
+ logger = logging.getLogger()
132
+
133
+ logger.setLevel(logging.DEBUG)
134
+
135
+ handler = logging.StreamHandler(sys.stdout)
136
+
137
+ log_format = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
138
+
139
+ handler.setFormatter(log_format)
140
+
141
+ logger.addHandler(handler)
142
+
143
+
144
+
145
+ def ssl_alpn():
146
+
147
+ try:
148
+
149
+ #debug print opnessl version
150
+
151
+ logger.info("open ssl version:{}".format(ssl.OPENSSL_VERSION))
152
+
153
+ ssl_context = ssl.create_default_context()
154
+
155
+ ssl_context.set_alpn_protocols([IoT_protocol_name])
156
+
157
+ ssl_context.load_verify_locations(cafile=ca)
158
+
159
+ ssl_context.load_cert_chain(certfile=cert, keyfile=private)
160
+
161
+
162
+
163
+ return ssl_context
164
+
165
+ except Exception as e:
166
+
167
+ print("exception ssl_alpn()")
168
+
169
+ raise e
170
+
171
+
172
+
173
+ if __name__ == '__main__':
174
+
175
+ topic = "test/date"
176
+
177
+ try:
178
+
179
+ mqttc = mqtt.Client()
180
+
181
+ ssl_context= ssl_alpn()
182
+
183
+ mqttc.tls_set_context(context=ssl_context)
184
+
185
+ logger.info("start connect")
186
+
187
+ mqttc.connect(aws_iot_endpoint, port=443)
188
+
189
+ logger.info("connect success")
190
+
191
+ mqttc.loop_start()
192
+
193
+
194
+
195
+ while True:
196
+
197
+ now = datetime.datetime.now().strftime('%Y-%m-%dT%H:%M:%S')
198
+
199
+ logger.info("try to publish:{}".format(now))
200
+
201
+ mqttc.publish(topic, now)
202
+
203
+ time.sleep(1)
204
+
205
+
206
+
207
+ except Exception as e:
208
+
209
+ logger.error("exception main()")
210
+
211
+ logger.error("e obj:{}".format(vars(e)))
212
+
213
+ logger.error("message:{}".format(e.message))
214
+
215
+ traceback.print_exc(file=sys.stdout)
216
+
217
+ ```