{"id":1925,"date":"2021-08-09T17:25:04","date_gmt":"2021-08-09T08:25:04","guid":{"rendered":"https:\/\/stacstar.jp\/blog\/?p=1925"},"modified":"2021-08-09T17:25:04","modified_gmt":"2021-08-09T08:25:04","slug":"m5stick-%e3%82%92%e4%bd%bf%e3%81%a3%e3%81%9f%e5%91%bc%e3%81%b3%e9%88%b4","status":"publish","type":"post","link":"https:\/\/stacstar.jp\/blog\/?p=1925","title":{"rendered":"M5Stick \u3092\u4f7f\u3063\u305f\u547c\u3073\u9234"},"content":{"rendered":"<div style=\"width: 792px;\" class=\"wp-video\"><video class=\"wp-video-shortcode\" id=\"video-1925-1\" width=\"792\" height=\"900\" preload=\"metadata\" controls=\"controls\"><source type=\"video\/mp4\" src=\"https:\/\/stacstar.jp\/blog\/.\/wp-content\/uploads\/ring.mp4?_=1\" \/><a href=\"https:\/\/stacstar.jp\/blog\/.\/wp-content\/uploads\/ring.mp4\">https:\/\/stacstar.jp\/blog\/.\/wp-content\/uploads\/ring.mp4<\/a><\/video><\/div>\n<p>\u30ea\u30d3\u30f3\u30b0\u304b\u3089\u547c\u3093\u3067\u3082\u3089\u3046\u3068\u304d\u306e\u30a4\u30f3\u30bf\u30fc\u30d5\u30a9\u30f3\u7684\u306a\u3082\u306e\u3092\u3002Arduino (M5Stick) \u3092\u4f7f\u3063\u3066\u3001\u3053\u3061\u3089\u306e\u90e8\u5c4b\u306e Raspberry Pi \u306b OSC \u3092\u98db\u3070\u3057\u3066\u96fb\u5b50\u30d6\u30b6\u30fc\u3092\u9cf4\u3089\u3059\u3002\u53d7\u4fe1\u3057\u305f\u969b\u306b\u4eca\u5ea6\u306f\u9006\u5074\u306b\u53d7\u4fe1\u4fe1\u53f7\u3092\u6e21\u3057\u3066 Arduino \u5074\u306e\u30c7\u30a3\u30b9\u30d7\u30ec\u30a4\u306b\u53d7\u4fe1\u3057\u305f\u3088(\u9752\u306b\u306a\u308b\u3060\u3051)\u3068\u8868\u793a\u3002\u30dc\u30bf\u30f3\u3092\u62bc\u3059\u3068\u300c\u308f\u304b\u3063\u305f\u3088\u30fc\u300d\u3068\u3044\u3046\u3053\u3068\u3067\u518d\u3073 OSC \u3092\u98db\u3070\u3057\u3066\u6db2\u6676\u3068 LED \u30e9\u30a4\u30c8\u3092\u6d88\u3059\u3002<br \/>\n\u3064\u3044\u3067\u306b\u4ee5\u524d\u3084\u3063\u305f\u4e8c\u9178\u5316\u70ad\u7d20\u6fc3\u5ea6\u3082950\u3092\u8d85\u3048\u308b\u3068 LED \u304c\u4ed8\u304f\u3088\u3046\u306b\u3057\u3066\u3001SpreadSheet \u3092\u898b\u3066\u306a\u304f\u3066\u3082\u3044\u3044\u3088\u3046\u306b\u3002<\/p>\n<p><!--more--><\/p>\n<p>\u30b3\u30fc\u30c9\u306f\u304b\u306a\u308a\u9069\u5f53\u306a\u306e\u3067\u30a4\u30b1\u3066\u306a\u3044\u3002<\/p>\n<p>[\u547c\u3073\u51fa\u3057\u9001\u4fe1\u5074] (M5Stick)<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n#include &lt;M5StickC.h&gt;\r\n#include &lt;ArduinoOSCWiFi.h&gt;\r\n\r\n#define BUTTON01_PIN  37               \/\/ \u771f\u4e2d\u306e\u30dc\u30bf\u30f3\r\n#define LED_PIN       10.              \/\/ LED\r\n#define SEND_PORT     55555            \/\/ OSC \u3067\u4f7f\u3046\u30dd\u30fc\u30c8\u756a\u53f7\r\n#define SEND_IP       &quot;&quot;               \/\/ \u53d7\u3051\u53d6\u308b\u5074\u306e IP\r\n#define LED_ON        LOW\r\n#define LED_OFF       HIGH\r\n#define BUTTON_ON     LOW\r\n#define BUTTON_OFF    HIGH\r\n\r\nconst char* ssid = &quot;&quot;;                 \/\/ WiFi \u306e SSID\r\nconst char* pw = &quot;&quot;;                   \/\/ WiFi \u306e \u30d1\u30b9\u30ef\u30fc\u30c9\r\nIPAddress ip(192, 168, ?, ?);          \/\/ \u56fa\u5b9a\u3057\u305f\u3044 IP\r\nIPAddress gateway(192,168, ?, ?);\r\nIPAddress subnet(255, 255, 255, 0);\r\nIPAddress DNS(192,168, ?, ?);\r\n\r\nbool received = false;\r\nbool gotit = false;\r\n\r\nvoid setup() {\r\n  pinMode(BUTTON01_PIN, INPUT);\r\n  pinMode(LED_PIN,      OUTPUT);\r\n\r\n  Serial.begin(115200);\r\n  while (!Serial) {\r\n    delay(100);\r\n  }\r\n  Serial.println(&quot;Serial OK&quot;);\r\n\r\n  WiFi.disconnect();\r\n  while (WiFi.status() == WL_DISCONNECTED) {\r\n    Serial.println(&quot;WiFi disconnecting...&quot;);\r\n    delay(1000);\r\n  }\r\n  \r\n  WiFi.config(ip, gateway, subnet, DNS);\r\n  WiFi.begin(ssid, pw);\r\n\r\n  while (WiFi.status() != WL_CONNECTED) {\r\n    Serial.println(&quot;WiFi connecting...&quot;);\r\n    delay(1000);\r\n  }\r\n\r\n  Serial.println(&quot;WiFi connected&quot;);\r\n  digitalWrite(LED_PIN, LED_OFF);\r\n\r\n  OscWiFi.subscribe(SEND_PORT, &quot;\/ring\/receive&quot;,\r\n      &#x5B;&amp;](bool _gotit) {\r\n        Serial.println(&quot;Receive&quot;);\r\n        if (_gotit) gotit = true;\r\n        else received = true;\r\n      }\r\n  );\r\n\r\n  M5.begin();\r\n  M5.Axp.ScreenBreath(8);\r\n  M5.Lcd.setRotation(0);\r\n  M5.Lcd.fillScreen(WHITE);\r\n  M5.Lcd.setTextSize(1);\r\n  M5.Lcd.print(&quot;SUCCESS&quot;);\r\n\r\n  delay(1000);\r\n  M5.Axp.ScreenBreath(6);\r\n  M5.Lcd.fillScreen(BLACK);\r\n}\r\n\r\nvoid loop() {\r\n  if (digitalRead(BUTTON01_PIN) == BUTTON_ON) {\r\n    OscWiFi.send(SEND_IP, SEND_PORT, &quot;\/ring\/call&quot;, true);\r\n    digitalWrite(LED_PIN, LED_ON);\r\n    M5.Axp.ScreenBreath(10);\r\n    M5.Lcd.fillScreen(WHITE);\r\n    delay(1000);\r\n  }\r\n  else if (received)\r\n  {\r\n    M5.Lcd.fillScreen(BLUE);\r\n    received = false;\r\n  }\r\n  else if (gotit)\r\n  {\r\n    digitalWrite(LED_PIN, LED_OFF);\r\n    M5.Axp.ScreenBreath(6);\r\n    M5.Lcd.fillScreen(BLACK);\r\n    gotit = false;\r\n  }\r\n\r\n  OscWiFi.update();\r\n}\r\n<\/pre>\n<p>[\u53d7\u4fe1\u5074] (Raspberry Pi)<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nimport socket\r\nimport threading\r\nimport time\r\nimport RPi.GPIO as GPIO\r\n\r\nfrom pythonosc import osc_server\r\nfrom pythonosc.dispatcher import Dispatcher\r\nfrom pythonosc import udp_client\r\nfrom pythonosc.osc_message_builder import OscMessageBuilder\r\n\r\nBUZZER_PIN = ? \/\/ \u30d6\u30b6\u30fc\u306e\u30d4\u30f3\r\nTACT_PIN = ? \/\/ \u30bf\u30af\u30c8\u30b9\u30a4\u30c3\u30c1\u306e\u30d4\u30f3\r\nBUZZER_ON = GPIO.HIGH\r\nBUZZER_OFF = GPIO.LOW\r\nTACT_ON = GPIO.HIGH\r\n\r\nIP = &quot;&quot;;\r\nSEND_IP = &quot;&quot; \/\/ \u9001\u308b IP\r\nPORT = 55555 \/\/ OSC \u3067\u4f7f\u3046\u30dd\u30fc\u30c8\u756a\u53f7\r\n\r\nGPIO.setmode(GPIO.BCM)\r\nGPIO.setup(BUZZER_PIN, GPIO.OUT)\r\nGPIO.setup(TACT_PIN, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) # innter pull down resitance\r\n\r\n# Get local IP\r\nfor i in range(20):\r\n    try:\r\n        IP = socket.gethostbyname(&quot;raspberrypi.local&quot;)\r\n    except Exception:\r\n        time.sleep(3);\r\n    else:\r\n        break\r\n\r\n\r\n# Receive OSC\r\ndef on_received(address, message):\r\n    client.send(msg_received.build())\r\n        \r\n    for num in range(6):\r\n        GPIO.output(BUZZER_PIN, BUZZER_ON if num % 2 == 0 else BUZZER_OFF)\r\n        time.sleep(0.2)\r\n\r\ndispatcher = Dispatcher()\r\ndispatcher.map(&quot;\/ring\/call&quot;, on_received)\r\n\r\nserver = osc_server.ThreadingOSCUDPServer((IP, PORT), dispatcher)\r\nserver_thread = threading.Thread(target=server.serve_forever);\r\nserver_thread.start()\r\n\r\n\r\n# Send OSC\r\nclient = udp_client.UDPClient(SEND_IP, PORT)\r\nmsg_gotit = OscMessageBuilder(address='\/ring\/receive')\r\nmsg_gotit.add_arg(True)\r\nmsg_received = OscMessageBuilder(address='\/ring\/receive')\r\nmsg_received.add_arg(False)\r\n\r\ntry:\r\n    while True:\r\n        if GPIO.input(TACT_PIN) == TACT_ON:\r\n            client.send(msg_gotit.build())\r\n            time.sleep(0.5)\r\nfinally:\r\n    GPIO.cleanup()\r\n    server.shutdown()\r\n    print('--- stop program ---');\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u30ea\u30d3\u30f3\u30b0\u304b\u3089\u547c\u3093\u3067\u3082\u3089\u3046\u3068\u304d\u306e\u30a4\u30f3\u30bf\u30fc\u30d5\u30a9\u30f3\u7684\u306a\u3082\u306e\u3092\u3002Arduino (M5Stick) \u3092\u4f7f\u3063\u3066\u3001\u3053\u3061\u3089\u306e\u90e8\u5c4b\u306e Raspberry Pi \u306b OSC \u3092\u98db\u3070\u3057\u3066\u96fb\u5b50\u30d6\u30b6\u30fc\u3092\u9cf4\u3089\u3059\u3002\u53d7\u4fe1\u3057\u305f\u969b\u306b\u4eca\u5ea6\u306f\u9006\u5074\u306b\u53d7\u4fe1\u4fe1\u53f7\u3092 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25,26,24],"tags":[],"class_list":["post-1925","post","type-post","status-publish","format-standard","hentry","category-arduino","category-electronics","category-raspberrypi"],"_links":{"self":[{"href":"https:\/\/stacstar.jp\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1925","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/stacstar.jp\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/stacstar.jp\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/stacstar.jp\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/stacstar.jp\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1925"}],"version-history":[{"count":6,"href":"https:\/\/stacstar.jp\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1925\/revisions"}],"predecessor-version":[{"id":1932,"href":"https:\/\/stacstar.jp\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1925\/revisions\/1932"}],"wp:attachment":[{"href":"https:\/\/stacstar.jp\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1925"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/stacstar.jp\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1925"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/stacstar.jp\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1925"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}