{"id":5445,"date":"2024-07-24T17:21:16","date_gmt":"2024-07-24T08:21:16","guid":{"rendered":"https:\/\/blog.smartlight.co.jp\/?p=5445"},"modified":"2024-07-24T17:21:16","modified_gmt":"2024-07-24T08:21:16","slug":"m5stack%e3%81%a7%e5%8f%96%e5%be%97%e3%81%97%e3%81%9fgps%e3%83%87%e3%83%bc%e3%82%bf%e3%82%92lte%e9%80%9a%e4%bf%a1%e3%81%a7%e7%a2%ba%e8%aa%8d%e3%81%99%e3%82%8b%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/blog.smartlight.co.jp\/?p=5445","title":{"rendered":"M5Stack\u3067\u53d6\u5f97\u3057\u305fGPS\u30c7\u30fc\u30bf\u3092LTE\u901a\u4fe1\u3067\u78ba\u8a8d\u3059\u308b\u65b9\u6cd5"},"content":{"rendered":"\n<div class=\"wp-block-jin-gb-block-box-with-headline kaisetsu-box5\"><div class=\"kaisetsu-box5-title\">\u76ee\u6b21<\/div>\n<p>\u30fb<a href=\"#a\">\u306f\u3058\u3081\u306b<\/a><\/p>\n\n\n\n<p>\u30fb<a href=\"#a1\">LTE\u901a\u4fe1\u306e\u8a2d\u5b9a(\u5909\u66f4\u70b9)<\/a><\/p>\n\n\n\n<p>\u30fb<a href=\"#a2\">Node-RED\u3067\u30c7\u30fc\u30bf\u3092\u78ba\u8a8d<\/a><\/p>\n\n\n\n<p><\/p>\n<\/div>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"a\">\u306f\u3058\u3081\u306b<\/h2>\n\n\n\n<p>\u3000\u4eca\u56de\u306f\u300cM5Stack Basic V2.7\u300d\u3068\u300cCatM + GNSS\u300d\u3092\u4f7f\u7528\u3057\u3066\u53d6\u5f97\u3057\u305fGPS(\u4f4d\u7f6e\u60c5\u5831)\u3092LTE\u901a\u4fe1\u3067\u78ba\u8a8d\u3059\u308b\u65b9\u6cd5\u3092\u7d39\u4ecb\u3059\u308b\u3002<br>\u3000\u203bSIM\u30ab\u30fc\u30c9\u304c\u306a\u3044\u305f\u3081\u3001\u5b9f\u969b\u306e\u5b9f\u88c5\u306f\u3057\u3066\u3044\u306a\u3044\u3002<\/p>\n\n\n\n<p>\u3000\u524d\u56de\u306e\u8a18\u4e8b\u3067\u306f\u30b7\u30ea\u30a2\u30eb\u901a\u4fe1\u3067\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3059\u308b\u65b9\u6cd5\u3092\u307e\u3068\u3081\u3066\u3044\u308b\u3001\u4eca\u56de\u306e\u8a18\u4e8b\u3067\u306fLTE\u901a\u4fe1\u3067\u884c\u3046\u5834\u5408\u306e\u5909\u66f4\u70b9\u306e\u307f\u3092\u7d39\u4ecb\u3059\u308b\u305f\u3081\u3001\u5927\u90e8\u5206\u3092\u629c\u7c8b\u3057\u3066\u3044\u308b\u3002<\/p>\n\n\n\nhttps:\/\/blog.smartlight.co.jp\/?p=5425\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"a1\">LTE\u901a\u4fe1\u306e\u8a2d\u5b9a(\u5909\u66f4\u70b9)<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u3000\u30cf\u30fc\u30c9\u30a6\u30a7\u30a2\u63a5\u7d9a<\/h3>\n\n\n\n<p>\u30001. CatM+GNSS\u306bLTE\u30a2\u30f3\u30c6\u30ca\u3092\u53d6\u308a\u4ed8\u3051\u308b<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"511\" height=\"791\" src=\"https:\/\/blog.smartlight.co.jp\/wp-content\/uploads\/2024\/07\/image-461.png\" alt=\"\" class=\"wp-image-5453\" style=\"width:251px;height:auto\" \/><\/figure>\n\n\n\n<p>\u30002. CatM+GNSS\u306bSIM\u30ab\u30fc\u30c9\u3092\u633f\u5165\u3059\u308b <\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"586\" src=\"https:\/\/blog.smartlight.co.jp\/wp-content\/uploads\/2024\/07\/image-462-1024x586.png\" alt=\"\" class=\"wp-image-5454\" style=\"width:461px;height:auto\" srcset=\"https:\/\/blog.smartlight.co.jp\/wp-content\/uploads\/2024\/07\/image-462-1024x586.png 1024w, https:\/\/blog.smartlight.co.jp\/wp-content\/uploads\/2024\/07\/image-462-300x172.png 300w, https:\/\/blog.smartlight.co.jp\/wp-content\/uploads\/2024\/07\/image-462-768x439.png 768w, https:\/\/blog.smartlight.co.jp\/wp-content\/uploads\/2024\/07\/image-462.png 1075w, https:\/\/blog.smartlight.co.jp\/wp-content\/uploads\/2024\/07\/image-462-1024x586.png 856w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u3000\u30b3\u30fc\u30c9\u6982\u8981<\/h3>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-plain\"><code>#include &lt;M5Stack.h&gt;\n#include &lt;TinyGsmClient.h&gt;\n#include &lt;PubSubClient.h&gt;\n\n\/\/ GPRS\u8a8d\u8a3c\u60c5\u5831\nconst char apn[] = &quot;your_apn&quot;;  \/\/ \u4f7f\u7528\u3059\u308bAPN\u306b\u7f6e\u304d\u63db\u3048\u3066\u304f\u3060\u3055\u3044\nconst char gprsUser[] = &quot;&quot;;     \/\/ \u5fc5\u8981\u306a\u3051\u308c\u3070\u7a7a\u306e\u307e\u307e\u3067OK\nconst char gprsPass[] = &quot;&quot;;     \/\/ \u5fc5\u8981\u306a\u3051\u308c\u3070\u7a7a\u306e\u307e\u307e\u3067OK\n\n\/\/ MQTT\u8a2d\u5b9a\nconst char* mqtt_server = &quot;your_mqtt_server&quot;; \/\/ MQTT\u30d6\u30ed\u30fc\u30ab\u30fc\u306eIP\u30a2\u30c9\u30ec\u30b9\u307e\u305f\u306f\u30db\u30b9\u30c8\u540d\nconst char* mqtt_topic = &quot;\/m5stack\/gps&quot;;  \/\/ MQTT\u30c8\u30d4\u30c3\u30af\n\nTinyGsm modem(Serial2);\nTinyGsmClient espClient(modem);\nPubSubClient client(espClient);\n\n\/\/ GPS\u30e2\u30b8\u30e5\u30fc\u30eb\u3068\u306e\u30b7\u30ea\u30a2\u30eb\u63a5\u7d9a\u3092\u5b9a\u7fa9\n#define SerialGPS Serial2\n\nvoid setup_modem() {\n  Serial.println(&quot;\u30e2\u30c7\u30e0\u3092\u521d\u671f\u5316\u3057\u3066\u3044\u307e\u3059...&quot;);\n  modem.restart();\n\n  \/\/ \u5fc5\u8981\u306b\u5fdc\u3058\u3066SIM\u30ab\u30fc\u30c9\u306ePIN\u3092\u89e3\u9664\u3057\u307e\u3059\n  \/\/ modem.simUnlock(&quot;1234&quot;);\n\n  Serial.print(&quot;\u30cd\u30c3\u30c8\u30ef\u30fc\u30af\u3092\u5f85\u3063\u3066\u3044\u307e\u3059...&quot;);\n  if (!modem.waitForNetwork()) {\n    Serial.println(&quot; \u5931\u6557&quot;);\n    while (true);\n  }\n  Serial.println(&quot; \u6210\u529f&quot;);\n\n  Serial.print(F(&quot;APN\u306b\u63a5\u7d9a\u3057\u3066\u3044\u307e\u3059: &quot;));\n  Serial.print(apn);\n  if (!modem.gprsConnect(apn, gprsUser, gprsPass)) {\n    Serial.println(&quot; \u5931\u6557&quot;);\n    while (true);\n  }\n  Serial.println(&quot; \u6210\u529f&quot;);\n\n  if (modem.isGprsConnected()) {\n    Serial.println(&quot;GPRS\u306b\u63a5\u7d9a\u3055\u308c\u307e\u3057\u305f&quot;);\n  }\n}\n\nvoid reconnect() {\n  \/\/ \u518d\u63a5\u7d9a\u30eb\u30fc\u30d7\n  while (!client.connected()) {\n    Serial.print(&quot;MQTT\u63a5\u7d9a\u3092\u8a66\u307f\u3066\u3044\u307e\u3059...&quot;);\n    \/\/ \u63a5\u7d9a\u3092\u8a66\u307f\u308b\n    if (client.connect(&quot;M5StackClient&quot;)) {\n      Serial.println(&quot;\u63a5\u7d9a\u3055\u308c\u307e\u3057\u305f&quot;);\n    } else {\n      Serial.print(&quot;\u5931\u6557, rc=&quot;);\n      Serial.print(client.state());\n      Serial.println(&quot; 5\u79d2\u5f8c\u306b\u518d\u8a66\u884c\u3057\u307e\u3059&quot;);\n      \/\/ \u518d\u8a66\u884c\u307e\u30675\u79d2\u5f85\u6a5f\n      delay(5000);\n    }\n  }\n}\n\nvoid setup() {\n  M5.begin();\n  Serial.begin(115200); \/\/ \u30c7\u30d0\u30c3\u30b0\u7528\u30b7\u30ea\u30a2\u30eb\u901a\u4fe1\n  SerialGPS.begin(9600, SERIAL_8N1, 16, 17); \/\/ GPS\u30e2\u30b8\u30e5\u30fc\u30eb\u3068\u306e\u30b7\u30ea\u30a2\u30eb\u63a5\u7d9a\u8a2d\u5b9a\n\n  Serial.println(&quot;GPS\u30e2\u30b8\u30e5\u30fc\u30eb\u3092\u521d\u671f\u5316\u3057\u3066\u3044\u307e\u3059...&quot;);\n\n  \/\/ GPS\u30e2\u30b8\u30e5\u30fc\u30eb\u306e\u521d\u671f\u5316\n  SerialGPS.println(&quot;AT+CGNSPWR=1&quot;); \/\/ GNSS\u96fb\u6e90\u3092\u30aa\u30f3\u306b\u3059\u308b\n  delay(1000);\n  while (SerialGPS.available()) {\n    char c = SerialGPS.read();\n    Serial.print(c);\n  }\n  Serial.println();\n\n  SerialGPS.println(&quot;AT+CGNSSEQ=\\&quot;RMC\\&quot;&quot;); \/\/ GNSS\u51fa\u529b\u30b7\u30fc\u30b1\u30f3\u30b9\u3092\u8a2d\u5b9a\n  delay(1000);\n  while (SerialGPS.available()) {\n    char c = SerialGPS.read();\n    Serial.print(c);\n  }\n  Serial.println();\n\n  Serial.println(&quot;GPS\u521d\u671f\u5316\u5b8c\u4e86&quot;);\n\n  setup_modem();\n  client.setServer(mqtt_server, 1883);\n}\n\nvoid loop() {\n  if (!client.connected()) {\n    reconnect();\n  }\n  client.loop();\n\n  SerialGPS.println(&quot;AT+CGNSINF&quot;); \/\/ GNSS\u60c5\u5831\u3092\u7167\u4f1a\n  delay(1000);\n\n  String gpsData = &quot;&quot;;\n  while (SerialGPS.available()) {\n    char c = SerialGPS.read();\n    gpsData += c;\n  }\n\n  if (gpsData.length() &gt; 0) {\n    Serial.println(&quot;\u30c7\u30fc\u30bf\u3092\u53d7\u4fe1\u3057\u307e\u3057\u305f:&quot;);\n    Serial.println(gpsData);\n\n    \/\/ \u53d7\u4fe1\u30c7\u30fc\u30bf\u306e\u89e3\u6790\n    if (gpsData.startsWith(&quot;AT+CGNSINF&quot;)) {\n      int index = gpsData.indexOf(&quot;:&quot;);\n      if (index != -1) {\n        String gpsInfo = gpsData.substring(index + 1);\n        String jsonPayload = parseGPSData(gpsInfo);\n\n        \/\/ MQTT\u30e1\u30c3\u30bb\u30fc\u30b8\u306e\u9001\u4fe1\n        client.publish(mqtt_topic, jsonPayload.c_str());\n\n        \/\/ \u30c7\u30d0\u30c3\u30b0\u7528\u306b\u30b7\u30ea\u30a2\u30eb\u51fa\u529b\n        Serial.println(&quot;GPS\u30c7\u30fc\u30bf\u3092\u9001\u4fe1\u3057\u307e\u3057\u305f:&quot;);\n        Serial.println(jsonPayload);\n      }\n    } else {\n      Serial.println(&quot;\u6709\u52b9\u306aGPS\u30c7\u30fc\u30bf\u304c\u3042\u308a\u307e\u305b\u3093&quot;);\n    }\n  }\n  delay(5000); \/\/ 5\u79d2\u5f85\u6a5f\u5f8c\u3001\u518d\u5ea6\u7167\u4f1a\n}\n\nString parseGPSData(String gpsInfo) {\n  gpsInfo.trim();\n  String data[20];\n  int index = 0;\n\n  while (gpsInfo.length() &gt; 0 &amp;&amp; index &lt; 20) {\n    int commaIndex = gpsInfo.indexOf(&quot;,&quot;);\n    if (commaIndex == -1) {\n      data[index++] = gpsInfo;\n      break;\n    } else {\n      data[index++] = gpsInfo.substring(0, commaIndex);\n      gpsInfo = gpsInfo.substring(commaIndex + 1);\n    }\n  }\n\n  String jsonPayload = &quot;{&quot;;\n  jsonPayload += &quot;\\&quot;latitude\\&quot;: &quot; + data<img decoding=\"async\" class=\"ranking-number\" src=\"https:\/\/blog.smartlight.co.jp\/wp-content\/themes\/jin\/img\/rank03.png\" \/> + &quot;,&quot;;\n  jsonPayload += &quot;\\&quot;longitude\\&quot;: &quot; + data<img decoding=\"async\" class=\"ranking-number\" src=\"https:\/\/blog.smartlight.co.jp\/wp-content\/themes\/jin\/img\/rank04.png\" \/> + &quot;,&quot;;\n  jsonPayload += &quot;\\&quot;altitude\\&quot;: &quot; + data[5] + &quot;,&quot;;\n  jsonPayload += &quot;\\&quot;speed\\&quot;: &quot; + data[6];\n  jsonPayload += &quot;}&quot;;\n\n  return jsonPayload;\n}<\/code><\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u3000\u30b3\u30fc\u30c9\u306e\u8aac\u660e<\/h3>\n\n\n\n<p>\u3000 GPRS\u306e\u8a2d\u5b9a<br>\u3000\u3000\u30fb\u4f7f\u7528\u3059\u308bAPN\u306b\u5408\u308f\u305b\u305f\u8a2d\u5b9a\u3092\u884c\u3046<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"169\" src=\"https:\/\/blog.smartlight.co.jp\/wp-content\/uploads\/2024\/07\/image-454-1024x169.png\" alt=\"\" class=\"wp-image-5446\" srcset=\"https:\/\/blog.smartlight.co.jp\/wp-content\/uploads\/2024\/07\/image-454-1024x169.png 1024w, https:\/\/blog.smartlight.co.jp\/wp-content\/uploads\/2024\/07\/image-454-300x50.png 300w, https:\/\/blog.smartlight.co.jp\/wp-content\/uploads\/2024\/07\/image-454-768x127.png 768w, https:\/\/blog.smartlight.co.jp\/wp-content\/uploads\/2024\/07\/image-454.png 1130w, https:\/\/blog.smartlight.co.jp\/wp-content\/uploads\/2024\/07\/image-454-1024x169.png 856w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>\u3000\u3000\u30fb MQTT\u30b5\u30fc\u30d0\u30fc\u306e\u8a2d\u5b9a\u3092\u3059\u308b<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"99\" src=\"https:\/\/blog.smartlight.co.jp\/wp-content\/uploads\/2024\/07\/image-459-1024x99.png\" alt=\"\" class=\"wp-image-5451\" srcset=\"https:\/\/blog.smartlight.co.jp\/wp-content\/uploads\/2024\/07\/image-459-1024x99.png 1024w, https:\/\/blog.smartlight.co.jp\/wp-content\/uploads\/2024\/07\/image-459-300x29.png 300w, https:\/\/blog.smartlight.co.jp\/wp-content\/uploads\/2024\/07\/image-459-768x75.png 768w, https:\/\/blog.smartlight.co.jp\/wp-content\/uploads\/2024\/07\/image-459.png 1515w, https:\/\/blog.smartlight.co.jp\/wp-content\/uploads\/2024\/07\/image-459-1024x99.png 856w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>\u3000\u3000\u30fb\u305d\u306e\u4ed6Wifi\u304b\u3089\u30e2\u30c7\u30e0\u306e\u8a2d\u5b9a\u306b\u5909\u66f4\u3059\u308b<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"781\" height=\"117\" src=\"https:\/\/blog.smartlight.co.jp\/wp-content\/uploads\/2024\/07\/image-456.png\" alt=\"\" class=\"wp-image-5448\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"a2\">Node-RED\u3067\u30c7\u30fc\u30bf\u3092\u78ba\u8a8d<\/h2>\n\n\n\n<p> \u3000\u30fb\u30b5\u30fc\u30d0\u3092LTE\u3067\u30c7\u30fc\u30bf\u3092\u98db\u3070\u3057\u305fMQTT\u30d6\u30ed\u30fc\u30ab\u30fc\u306eIP\u30a2\u30c9\u30ec\u30b9\u306b\u5909\u66f4\u3059\u308b<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"841\" src=\"https:\/\/blog.smartlight.co.jp\/wp-content\/uploads\/2024\/07\/image-457-1024x841.png\" alt=\"\" class=\"wp-image-5449\" srcset=\"https:\/\/blog.smartlight.co.jp\/wp-content\/uploads\/2024\/07\/image-457-1024x841.png 1024w, https:\/\/blog.smartlight.co.jp\/wp-content\/uploads\/2024\/07\/image-457-300x246.png 300w, https:\/\/blog.smartlight.co.jp\/wp-content\/uploads\/2024\/07\/image-457-768x631.png 768w, https:\/\/blog.smartlight.co.jp\/wp-content\/uploads\/2024\/07\/image-457.png 1157w, https:\/\/blog.smartlight.co.jp\/wp-content\/uploads\/2024\/07\/image-457-1024x841.png 856w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>\u306f\u3058\u3081\u306b \u3000\u4eca\u56de\u306f\u300cM5Stack Basic V2.7\u300d\u3068\u300cCatM + GNSS\u300d\u3092\u4f7f\u7528\u3057\u3066\u53d6\u5f97\u3057\u305fGPS(\u4f4d\u7f6e\u60c5\u5831)\u3092LTE\u901a\u4fe1\u3067\u78ba\u8a8d\u3059\u308b\u65b9\u6cd5\u3092\u7d39\u4ecb\u3059\u308b\u3002\u3000\u203bSIM\u30ab\u30fc\u30c9\u304c\u306a\u3044\u305f\u3081\u3001\u5b9f\u969b\u306e\u5b9f\u88c5\u306f\u3057\u3066\u3044\u306a\u3044\u3002 \u3000\u524d\u56de<\/p>\n","protected":false},"author":16,"featured_media":5452,"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-5445","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-460.png","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/blog.smartlight.co.jp\/index.php?rest_route=\/wp\/v2\/posts\/5445","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\/16"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.smartlight.co.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5445"}],"version-history":[{"count":2,"href":"https:\/\/blog.smartlight.co.jp\/index.php?rest_route=\/wp\/v2\/posts\/5445\/revisions"}],"predecessor-version":[{"id":5456,"href":"https:\/\/blog.smartlight.co.jp\/index.php?rest_route=\/wp\/v2\/posts\/5445\/revisions\/5456"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.smartlight.co.jp\/index.php?rest_route=\/wp\/v2\/media\/5452"}],"wp:attachment":[{"href":"https:\/\/blog.smartlight.co.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5445"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.smartlight.co.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5445"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.smartlight.co.jp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5445"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}