LoginSignup
12
8

More than 3 years have passed since last update.

新米エンジニアがm5stickCで温度を測ってグラフ化してみた

Posted at

アドベントカレンダーもはやいもので10日めですね。毎日楽しく読ませていただいております。
今日は私も微力ながら記事を書かせていただきます。

概要

m5stickCというセンサーを使い、部屋の室温を測ってグラフ化してみました。
image0.jpeg
m5stickCさん。実はm5stackとm5stickVという仲間もいる。
公式:https://docs.m5stack.com/#/en/core/m5stickc

グラフ化にはmachinistというサービスを使っています。
machinist.png
公式:https://machinist.iij.jp/#/
値をPOSTするだけでグラフ化してくれるので楽ちん。

温度センサー

M5StickC ENV Hat(DHT12/BMP280/BMM150搭載)を使いました。
image2.jpeg
https://www.switch-science.com/catalog/5755/

m5stickCに刺すだけで使えるのでとても簡単です。
image1.jpeg
さしたところ。
これを使ってまずは温度を測定してみましょう。

手順

templature.c
#include <M5StickC.h>
#include "DHT12.h"
#include <Adafruit_BMP280.h>

DHT12 dht12;
Adafruit_BMP280 bmp280;
unsigned long nextUpdate = 0;

void setup() {
  // M5StickC初期化
  M5.begin();
  M5.Lcd.setRotation(3);
  M5.Lcd.fillScreen(BLACK);
  M5.Lcd.setCursor(0, 0, 2);
  M5.Lcd.println("ENV TEST");

  Serial.begin(115200);

  // I2C初期化
  Wire.begin(0, 26);

  // bmp280初期化
  if (!bmp280.begin(0x76)) {
    Serial.println("bmp280センサーが見つかりません");
    while (1);
  }
}

void loop() {
  getTemperature();
  // 30秒待機
  delay(30000);
}

void getTemperature()
{
  // 気温
  float tmp = dht12.readTemperature();
  M5.Lcd.setCursor(0, 20, 2);
  M5.Lcd.printf("Temp: %2.1f C", tmp);

  // 湿度
  float hum = dht12.readHumidity();
  M5.Lcd.setCursor(0, 40, 2);
  M5.Lcd.printf("Humi: %2.0f %%", hum);

  // 気圧
  float pressure = bmp280.readPressure() / 100.0F;
  M5.Lcd.setCursor(0, 60, 2);
  M5.Lcd.printf("pressure: %6.1f hPa", pressure);

  // シリアル出力
  Serial.printf("Temp: %2.1f C\n", tmp);
  Serial.printf("Humi: %2.0f %%\n", hum);
  Serial.printf("pressure: %6.1f hPa\n", pressure);
}

image0 (1).jpeg
温度がはかれた!けど、んん?私の部屋暑すぎない?
おそらくですがPCのそばにおいておいたのでその熱を拾っているのかと。

Machinistへ温度データ送信

今回はm5stickCから直接送ってみます。

手順

ArduinoJsonの最新バージョンは6ですが使い方のリンクは5を使っているので適宜読み替えてください。
以下MachinistにPOSTするコード。

machinist.c
#include <WiFi.h>
#include <ArduinoJson.h>
#include <HTTPClient.h>
#include <M5StickC.h>
#include "DHT12.h"
#include <Adafruit_BMP280.h>

// WiFi
const char ssid[] = "YOUR_SSID";
const char passwd[] = "YOUR_PW";

WiFiClient wifiClient;
HTTPClient http;

//センサ
DHT12 dht12;
Adafruit_BMP280 bmp280;
unsigned long nextUpdate = 0;
float temp = 0.0;

void setup() {
  Serial.begin(115200);

    // I2C初期化
  Wire.begin(0, 26);

  // bmp280初期化
  if (!bmp280.begin(0x76)) {
    Serial.println("bmp280センサーが見つかりません");
    while (1);
  }

    // Connect WiFi
    connectWiFi();
    getTemperature();
    postJson();
}

void loop() {
    delay(60000);

    // WiFi
    if ( WiFi.status() == WL_DISCONNECTED ) {
        connectWiFi(); 
    }

    getTemperature();
    postJson();
}

void connectWiFi()
{
    WiFi.begin(ssid, passwd);
    Serial.print("WiFi connecting...");
    while(WiFi.status() != WL_CONNECTED) {
        Serial.print(".");
        delay(100);
    }
    Serial.print(" connected. ");
    Serial.println(WiFi.localIP());
}

void getTemperature()
{
  // 気温
  temp = dht12.readTemperature();
  Serial.print(temp);
}

void postJson()
{
  //POST json
  const size_t capacity = JSON_ARRAY_SIZE(1) + JSON_OBJECT_SIZE(1) + JSON_OBJECT_SIZE(2) + JSON_OBJECT_SIZE(3);
  DynamicJsonDocument doc(capacity);
  char Buffer[512];

  doc["agent"] = "m5stickC";

  JsonArray metrics = doc.createNestedArray("metrics");

  JsonObject metrics_0 = metrics.createNestedObject();
  metrics_0["name"] = "temperature";
  metrics_0["namespace"] = "Environment Sensor";
  JsonObject metrics_0_data_point = metrics_0.createNestedObject("data_point");
  metrics_0_data_point["value"] = temp;

  serializeJson(doc, Buffer, sizeof(Buffer));
  Serial.print(Buffer);

  HTTPClient http;
  http.begin("https://gw.machinist.iij.jp/endpoint");      
  http.addHeader("Content-Type", "application/json");
  http.addHeader("Authorization", "Bearer YOUR_APIKEY");
  int httpCode = http.POST(Buffer);

  Serial.print(httpCode);
}

では早速見てみましょうか。
graph.png
グラフになってるー。うれしい。

所感

IoTの部署に配属されたので、生まれて初めてIoT機器を触ってみたのですが

  • 電子回路をあっさり組む猛者だらけ
  • 基盤ごとに違うライブラリ
  • センサー15円送料600円!

などなかなか大変な業界だなという印象です。
初心者向けの記事がもう少しあれば助かるなあ。

もともとは温度センサーとエアコンのリモコンを連動して、自動で部屋の温度調整をするというシステムの構築が目的だったので、もう少し頑張ってみます。機会があればまたどこかで。
  

12
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
8