質問編集履歴
2
発生しているエラーの内容を更新しました。
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
|
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
|
59
|
+
2020-01-21 11:11:30,051 - root - INFO - start connect
|
60
|
-
|
60
|
+
|
61
|
-
2020-01-21
|
61
|
+
2020-01-21 11:14:30,351 - root - ERROR - exception main()
|
62
|
-
|
62
|
+
|
63
|
-
2020-01-21
|
63
|
+
2020-01-21 11:14:30,351 - root - ERROR - e obj:{}
|
64
|
-
|
64
|
+
|
65
|
-
2020-01-21
|
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 55
|
85
|
+
File "/usr/lib/python2.7/socket.py", line 575, in create_connection
|
86
|
-
|
86
|
+
|
87
|
-
|
87
|
+
raise err
|
88
|
-
|
88
|
+
|
89
|
-
|
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 = "
|
117
|
+
aws_iot_endpoint = "hogehoge" # <random>.iot.<region>.amazonaws.com
|
118
118
|
|
119
119
|
url = "https://{}".format(aws_iot_endpoint)
|
120
120
|
|
1
プログラムコードを追加しました。
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
AWS
|
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
|
-
|
11
|
+
サンプルプログラムを使って、MQTT Publishテストを行いたいです。
|
6
12
|
|
7
13
|
|
8
14
|
|
@@ -10,35 +16,35 @@
|
|
10
16
|
|
11
17
|
|
12
18
|
|
19
|
+
①前提条件のチェック
|
20
|
+
|
13
|
-
|
21
|
+
OpenSSL 1.1.1d 10 Sep 2019
|
14
|
-
|
22
|
+
|
15
|
-
|
23
|
+
Python version 2.7.16
|
16
|
-
|
17
|
-
|
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
|
-
|
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
|
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/
|
50
|
-
|
51
|
-
2020-01-20
|
52
|
-
|
53
|
-
2020-01-20
|
54
|
-
|
55
|
-
2020-01-20
|
56
|
-
|
57
|
-
2020-01-20
|
58
|
-
|
59
|
-
2020-01-20
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
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
|
+
```
|