{"id":18513,"date":"2020-05-01T19:10:13","date_gmt":"2020-05-01T10:10:13","guid":{"rendered":"https:\/\/www.skyarch.net\/blog\/?p=18513"},"modified":"2024-02-02T13:15:09","modified_gmt":"2024-02-02T04:15:09","slug":"m5stick-with-env-hat%e3%81%a7aws-iot-device-shadow%e3%82%92%e5%88%a9%e7%94%a8%e3%81%97%e3%81%a6%e3%81%bf%e3%82%8b","status":"publish","type":"post","link":"https:\/\/www.skyarch.net\/blog\/m5stick-with-env-hat%e3%81%a7aws-iot-device-shadow%e3%82%92%e5%88%a9%e7%94%a8%e3%81%97%e3%81%a6%e3%81%bf%e3%82%8b\/","title":{"rendered":"M5StickC with ENV Hat\u3067AWS IoT Device Shadow\u3092\u5229\u7528\u3057\u3066\u307f\u308b"},"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=18393\">https:\/\/www.skyarch.net\/blog\/?p=18393<\/a><\/p>\n<p>\u524d\u56de\u307e\u3067\u3067\u3001AWS IoT\u30b5\u30fc\u30d3\u30b9\u3078\u30bb\u30f3\u30b5\u5024\u3092Publish\u51fa\u6765\u305f\u305f\u3081<br \/>\n\u4eca\u56de\u306fShadow\u3092\u5229\u7528\u3057\u3066\u3001LED\u72b6\u614b\u306e\u7ba1\u7406\u3092\u3057\u3066\u307f\u307e\u3059\u3002<\/p>\n<h2>\u51fa\u6765\u305f\u3082\u306e<\/h2>\n<h3>Shadow\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u3092\u66f4\u65b0\u3059\u308b\u4e8b\u3067Delta\u3092\u767a\u751f\u3055\u305b\u30c7\u30d0\u30a4\u30b9\u5074LED\u3092\u5236\u5fa1<\/h3>\n<img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-18515\" src=\"https:\/\/www.skyarch.net\/blog\/wp-content\/uploads\/2020\/05\/VID_1.gif\" alt=\"\" width=\"640\" height=\"480\" \/>\n<h4>\u5de6\u5074\u306e\u30a6\u30a3\u30f3\u30c9\u30a6<\/h4>\n<p>AWS IoT \u21e2 \u30e2\u30ce \u21e2 \u30b7\u30e3\u30c9\u30a6<br \/>\n\u306b\u3066\u4e0b\u8a18\u30b7\u30e3\u30c9\u30a6\u30b9\u30c6\u30fc\u30bf\u30b9\u306e desired \u5185\u3001led_status \u3092 1 \u306b\u66f4\u65b0\u3059\u308b\u3068\u3001delta\u304c\u767a\u751f\u3057\u30c7\u30d0\u30a4\u30b9\u304c\u53d7\u4fe1\u3057\u3066LED\u3092\u4ed8\u3051\u308b<br \/>\nled_status\u3092 0 \u306b\u66f4\u65b0\u3059\u308b\u3068\u3001LED\u304c\u6d88\u3048\u307e\u3059\u3002<\/p>\n<pre>{\n  \"desired\": {\n    \"welcome\": \"aws-iot\",\n    \"led_status\": 0\n  },\n  \"reported\": {\n    \"welcome\": \"aws-iot\",\n    \"led_status\": 0\n  }\n}\n<\/pre>\n<h4>\u53f3\u5074\u306e\u30a6\u30a3\u30f3\u30c9\u30a6<\/h4>\n<p>\u30c6\u30b9\u30c8 \u21e2 MQTT\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u306b\u3066\u4e0b\u8a18\u30c8\u30d4\u30c3\u30af\u3092\u30b5\u30d6\u30b9\u30af\u30e9\u30a4\u30d6\u3057\u3066\u3044\u307e\u3059\u3002<br \/>\n\u3053\u306e\u305f\u3081 delta \u304c\u767a\u751f\u3057\u305f\u969b\u306b\u3001\u30c7\u30d0\u30a4\u30b9\u5074\u304c\u53d7\u4fe1(Subscribe\u3057\u3066\u3044\u308b)\u3059\u308b\u30c8\u30d4\u30c3\u30af\u5185\u5bb9\u3068\u306a\u308a\u307e\u3059\u3002<\/p>\n<p><code>$aws\/things\/M5Stick01\/shadow\/update\/delta<\/code><\/p>\n<h3>\u30c7\u30d0\u30a4\u30b9\u5074\u3067LED\u30b9\u30c6\u30fc\u30bf\u30b9\u3092\u5909\u5316\u3055\u305b\u3066\u3082Delta\u304c\u767a\u751f\u3057Shadow\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u72b6\u614b\u306b\u53ce\u675f\u3059\u308b<\/h3>\n<img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-18516\" src=\"https:\/\/www.skyarch.net\/blog\/wp-content\/uploads\/2020\/05\/VID_2.gif\" alt=\"\" width=\"640\" height=\"480\" \/>\n<p>\u30c7\u30d0\u30a4\u30b9\u5074\u3067\u306f\u3001\u81ea\u8eab\u306eLED\u70b9\u706f\/\u6d88\u706f\u60c5\u5831\u3092Flag\u306e\u3088\u3046\u306a\u5f62\u3067\u6301\u3063\u3066\u304a\u308a<br \/>\n\u304a\u3088\u305d5\u79d2\u306b1\u56de\u3001\u305d\u306e\u72b6\u614b\u3092 Shadow\u306b reported\u3068\u3057\u3066\u901a\u77e5\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>\u3053\u306e\u305f\u3081\u3001Shadow\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u5185\u3067\u306f desired \u304c \u6d88\u706f \u306b\u3082\u95a2\u308f\u3089\u305a<br \/>\n\u30c7\u30d0\u30a4\u30b9\u5074\u3067\u70b9\u706f\u3055\u308c\u308b\u3068\u3001desired \u3068 reported\u306b delta\u304c\u767a\u751f<br \/>\n\u53d7\u4fe1\u3057\u305f\u30c7\u30d0\u30a4\u30b9\u304c desired\u306e\u72b6\u614b\u306bLED\u3092\u70b9\u706f\/\u6d88\u706f\u3057\u5408\u308f\u305b\u308b\u3068\u3044\u3046\u5f62\u3067\u3059\u3002<\/p>\n<p>\u3053\u306e\u3042\u305f\u308a\u306e\u6319\u52d5\u306fdesired\u3092null\u3068\u3057\u3066\u9001\u4fe1\u3059\u308b\u4e8b\u3067\u5909\u66f4\u3067\u304d\u305d\u3046\u3067\u3059\u306d\u3002<br \/>\n<a href=\"https:\/\/docs.aws.amazon.com\/ja_jp\/iot\/latest\/developerguide\/device-shadow-document.html\">https:\/\/docs.aws.amazon.com\/ja_jp\/iot\/latest\/developerguide\/device-shadow-document.html<\/a><\/p>\n<h4>\u5de6\u5074\u306e\u30a6\u30a3\u30f3\u30c9\u30a6<\/h4>\n<p>AWS IoT \u21e2 \u30e2\u30ce \u21e2 \u30b7\u30e3\u30c9\u30a6<br \/>\n\u306b\u3066\u30b7\u30e3\u30c9\u30a6\u30b9\u30c6\u30fc\u30bf\u30b9\u3092\u78ba\u8a8d\u3057\u3066\u3044\u307e\u3059\u304c\u3001\u30b7\u30e3\u30c9\u30a6\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u306e\u66f4\u65b0\u306f\u884c\u3063\u3066\u3044\u307e\u305b\u3093\u3002<\/p>\n<h4>\u53f3\u5074\u306e\u30a6\u30a3\u30f3\u30c9\u30a6<\/h4>\n<p>\u30c6\u30b9\u30c8 \u21e2 MQTT\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u306b\u3066\u4e0b\u8a18\u30c8\u30d4\u30c3\u30af\u3092\u30b5\u30d6\u30b9\u30af\u30e9\u30a4\u30d6\u3057\u3066\u3044\u307e\u3059\u3002<br \/>\n\u624b\u52d5\u3067LED\u72b6\u614b\u3092\u5909\u66f4\u3059\u308b\u3068\u3001delta\u304c\u767a\u751f\u3057\u307e\u3059\u3002<\/p>\n<p><code>$aws\/things\/M5Stick01\/shadow\/update\/delta<\/code><\/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=\"#point\">\u30b3\u30fc\u30c9\u4e0a\u306e\u30dd\u30a4\u30f3\u30c8\u3068\u306a\u308b\u51e6\u7406<\/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) \u524d\u56de\u307e\u3067\u306b\u5b9f\u65bd<\/li>\n<li>AWS IoT\u3067 Shadow\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u3092\u5909\u66f4<\/li>\n<li>PubSubClient\u30d8\u30c3\u30c0\u30d5\u30a1\u30a4\u30eb\u5909\u66f4<\/li>\n<li>\u30b3\u30fc\u30c9\u66f8\u304f\/\u8ee2\u9001<\/li>\n<\/ol>\n<h3>M5StickC\u3067\u5229\u7528\u3059\u308b\u30e9\u30a4\u30d6\u30e9\u30ea\u7b49<\/h3>\n<p>\u524d\u56de\u3068\u540c\u69d8<\/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 Arduino: Timer interrupts<br \/>\n<a href=\"https:\/\/techtutorialsx.com\/2017\/10\/07\/esp32-arduino-timer-interrupts\/\">https:\/\/techtutorialsx.com\/2017\/10\/07\/esp32-arduino-timer-interrupts\/<\/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>2. AWS IoT\u3067 Shadow\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u3092\u5909\u66f4<\/h3>\n<p>\u4f5c\u6210\u3057\u305f\u30e2\u30ce\u306e\u30b7\u30e3\u30c9\u30a6\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u3067\u521d\u671f\u5024\u306f\u8272\u3005\u5165\u3063\u3066\u304a\u308a\u307e\u3059\u304c\u3001\u30b7\u30f3\u30d7\u30eb\u306bled_status\u306e\u307f\u3068\u3057\u307e\u3059\u3002<br \/>\n\"welcome\": \"aws-iot\" \u306f\u6d88\u305b\u306a\u304b\u3063\u305f\u305f\u3081\u3002<\/p>\n<pre>{\n  \"desired\": {\n    \"welcome\": \"aws-iot\",\n    \"led_status\": 0\n  },\n  \"reported\": {\n    \"welcome\": \"aws-iot\",\n    \"led_status\": 0\n  }\n}\n<\/pre>\n<h3>3. PubSubClient\u30d8\u30c3\u30c0\u30d5\u30a1\u30a4\u30eb\u5909\u66f4<\/h3>\n<p>\u79c1\u306ePC\u3067\u306f\u4e0b\u8a18\u5834\u6240\u306b\u3042\u308a\u307e\u3057\u305f\u3002<br \/>\n\/Users\/[\u81ea\u5206\u306e\u30e6\u30fc\u30b6\u540d]\/Documents\/Arduino\/libraries\/PubSubClient\/src\/PubSubClient.h<\/p>\n<p>Mac\/Linux\u3067\u3057\u305f\u3089\u4e0b\u8a18\u306b\u3066\u691c\u7d22\u51fa\u6765\u308b\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n<pre>$ cd\n$ sudo find . -name 'PubSubClient.h'\n<\/pre>\n<p>\u30d5\u30a1\u30a4\u30eb\u5185\u306e\u4e0b\u8a18\u3092\u66f8\u304d\u63db\u3048\u307e\u3059\u3002<br \/>\n\u30b5\u30a4\u30ba\u30aa\u30fc\u30d0\u3059\u308b\u3068\u3001Callback\u95a2\u6570\u5185\u3082\u5b9f\u884c\u3055\u308c\u305aDebug\u304c\u96e3\u3057\u3044\u305f\u3081\u304a\u6c17\u3092\u3064\u3051\u4e0b\u3055\u3044\u3002<\/p>\n<pre>#define MQTT_MAX_PACKET_SIZE 128\n\u2193\n#define MQTT_MAX_PACKET_SIZE 1024\n<\/pre>\n<h3>4. \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<br \/>\n\u8a73\u7d30\u306f\u524d\u56de\u306e\u8a18\u4e8b\u3092\u3054\u89a7\u4e0b\u3055\u3044\u3002<br \/>\n<a href=\"https:\/\/www.skyarch.net\/blog\/?p=18393\">https:\/\/www.skyarch.net\/blog\/?p=18393<\/a><\/p>\n<h4>\u30b3\u30fc\u30c9<\/h4>\n<p><strong>\u304a\u3088\u305d5\u79d2\u6bce\u306b\u30c7\u30fc\u30bf\u3092AWSIoT\u3078\u9001\u4fe1\u3057\u3066\u304a\u308a\u307e\u3059<\/strong><br \/>\n\u30bf\u30a4\u30de\u30fc\u3092\u5272\u308a\u8fbc\u307f\u306b\u3066\u51e6\u7406\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<pre>\/*\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\/\/ PubSubClient.h \u5b9f\u30d5\u30a1\u30a4\u30eb\u306eMQTT_MAX_PACKET_SIZE\u30921024\u7b49\u306b\u8a2d\u5b9a\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\n\/\/ https:\/\/github.com\/knolleary\/pubsubclient\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]\"; \/\/ \nchar pubTopicShadow[128];\nchar subTopicShadow[128];\n#define QOS_SUB 0\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[1024];\n\n\/\/ Timer interrupts\n\/\/ https:\/\/techtutorialsx.com\/2017\/10\/07\/esp32-arduino-timer-interrupts\/\nvolatile int interruptCounter;\n \nhw_timer_t * timer = NULL;\nportMUX_TYPE timerMux = portMUX_INITIALIZER_UNLOCKED;\n\nvoid IRAM_ATTR onTimer() {\n  portENTER_CRITICAL_ISR(&amp;timerMux);\n  interruptCounter++;\n  portEXIT_CRITICAL_ISR(&amp;timerMux);\n}\n\n\/\/ LED_PIN\n#define M5_STICK_PIN_LED 10\nboolean ledState = true; \/\/ HIGH: LED Off\/Low: LED On\n\n\/\/ ENV HAT\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      mqttClient.subscribe(subTopicShadow, QOS_SUB);\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 mqttCallback(char* topic, byte* payload, unsigned int length) {\n    \/\/ JSON Parse\n    payload[length] = '\\0';\n    String json = String((char*) payload);\n    \/\/ Debug JSON in SerialConsole\n    Serial.println(json);\n\n    StaticJsonDocument&lt;1024&gt; docShadowDesired;\n    DeserializationError error = deserializeJson(docShadowDesired, json);\n    \n    M5.Lcd.setCursor(0, 60, 2);\n    if (error) {\n      M5.Lcd.printf(\"deserializeJson Failed\");\n    } else {      \n      if (sizeof(docShadowDesired[\"state\"][\"led_status\"]) &gt; 0) {\n        M5.Lcd.printf(\"Desired: %d\", int(docShadowDesired[\"state\"][\"led_status\"]));\n        ledState = !boolean(docShadowDesired[\"state\"][\"led_status\"]);\n        digitalWrite(M5_STICK_PIN_LED, ledState);\n      } else {\n        M5.Lcd.printf(\"Desired: corrupted value\");\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  \/\/ Timer\n  timer = timerBegin(0, 80, true);\n  timerAttachInterrupt(timer, &amp;onTimer, true);\n  timerAlarmWrite(timer, 5000000, true);\n  timerAlarmEnable(timer);\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  mqttClient.setCallback(mqttCallback);\n  \/\/ pubTopic\/subTopic\n  sprintf(pubTopicShadow, \"$aws\/things\/%s\/shadow\/update\", clientId);\n  sprintf(subTopicShadow, \"$aws\/things\/%s\/shadow\/update\/delta\", clientId);\n  \/\/ Debug topic name in SerialConsole\n  Serial.printf(\"pubTopicShadow=%s\\n\", pubTopicShadow);\n  Serial.printf(\"subTopicShadow=%s\\n\", subTopicShadow);\n}\n\nvoid loop() {  \n  \/\/ update button state\n  M5.update();\n\n  \/\/ Button Pressed\n  if (M5.BtnA.wasReleased()) {\n    \/\/ turnOn\/Off LED\n    ledState = !ledState;\n    digitalWrite(M5_STICK_PIN_LED, ledState);\n  }\n\n  \/\/ check ENVHat\n  checkWiring();\n\n  \/\/ reconnect\n  if (!mqttClient.connected()) {\n    reconnect();\n  }\n  mqttClient.loop();\n  \n  \/\/ Timer Interrupt execute Every 5sec\n  if (interruptCounter &gt; 0) {\n    Serial.println(\"Timer interrupted\");\n    \/\/ CounterReset\n    portENTER_CRITICAL(&amp;timerMux);\n    interruptCounter--;\n    portEXIT_CRITICAL(&amp;timerMux);\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 docEnv(1024);\n    JsonObject data = docEnv.createNestedObject(\"env\");\n    data[\"temperture\"] = tmp;\n    data[\"humidity\"] = hum;\n    data[\"pressure\"] = pressure;\n    serializeJson(docEnv, buffer, sizeof(buffer));\n    \/\/ Debug JSON in SerialConsole\n    Serial.println(buffer);\n        \n    \/\/ Publish - The client only supports publishing at QoS 0\n    mqttClient.publish(pubTopic, buffer);\n\n    \/\/ report current LED status to ShadowTopic\n    \/\/ create JSON\n    DynamicJsonDocument docShadowReport(1024);\n    JsonObject state = docShadowReport.createNestedObject(\"state\");\n    JsonObject reported = state.createNestedObject(\"reported\");\n    reported[\"led_status\"] = int(!ledState);\n    serializeJson(docShadowReport, buffer, sizeof(buffer));\n    \/\/ Debug JSON in SerialConsole\n    Serial.println(buffer);\n\n    \/\/ Publish - The client only supports publishing at QoS 0\n    mqttClient.publish(pubTopicShadow, buffer);\n  }\n  \n  \/\/ wait\n  delay(500);\n}\n<\/pre>\n<h2 id=\"point\">\u30b3\u30fc\u30c9\u4e0a\u306e\u30dd\u30a4\u30f3\u30c8\u3068\u306a\u308b\u51e6\u7406<\/h2>\n<ul>\n<li>\u30bb\u30f3\u30b5\u5024\u53d6\u5f97\u306f\u3001ENV HAT\u7528\u306e\u30b5\u30f3\u30d7\u30eb\u30b9\u30b1\u30c3\u30c1\u304b\u3089\u307b\u307c\u5909\u3048\u3066\u3044\u307e\u305b\u3093\u3002<\/li>\n<li>\u30bf\u30a4\u30de\u30fc\u306b\u95a2\u3057\u3066\u306f\u3001\u53c2\u8003\u30d6\u30ed\u30b0\u304b\u3089\u62dd\u501f\u3057\u305f\u5185\u5bb9\u3068\u306a\u308a\u307e\u3059\u3002<\/li>\n<\/ul>\n<h3>Shadow Topic\u306e\u30d1\u30d6\u30ea\u30c3\u30b7\u30e5\/\u30b5\u30d6\u30b9\u30af\u30e9\u30a4\u30d6<\/h3>\n<p>clientId\u3092\u5143\u306bTopic\u30a8\u30f3\u30c9\u30dd\u30a4\u30f3\u30c8\u540d\u3092\u751f\u6210\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<pre>void setup() {\n  \/\/ pubTopic\/subTopic\n  sprintf(pubTopicShadow, \"$aws\/things\/%s\/shadow\/update\", clientId);\n  sprintf(subTopicShadow, \"$aws\/things\/%s\/shadow\/update\/delta\", clientId);\n  ...\n<\/pre>\n<h4>\u30d1\u30d6\u30ea\u30c3\u30b7\u30e5<\/h4>\n<p>\u73fe\u5728\u306e\u30c7\u30d0\u30a4\u30b9\u72b6\u614b(LED\u70b9\u706f\u72b6\u6cc1)\u3092Shadow\u306b\u3066\u30ec\u30dd\u30fc\u30c8\u3059\u308b\u305f\u30815\u79d2\u6bce\u306b<br \/>\nLED\u306e\u70b9\u706f\/\u6d88\u706f\u72b6\u614b\u5909\u6570\u3092\u5143\u306bJSON\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u3092\u751f\u6210\u3057\u3001Publish\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>\u751f\u6210\u3055\u308c\u308bJSON\u306f\u4e0b\u8a18\u306e\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<br \/>\n<code>{\"state\": {\"reported\" : {\"led_status\" : 0}}}<\/code><\/p>\n<pre>void loop() {\n  ...\n  \/\/ Timer Interrupt execute Every 5sec\n  if (interruptCounter &gt; 0) {\n    ..\n    \/\/ report current LED status to ShadowTopic\n    \/\/ create JSON\n    DynamicJsonDocument docShadowReport(1024);\n    JsonObject state = docShadowReport.createNestedObject(\"state\");\n    JsonObject reported = state.createNestedObject(\"reported\");\n    reported[\"led_status\"] = int(!ledState);\n    serializeJson(docShadowReport, buffer, sizeof(buffer));\n    \/\/ Debug JSON in SerialConsole\n    Serial.println(buffer);\n\n    \/\/ Publish - The client only supports publishing at QoS 0\n    mqttClient.publish(pubTopicShadow, buffer);\n...\n<\/pre>\n<h4>\u30b5\u30d6\u30b9\u30af\u30e9\u30a4\u30d6<\/h4>\n<p>\u30b5\u30d6\u30b9\u30af\u30e9\u30a4\u30d6\u8a2d\u5b9a\u3092\u884c\u3046\u3068\u3001\u30b3\u30fc\u30eb\u30d0\u30c3\u30af\u95a2\u6570\u306b\u30c8\u30d4\u30c3\u30af\u540d\u3001\u30e1\u30c3\u30bb\u30fc\u30b8\u3001\u30e1\u30c3\u30bb\u30fc\u30b8\u9577\u304c\u901a\u77e5\u3055\u308c\u307e\u3059\u3002<br \/>\n\u901a\u77e5\u3055\u308c\u305f\u30e1\u30c3\u30bb\u30fc\u30b8\u3092\u3001char\u578b\u304b\u3089String\u578b\u306b\u30ad\u30e3\u30b9\u30c8\u3057\u3001deserialize\u3092\u5b9f\u65bd\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>\u53d6\u5f97\u3057\u305fJSON\u5185\u306e\u6570\u5024\u3092\u3001boolean\u306b\u5909\u63db\u3057\u51fa\u529b\u3057\u3066\u3044\u307e\u3059\u3002<br \/>\n\u203bM5Stick\u306eLED\u63a5\u7d9aI\/O\u30d4\u30f3\u304cHigh\u3060\u3068\u6d88\u706f\/Low\u3060\u3068\u70b9\u706f\u306e\u305f\u3081\u53cd\u8ee2\u3059\u308b\u5fc5\u8981\u3042\u308a<\/p>\n<pre>ledState = !boolean(docShadowDesired[\"state\"][\"led_status\"]);\ndigitalWrite(M5_STICK_PIN_LED, ledState);\n<\/pre>\n<h5>\u95a2\u9023\u30b3\u30fc\u30c9\u90e8\u5206<\/h5>\n<pre>void mqttCallback(char* topic, byte* payload, unsigned int length) {\n    \/\/ JSON Parse\n    payload[length] = '\\0';\n    String json = String((char*) payload);\n    \/\/ Debug JSON in SerialConsole\n    Serial.println(json);\n\n    StaticJsonDocument&lt;1024&gt; docShadowDesired;\n    DeserializationError error = deserializeJson(docShadowDesired, json);\n    \n    M5.Lcd.setCursor(0, 60, 2);\n    if (error) {\n      M5.Lcd.printf(\"deserializeJson Failed\");\n    } else {      \n      if (sizeof(docShadowDesired[\"state\"][\"led_status\"]) &gt; 0) {\n        M5.Lcd.printf(\"Desired: %d\", int(docShadowDesired[\"state\"][\"led_status\"]));\n        ledState = !boolean(docShadowDesired[\"state\"][\"led_status\"]);\n        digitalWrite(M5_STICK_PIN_LED, ledState);\n      } else {\n        M5.Lcd.printf(\"Desired: corrupted value\");\n      }\n    }\n}\n\nvoid setup() {\n  ...\n  mqttClient.setCallback(mqttCallback);\n...\n\nvoid reconnect() {\n  while (!mqttClient.connected()) {\n    if (mqttClient.connect(clientId)) {\n      ...\n      mqttClient.subscribe(subTopicShadow, QOS_SUB);\n<\/pre>\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=18393 \u524d\u56de\u307e\u3067\u3067\u3001AWS IoT\u30b5\u30fc\u30d3\u30b9\u3078\u30bb\u30f3\u30b5\u5024\u3092Publish\u51fa\u6765\u305f\u305f\u3081 \u4eca\u56de\u306fShadow\u3092\u5229\u7528\u3057\u3066\u3001LED\u72b6&#8230;<\/p>\n","protected":false},"author":2,"featured_media":18520,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_locale":"ja","_original_post":"https:\/\/www.skyarch.net\/blog\/?p=18513","footnotes":""},"categories":[20,115,276],"tags":[],"class_list":{"0":"post-18513","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-aws","8":"category-iot","9":"category-serverless","10":"ja"},"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.skyarch.net\/blog\/wp-json\/wp\/v2\/posts\/18513","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=18513"}],"version-history":[{"count":13,"href":"https:\/\/www.skyarch.net\/blog\/wp-json\/wp\/v2\/posts\/18513\/revisions"}],"predecessor-version":[{"id":25792,"href":"https:\/\/www.skyarch.net\/blog\/wp-json\/wp\/v2\/posts\/18513\/revisions\/25792"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.skyarch.net\/blog\/wp-json\/wp\/v2\/media\/18520"}],"wp:attachment":[{"href":"https:\/\/www.skyarch.net\/blog\/wp-json\/wp\/v2\/media?parent=18513"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skyarch.net\/blog\/wp-json\/wp\/v2\/categories?post=18513"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skyarch.net\/blog\/wp-json\/wp\/v2\/tags?post=18513"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}