{"id":534,"date":"2015-10-13T01:24:06","date_gmt":"2015-10-12T16:24:06","guid":{"rendered":"http:\/\/goji2100.s199.coreserver.jp\/blog\/?p=534"},"modified":"2022-04-11T07:11:13","modified_gmt":"2022-04-10T22:11:13","slug":"%e5%8a%a0%e9%80%9f%e5%ba%a6%e3%82%bb%e3%83%b3%e3%82%b5adxl345%e3%82%92%e4%bd%bf%e3%81%a3%e3%81%9fwi-fi%e3%83%a9%e3%82%b8%e3%82%b3%e3%83%b3%e3%83%bb%e3%82%b3%e3%83%b3%e3%83%88%e3%83%ad%e3%83%bc","status":"publish","type":"post","link":"https:\/\/goji2100.com\/blog\/?p=534","title":{"rendered":"\u52a0\u901f\u5ea6\u30bb\u30f3\u30b5(ADXL345)\u3092\u4f7f\u3063\u305fWi-Fi\u30e9\u30b8\u30b3\u30f3\u30fb\u30b3\u30f3\u30c8\u30ed\u30fc\u30e9"},"content":{"rendered":"<p>\u52a0\u901f\u5ea6\u30bb\u30f3\u30b5(ADXL345)\u3092\u4f7f\u3063\u305fWi-Fi\u30e9\u30b8\u30b3\u30f3\u30ab\u30fc\u306e\u30b3\u30f3\u30c8\u30ed\u30fc\u30e9\u3092\u4f5c\u308a\u307e\u3057\u305f\u3002<br \/>\n\u30b9\u30de\u30db\u3092\u4f7f\u3048\u306a\u3044\u5e7c\u5150\u5411\u3051\u306e\u30b3\u30f3\u30c8\u30ed\u30fc\u30e9\u306b\u306a\u308a\u307e\u3059\u3002<br \/>\nESP-WROOM-02\u306bI2C\u3067\uff13\u8ef8\u52a0\u901f\u5ea6\u30bb\u30f3\u30b5(ADXL345)\u3092\u63a5\u7d9a\u3057\u307e\u3059\u3002<br \/>\n\u30b3\u30f3\u30c8\u30ed\u30fc\u30e9\u304b\u3089Wi-Fi AP(SoftAP)\u30e2\u30fc\u30c9\u306e\u30e9\u30b8\u30b3\u30f3\u30ab\u30fc\u306b\u63a5\u7d9a\u3057\u3001UDP\u30d1\u30b1\u30c3\u30c8\u3067\u5de6\u53f3\u306e\u30e2\u30fc\u30bf\u306e\u901f\u5ea6\u5236\u5fa1\u30c7\u30fc\u30bf\u3092\u9001\u4fe1\u3057\u307e\u3059\u3002<br \/>\nUDP\u30d1\u30b1\u30c3\u30c8\u3092iOSC\u306e\u5f62\u5f0f\u306b\u5408\u308f\u305b\u308b\u3053\u3068\u3067\u30e9\u30b8\u30b3\u30f3\u30ab\u30fc\u5074\u306e\u5909\u66f4\u3092\u4e0d\u8981\u306b\u3057\u307e\u3059\u3002<br \/>\n\u3053\u306e\u30b3\u30f3\u30c8\u30ed\u30fc\u30e9\u3092\u304a\u3082\u3061\u3083\u306e\u300c\u9b54\u6cd5\u306e\u6756\u300d\u3068\u304b\u300c\u304a\u304d\u3042\u304c\u308a\u3053\u307c\u3057\u300d\u306a\u3069\u306b\u7d44\u8fbc\u3081\u3070\u9762\u767d\u3044\u304b\u3068\u30fb\u30fb\u30fb(^-^;<\/p>\n<div id=\"attachment_535\" style=\"width: 310px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/goji2100.com\/blog\/wp-content\/uploads\/2015\/10\/CONTROL.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-535\" class=\"wp-image-535 size-medium\" src=\"https:\/\/goji2100.com\/blog\/wp-content\/uploads\/2015\/10\/CONTROL-300x183.jpg\" alt=\"\u64cd\u4f5c\u65b9\u6cd5\" width=\"300\" height=\"183\" srcset=\"https:\/\/goji2100.com\/blog\/wp-content\/uploads\/2015\/10\/CONTROL-300x183.jpg 300w, https:\/\/goji2100.com\/blog\/wp-content\/uploads\/2015\/10\/CONTROL.jpg 1024w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><p id=\"caption-attachment-535\" class=\"wp-caption-text\">\u64cd\u4f5c\u306e\u65b9\u6cd5<\/p><\/div>\n<p>\u3053\u308c\u3092\u4f7f\u3063\u3066\u64cd\u4f5c\u3057\u3066\u307f\u307e\u3057\u305f\u3002<br \/>\n<iframe loading=\"lazy\" width=\"480\" height=\"300\" src=\"https:\/\/www.youtube.com\/embed\/aal9cHwglsY\" frameborder=\"0\" allowfullscreen><\/iframe><\/p>\n<p><!--more \u3010\u7d9a\u304d\u3092\u8aad\u3080\u3011--><\/p>\n<div id=\"attachment_536\" style=\"width: 310px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/goji2100.com\/blog\/wp-content\/uploads\/2015\/10\/IMG_1726S.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-536\" class=\"wp-image-536 size-medium\" src=\"https:\/\/goji2100.com\/blog\/wp-content\/uploads\/2015\/10\/IMG_1726S-300x188.jpg\" alt=\"\u30b3\u30f3\u30c8\u30ed\u30fc\u30e9\u5168\u4f53\" width=\"300\" height=\"188\" srcset=\"https:\/\/goji2100.com\/blog\/wp-content\/uploads\/2015\/10\/IMG_1726S-300x188.jpg 300w, https:\/\/goji2100.com\/blog\/wp-content\/uploads\/2015\/10\/IMG_1726S.jpg 1024w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><p id=\"caption-attachment-536\" class=\"wp-caption-text\">\u30b3\u30f3\u30c8\u30ed\u30fc\u30e9\u5168\u4f53<\/p><\/div>\n<div id=\"attachment_537\" style=\"width: 310px\" class=\"wp-caption alignleft\"><a href=\"https:\/\/goji2100.com\/blog\/wp-content\/uploads\/2015\/10\/IMG_1787S.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-537\" class=\"wp-image-537 size-medium\" src=\"https:\/\/goji2100.com\/blog\/wp-content\/uploads\/2015\/10\/IMG_1787S-300x265.jpg\" alt=\"\u30d9\u30fc\u30b9\u30dc\u30fc\u30c9\u3068CPU\u30dc\u30fc\u30c9\" width=\"300\" height=\"265\" srcset=\"https:\/\/goji2100.com\/blog\/wp-content\/uploads\/2015\/10\/IMG_1787S-300x265.jpg 300w, https:\/\/goji2100.com\/blog\/wp-content\/uploads\/2015\/10\/IMG_1787S.jpg 1024w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><p id=\"caption-attachment-537\" class=\"wp-caption-text\">\u30d9\u30fc\u30b9\u30dc\u30fc\u30c9\u3068CPU\u30dc\u30fc\u30c9<\/p><\/div>\n<div id=\"attachment_538\" style=\"width: 310px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/goji2100.com\/blog\/wp-content\/uploads\/2015\/10\/IMG_1772S.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-538\" class=\"wp-image-538 size-medium\" src=\"https:\/\/goji2100.com\/blog\/wp-content\/uploads\/2015\/10\/IMG_1772S-300x136.jpg\" alt=\"\u30d9\u30fc\u30b9\u30dc\u30fc\u30c9\u306e\u80cc\u9762\" width=\"300\" height=\"136\" srcset=\"https:\/\/goji2100.com\/blog\/wp-content\/uploads\/2015\/10\/IMG_1772S-300x136.jpg 300w, https:\/\/goji2100.com\/blog\/wp-content\/uploads\/2015\/10\/IMG_1772S.jpg 1024w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><p id=\"caption-attachment-538\" class=\"wp-caption-text\">\u30d9\u30fc\u30b9\u30dc\u30fc\u30c9\u306e\u80cc\u9762<\/p><\/div>\n<div id=\"attachment_553\" style=\"width: 310px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/goji2100.com\/blog\/wp-content\/uploads\/2015\/10\/CarCont3_schematic.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-553\" class=\"size-medium wp-image-553\" src=\"https:\/\/goji2100.com\/blog\/wp-content\/uploads\/2015\/10\/CarCont3_schematic-300x173.png\" alt=\"\u30e9\u30b8\u30b3\u30f3\u30fb\u30b3\u30f3\u30c8\u30ed\u30fc\u30e9\u56de\u8def\u56f3\" width=\"300\" height=\"173\" srcset=\"https:\/\/goji2100.com\/blog\/wp-content\/uploads\/2015\/10\/CarCont3_schematic-300x173.png 300w, https:\/\/goji2100.com\/blog\/wp-content\/uploads\/2015\/10\/CarCont3_schematic-1024x589.png 1024w, https:\/\/goji2100.com\/blog\/wp-content\/uploads\/2015\/10\/CarCont3_schematic.png 1153w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><p id=\"caption-attachment-553\" class=\"wp-caption-text\">\u30e9\u30b8\u30b3\u30f3\u30fb\u30b3\u30f3\u30c8\u30ed\u30fc\u30e9\u56de\u8def\u56f3<\/p><\/div>\n<p><strong>\u9001\u4fe1\u6a5f\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9(main)<\/strong><\/p>\n<pre class=\"lang:arduino decode:true\" title=\"CarCont3.ino\">\r\n#include &lt;ESP8266WiFi.h&gt;\r\nconst char ssid[] = &quot;ESPCar&quot;;\r\nconst char pass[] = &quot;ESP8266ap&quot;;\r\n\r\n#include &lt;WiFiUdp.h&gt;\r\nWiFiUDP udp;\r\nIPAddress carServerIP(192, 168, 4, 1);\r\nconst int carPort = 10000;\r\n\r\n#include &lt;Ticker.h&gt;\r\n#define STATUS_LED 13\r\nint Blink_val = -2048, Blink_inc = 0, Blink_add = 0;\r\nTicker Ticks_100, Blink_led;\r\n\r\nunsigned long ticks0 = 0, ticks1 = 0, ticks2 = 0;\r\nvoid ticks100()\r\n{\r\n  if (ticks0) --ticks0;\r\n  ticks1++;\r\n}\r\n\r\nvoid blinkLED()\r\n{\r\n  if (Blink_add &lt; 0) return;\r\n  if (Blink_add &lt; 2) {\r\n    analogWrite(STATUS_LED, ((Blink_add == 1) ? 1023 : 0));\r\n    return;\r\n  }\r\n  if (Blink_inc ==    1) Blink_val += Blink_add; else Blink_val -= Blink_add;\r\n  if (Blink_val &lt;=    0) Blink_inc = 1, Blink_val = 0;\r\n  if (Blink_val &gt;= 1000) Blink_inc = 0;\r\n  analogWrite(STATUS_LED, Blink_val);\r\n}\r\n\r\nvoid setBlink(int n)\r\n{\r\n  if (Blink_val != -2048) Blink_led.detach(); \r\n  Blink_val = 0; Blink_inc = 1; Blink_add = n;\r\n  Blink_led.attach(0.01, blinkLED);\r\n}\r\n\r\n#define POWER_PIN 12\r\nvoid powerON()\r\n{\r\n  setBlink(1);\r\n  digitalWrite(POWER_PIN, HIGH);\r\n}\r\n\r\nvoid powerOFF()\r\n{\r\n  setBlink(0);\r\n  digitalWrite(POWER_PIN, LOW);\r\n  while(1) delay(100);\r\n}\r\n\r\nvoid sendUDPpacket(byte *buff, size_t plen)\r\n{\r\n  udp.beginPacket(carServerIP, carPort);\r\n  udp.write(buff, plen);\r\n  udp.endPacket();\r\n}\r\n\r\nbyte sendBuff[32] = &quot;\/osc\/\\0  ,i  \\0\\0\\0\\0&quot;;\r\nint  loopc = 10, pll, prr, phl, phn;\r\nvoid sendCar(int ll, int rr, int hl, int hn)\r\n{\r\n  if (++loopc &gt; 10) { pll = prr = phl = phn = -1; loopc = 0; }\r\n  if (pll != ll) { sendBuff[ 5] = 'L'; sendBuff[15] = pll = ll; sendUDPpacket(sendBuff, 16); }\r\n  if (prr != rr) { sendBuff[ 5] = 'R'; sendBuff[15] = prr = rr; sendUDPpacket(sendBuff, 16); }\r\n  if (phl != hl) { sendBuff[ 5] = 'F'; sendBuff[15] = phl = hl; sendUDPpacket(sendBuff, 16); }\r\n  if (phn != hn) { sendBuff[ 5] = 'C'; sendBuff[15] = phn = hn; sendUDPpacket(sendBuff, 16); }\r\n}\r\n\r\nvoid setup() {\r\n  pinMode(STATUS_LED, OUTPUT);\r\n  pinMode(POWER_PIN,  OUTPUT);\r\n  powerON();\r\n\r\n  Serial.begin(115200);\r\n  Serial.printf(&quot;\\nStart\\n&quot;);\r\n\r\n  Serial.printf(&quot;\\nADXL345 &quot;);\r\n  if (ADXL345_Init() != 0) { Serial.println(&quot;NG&quot;); powerOFF(); }\r\n  Serial.println(&quot;OK&quot;);\r\n\r\n  Serial.print(&quot;Connecting to &quot;);\r\n  Serial.println(ssid);\r\n  WiFi.begin(ssid, pass);\r\n\r\n  for (int i = 0; (WiFi.status() != WL_CONNECTED); i++) {\r\n    if (i &gt;= 40) { Serial.println(&quot;Wi-Fi Car NG&quot;); powerOFF(); }\r\n    Serial.print(&quot;.&quot;);\r\n    delay(500);\r\n  }\r\n\r\n  Serial.print(&quot;\\nWiFi connected\\n&quot;);\r\n  Serial.print(&quot;IP address: &quot;); Serial.println(WiFi.localIP());\r\n\r\n  Serial.println(&quot;Starting UDP&quot;);\r\n  udp.begin(carPort);\r\n  Serial.print(&quot;Local port: &quot;); Serial.println(udp.localPort());\r\n}\r\n\r\nconst int HomeXmin = - 8, HomeXmax =  8;\r\nconst int HomeYmin = - 8, HomeYmax =  8;\r\nconst int RangXmin = -64, RangXmax = 63;\r\nconst int RangYmin = -64, RangYmax = 63;\r\n\r\nvoid loop() {\r\n  int nX, nY, nZ;\r\n  int pstat = 9, stat = 0, homec = 0, holdc = 0;\r\n  int vL = 0, vR = 0, HL = 0, HN = 0;\r\n  int pX = -1, pY = -1, wX, wY;\r\n\r\n  Ticks_100.attach(0.1, ticks100);\r\n  setBlink(10);\r\n  loopc = 10;\r\n\r\n  while (1) {\r\n    if (pstat != stat) { pstat = stat; Serial.printf(&quot;stat=%d\\n&quot;,stat); }\r\n    ADXL345_readXYZ(&amp;nX, &amp;nY, &amp;nZ);\r\n\/\/  Serial.printf(&quot;%3d %3d &quot;, nX, nY);\r\n    nX \/= 2;  nY \/= 2;\r\n    if  (nX &lt; RangXmin) nX = RangXmin;\r\n    if  (nY &lt; RangYmin) nY = RangYmin;\r\n    if  (nX &gt; RangXmax) nX = RangXmax;\r\n    if  (nY &gt; RangYmax) nY = RangYmax;\r\n    if ((nX &gt; HomeXmin) &amp;&amp; (nX &lt; HomeXmax)) nX = 0;\r\n    if ((nY &gt; HomeYmin) &amp;&amp; (nY &lt; HomeYmax)) nY = 0;\r\n\r\n    wX = (nX &gt; 0) ? nX : -nX;\r\n    wY = (nY &gt; 0) ? nY : -nY;\r\n    if (wY &gt; wX) wX = wY;\r\n    Blink_add = (wX &gt; 2) ? wX : 2;\r\n\r\n    switch (stat) {\r\n      case 0:\r\n        if ((nX == 0) &amp;&amp; (nY == 0)) { if (++homec &gt; 20) stat++; } else homec = 0;\r\n        sendCar(64, 64, ((ticks1 &amp; (1 &lt;&lt; 4)) ? 1 : 0), HN);\r\n\/\/      Serial.printf(&quot;- %3d\\n&quot;, homec);\r\n        break;\r\n      case 1:\r\n        nX = -nX + 64; nY = -nY + 64;\r\n        pX = (pX &gt; nX) ? (pX - nX) : (nX - pX);\r\n        pY = (pY &gt; nY) ? (pY - nY) : (nY - pY);\r\n        if ((pX &lt; 5) &amp;&amp; (pY &lt; 5)) { if (++holdc &gt; 500) powerOFF(); } else holdc = 0;\r\n        pX = nX; pY = nY;\r\n        vR = vL = nX;\r\n        if (nY -= 64) { if (nY &gt; 0) vL -= (nY - HomeYmax); else vR += (nY - HomeYmin); }\r\n        HL = ((ticks1 &amp; (1 &lt;&lt; 2)) ? 1 : 0);\r\n        sendCar(vL, vR, HL, HN);\r\n\/\/      Serial.printf(&quot;= %3d %3d\\n&quot;, vL, vR);\r\n        break;\r\n    }\r\n    delay(20);\r\n  }\r\n}\r\n<\/pre>\n<p><strong>\u9001\u4fe1\u6a5f\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9(ADXL345)<\/strong><\/p>\n<pre class=\"lang:arduino decode:true \" title=\"ADXL345.ino\">\r\n#include &lt;Wire.h&gt;\r\n#define I2C_SDA  4\r\n#define I2C_CLK  5\r\n#define ADXL345_ADDR  0x1D\r\n\r\nvoid i2c_write_block(uint8_t addr, uint8_t *buff, size_t blen)\r\n{\r\n  Wire.beginTransmission(addr);\r\n  while (blen-- &gt; 0) { Wire.write(*buff++); delayMicroseconds(30); }\r\n  Wire.endTransmission();\r\n}\r\n\r\nvoid i2c_read_block(uint8_t addr, uint8_t *buff, size_t blen)\r\n{\r\n  Wire.requestFrom(addr, blen);\r\n  while (blen-- &gt; 0) *buff++ = Wire.read();\r\n}\r\n\r\nconst int BuffB = 10;\r\nbyte buff[10 + 2 + 6 + (2)] = {\r\n  0x2C, 0x0B,   \/\/ ADXL345_BW_RATE\r\n  0x2D, 0x28,   \/\/ ADXL345_POWER_CTL\r\n  0x31, 0x00,   \/\/ ADXL345_DATA_FORMAT\r\n  0x38, 0x00,   \/\/ ADXL345_FIFO_CTL\r\n  0x00, 0x00    \/\/ --\r\n};\r\n\r\nsize_t ADXL345_Init()\r\n{\r\n  Wire.begin(I2C_SDA, I2C_CLK);\r\n  buff[BuffB] = 0x00;      \/\/ ADXL345_READ_DEVID\r\n  i2c_write_block(ADXL345_ADDR, &amp;buff[BuffB], 1);\r\n  i2c_read_block (ADXL345_ADDR, &amp;buff[BuffB], 1);\r\n  if (buff[BuffB] != 0xE5) \/\/ ADXL345_DEVID_VALUE\r\n    return(-1);\r\n  for (int i = 0; (buff[i] != 0x00); i += 2)\r\n    i2c_write_block(ADXL345_ADDR, &amp;buff[i], 2);\r\n  return(0);\r\n}\r\n\r\nvoid ADXL345_readXYZ(int *x, int *y, int *z)\r\n{\r\n  buff[BuffB] = 0x32; \/\/ ADXL345_DATAX0;\r\n  i2c_write_block(ADXL345_ADDR, &amp;buff[BuffB    ], 1);\r\n  i2c_read_block (ADXL345_ADDR, &amp;buff[BuffB + 2], 6);\r\n  *x = *(int16_t *)&amp;buff[BuffB + 2];\r\n  *y = *(int16_t *)&amp;buff[BuffB + 4];\r\n  *z = *(int16_t *)&amp;buff[BuffB + 6];\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u52a0\u901f\u5ea6\u30bb\u30f3\u30b5(ADXL345)\u3092\u4f7f\u3063\u305fWi-Fi\u30e9\u30b8\u30b3\u30f3\u30ab\u30fc\u306e\u30b3\u30f3\u30c8\u30ed\u30fc\u30e9\u3092\u4f5c\u308a\u307e\u3057\u305f\u3002 \u30b9\u30de\u30db\u3092\u4f7f\u3048\u306a\u3044\u5e7c\u5150\u5411\u3051\u306e\u30b3\u30f3\u30c8\u30ed\u30fc\u30e9\u306b\u306a\u308a\u307e\u3059\u3002 ESP-WROOM-02\u306bI2C\u3067\uff13\u8ef8\u52a0\u901f\u5ea6\u30bb\u30f3\u30b5(ADXL345)\u3092\u63a5\u7d9a\u3057\u307e [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[26,9,3,29],"tags":[30],"_links":{"self":[{"href":"https:\/\/goji2100.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/534"}],"collection":[{"href":"https:\/\/goji2100.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/goji2100.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/goji2100.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/goji2100.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=534"}],"version-history":[{"count":25,"href":"https:\/\/goji2100.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/534\/revisions"}],"predecessor-version":[{"id":1418,"href":"https:\/\/goji2100.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/534\/revisions\/1418"}],"wp:attachment":[{"href":"https:\/\/goji2100.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=534"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/goji2100.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=534"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/goji2100.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=534"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}