1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
| #if defined(ARDUINO_M5Stack_Core_ESP32) #include <M5Stack.h> #elif defined(ARDUINO_M5Stick_C) #include <M5StickC.h> #endif #include <Preferences.h> #include <WiFi.h> #include <HTTPClient.h> #include <Wire.h> // for DHT12+BMP280 #include "DHT12.h" // for DHT12 #include "Adafruit_Sensor.h" // for DHT12 #include <Adafruit_BMP280.h> // for BMP280
DHT12 dht12; Adafruit_BMP280 bme; Preferences prefs; HTTPClient http;
char wifi_ssid[128]; // 127文字まで char wifi_pass[128]; // 127文字まで String apiKey; const String endPoint = "https://gw.machinist.iij.jp/endpoint"; const char* dataFormat = "{\"agent\": \"%s\", \"metrics\": [{\"namespace\": \"%s\", \"name\": \"%s\", " "\"data_point\": {\"value\": %4.1f}, \"tags\": {\"sensor\": \"%s\"}}]}"; char core_name[16]; // 15文字まで float sensor_offset1; unsigned long nextUpdate;
void load_preferences() { // WiFi設定 prefs.begin("wifi", true); prefs.getString("ssid", wifi_ssid, sizeof(wifi_ssid)); prefs.getString("password", wifi_pass, sizeof(wifi_pass)); prefs.end(); // Machinist設定 prefs.begin("machinist", true); apiKey = prefs.getString("apikey"); prefs.end(); // M5Stackコア設定 prefs.begin("core", true); prefs.getString("name", core_name, sizeof(core_name)); prefs.end(); // M5Stackセンサー設定 prefs.begin("sensor", true); sensor_offset1 = prefs.getFloat("offset1"); prefs.end(); }
void wifi_connect() { M5.Lcd.print("WiFi Connetcing..."); WiFi.begin(wifi_ssid, wifi_pass); // 接続完了するまでループ while (WiFi.status() != WL_CONNECTED) { delay(1000); M5.Lcd.print("."); } M5.Lcd.printf("Connected!\n"); }
void sendData(char *nameSpace, char *metricName, double metricValue, char *sensor_name) { // 送信データを用意 char dataBuffer[1024]; // 1023文字まで sprintf(dataBuffer, dataFormat, core_name, nameSpace, metricName, metricValue, sensor_name); // データを送信 http.begin(endPoint); http.addHeader("Content-Type", "application/json"); http.addHeader("Authorization", "Bearer " + apiKey); http.POST(dataBuffer); }
void setup() { // 初期設定 M5.begin(); #if defined(ARDUINO_M5Stack_Core_ESP32) M5.Power.begin(); #endif // 画面設定 #if defined(ARDUINO_M5Stack_Core_ESP32) M5.Lcd.setBrightness(3); M5.Lcd.setTextSize(3); #elif defined(ARDUINO_M5Stick_C) M5.Axp.ScreenBreath(7); M5.Lcd.setTextSize(2); M5.Lcd.setRotation(3); #endif M5.Lcd.setTextColor(WHITE, BLACK); // センサー設定 #if defined(ARDUINO_M5Stack_Core_ESP32) Wire.begin(); #elif defined(ARDUINO_M5Stick_C) Wire.begin(0,26); // for ENV Hat #endif while (!bme.begin(0x76)){ M5.Lcd.println("Could not find a valid BMP280 sensor, check wiring!"); } // NVSの読み出し load_preferences(); // WiFi接続 wifi_connect(); // 機器固有情報を表示 M5.Lcd.fillScreen(BLACK); M5.Lcd.setCursor(0, 0); M5.Lcd.printf("Name:%s\n", core_name); M5.Lcd.printf("Offset:%+4.2f\n", sensor_offset1); #if defined(ARDUINO_M5Stack_Core_ESP32) M5.Lcd.print("IP:"); M5.Lcd.println(WiFi.localIP()); #endif // 初回データ送信時刻を設定 nextUpdate = millis() + 1000; }
void loop() { // 最新状態の取得 float temp = dht12.readTemperature() + sensor_offset1; float humi = dht12.readHumidity(); float pres = bme.readPressure(); // 画面更新 #if defined(ARDUINO_M5Stack_Core_ESP32) M5.Lcd.setCursor(0, 120); M5.Lcd.printf("Temp:%4.1fC\nHumi:%2.0f%%\n", temp, humi); M5.Lcd.printf("Pressure:%6.0fPa\n", pres); #elif defined(ARDUINO_M5Stick_C) M5.Lcd.setCursor(0, 40); M5.Lcd.printf("T:%4.1fC H:%2.0f%%\nP:%6.0fPa\n", temp, humi, pres); #endif // Machinistへ送信 if ( nextUpdate <= millis() ) { sendData("Env", "Temp", temp, "1"); // 次回送信は1分=60000ミリ秒後 nextUpdate = nextUpdate + 60000; } // 画面更新は1秒=1000ミリ秒後 delay(1000); }
|