Created
September 3, 2020 01:00
-
-
Save TareObjects/556a8efe986780fffa818610bb955c75 to your computer and use it in GitHub Desktop.
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
#include "Adafruit_CCS811.h" | |
#include "Adafruit_Si7021.h" | |
#include "Adafruit_BMP280.h" | |
Adafruit_CCS811 ccs; | |
Adafruit_BMP280 bmp280; | |
Adafruit_Si7021 SI702x = Adafruit_Si7021(); | |
// | |
// Wifi, ambient | |
// | |
#include <WiFi.h> | |
#include "Ambient.h" | |
WiFiClient client; | |
Ambient ambient; | |
const char* ssid = "<your wifi ssid>"; | |
const char* password = "<your wifi password>"; | |
unsigned int channelId = <AmbientのチャネルID>; | |
const char* writeKey = "<Abmientのライトキー>"; | |
// | |
// Setup | |
// | |
void setup() { | |
Serial.begin(115200); | |
Wire.begin(32, 26); | |
WiFi.begin(ssid, password); // Wi-Fi APに接続 | |
Serial.println("CCS811 test"); | |
if (!ccs.begin(0x5A)) { | |
Serial.println("Failed to start sensor! Please check your wiring."); | |
while (1); | |
} | |
//calibrate temperature sensor | |
while (!ccs.available()); | |
float temp = ccs.calculateTemperature(); | |
ccs.setTempOffset(temp - 25.0); | |
Serial.println("BMP280 test"); /* --- SETUP BMP on 0x76 ------ */ | |
if (!bmp280.begin(0x76)) { | |
Serial.println("Could not find a valid BMP280 sensor, check wiring!"); | |
while (true); | |
} | |
Serial.println("Si7021 test!"); /* ---- SETUP SI702x ----- */ | |
if (!SI702x.begin()) { | |
Serial.println("Did not find Si702x sensor!"); | |
while (true); | |
} | |
while (WiFi.status() != WL_CONNECTED) { // Wi-Fi AP接続待ち | |
delay(100); | |
Serial.print("."); | |
} | |
ambient.begin(channelId, writeKey, &client); // チャネルIDとライトキーを指定してAmbientの初期化 | |
} | |
float sumTemp = 0; | |
float sumHumd = 0; | |
float sumPres = 0; | |
float sumCo2 = 0; | |
float sumTVOC = 0; | |
int cnt811 = 0; | |
int sumCount = 0; | |
float period = 1000 * 60; | |
float prevPeriod = -1; | |
// | |
// Loop | |
// | |
void loop() { | |
if (ccs.available()) { | |
float temp = ccs.calculateTemperature(); | |
if (!ccs.readData()) { | |
float co2 = ccs.geteCO2(); | |
float tvoc = ccs.getTVOC(); | |
Serial.print("CO2: "); | |
Serial.print(co2); | |
Serial.print("ppm, TVOC: "); | |
Serial.print(tvoc); | |
Serial.print("ppb Temp:"); | |
Serial.print(temp); | |
Serial.print(" "); | |
sumCo2 += co2; | |
sumTVOC += tvoc; | |
cnt811 ++; | |
} | |
else { | |
Serial.println("ERROR CCS811"); | |
} | |
} | |
float bmpTemp, bmpPressure; | |
bmpTemp = bmp280.readTemperature(); | |
Serial.print("BMP280 => Temperature = "); | |
Serial.print(bmpTemp); | |
Serial.print(" °C, "); | |
bmpPressure = bmp280.readPressure() / 100; | |
Serial.print("Pressure = "); | |
Serial.print(bmpPressure); | |
Serial.print(" Pa, "); | |
sumTemp += bmpTemp; | |
sumPres += bmpPressure; | |
float siTemp = SI702x.readTemperature(); | |
float siHumidity = SI702x.readHumidity(); | |
Serial.print(" SI702x => Temperature = "); | |
Serial.print(siTemp, 2); | |
Serial.print(" °C, "); | |
Serial.print("Humidity = "); | |
Serial.println(siHumidity, 2); | |
sumHumd += siHumidity; | |
sumCount ++; | |
int now = millis() / period; | |
if (now != prevPeriod) { | |
if (sumCount > 0) { | |
ambient.set(1, sumTemp / (float)sumCount); | |
ambient.set(2, sumHumd / (float)sumCount); | |
ambient.set(3, sumPres / (float)sumCount); | |
ambient.set(4, sumCo2 / (float)cnt811); | |
ambient.set(5, sumTVOC / (float)cnt811); | |
Serial.println("sending"); | |
ambient.send(); | |
} | |
sumTemp = 0; | |
sumHumd = 0; | |
sumPres = 0; | |
sumCo2 = 0; | |
sumTVOC = 0; | |
sumCount = 0; | |
cnt811 = 0; | |
prevPeriod = now; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment