{"id":5362,"date":"2024-07-23T10:50:53","date_gmt":"2024-07-23T01:50:53","guid":{"rendered":"https:\/\/blog.smartlight.co.jp\/?p=5362"},"modified":"2024-07-23T10:50:55","modified_gmt":"2024-07-23T01:50:55","slug":"m5stack%e3%81%ae%e4%b8%ad%e3%81%ae%e5%8a%a0%e9%80%9f%e5%ba%a6%e3%82%bb%e3%83%b3%e3%82%b5%e3%83%bc%e3%81%ae%e6%83%85%e5%a0%b1%e3%82%92mqtt%e3%81%a7node-red%e3%81%ab%e9%80%81%e3%81%a3%e3%81%a6%e3%81%bf","status":"publish","type":"post","link":"https:\/\/blog.smartlight.co.jp\/?p=5362","title":{"rendered":"M5Stack\u306e\u4e2d\u306e\u52a0\u901f\u5ea6\u30bb\u30f3\u30b5\u30fc\u306e\u60c5\u5831\u3092Mqtt\u3067Node-RED\u306b\u9001\u3063\u3066\u307f\u3088\u3046"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">\u306f\u3058\u3081\u306b<\/h2>\n\n\n\n<p class=\"has-medium-font-size\">\u4eca\u56de\u306f\u524d\u56de\u30bb\u30c3\u30c8\u30a2\u30c3\u30d7\u3057\u305fM5Stack\u306e\u4e2d\u306b\u3042\u308b\u52a0\u901f\u5ea6\u30bb\u30f3\u30b5\u306e\u60c5\u5831\u3092MQTT\u3067Node-RED\uff08Windows\uff09\u306b\u30c7\u30fc\u30bf\u3092\u9001\u3063\u3066\u3044\u304d\u307e\u3059<\/p>\n\n\n\n<p>\u30bb\u30c3\u30c8\u30a2\u30c3\u30d7\u7de8<\/p>\n\n\n\nhttps:\/\/blog.smartlight.co.jp\/?p=5360\n\n\n\n<h2 class=\"wp-block-heading\">\u6e96\u5099<\/h2>\n\n\n\n<p class=\"has-medium-font-size\"><span class=\"marker2\">\u30fbArduino<\/span><\/p>\n\n\n\n<p class=\"has-medium-font-size\">\u30e9\u30a4\u30d6\u30e9\u30ea\u30de\u30cd\u30fc\u30b8\u30e3\u30fc\u3067\u300cPubSubClient\u300d\u3092\u691c\u7d22\uff06\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"236\" src=\"https:\/\/blog.smartlight.co.jp\/wp-content\/uploads\/2024\/07\/image-402-1024x236.png\" alt=\"\" class=\"wp-image-5366\" srcset=\"https:\/\/blog.smartlight.co.jp\/wp-content\/uploads\/2024\/07\/image-402-1024x236.png 1024w, https:\/\/blog.smartlight.co.jp\/wp-content\/uploads\/2024\/07\/image-402-300x69.png 300w, https:\/\/blog.smartlight.co.jp\/wp-content\/uploads\/2024\/07\/image-402-768x177.png 768w, https:\/\/blog.smartlight.co.jp\/wp-content\/uploads\/2024\/07\/image-402-1536x354.png 1536w, https:\/\/blog.smartlight.co.jp\/wp-content\/uploads\/2024\/07\/image-402.png 1572w, https:\/\/blog.smartlight.co.jp\/wp-content\/uploads\/2024\/07\/image-402-1024x236.png 856w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"has-medium-font-size\"><span class=\"marker2\">\u30fbNode-RED<\/span><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li class=\"has-medium-font-size\">Mqtt IN\u30ce\u30fc\u30c9<\/li>\n\n\n\n<li class=\"has-medium-font-size\">\u30c7\u30d0\u30c3\u30af\u30ce\u30fc\u30c9<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"387\" height=\"61\" src=\"https:\/\/blog.smartlight.co.jp\/wp-content\/uploads\/2024\/07\/image-408.png\" alt=\"\" class=\"wp-image-5377\" \/><\/figure>\n\n\n\n<p class=\"has-medium-font-size\">\u30c8\u30d4\u30c3\u30af\u306f<strong>\/m5stack\/data<\/strong>\u306b\u3057\u307e\u3059<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"497\" height=\"335\" src=\"https:\/\/blog.smartlight.co.jp\/wp-content\/uploads\/2024\/07\/image-411.png\" alt=\"\" class=\"wp-image-5380\" \/><\/figure>\n\n\n\n<p class=\"has-medium-font-size\">\u4eca\u56de\u306flocal\u74b0\u5883\u3067\u53d7\u3051\u308b\u306e\u3067\u30b5\u30fc\u30d0\u306f<strong>localhost<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"537\" height=\"451\" src=\"https:\/\/blog.smartlight.co.jp\/wp-content\/uploads\/2024\/07\/image-412.png\" alt=\"\" class=\"wp-image-5381\" \/><\/figure>\n\n\n\n<p class=\"has-medium-font-size\">\u30fb<span class=\"marker2\">MQTT(windows\u5074)<\/span><br>\u7ba1\u7406\u8005\u6a29\u9650\u3067CMD\u3092\u7acb\u3061\u4e0a\u3052\u3066moquitto\u3092\u7acb\u3061\u4e0a\u3052\u307e\u3059<\/p>\n\n\n\n<p class=\"has-medium-font-size\">\u30fb\u7acb\u3061\u4e0a\u3052\u30b3\u30de\u30f3\u30c9<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-plain\"><code>net start mosquitto<\/code><\/pre><\/div>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"613\" height=\"102\" src=\"https:\/\/blog.smartlight.co.jp\/wp-content\/uploads\/2024\/07\/image-409.png\" alt=\"\" class=\"wp-image-5378\" \/><\/figure>\n\n\n\n<p class=\"has-medium-font-size\">\u30fb1883port\u304c\u958b\u3044\u3066\u3044\u308b\u304b\u3092\u78ba\u8a8d\u3059\u308b\u30b3\u30de\u30f3\u30c9<br>\u3000\u3053\u308c\u30671883\u304cLISTEN\u3055\u308c\u3066\u3044\u308b\u304b\u3092\u78ba\u8a8d<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-plain\"><code>netstat -an | find &quot;1883&quot;<\/code><\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">M5Stack\u306b\u66f8\u304d\u8fbc\u3093\u3067\u3044\u304f<\/h2>\n\n\n\n<p class=\"has-medium-font-size\">\u30fb\u66f8\u304d\u8fbc\u3080\u30b3\u30fc\u30c9<br>(\u4e00\u79d2\u306b\u4e00\u56de\u30c7\u30fc\u30bf\u3092\u9001\u308b)<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-plain\"><code>#include &lt;M5Stack.h&gt;\n#include &lt;WiFi.h&gt;\n#include &lt;PubSubClient.h&gt;\n\n\/\/ WiFi\u8a2d\u5b9a\nconst char* ssid = &quot;**********&quot;;  \/\/ WiFi\u306eSSID\nconst char* password = &quot;********&quot;;  \/\/ WiFi\u306e\u30d1\u30b9\u30ef\u30fc\u30c9\n\n\/\/ MQTT\u30d6\u30ed\u30fc\u30ab\u30fc\u306e\u8a2d\u5b9a\nconst char* mqtt_server = &quot;***********&quot;;  \/\/ \u30db\u30b9\u30c8\u30de\u30b7\u30f3\u306eIP\u30a2\u30c9\u30ec\u30b9\nconst char* mqtt_topic = &quot;\/m5stack\/data&quot;;  \/\/ MQTT\u30c8\u30d4\u30c3\u30af\n\nWiFiClient espClient;\nPubSubClient client(espClient);\n\nvoid setup_wifi() {\n  delay(10);\n  Serial.println();\n  Serial.print(&quot;Connecting to &quot;);\n  Serial.println(ssid);\n\n  WiFi.begin(ssid, password);\n\n  while (WiFi.status() != WL_CONNECTED) {\n    delay(500);\n    Serial.print(&quot;.&quot;);\n  }\n\n  Serial.println(&quot;&quot;);\n  Serial.println(&quot;WiFi connected&quot;);\n  Serial.print(&quot;IP address: &quot;);\n  Serial.println(WiFi.localIP());\n}\n\nvoid reconnect() {\n  \/\/ \u518d\u63a5\u7d9a\u30eb\u30fc\u30d7\n  while (!client.connected()) {\n    Serial.print(&quot;Attempting MQTT connection...&quot;);\n    \/\/ \u63a5\u7d9a\u3092\u8a66\u307f\u308b\n    if (client.connect(&quot;M5StackClient&quot;)) {\n      Serial.println(&quot;connected&quot;);\n    } else {\n      Serial.print(&quot;failed, rc=&quot;);\n      Serial.print(client.state());\n      Serial.println(&quot; try again in 5 seconds&quot;);\n      \/\/ \u518d\u8a66\u884c\u307e\u30675\u79d2\u5f85\u6a5f\n      delay(5000);\n    }\n  }\n}\nvoid setup() {\n  M5.begin();\n  Serial.begin(9600);\n  setup_wifi();\n  client.setServer(mqtt_server, 1883);\n\n  \/\/ MPU6886\u306e\u521d\u671f\u5316\n  if (M5.IMU.Init() != 0) {\n    Serial.println(&quot;MPU6886 initialization failed!&quot;);\n    while (1) {\n      delay(10);\n    }\n  }\n\n  Serial.println(&quot;MPU6886 initialized successfully!&quot;);\n}\n\n\nvoid loop() {\n  if (!client.connected()) {\n    reconnect();\n  }\n  client.loop();\n\n  float accX, accY, accZ;\n\n  \/\/ \u52a0\u901f\u5ea6\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\n  M5.IMU.getAccelData(&amp;accX, &amp;accY, &amp;accZ);\n\n  \/\/ JSON\u30da\u30a4\u30ed\u30fc\u30c9\u306e\u751f\u6210\n  String jsonPayload = &quot;{\\&quot;accX\\&quot;: &quot; + String(accX) + &quot;, \\&quot;accY\\&quot;: &quot; + String(accY) + &quot;, \\&quot;accZ\\&quot;: &quot; + String(accZ) + &quot;}&quot;;\n  char payload[100];\n  jsonPayload.toCharArray(payload, 100);\n\n  \/\/ MQTT\u30e1\u30c3\u30bb\u30fc\u30b8\u306e\u9001\u4fe1\n  client.publish(mqtt_topic, payload);\n\n  delay(1000);  \/\/ 1\u79d2\u5f85\u6a5f\n}\n\n<\/code><\/pre><\/div>\n\n\n\n<p class=\"has-medium-font-size\">\u30fbSSID\u3092\u78ba\u8a8d\u3059\u308b\u6642\u306f\u30cd\u30c3\u30c8\u30ef\u30fc\u30af\u30fb\u30a4\u30f3\u30bf\u30fc\u30cd\u30c3\u30c8\u3067\u63a5\u7d9a\u3057\u3066\u3044\u308b\u30cd\u30c3\u30c8\u30ef\u30fc\u30af\u306eSSID\u3092\u898b\u308b\u4e8b\u304c\u51fa\u6765\u307e\u3059<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"848\" height=\"632\" src=\"https:\/\/blog.smartlight.co.jp\/wp-content\/uploads\/2024\/07\/image-415.png\" alt=\"\" class=\"wp-image-5384\" \/><\/figure>\n\n\n\n<p class=\"has-medium-font-size\">MQTT\u3067\u3057\u3063\u304b\u308a\u3068\u9001\u308c\u307e\u3057\u305f<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"576\" src=\"https:\/\/blog.smartlight.co.jp\/wp-content\/uploads\/2024\/07\/image-418-1024x576.png\" alt=\"\" class=\"wp-image-5387\" srcset=\"https:\/\/blog.smartlight.co.jp\/wp-content\/uploads\/2024\/07\/image-418-1024x576.png 1024w, https:\/\/blog.smartlight.co.jp\/wp-content\/uploads\/2024\/07\/image-418-300x169.png 300w, https:\/\/blog.smartlight.co.jp\/wp-content\/uploads\/2024\/07\/image-418-768x432.png 768w, https:\/\/blog.smartlight.co.jp\/wp-content\/uploads\/2024\/07\/image-418-1536x863.png 1536w, https:\/\/blog.smartlight.co.jp\/wp-content\/uploads\/2024\/07\/image-418-320x180.png 320w, https:\/\/blog.smartlight.co.jp\/wp-content\/uploads\/2024\/07\/image-418-640x360.png 640w, https:\/\/blog.smartlight.co.jp\/wp-content\/uploads\/2024\/07\/image-418-1280x720.png 1280w, https:\/\/blog.smartlight.co.jp\/wp-content\/uploads\/2024\/07\/image-418.png 1916w, https:\/\/blog.smartlight.co.jp\/wp-content\/uploads\/2024\/07\/image-418-1024x576.png 856w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>\u306f\u3058\u3081\u306b \u4eca\u56de\u306f\u524d\u56de\u30bb\u30c3\u30c8\u30a2\u30c3\u30d7\u3057\u305fM5Stack\u306e\u4e2d\u306b\u3042\u308b\u52a0\u901f\u5ea6\u30bb\u30f3\u30b5\u306e\u60c5\u5831\u3092MQTT\u3067Node-RED\uff08Windows\uff09\u306b\u30c7\u30fc\u30bf\u3092\u9001\u3063\u3066\u3044\u304d\u307e\u3059 \u30bb\u30c3\u30c8\u30a2\u30c3\u30d7\u7de8 \u6e96\u5099 \u30fbArduino \u30e9\u30a4\u30d6\u30e9\u30ea\u30de\u30cd\u30fc\u30b8\u30e3\u30fc\u3067\u300cPu<\/p>\n","protected":false},"author":15,"featured_media":5388,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jin_ogp_image_url":"","_jin_last_featured_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[3],"tags":[],"class_list":["post-5362","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-intern"],"jetpack_featured_media_url":"https:\/\/blog.smartlight.co.jp\/wp-content\/uploads\/2024\/07\/image-419.png","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/blog.smartlight.co.jp\/index.php?rest_route=\/wp\/v2\/posts\/5362","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.smartlight.co.jp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.smartlight.co.jp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.smartlight.co.jp\/index.php?rest_route=\/wp\/v2\/users\/15"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.smartlight.co.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5362"}],"version-history":[{"count":2,"href":"https:\/\/blog.smartlight.co.jp\/index.php?rest_route=\/wp\/v2\/posts\/5362\/revisions"}],"predecessor-version":[{"id":5392,"href":"https:\/\/blog.smartlight.co.jp\/index.php?rest_route=\/wp\/v2\/posts\/5362\/revisions\/5392"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.smartlight.co.jp\/index.php?rest_route=\/wp\/v2\/media\/5388"}],"wp:attachment":[{"href":"https:\/\/blog.smartlight.co.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5362"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.smartlight.co.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5362"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.smartlight.co.jp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5362"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}