Created
November 18, 2020 15:05
-
-
Save shikarunochi/d3f58e2973912b0d40bad08881fc1f3f to your computer and use it in GitHub Desktop.
SHT30+BMP280+SGP30 for M5Atom
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://github.com/m5stack/M5-ProductExampleCodes/tree/master/Unit/ENVII/Arduino/ENVII ENV-II Unit example | |
// https://github.com/m5stack/M5-ProductExampleCodes/tree/master/Unit/TVOC TVOC/eCO2 Unit example | |
// https://ambidata.io/samples/m5stack/m5stackcurrent/ DEEP SLEEP | |
// https://qazsedcftf.blogspot.com/2019/01/esp32arduino.html Wi-Fi 接続リトライ | |
#include <M5Atom.h> | |
#include <Wire.h> | |
#include "Adafruit_Sensor.h" | |
#include <Adafruit_BMP280.h> | |
#include "SHT3X.h" | |
#include "Adafruit_SGP30.h" | |
#include "Ambient.h" | |
#define TIME_TO_SLEEP 60 /* Time ESP32 will go to sleep (in seconds) */ | |
SHT3X sht30; | |
Adafruit_BMP280 bme; | |
Adafruit_SGP30 sgp; | |
int i = 15; | |
long last_millis = 0; | |
float tmp = 0.0; | |
float hum = 0.0; | |
float pressure = 0.0; | |
WiFiClient client; | |
Ambient ambient; | |
#define LED_RED 0x000500 | |
#define LED_GREEN 0x050000 | |
#define LED_BLUE 0x000005 | |
const char* ssid = [YOUR_SSID]; | |
const char* password = [YOUR_PASSWORD]; | |
unsigned int channelId = [YOUR_AMBIENT_CHANNEL_ID]; // AmbientのチャネルID | |
const char* writeKey = [YOUR_AMBIENT_WRITE_KEY]; // ライトキー | |
void setup() { | |
unsigned long starttime = millis(); | |
int lpcnt=0 ; | |
int lpcnt2 = 0; | |
M5.begin(true,false,true); | |
Wire.begin(23,33); | |
delay(100); | |
M5.dis.drawpix(0, LED_RED); | |
Serial.println(F("ENV Unit(SHT30 and BMP280) test...")); | |
while (!bme.begin(0x76)){ | |
Serial.println("Could not find a valid BMP280 sensor, check wiring!"); | |
} | |
if (! sgp.begin()){ | |
Serial.println("Sensor not found :("); | |
while (1); | |
} | |
Serial.print("Found SGP30 serial #"); | |
Serial.print(sgp.serialnumber[0], HEX); | |
Serial.print(sgp.serialnumber[1], HEX); | |
Serial.println(sgp.serialnumber[2], HEX); | |
WiFi.begin(ssid, password); // Wi-Fi APに接続 | |
while (WiFi.status() != WL_CONNECTED) { // Wi-Fi AP接続待ち | |
delay(500); // 0.5秒毎にチェック | |
lpcnt += 1 ; // | |
if (lpcnt > 6) { // 6回目(3秒) で切断/再接続 | |
WiFi.disconnect(true,true) ; // | |
WiFi.begin(ssid, password); // | |
lpcnt = 0 ; // | |
lpcnt2 += 1 ; // 再接続の回数をカウント | |
} // | |
if (lpcnt2 > 3) { // 3回 接続できなければ、 | |
ESP.restart() ; // ソフトウェアリセット | |
} // | |
Serial.print("."); // | |
} // | |
ambient.begin(channelId, writeKey, &client); // チャネルIDとライトキーを指定してAmbientの初期化 | |
M5.dis.drawpix(0, LED_GREEN); | |
int t = millis(); | |
float temp, humid, pressure, gas; | |
if(sht30.get()==0){ | |
temp = sht30.cTemp; | |
humid = sht30.humidity; | |
} | |
pressure = (float)bme.readPressure(); | |
if (! sgp.IAQmeasure()) { | |
Serial.println("Measurement failed"); | |
} | |
gas = sgp.eCO2; | |
if (pressure > 800.0) { // 電源投入直後に異常値が読めたら捨てる | |
// 温度、湿度、気圧、CO2、TVOCの値をAmbientに送信する | |
ambient.set(1, String(temp).c_str()); | |
ambient.set(2, String(humid).c_str()); | |
ambient.set(3, String(pressure / 100.0F ).c_str()); | |
ambient.set(4, String(gas).c_str()); | |
bool ret= ambient.send(); | |
if(ret == false){ | |
M5.dis.drawpix(0, LED_BLUE); | |
}else{ | |
M5.dis.drawpix(0, LED_GREEN); | |
} | |
Serial.printf("%2.2f'C :", temp); | |
Serial.printf("%0.2f%% :", humid); | |
Serial.printf("%2.1f hPa :", pressure / 100.0F); | |
Serial.printf("%2.1f ppm :", gas); | |
Serial.println(); | |
} | |
// Deep sleepする時間を計算する | |
uint64_t sleeptime = TIME_TO_SLEEP * 1000000 - (millis() - starttime) * 1000; | |
esp_deep_sleep(sleeptime); // DeepSleepモードに移行 | |
// ここには戻らない | |
} | |
void loop() { | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment