{"id":18393,"date":"2020-04-22T18:42:32","date_gmt":"2020-04-22T09:42:32","guid":{"rendered":"https:\/\/www.skyarch.net\/blog\/?p=18393"},"modified":"2020-05-22T17:10:25","modified_gmt":"2020-05-22T08:10:25","slug":"m5stick-with-env-hat%e3%81%a7aws-iot%e3%81%abpublish%e3%81%97cloudwatch-logs-insights%e3%81%a7%e5%8f%af%e8%a6%96%e5%8c%96","status":"publish","type":"post","link":"https:\/\/www.skyarch.net\/blog\/m5stick-with-env-hat%e3%81%a7aws-iot%e3%81%abpublish%e3%81%97cloudwatch-logs-insights%e3%81%a7%e5%8f%af%e8%a6%96%e5%8c%96\/","title":{"rendered":"M5StickC with ENV Hat\u3067AWS IoT\u306bPublish\u3057CloudWatch Logs Insights\u3067\u53ef\u8996\u5316"},"content":{"rendered":"<h1>\u306f\u3058\u3081\u306b<\/h1>\n<p>\u524d\u56de\u306e\u7d9a\u304d\u3068\u306a\u308a\u307e\u3059\u3002<br \/>\n<a href=\"https:\/\/www.skyarch.net\/blog\/?p=18370\">https:\/\/www.skyarch.net\/blog\/?p=18370<\/a><\/p>\n<p>\u3088\u304f\u3042\u308b\u8a71\u3067\u3001\u6e29\u5ea6\u3001\u6e7f\u5ea6\u3001\u6c17\u5727\u3092AWS IoT\u306b\u6295\u3052\u53ef\u8996\u5316\u3059\u308b\u306e\u3067\u3059\u304c<br \/>\n\u6700\u8fd1\u9ad8\u983b\u5ea6\u3067Update\u3055\u308c\u3001\u4fbf\u5229\u306b\u306a\u3063\u3066\u5927\u597d\u304d\u306aCloudWatch Logs Insights\u3092\u4f7f\u3063\u3066\u307f\u307e\u3059\u3002<\/p>\n<h2>\u51fa\u6765\u305f\u3082\u306e<\/h2>\n<img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.skyarch.net\/blog\/wp-content\/uploads\/2020\/04\/M5StickPublishtoAWSIoT.gif\" alt=\"\" width=\"360\" height=\"240\" class=\"aligncenter size-full wp-image-18395\" \/>\n<p>\u96fb\u6e90\u3092\u5165\u308c\u308b\u3068\u3001Wifi\u63a5\u7d9a \u21e2 AWS IoT\u3078\u63a5\u7d9a \u21e2 \u30bb\u30f3\u30b5\u5024\u3092Publish\u3068\u9032\u307f\u3001AWS IoT\u306e\u30c6\u30b9\u30c8\u753b\u9762\u306b\u3066\u53d7\u4fe1\u3067\u304d\u305f\u4e8b\u3092\u78ba\u8a8d\u3067\u304d\u308b\u3068\u3044\u3046\u52d5\u753b\u3067\u3059\u3002<\/p>\n<ul>\n<li>\u6e29\u5ea6\u30fb\u6e7f\u5ea6\u30fb\u6c17\u5727\u3092\u753b\u9762\u5185\u306b\u8868\u793a<\/li>\n<li>\u6e29\u5ea6\u30fb\u6e7f\u5ea6\u30fb\u6c17\u5727\u3092AWS IoT\u306bPublish<\/li>\n<li>A\u30dc\u30bf\u30f3\u3092\u62bc\u4e0b\u3059\u308b\u3068 (\u96e2\u3057\u305f\u969b\u306b\uff09LED\u72b6\u614b\u304c\u5909\u5316\u3057\u307e\u3059\u3002 (\u5c06\u6765\u7528\u306b\u524d\u56de\u306e\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9\u305d\u306e\u307e\u307e)<\/li>\n<\/ul>\n<h3>\u53ef\u8996\u5316<\/h3>\n<img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.skyarch.net\/blog\/wp-content\/uploads\/2020\/04\/CloudWatch-Insights-Visualization-921x1024.png\" alt=\"\" width=\"728\" height=\"809\" class=\"aligncenter size-large wp-image-18400\" srcset=\"https:\/\/www.skyarch.net\/blog\/wp-content\/uploads\/2020\/04\/CloudWatch-Insights-Visualization-921x1024.png 921w, https:\/\/www.skyarch.net\/blog\/wp-content\/uploads\/2020\/04\/CloudWatch-Insights-Visualization-270x300.png 270w, https:\/\/www.skyarch.net\/blog\/wp-content\/uploads\/2020\/04\/CloudWatch-Insights-Visualization-768x854.png 768w, https:\/\/www.skyarch.net\/blog\/wp-content\/uploads\/2020\/04\/CloudWatch-Insights-Visualization-728x810.png 728w, https:\/\/www.skyarch.net\/blog\/wp-content\/uploads\/2020\/04\/CloudWatch-Insights-Visualization.png 1024w\" sizes=\"auto, (max-width: 728px) 100vw, 728px\" \/>\n<p>CloudWatch Logs Insights\u3092\u5229\u7528\u3057\u7c21\u6613\u7684\u306b\u53ef\u8996\u5316\u3057\u3066\u3044\u307e\u3059\u3002<br \/>\n\u6e29\u5ea6\u3001\u6e7f\u5ea6\u306e1\u5206\u6bce\u5e73\u5747\u5024\u3092\u30d7\u30ed\u30c3\u30c8<\/p>\n<h2>\u76ee\u6b21<\/h2>\n<ul>\n<li><a href=\"#overview\">\u624b\u9806\u306e\u6982\u8981<\/a><\/li>\n<li><a href=\"#env\">\u5b9f\u884c\u74b0\u5883<\/a><\/li>\n<li><a href=\"#tejun\">\u624b\u9806<\/a><\/li>\n<li><a href=\"#matome\">\u306f\u307e\u3063\u305f\u70b9<\/a><\/li>\n<\/ul>\n<h2 id=\"overview\">\u624b\u9806\u306e\u6982\u8981<\/h2>\n<ol>\n<li>AWS IoT\u3067 \u30e2\u30ce\/\u8a3c\u660e\u66f8\/\u30dd\u30ea\u30b7\u30fc\u3092\u4f5c\u6210\u3057\u9069\u5207\u306a\u95a2\u9023\u306bAttach (AWS CLI\u306b\u3066\u5b9f\u65bd)<\/li>\n<li>\u30b3\u30fc\u30c9\u66f8\u304f\/\u8ee2\u9001<\/li>\n<li>AWS IoT\u3067 \u30c7\u30fc\u30bf\u53d7\u4fe1\u78ba\u8a8d<\/li>\n<li>AWS IoT\u306e\u30eb\u30fc\u30eb\u3067CloudWatch Logs\u3078\u66f8\u304d\u51fa\u3057<\/li>\n<li>CloudWatch Logs Insights\u3067\u30af\u30a8\u30ea\u3092\u66f8\u304d\u53ef\u8996\u5316<\/li>\n<\/ol>\n<h3>M5StickC\u3067\u5229\u7528\u3059\u308b\u30e9\u30a4\u30d6\u30e9\u30ea\u7b49<\/h3>\n<p>\u4ed6\u306bMQTT\u901a\u4fe1\u3067\u304d\u308b\u30e9\u30a4\u30d6\u30e9\u30ea\u306f\u7121\u3044\u3082\u306e\u304b\u3068<br \/>\nAruduino IDE\u306e\u30e9\u30a4\u30d6\u30e9\u30ea\u30de\u30cd\u30fc\u30b8\u30e3\u30fc\u304b\u3089\u63a2\u3057\u3066\u307f\u307e\u3057\u305f\u304c<br \/>\nBeta\u3060\u3063\u305f\u308a\u3001\u3042\u307e\u308a\u4eba\u6c17\u304c\u7121\u304b\u3063\u305f\u308a\u3068\u3044\u3046\u4e8b\u3067\u7d50\u5c40\u4eba\u6c17\u304c\u4e00\u756a\u3042\u308b\u4e0b\u8a18\u30e9\u30a4\u30d6\u30e9\u30ea\u3092\u5229\u7528\u3057\u307e\u3057\u305f\u3002<\/p>\n<p>\u5236\u9650\u4e8b\u9805\u3068\u3057\u3066\u3001Publish\u306fQoS 0\u306e\u307f\u3001Subscribe QoS 0 or 1\u3001\u30e1\u30c3\u30bb\u30fc\u30b8\u30b5\u30a4\u30ba\u304c\u6a19\u6e96\u3067\u306f\u3068\u3066\u3082\u5c0f\u3055\u3044(128Byte)\u7b49\u8272\u3005\u3042\u308a\u307e\u3059\u3002<\/p>\n<p>PubSubClient<br \/>\n<a href=\"https:\/\/pubsubclient.knolleary.net\/\">https:\/\/pubsubclient.knolleary.net\/<\/a><\/p>\n<p>Github\u30da\u30fc\u30b8 - \u5236\u9650\u4e8b\u9805\u304c\u8272\u3005\u3068\u8a18\u8f09\u3055\u308c\u3066\u3044\u307e\u3059<br \/>\n<a href=\"https:\/\/github.com\/knolleary\/pubsubclient\">https:\/\/github.com\/knolleary\/pubsubclient<\/a><\/p>\n<h4>\u53c2\u8003\u306b\u3055\u305b\u3066\u9802\u3044\u305f\u8a18\u4e8b<\/h4>\n<p>ESP32\u3067AWS IoT\u306b\u7e4b\u3044\u3067Thing Shadow\u3092\u5f04\u308b<br \/>\n<a href=\"https:\/\/blog.maripo.org\/2017\/07\/esp32-aws-iot\/\">https:\/\/blog.maripo.org\/2017\/07\/esp32-aws-iot\/<\/a><\/p>\n<p>ESP32\u301cAWS IoT\u3067MQTT\u901a\u4fe1\u3057\u3066\u8a70\u3093\u3060\u8a71\u3010ClientID\u3011<br \/>\n<a href=\"https:\/\/qiita.com\/rockguitar67\/items\/4f028500d520dbf14be1\">https:\/\/qiita.com\/rockguitar67\/items\/4f028500d520dbf14be1<\/a><\/p>\n<p>\u3010macOS\u3011\u30c7\u30d0\u30a4\u30b9\u3068AWS IoT\u3068\u306e\u63a5\u7d9a\u8a2d\u5b9a\u3092\u884c\u3046\u30b9\u30af\u30ea\u30d7\u30c8\u3092\u304b\u3044\u305f\u3063\u305f\uff1a\u8a3c\u660e\u66f8\u306e\u8a2d\u5b9a\u304b\u3089Thing\u767b\u9332\u307e\u3067\u884c\u3046<br \/>\n<a href=\"https:\/\/dev.classmethod.jp\/articles\/aws-iot-connect-device-script\/\">https:\/\/dev.classmethod.jp\/articles\/aws-iot-connect-device-script\/<\/a><\/p>\n<h2 id=\"env\">\u5b9f\u884c\u74b0\u5883<\/h2>\n<p>MacOS\u4e0a\u3067\u5b9f\u65bd\u3057\u307e\u3057\u305f\u3002<\/p>\n<h2 id=\"tejun\">\u624b\u9806<\/h2>\n<h3>1. AWS IoT\u3067 \u30e2\u30ce\/\u8a3c\u660e\u66f8\/\u30dd\u30ea\u30b7\u30fc\u3092\u4f5c\u6210\u3057\u9069\u5207\u306a\u95a2\u9023\u306bAttach (AWS CLI\u306b\u3066\u5b9f\u65bd)<\/h3>\n<p>\u6ce8\u8a18<\/p>\n<ul>\n<li>\u4e0b\u8a18\u30b3\u30de\u30f3\u30c9\u3092\u6d41\u3057\u3066\u884c\u304f\u3068\u3001\u30ab\u30ec\u30f3\u30c8\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u306bAWS IoT\u306b\u3066\u4f5c\u6210\u3055\u308c\u305f\u8a3c\u660e\u66f8\/\u9375\u30d5\u30a1\u30a4\u30eb\u304c\u4fdd\u5b58\u3055\u308c\u307e\u3059\u3002<\/li>\n<li>\u30dd\u30ea\u30b7\u30fc\u306f\u5168\u8a31\u53ef\u306e\u30c6\u30b9\u30c8\u7528\u306e\u3082\u306e\u306e\u305f\u3081\u3001\u3054\u5229\u7528\u306f\u8a08\u753b\u7684\u306b<\/li>\n<\/ul>\n<h4>\u30e2\u30ce\u3092\u4f5c\u6210<\/h4>\n<pre>\nDEVICE_NAME=\"M5Stick01\"\naws iot create-thing --thing-name ${DEVICE_NAME}\n<\/pre>\n<h4>\u30dd\u30ea\u30b7\u30fc\u3092\u4f5c\u6210 (\u30c6\u30b9\u30c8\u7528\u5168\u8a31\u53ef\u30dd\u30ea\u30b7\u30fc\u306e\u305f\u3081\u8981\u6ce8\u610f)<\/h4>\n<pre>\naws iot create-policy --policy-name ${DEVICE_NAME}-policy --policy-document \\\n'{ \"Version\": \"2012-10-17\", \"Statement\": [{ \"Effect\": \"Allow\", \"Action\": \"iot:*\", \"Resource\": \"*\" }]}'\n<\/pre>\n<h4>\u8a3c\u660e\u66f8\u3068\u9375\u3092\u4f5c\u6210<\/h4>\n<pre>\naws iot create-keys-and-certificate --set-as-active \\\n--certificate-pem-outfile ${DEVICE_NAME}-certificate.pem.crt \\\n--private-key-outfile ${DEVICE_NAME}-private.pem.key \\\n--public-key-outfile ${DEVICE_NAME}-public.pem.key \\\n--query \"certificateArn\"\n<\/pre>\n<p><strong>\u3053\u3061\u3089\u306e\u7d50\u679c(ARN\u8868\u8a18\u3067\u51fa\u529b\u3055\u308c\u308b\u8a3c\u660e\u66f8)\u3092\u4e0b\u8a18\u30b3\u30de\u30f3\u30c9(Policy\u3068\u8a3c\u660e\u66f8\u3092\u7d10\u4ed8\u3051\/\u30e2\u30ce\u3068\u8a3c\u660e\u66f8\u3092\u7d10\u4ed8\u3051)\u306b\u3066\u5229\u7528\u3059\u308b<\/strong><\/p>\n<h4>Policy\u3068\u8a3c\u660e\u66f8\u3092\u7d10\u4ed8\u3051<\/h4>\n<pre>\naws iot attach-principal-policy --policy-name ${DEVICE_NAME}-policy --principal \"\u4e0a\u8a18\u30b3\u30de\u30f3\u30c9\u5b9f\u884c\u7d50\u679c\u3092\u30da\u30fc\u30b9\u30c8arn:aws:iot:...\"\n<\/pre>\n<h4>\u30e2\u30ce\u3068\u8a3c\u660e\u66f8\u3092\u7d10\u4ed8\u3051<\/h4>\n<pre>\naws iot attach-thing-principal  --thing-name ${DEVICE_NAME} --principal \"\u4e0a\u8a18\u30b3\u30de\u30f3\u30c9\u5b9f\u884c\u7d50\u679c\u3092\u30da\u30fc\u30b9\u30c8arn:aws:iot:...\"\n<\/pre>\n<h4>AWS IoT \u30a8\u30f3\u30c9\u30dd\u30a4\u30f3\u30c8\u3092\u53d6\u5f97<\/h4>\n<pre>\naws iot describe-endpoint --query \"endpointAddress\" --endpoint-type iot:Data-ATS\n<\/pre>\n<h3>2. \u30b3\u30fc\u30c9\u66f8\u304f\/\u8ee2\u9001<\/h3>\n<p>\u4e0b\u8a18\u306e [] \u62ec\u5f27\u90e8\u5206\u3092\u3054\u81ea\u8eab\u306e\u7269\u306b\u66f8\u304d\u63db\u3048\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<h4>rootCA<\/h4>\n<p>AWS IoT\u306e\u958b\u767a\u8005\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u5185\u306b\u8a18\u8f09\u306e\u30ea\u30f3\u30af\u304b\u3089\u53d6\u5f97\u3057\u307e\u3059\u3002<br \/>\n<a href=\"https:\/\/docs.aws.amazon.com\/ja_jp\/iot\/latest\/developerguide\/server-authentication.html#server-authentication-certs\">https:\/\/docs.aws.amazon.com\/ja_jp\/iot\/latest\/developerguide\/server-authentication.html#server-authentication-certs<\/a><\/p>\n<h4>clientCert\/clientPrivateKey<\/h4>\n<p>\u4e0a\u8a18CLI\u30b3\u30de\u30f3\u30c9\u3092\u5b9f\u884c\u3057\u305f\u7d50\u679c\u4f5c\u6210\u3055\u308c\u308b3\u30d5\u30a1\u30a4\u30eb\u306e\u3046\u30612\u30d5\u30a1\u30a4\u30eb\u3092\u5229\u7528\u3057\u307e\u3059\u3002<\/p>\n<ul>\n<li>[DEVICE_NAME]-certificate.pem.crt<\/li>\n<li>[DEVICE_NAME]-private.pem.key<\/li>\n<\/ul>\n<h4>\u30b3\u30fc\u30c9<\/h4>\n<p><strong>500msec\u6bce\u306b\u30c7\u30fc\u30bf\u3092AWSIoT\u3078\u9001\u4fe1\u3057\u3066\u304a\u308a\u307e\u3059<\/strong><\/p>\n<pre>\n\/*\n    note: need add library Adafruit_BMP280 from library manage\n    Github: https:\/\/github.com\/adafruit\/Adafruit_BMP280_Library\n*\/\n\n#include &lt;M5StickC.h&gt;\n#include \"DHT12.h\"\n#include &lt;Wire.h&gt;\n#include \"Adafruit_Sensor.h\"\n#include &lt;Adafruit_BMP280.h&gt;\n#include &lt;ArduinoJson.h&gt;\n#include &lt;WiFiClientSecure.h&gt;\n#include &lt;PubSubClient.h&gt;\n\n\/\/ Wifi Settings\nconst char* ssid     = \"[\u3054\u81ea\u8eab\u306e\u74b0\u5883\u306eWifiSSID]\";\nconst char* password = \"[\u3054\u81ea\u8eab\u306e\u74b0\u5883\u306eWifiPassowrd]\";\n\n\/\/ AWS IoT Settings\nconst char* endpoint = \"[\u3054\u81ea\u8eab\u306eAWS\u30a2\u30ab\u30a6\u30f3\u30c8\u306eIoT\u30a8\u30f3\u30c9\u30dd\u30a4\u30f3\u30c8]\"; \/\/ \u6771\u4eac\u30ea\u30fc\u30b8\u30e7\u30f3\u306a\u3089\u53f3\u8a18\u306e\u3088\u3046\u306a\u5f62 xxxx-ats.iot.ap-northeast-1.amazonaws.com\nconst int   port     = 8883;\nconst char* pubTopic = \"[\u9069\u5f53\u306aTopic\u540d - \u8a18\u4e8b\u4e2d\u3067\u306f envTopic \u3068\u3057\u3066\u3044\u307e\u3057\u305f]\";\nconst char* clientId = \"[\u9069\u5f53\u306a\u30c7\u30d0\u30a4\u30b9\u540d - \u8a18\u4e8b\u4e2d\u3067\u306f M5Stick01 \u3068\u3057\u3066\u3044\u307e\u3057\u305f]\"; \/\/ \n\nconst char* rootCA = \\\n\"-----BEGIN CERTIFICATE-----\\n\" \\\n\"...\\n\" \\ \/\/ [\u3054\u81ea\u8eab\u3067\u53d6\u5f97\u3057\u305fRootCA]\n\"-----END CERTIFICATE-----\\n\";\n\nconst char* clientCert = \\\n\"-----BEGIN CERTIFICATE-----\\n\" \\\n\"...\\n\" \\ \/\/ [\u4e0a\u8a18CLI\u30b3\u30de\u30f3\u30c9\u3067\u751f\u6210\u3057\u305f\u30c7\u30d0\u30a4\u30b9\u7528\u8a3c\u660e\u66f8]\n\"-----END CERTIFICATE-----\\n\";\n\nconst char* clientPrivateKey = \\\n\"-----BEGIN RSA PRIVATE KEY-----\\n\" \\\n\"...\\n\" \\\u3000\/\/ [\u4e0a\u8a18CLI\u30b3\u30de\u30f3\u30c9\u3067\u751f\u6210\u3057\u305f\u30c7\u30d0\u30a4\u30b9\u7528\u79d8\u5bc6\u9375]\n\"-----END RSA PRIVATE KEY-----\\n\";\n\n\/\/ Wifi\/Network\nWiFiClientSecure httpsClient;\nPubSubClient mqttClient(httpsClient);\nchar buffer[128];\n\n\/\/ LED_PIN\n#define M5_STICK_PIN_LED 10\nboolean ledState = true; \/\/ HIGH: LED Off\/Low: LED On\n\nDHT12 dht12; \nAdafruit_BMP280 bme;\n\nvoid checkWiring() {\n  while (!bme.begin(0x76)) {\n    M5.Lcd.fillScreen(BLACK);\n    M5.Lcd.setCursor(0, 0, 2);\n    M5.Lcd.println(\"Could not find a valid BMP280 sensor, check wiring!\");\n    delay(1000);\n  }\n}\n\nvoid reconnect() {\n  while (!mqttClient.connected()) {\n    if (mqttClient.connect(clientId)) {\n      M5.Lcd.setCursor(0, 40, 2);\n      M5.Lcd.println(\"AWSIoTConnected\");\n    } else {\n      M5.Lcd.setCursor(0, 40, 2);\n      M5.Lcd.printf(\"AWSIoTConnect Failed. state=%d\", mqttClient.state());\n      \/\/ Wait 5 seconds before retrying\n      delay(5000);\n    }\n  }\n}\n\nvoid setup() {  \n  \/\/ put your setup code here, to run once:\n  M5.begin();\n  M5.Lcd.setRotation(3);\n  M5.Lcd.fillScreen(BLACK);\n  \/\/ I2C\n  Wire.begin(0,26);\n  \/\/ GPIOSetup\n  pinMode(M5_STICK_PIN_LED, OUTPUT);\n  digitalWrite(M5_STICK_PIN_LED, ledState);\n  \/\/ check ENVHat\n  checkWiring();\n  \/\/ WifiSetup\n  \/\/\/\/ Avoid Connect Error (JIC)\n  WiFi.disconnect(true);\n  delay(1000);\n  \/\/\/\/ Connect\n  WiFi.begin(ssid, password);\n  while (WiFi.status() != WL_CONNECTED) {\n    delay(500);\n  }\n  M5.Lcd.setCursor(0, 40, 2);\n  M5.Lcd.printf(\"Wifi Connected\");\n  \/\/ Configure MQTT Client\n  httpsClient.setCACert(rootCA);\n  httpsClient.setCertificate(clientCert);\n  httpsClient.setPrivateKey(clientPrivateKey);\n  mqttClient.setServer(endpoint, port);\n}\n\nvoid loop() {\n  \/\/ update button state\n  M5.update();\n\n  \/\/ Button Pressed\n  if (M5.BtnA.wasReleased()) {\n    ledState = !ledState;\n    digitalWrite(M5_STICK_PIN_LED, ledState);\n  }\n\n  \/\/ check ENVHat\n  checkWiring();\n  \n  \/\/ display Temp\/Humidity\/Pressure\n  float tmp = dht12.readTemperature();\n  float hum = dht12.readHumidity();\n  M5.Lcd.setCursor(0, 0, 2);\n  M5.Lcd.printf(\"Temp: %2.1f Humi: %2.0f%%\", tmp, hum);\n  \n  float pressure = bme.readPressure();\n  M5.Lcd.setCursor(0, 20, 2);\n  M5.Lcd.printf(\"pressure: %2.1f\", pressure);\n\n  \/\/ create JSON\n  DynamicJsonDocument doc(1024);\n  JsonObject data = doc.createNestedObject(\"env\");\n  data[\"temperture\"] = tmp;\n  data[\"humidity\"] = hum;\n  data[\"pressure\"] = pressure;\n  serializeJson(doc, buffer, sizeof(buffer));\n  \/\/ Debug JSON in SerialConsole\n  Serial.println(buffer);\n\n  \/\/ reconnect\n  if (!mqttClient.connected()) {\n    reconnect();\n  }\n  \n  \/\/ The client only supports publishing at QoS 0\n  mqttClient.publish(pubTopic, buffer);\n\n  \/\/ wait\n  delay(500);\n}\n<\/pre>\n<h3>3. AWS IoT\u3067 \u30c7\u30fc\u30bf\u53d7\u4fe1\u78ba\u8a8d<\/h3>\n<img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.skyarch.net\/blog\/wp-content\/uploads\/2020\/04\/AWS_IoT_Console.png\" alt=\"\" width=\"1024\" height=\"925\" class=\"aligncenter size-full wp-image-18402\" srcset=\"https:\/\/www.skyarch.net\/blog\/wp-content\/uploads\/2020\/04\/AWS_IoT_Console.png 1024w, https:\/\/www.skyarch.net\/blog\/wp-content\/uploads\/2020\/04\/AWS_IoT_Console-300x271.png 300w, https:\/\/www.skyarch.net\/blog\/wp-content\/uploads\/2020\/04\/AWS_IoT_Console-768x694.png 768w, https:\/\/www.skyarch.net\/blog\/wp-content\/uploads\/2020\/04\/AWS_IoT_Console-728x658.png 728w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/>\n<p>AWS IoT\u30b5\u30fc\u30d3\u30b9\u306e\u5de6\u30e1\u30cb\u30e5\u30fc\u3001\u30c6\u30b9\u30c8\u3092\u9078\u629e\u5f8c<br \/>\n\u30c8\u30d4\u30c3\u30af\u306e\u30b5\u30d6\u30b9\u30af\u30ea\u30d7\u30b7\u30e7\u30f3\u306b\u3001\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9\u4e2d\u306e pubTopic \u3068\u5408\u308f\u305b\u305fTopic\u540d\u3092\u5165\u529b\u3057 [\u30c8\u30d4\u30c3\u30af\u3078\u306e\u30b5\u30d6\u30b9\u30af\u30e9\u30a4\u30d6] \u3092\u62bc\u4e0b\u3057\u307e\u3059\u3002<br \/>\n\u30c7\u30d0\u30a4\u30b9\u304c\u6b63\u5e38\u306b\u901a\u4fe1\u3067\u304d\u308c\u3070\u3001500msec \u6bce\u306b\u9001\u4fe1\u3055\u308c\u305f\u30c7\u30fc\u30bf\u304c\u6d41\u308c\u308b\u3088\u3046\u306b\u8868\u793a\u3055\u308c\u307e\u3059\u3002<\/p>\n<h3>4. AWS IoT\u306e\u30eb\u30fc\u30eb\u3067CloudWatch Logs\u3078\u66f8\u304d\u51fa\u3057<\/h3>\n<p>IoT\u30eb\u30fc\u30eb\u3067CloudWatch Logs\u3078\u306e\u5410\u304d\u51fa\u3057\u3092\u4f5c\u6210\u3057\u307e\u3059\u3002<br \/>\n<img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.skyarch.net\/blog\/wp-content\/uploads\/2020\/04\/AWS_IoT_ACT.png\" alt=\"\" width=\"1024\" height=\"885\" class=\"aligncenter size-full wp-image-18408\" srcset=\"https:\/\/www.skyarch.net\/blog\/wp-content\/uploads\/2020\/04\/AWS_IoT_ACT.png 1024w, https:\/\/www.skyarch.net\/blog\/wp-content\/uploads\/2020\/04\/AWS_IoT_ACT-300x259.png 300w, https:\/\/www.skyarch.net\/blog\/wp-content\/uploads\/2020\/04\/AWS_IoT_ACT-768x664.png 768w, https:\/\/www.skyarch.net\/blog\/wp-content\/uploads\/2020\/04\/AWS_IoT_ACT-728x629.png 728w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/p>\n<p>Topic\u540d\u304c\u9069\u5207\u306b\u6307\u5b9a\u3055\u308c\u3066\u3044\u308c\u3070\u30ed\u30ae\u30f3\u30b0\u3055\u308c\u308b\u4e8b\u3092\u78ba\u8a8d\u3067\u304d\u308b\u306f\u305a\u3067\u3059\u3002<br \/>\n<img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.skyarch.net\/blog\/wp-content\/uploads\/2020\/04\/CloudWatchLogs.png\" alt=\"\" width=\"1024\" height=\"685\" class=\"aligncenter size-full wp-image-18399\" srcset=\"https:\/\/www.skyarch.net\/blog\/wp-content\/uploads\/2020\/04\/CloudWatchLogs.png 1024w, https:\/\/www.skyarch.net\/blog\/wp-content\/uploads\/2020\/04\/CloudWatchLogs-300x201.png 300w, https:\/\/www.skyarch.net\/blog\/wp-content\/uploads\/2020\/04\/CloudWatchLogs-768x514.png 768w, https:\/\/www.skyarch.net\/blog\/wp-content\/uploads\/2020\/04\/CloudWatchLogs-728x487.png 728w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/p>\n<h3>5. CloudWatch Logs Insights\u3067\u30af\u30a8\u30ea\u3092\u66f8\u304d\u53ef\u8996\u5316<\/h3>\n<p>CloudWatch Insights \u3092\u958b\u304d\u30af\u30a8\u30ea\u3068\u3057\u3066\u4e0b\u8a18\u3092\u5165\u529b\u3057\u307e\u3059\u3002<\/p>\n<pre>\nfields @timestamp, env.humidity, env.temperture\n| stats avg(`env.temperture`), avg(`env.humidity`) by bin(1m)\n<\/pre>\n<p>\u4e0a\u8a18\u30af\u30a8\u30ea\u3092\u5b9f\u884c\u3059\u308b\u3068\u3001\u4e0b\u8a18\u7d50\u679c\u3092\u5f97\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<br \/>\n\u53ef\u8996\u5316\u3057\u305f\u7d50\u679c\u3092\u30c0\u30c3\u30b7\u30e5\u30dc\u30fc\u30c9\u306b\u8ffd\u52a0\u3067\u304d\u308b\u305f\u3081\u8272\u3005\u3068\u8868\u73fe\u51fa\u6765\u305d\u3046\u3067\u3059\u3002<\/p>\n<h4>\u30ed\u30b0\u96c6\u7d04\u3055\u308c\u305f\u7d50\u679c<\/h4>\n<img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.skyarch.net\/blog\/wp-content\/uploads\/2020\/04\/CloudWatch-Insights-Log-810x1024.png\" alt=\"\" width=\"728\" height=\"920\" class=\"aligncenter size-large wp-image-18401\" srcset=\"https:\/\/www.skyarch.net\/blog\/wp-content\/uploads\/2020\/04\/CloudWatch-Insights-Log-810x1024.png 810w, https:\/\/www.skyarch.net\/blog\/wp-content\/uploads\/2020\/04\/CloudWatch-Insights-Log-237x300.png 237w, https:\/\/www.skyarch.net\/blog\/wp-content\/uploads\/2020\/04\/CloudWatch-Insights-Log-768x971.png 768w, https:\/\/www.skyarch.net\/blog\/wp-content\/uploads\/2020\/04\/CloudWatch-Insights-Log-728x921.png 728w, https:\/\/www.skyarch.net\/blog\/wp-content\/uploads\/2020\/04\/CloudWatch-Insights-Log.png 1024w\" sizes=\"auto, (max-width: 728px) 100vw, 728px\" \/>\n<h4>\u53ef\u8996\u5316\u3057\u305f\u7d50\u679c<\/h4>\n<img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.skyarch.net\/blog\/wp-content\/uploads\/2020\/04\/CloudWatch-Insights-Visualization-921x1024.png\" alt=\"\" width=\"728\" height=\"809\" class=\"aligncenter size-large wp-image-18400\" srcset=\"https:\/\/www.skyarch.net\/blog\/wp-content\/uploads\/2020\/04\/CloudWatch-Insights-Visualization-921x1024.png 921w, https:\/\/www.skyarch.net\/blog\/wp-content\/uploads\/2020\/04\/CloudWatch-Insights-Visualization-270x300.png 270w, https:\/\/www.skyarch.net\/blog\/wp-content\/uploads\/2020\/04\/CloudWatch-Insights-Visualization-768x854.png 768w, https:\/\/www.skyarch.net\/blog\/wp-content\/uploads\/2020\/04\/CloudWatch-Insights-Visualization-728x810.png 728w, https:\/\/www.skyarch.net\/blog\/wp-content\/uploads\/2020\/04\/CloudWatch-Insights-Visualization.png 1024w\" sizes=\"auto, (max-width: 728px) 100vw, 728px\" \/>\n<h2 id=\"matome\">\u306f\u307e\u3063\u305f\u70b9<\/h2>\n<p>\u4e00\u901a\u308a\u30b5\u30fc\u30d3\u30b9\u7406\u89e3\u3092\u3057\u3066\u3044\u305f\u3064\u3082\u308a\u3067\u3057\u305f\u304c\u3001\u5f53\u521d\u30c7\u30d0\u30a4\u30b9\u304b\u3089\u30d7\u30ed\u30b0\u30e9\u30e0\u3092\u5b9f\u884c\u3057\u305f\u7d50\u679c\u4e0b\u8a18\u30a8\u30e9\u30fc\u304c\u51fa\u3066\u30cf\u30de\u308a\u307e\u3057\u305f...<br \/>\nWifi\u306b\u306f\u63a5\u7d9a\u3055\u308c\u308b\u304c\u3001mqttClient.state() = -2 \u3092\u8fd4\u3059\u72b6\u614b<\/p>\n<pre>\n[E][ssl_client.cpp:33] _handle_error(): [start_ssl_client():199]: (-9984) X509 - Certificate verification failed, e.g. CRL, CA or signature check failed\n[E][WiFiClientSecure.cpp:132] connect(): start_ssl_client: -9984\n<\/pre>\n<p>\u8a3c\u660e\u66f8\u30a8\u30e9\u30fc\u3060\u306a\u3068\u3001\u8272\u3005\u898b\u76f4\u3057\u305f\u304c\u554f\u984c\u306a\u304b\u3063\u305f\u306e\u3067\u30e2\u30ce\u30fb\u8a3c\u660e\u66f8\u306e\u4f5c\u308a\u76f4\u3057\u3001\u8a3c\u660e\u66f8\u3092\u30b7\u30ea\u30a2\u30eb\u30b3\u30f3\u30bd\u30fc\u30eb\u306b\u8868\u793a\u3057\u3001\u4e00\u8a9e\u4e00\u53e5\u9055\u308f\u306a\u3044\u304b\u7b49\u3092\u898b\u307e\u3057\u305f\u304c\u554f\u984c\u7121\u3057<br \/>\n\u3075\u3068AWS IoT\u306e\u8a3c\u660e\u66f8\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u3092\u8aad\u3080\u3068\u3001\"\u30a8\u30f3\u30c9\u30dd\u30a4\u30f3\u30c8\u306e\u7a2e\u985e\" \u306b\u3066\u3001\u65b0\u3057\u3044\u8a3c\u660e\u66f8\u306e\u5834\u5408\u30a8\u30f3\u30c9\u30dd\u30a4\u30f3\u30c8\u304c\u7570\u306a\u308b\u3088\u3068\u3044\u3046\u4e8b\u3067\u3057\u305f (\u30c8\u30db\u30db...)<\/p>\n<p>\u4e0a\u8a18CLI\u30b3\u30de\u30f3\u30c9\u3067\u3001describe-endpoint \u3059\u308b\u969b\u306b --endpoint-type iot:Data-ATS \u3068\u3044\u3046\u30aa\u30d7\u30b7\u30e7\u30f3\u4ed8\u304d\u3067\u306a\u3044\u3068\u65b0\u3057\u3044\u30a8\u30f3\u30c9\u30dd\u30a4\u30f3\u30c8\u304c\u8fd4\u5374\u3055\u308c\u306a\u3044\u3088\u3046\u3067\u3059\u3002<br \/>\n(AWSIoT\u306e\u8a2d\u5b9a\u753b\u9762\u3067\u30a8\u30f3\u30c9\u30dd\u30a4\u30f3\u30c8\u3092\u78ba\u8a8d\u3059\u308b\u3068\u3001-ats \u4ed8\u304d\u3067\u3059\u306d)<\/p>\n<blockquote><p>\n  AWS IoT Core \u306f\u3001iot:Data \u3068 iot:Data-ATS \u306e 2 \u3064\u306e\u7570\u306a\u308b\u30c7\u30fc\u30bf\u30a8\u30f3\u30c9\u30dd\u30a4\u30f3\u30c8\u30bf\u30a4\u30d7\u3092\u30b5\u30dd\u30fc\u30c8\u3057\u3066\u3044\u307e\u3059\u3002iot:Data \u30a8\u30f3\u30c9\u30dd\u30a4\u30f3\u30c8\u306f\u3001VerVeriSign Class 3 Public Primary G5 Root CA \u8a3c\u660e\u66f8\u306b\u3088\u3063\u3066\u7f72\u540d\u3055\u308c\u305f\u8a3c\u660e\u66f8\u3092\u63d0\u4f9b\u3057\u307e\u3059\u3002iot:Data-ATS \u30a8\u30f3\u30c9\u30dd\u30a4\u30f3\u30c8\u306f\u3001Amazon Trust Services CA \u306b\u3088\u3063\u3066\u7f72\u540d\u3055\u308c\u305f\u30b5\u30fc\u30d0\u30fc\u8a3c\u660e\u66f8\u3092\u63d0\u4f9b\u3057\u307e\u3059\u3002\n<\/p><\/blockquote>\n<p><a href=\"https:\/\/docs.aws.amazon.com\/ja_jp\/iot\/latest\/developerguide\/server-authentication.html#server-authentication-certs\">https:\/\/docs.aws.amazon.com\/ja_jp\/iot\/latest\/developerguide\/server-authentication.html#server-authentication-certs<\/a><\/p>\n<h3>Debug\u5b9f\u65bd\u65b9\u6cd5<\/h3>\n<h4>Debug\u30ec\u30d9\u30eb\u3092\u4e0a\u3052\u308b<\/h4>\n<p>\u30c4\u30fc\u30eb \u21e2 Core Debug Level \u3092\u4e0a\u3052\u308b<br \/>\n<img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.skyarch.net\/blog\/wp-content\/uploads\/2020\/04\/Debug_Setting.png\" alt=\"\" width=\"1024\" height=\"919\" class=\"aligncenter size-full wp-image-18398\" srcset=\"https:\/\/www.skyarch.net\/blog\/wp-content\/uploads\/2020\/04\/Debug_Setting.png 1024w, https:\/\/www.skyarch.net\/blog\/wp-content\/uploads\/2020\/04\/Debug_Setting-300x269.png 300w, https:\/\/www.skyarch.net\/blog\/wp-content\/uploads\/2020\/04\/Debug_Setting-768x689.png 768w, https:\/\/www.skyarch.net\/blog\/wp-content\/uploads\/2020\/04\/Debug_Setting-728x653.png 728w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/p>\n<h4>Debug\u30ed\u30b0\u3092\u78ba\u8a8d\u3059\u308b<\/h4>\n<p>\u30c4\u30fc\u30eb \u21e2 \u30b7\u30ea\u30a2\u30eb\u30e2\u30cb\u30bf \u3067\u78ba\u8a8d\u3059\u308b (\u53f3\u4e0b\u306e\u901a\u4fe1\u901f\u5ea6\u3092\u5408\u308f\u305b\u306a\u3044\u3068\u6587\u5b57\u5316\u3051\u3059\u308b\u306e\u3067\u6ce8\u610f)<br \/>\n<img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.skyarch.net\/blog\/wp-content\/uploads\/2020\/04\/Debug_Window.png\" alt=\"\" width=\"1024\" height=\"429\" class=\"aligncenter size-full wp-image-18397\" srcset=\"https:\/\/www.skyarch.net\/blog\/wp-content\/uploads\/2020\/04\/Debug_Window.png 1024w, https:\/\/www.skyarch.net\/blog\/wp-content\/uploads\/2020\/04\/Debug_Window-300x126.png 300w, https:\/\/www.skyarch.net\/blog\/wp-content\/uploads\/2020\/04\/Debug_Window-768x322.png 768w, https:\/\/www.skyarch.net\/blog\/wp-content\/uploads\/2020\/04\/Debug_Window-728x305.png 728w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/p>\n<h2>\u307e\u3068\u3081<\/h2>\n<p>\u6b21\u56de\u306fPub\/Sub\u306b\u5bfe\u5fdc\u3055\u305b\u3001Shadow\u3068\u622f\u308c\u3066\u307f\u3088\u3046\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n<p>\u5b9f\u65bd\u3057\u3066\u307f\u307e\u3057\u305f<br \/>\n<a href=\"https:\/\/www.skyarch.net\/blog\/?p=18513\">https:\/\/www.skyarch.net\/blog\/?p=18513<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u306f\u3058\u3081\u306b \u524d\u56de\u306e\u7d9a\u304d\u3068\u306a\u308a\u307e\u3059\u3002 https:\/\/www.skyarch.net\/blog\/?p=18370 \u3088\u304f\u3042\u308b\u8a71\u3067\u3001\u6e29\u5ea6\u3001\u6e7f\u5ea6\u3001\u6c17\u5727\u3092AWS IoT\u306b\u6295\u3052\u53ef\u8996\u5316\u3059\u308b\u306e\u3067\u3059\u304c \u6700\u8fd1\u9ad8\u983b\u5ea6\u3067Update\u3055\u308c\u3001\u4fbf\u5229\u306b\u306a&#8230;<\/p>\n","protected":false},"author":2,"featured_media":18406,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_locale":"ja","_original_post":"https:\/\/www.skyarch.net\/blog\/?p=18393","footnotes":""},"categories":[20,132,115,276],"tags":[],"class_list":{"0":"post-18393","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-aws","8":"category-aws-cli","9":"category-iot","10":"category-serverless","11":"ja"},"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.skyarch.net\/blog\/wp-json\/wp\/v2\/posts\/18393","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.skyarch.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.skyarch.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.skyarch.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.skyarch.net\/blog\/wp-json\/wp\/v2\/comments?post=18393"}],"version-history":[{"count":13,"href":"https:\/\/www.skyarch.net\/blog\/wp-json\/wp\/v2\/posts\/18393\/revisions"}],"predecessor-version":[{"id":18627,"href":"https:\/\/www.skyarch.net\/blog\/wp-json\/wp\/v2\/posts\/18393\/revisions\/18627"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.skyarch.net\/blog\/wp-json\/wp\/v2\/media\/18406"}],"wp:attachment":[{"href":"https:\/\/www.skyarch.net\/blog\/wp-json\/wp\/v2\/media?parent=18393"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skyarch.net\/blog\/wp-json\/wp\/v2\/categories?post=18393"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skyarch.net\/blog\/wp-json\/wp\/v2\/tags?post=18393"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}