{"id":46828,"date":"2020-01-05T16:59:39","date_gmt":"2020-01-05T07:59:39","guid":{"rendered":"http:\/\/kako.com\/blog\/?p=46828"},"modified":"2020-01-05T17:07:46","modified_gmt":"2020-01-05T08:07:46","slug":"m5stack%e3%81%a7%e3%80%81m5stickc%e7%94%a8%e3%81%aeenv-hat%e3%82%92%e5%8b%95%e3%81%8b%e3%81%97%e3%81%a6%e3%81%bf%e3%81%9f","status":"publish","type":"post","link":"http:\/\/kako.com\/blog\/?p=46828","title":{"rendered":"M5Stack\u3067\u3001M5StickC\u7528\u306eENV-HAT\u3092\u52d5\u304b\u3057\u3066\u307f\u305f"},"content":{"rendered":"\n<p><a href=\"http:\/\/kako.com\/blog\/?p=46808\">M5Stack\u306bM5StickC\u306eHAT\u3092\u7e4b\u3050\u306e\u3092\u8a66\u3057\u3066\u307f\u305f<\/a>\u3001\u3068\u3044\u3046\u8a71\u306e\u7d9a\u304d\u3002<\/p>\n\n\n\n<p>ENV-HAT\u306e\u6e29\u5ea6\u6e7f\u5ea6\u30bb\u30f3\u30b5\u30fc\u3068\u6c17\u5727\u30bb\u30f3\u30b5\u30fc\u3092\u5b9f\u969b\u306bM5Stack\u3067\u52d5\u304b\u3057\u3066\u307f\u305f\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" width=\"640\" height=\"480\" src=\"http:\/\/kako.com\/blog\/wp\/wp-content\/uploads\/2020\/01\/Img_1172.jpg\" alt=\"\" class=\"wp-image-46829\" srcset=\"http:\/\/kako.com\/blog\/wp\/wp-content\/uploads\/2020\/01\/Img_1172.jpg 640w, http:\/\/kako.com\/blog\/wp\/wp-content\/uploads\/2020\/01\/Img_1172-300x225.jpg 300w, http:\/\/kako.com\/blog\/wp\/wp-content\/uploads\/2020\/01\/Img_1172-624x468.jpg 624w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><\/figure>\n\n\n\n<!--more-->\n\n\n\n<p>\u3084\u308a\u65b9\u3068\u3057\u3066\u306f\u3001Arduino IDE\u306e\u30e9\u30a4\u30d6\u30e9\u30ea\u306eM5StickC ENV-HAT\u7528\u306e example\u30b9\u30b1\u30c3\u30c1\u3092\u5143\u306b\u3057\u3066\u3001\u5c11\u3057\u66f8\u304d\u63db\u3048\u3066\u52d5\u304b\u3057\u305f\u3002 <\/p>\n\n\n\n<p>\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9\u306f\u6b21\u306e\u3068\u304a\u308a\u3002 <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n\/*\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 &lt;M5Stack.h&gt;\n#include &quot;DHT12.h&quot;\n#include &lt;Wire.h&gt;\n#include &quot;Adafruit_Sensor.h&quot;\n#include &lt;Adafruit_BMP280.h&gt;\n#include &quot;bmm150.h&quot;\n#include &quot;bmm150_defs.h&quot;\nDHT12 dht12; \nBMM150 bmm = BMM150();\nbmm150_mag_data value_offset;\nAdafruit_BMP280 bme;\nvoid calibrate(uint32_t timeout)\n{\n  int16_t value_x_min = 0;\n  int16_t value_x_max = 0;\n  int16_t value_y_min = 0;\n  int16_t value_y_max = 0;\n  int16_t value_z_min = 0;\n  int16_t value_z_max = 0;\n  uint32_t timeStart = 0;\n\n  bmm.read_mag_data();  \n  value_x_min = bmm.raw_mag_data.raw_datax;\n  value_x_max = bmm.raw_mag_data.raw_datax;\n  value_y_min = bmm.raw_mag_data.raw_datay;\n  value_y_max = bmm.raw_mag_data.raw_datay;\n  value_z_min = bmm.raw_mag_data.raw_dataz;\n  value_z_max = bmm.raw_mag_data.raw_dataz;\n  delay(100);\n\n  timeStart = millis();\n  \n  while((millis() - timeStart) &lt; timeout)\n  {\n    bmm.read_mag_data();\n    \n    \/* Update x-Axis max\/min value *\/\n    if(value_x_min &gt; bmm.raw_mag_data.raw_datax)\n    {\n      value_x_min = bmm.raw_mag_data.raw_datax;\n      \/\/ Serial.print(&quot;Update value_x_min: &quot;);\n      \/\/ Serial.println(value_x_min);\n\n    } \n    else if(value_x_max &lt; bmm.raw_mag_data.raw_datax)\n    {\n      value_x_max = bmm.raw_mag_data.raw_datax;\n      \/\/ Serial.print(&quot;update value_x_max: &quot;);\n      \/\/ Serial.println(value_x_max);\n    }\n\n    \/* Update y-Axis max\/min value *\/\n    if(value_y_min &gt; bmm.raw_mag_data.raw_datay)\n    {\n      value_y_min = bmm.raw_mag_data.raw_datay;\n      \/\/ Serial.print(&quot;Update value_y_min: &quot;);\n      \/\/ Serial.println(value_y_min);\n\n    } \n    else if(value_y_max &lt; bmm.raw_mag_data.raw_datay)\n    {\n      value_y_max = bmm.raw_mag_data.raw_datay;\n      \/\/ Serial.print(&quot;update value_y_max: &quot;);\n      \/\/ Serial.println(value_y_max);\n    }\n\n    \/* Update z-Axis max\/min value *\/\n    if(value_z_min &gt; bmm.raw_mag_data.raw_dataz)\n    {\n      value_z_min = bmm.raw_mag_data.raw_dataz;\n      \/\/ Serial.print(&quot;Update value_z_min: &quot;);\n      \/\/ Serial.println(value_z_min);\n\n    } \n    else if(value_z_max &lt; bmm.raw_mag_data.raw_dataz)\n    {\n      value_z_max = bmm.raw_mag_data.raw_dataz;\n      \/\/ Serial.print(&quot;update value_z_max: &quot;);\n      \/\/ Serial.println(value_z_max);\n    }\n    \n    Serial.print(&quot;.&quot;);\n    delay(1);\n\n  }\n\n  value_offset.x = value_x_min + (value_x_max - value_x_min)\/2;\n  value_offset.y = value_y_min + (value_y_max - value_y_min)\/2;\n  value_offset.z = value_z_min + (value_z_max - value_z_min)\/2;\n}\nvoid setup() {\n  \/\/ put your setup code here, to run once:\n  M5.begin();\n  Wire.begin();  \/\/Wire.begin(0,26);\n  \/\/M5.Lcd.setRotation(3);\n  M5.Lcd.fillScreen(BLACK);\n  M5.Lcd.setCursor(0, 0, 2);\n  M5.Lcd.println(&quot;ENV TEST&quot;);\n  pinMode(BUTTON_A_PIN, INPUT);\n\n  if(bmm.initialize() == BMM150_E_ID_NOT_CONFORM) {\n    Serial.println(&quot;Chip ID can not read!&quot;);\n    while(1);\n  } else {\n    Serial.println(&quot;Initialize done!&quot;);\n  }\n  if (!bme.begin(0x76)){  \n      Serial.println(&quot;Could not find a valid BMP280 sensor, check wiring!&quot;);\n      while (1);\n  }\n  calibrate(10);\n  Serial.print(&quot;\\n\\rCalibrate done..&quot;);\n}\nuint8_t setup_flag = 1;\nvoid loop() {\n  \/\/ put your main code here, to run repeatedly:\n  float tmp = dht12.readTemperature();\n  float hum = dht12.readHumidity();\n  M5.Lcd.setCursor(0, 20, 2);\n  M5.Lcd.printf(&quot;Temp: %2.1f Humi: %2.0f%%&quot;, tmp, hum);\n\n\n  bmm150_mag_data value;\n  bmm.read_mag_data();\n\n  value.x = bmm.raw_mag_data.raw_datax - value_offset.x;\n  value.y = bmm.raw_mag_data.raw_datay - value_offset.y;\n  value.z = bmm.raw_mag_data.raw_dataz - value_offset.z;\n\n  float xyHeading = atan2(value.x, value.y);\n  float zxHeading = atan2(value.z, value.x);\n  float heading = xyHeading;\n\n  if(heading &lt; 0)\n    heading += 2*PI;\n  if(heading &gt; 2*PI)\n    heading -= 2*PI;\n  float headingDegrees = heading * 180\/M_PI; \n  float xyHeadingDegrees = xyHeading * 180 \/ M_PI;\n  float zxHeadingDegrees = zxHeading * 180 \/ M_PI;\n\n  Serial.print(&quot;Heading: &quot;);\n  Serial.println(headingDegrees);\n  Serial.print(&quot;xyHeadingDegrees: &quot;);\n  Serial.println(xyHeadingDegrees);\n  Serial.print(&quot;zxHeadingDegrees: &quot;);\n  Serial.println(zxHeadingDegrees);\n  M5.Lcd.setCursor(0, 40, 2);\n  M5.Lcd.printf(&quot;headingDegrees: %2.1f&quot;, headingDegrees);\n  \n  float pressure = bme.readPressure();\n  M5.Lcd.setCursor(0, 60, 2);\n  M5.Lcd.printf(&quot;pressure: %2.1f&quot;, pressure);\n  delay(100);\n\n  if(!setup_flag){\n     setup_flag = 1;\n\n     if(bmm.initialize() == BMM150_E_ID_NOT_CONFORM) {\n    Serial.println(&quot;Chip ID can not read!&quot;);\n    while(1);\n  } else {\n    Serial.println(&quot;Initialize done!&quot;);\n  }\n  if (!bme.begin(0x76)){  \n      Serial.println(&quot;Could not find a valid BMP280 sensor, check wiring!&quot;);\n      while (1);\n  }\n  calibrate(10);\n  Serial.print(&quot;\\n\\rCalibrate done..&quot;);\n }\n\n\n if(digitalRead(BUTTON_A_PIN) == LOW){\n  setup_flag = 0;\n  while(digitalRead(BUTTON_A_PIN) == LOW);\n }\n\n  \n}\n<\/pre><\/div>\n\n\n<p>\u78c1\u6c17\u30bb\u30f3\u30b5\u30fc\u3067\u5730\u78c1\u6c17\u3092\u8a08\u6e2c\u3057\u3066\u6c42\u3081\u305f\u65b9\u4f4d\u3082\u8868\u793a\u3057\u3066\u3044\u308b\u306e\u3060\u304c\u3001\u30bb\u30f3\u30b5\u30fc\u306e\u53d6\u308a\u4ed8\u3051\u306e\u5411\u304d\u304cM5StickC\u3068\u9055\u3046\u306e\u3067\u3001\u65b9\u4f4d\u304c\u305a\u308c\u3066\u3057\u307e\u3063\u3066\u3044\u308b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>M5Stack\u306bM5StickC\u306eHAT\u3092\u7e4b\u3050\u306e\u3092\u8a66\u3057\u3066\u307f\u305f\u3001\u3068\u3044\u3046\u8a71\u306e\u7d9a\u304d\u3002 ENV-HAT\u306e\u6e29\u5ea6\u6e7f\u5ea6\u30bb\u30f3\u30b5\u30fc\u3068\u6c17\u5727\u30bb\u30f3\u30b5\u30fc\u3092\u5b9f\u969b\u306bM5Stack\u3067\u52d5\u304b\u3057\u3066\u307f\u305f\u3002<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[5],"tags":[],"_links":{"self":[{"href":"http:\/\/kako.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/46828"}],"collection":[{"href":"http:\/\/kako.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/kako.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/kako.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/kako.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=46828"}],"version-history":[{"count":1,"href":"http:\/\/kako.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/46828\/revisions"}],"predecessor-version":[{"id":46830,"href":"http:\/\/kako.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/46828\/revisions\/46830"}],"wp:attachment":[{"href":"http:\/\/kako.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=46828"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/kako.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=46828"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/kako.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=46828"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}