見出し画像

温度計なんかを作ってみるのにちょうどいい季節になりましたね

マイコンとセンサーで適当に

ソフトウェア

#include <Arduino.h>
#include <WiFi.h>

#include <M5UnitGLASS2.h>
#include <M5Unified.h>

#include "M5UnitENV.h"

SHT4X sht4;
BMP280 bmp;

const char* ssid = "REPLACE_WITH_YOUR_SSID";
const char* password = "REPLACE_WITH_YOUR_PASSWORD";
WiFiServer server(80);
String header;

M5Canvas canvas(&M5.Display);

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

  auto cfg = M5.config();
	cfg.unit_glass2.pin_sda = 39;
	cfg.unit_glass2.pin_scl = 38;
  cfg.unit_glass2.i2c_port = I2C_NUM_1;
  M5.begin(cfg);
	M5.setPrimaryDisplayType({m5::board_t::board_M5UnitGLASS2});
  canvas.createSprite(128, 64);

  delay(1000);
  Serial.printf("In_I2C_SCL: %d\n", M5.In_I2C.getSCL());
  Serial.printf("In_I2C_SDA: %d\n", M5.In_I2C.getSDA());
  Serial.printf("Ex_I2C_SCL: %d\n", M5.Ex_I2C.getSCL());
  Serial.printf("Ex_I2C_SDA: %d\n", M5.Ex_I2C.getSDA());

  if (!sht4.begin(&Wire, SHT40_I2C_ADDR_44, G2, G1, 400000U)) {
    Serial.println("Couldn't find SHT4x");
    while (1) delay(1);
  }

  // You can have 3 different precisions, higher precision takes longer
  sht4.setPrecision(SHT4X_HIGH_PRECISION);
  sht4.setHeater(SHT4X_NO_HEATER);

  if (!bmp.begin(&Wire, BMP280_I2C_ADDR, G2, G1, 400000U)) {
    Serial.println("Couldn't find BMP280");
    while (1) delay(1);
  }
  /* Default settings from datasheet. */
  bmp.setSampling(BMP280::MODE_NORMAL,     /* Operating Mode. */
                  BMP280::SAMPLING_X2,     /* Temp. oversampling */
                  BMP280::SAMPLING_X16,    /* Pressure oversampling */
                  BMP280::FILTER_X16,      /* Filtering. */
                  BMP280::STANDBY_MS_500); /* Standby time. */
}

void loop() {
    if (sht4.update()) {
        Serial.println("-----SHT4X-----");
        Serial.print("Temperature: ");
        Serial.print(sht4.cTemp);
        Serial.println(" degrees C");
        Serial.print("Humidity: ");
        Serial.print(sht4.humidity);
        Serial.println("% rH");
        Serial.println("-------------\r\n");

    }

    if (bmp.update()) {
        Serial.println("-----BMP280-----");
        Serial.print(F("Temperature: "));
        Serial.print(bmp.cTemp);
        Serial.println(" degrees C");
        Serial.print(F("Pressure: "));
        Serial.print(bmp.pressure);
        Serial.println(" Pa");
        Serial.print(F("Approx altitude: "));
        Serial.print(bmp.altitude);
        Serial.println(" m");
        Serial.println("-------------\r\n");
    }

    canvas.clear();
    canvas.setFont(&fonts::lgfxJapanGothicP_16);
    canvas.setCursor(0, 0);
    canvas.setTextSize(1);

    canvas.setFont(&fonts::efontJA_16);
    canvas.printf("%4.1f 度\n", sht4.cTemp);

    canvas.setFont(&fonts::efontJA_16);
    canvas.printf("%4.1f %%\n", sht4.humidity);

    canvas.setFont(&fonts::efontJA_16);
    canvas.printf("%4.1f hPa\n", bmp.pressure / 100.0f);

    M5.Display.startWrite(); 
    canvas.pushSprite(0, 0);
    M5.Display.endWrite();

    delay(1000);
}

いいなと思ったら応援しよう!