Created
March 19, 2018 00:52
-
-
Save TareObjects/df287e1bcc93565e663cdd67f03f56a5 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
| // | |
| // WiFi | |
| #include <WiFi.h> | |
| #include <Ambient.h> | |
| WiFiClient client; | |
| Ambient ambient; | |
| const char *ssid = "your ssid"; | |
| const char *password = "your password"; | |
| unsigned int channelId = 1234; // your ch number | |
| const char* writeKey = "your write key"; | |
| // | |
| // BME280 | |
| // | |
| #include <BME280_MOD-1022.h> | |
| #include <Wire.h> | |
| void initBME280() { | |
| // need to read the NVM compensation parameters | |
| BME280.readCompensationParams(); | |
| // Need to turn on 1x oversampling, default is os_skipped, which means it doesn't measure anything | |
| BME280.writeOversamplingPressure(os1x); // 1x over sampling (ie, just one sample) | |
| BME280.writeOversamplingTemperature(os1x); | |
| BME280.writeOversamplingHumidity(os1x); | |
| // example of a forced sample. After taking the measurement the chip goes back to sleep | |
| // BME280.writeMode(smForced); | |
| // while (BME280.isMeasuring()) { | |
| // Serial.println("Measuring..."); | |
| // delay(50); | |
| // } | |
| // Serial.println("Done!"); | |
| BME280.writeMode(smNormal); | |
| } | |
| // | |
| // 時間 | |
| #include <Time.h> | |
| #include <TimeLib.h> | |
| int prevMinute = -1; // 分単位の処理をするためのフラグ | |
| // | |
| // PIR Sensor | |
| // | |
| const int kPIRSensor = 23; | |
| volatile int pirCounter = 0; // volatileは最適化せずメモリに置いとけというコンパイラへの指示 | |
| // 割り込みの中でアクセスする変数などに指定。 | |
| // | |
| // タイマー割り込み関連 | |
| // | |
| hw_timer_t * timer = NULL; | |
| volatile SemaphoreHandle_t timerSemaphore; | |
| portMUX_TYPE timerMux = portMUX_INITIALIZER_UNLOCKED; | |
| // | |
| // タイマー割り込みでPIRがHIGHならカウントアップ | |
| // | |
| void IRAM_ATTR timerCountPIR() { | |
| portENTER_CRITICAL_ISR(&timerMux); | |
| if (digitalRead(kPIRSensor) == HIGH) { | |
| pirCounter ++; | |
| Serial.print('*'); | |
| } else { | |
| Serial.print(' '); | |
| } | |
| portEXIT_CRITICAL_ISR(&timerMux); | |
| // Give a semaphore that we can check in the loop | |
| xSemaphoreGiveFromISR(timerSemaphore, NULL); | |
| // It is safe to use digitalRead/Write here if you want to toggle an output | |
| } | |
| void connectWiFi() { | |
| if (WiFi.status() != WL_CONNECTED) { | |
| WiFi.mode(WIFI_STA); | |
| WiFi.begin(ssid, password); | |
| while (WiFi.status() != WL_CONNECTED) { | |
| delay(100); | |
| Serial.print("."); | |
| } | |
| } | |
| } | |
| // | |
| // Setup | |
| // | |
| void setup() { | |
| // | |
| // Serial | |
| Serial.begin(115200); | |
| // | |
| // WiFi | |
| // | |
| // 接続&接続確認 | |
| Serial.print("WiFi Connectiong"); | |
| delay(500); | |
| connectWiFi(); | |
| ambient.begin(channelId, writeKey, &client); | |
| // | |
| // Wire / BME280 | |
| Wire.begin(4, 16); | |
| uint8_t chipID; | |
| Serial.println("Welcome to the BME280 MOD-1022 weather multi-sensor test sketch!"); | |
| Serial.println("Embedded Adventures (www.embeddedadventures.com)"); | |
| chipID = BME280.readChipId(); | |
| // find the chip ID out just for fun | |
| Serial.print("ChipID = 0x"); | |
| Serial.println(chipID, HEX); | |
| initBME280(); | |
| // | |
| // PIR | |
| pinMode(kPIRSensor, INPUT); // PIR Sensorを入力モードで使う | |
| prevMinute = -1; | |
| // Interval Timer | |
| // Create semaphore to inform us when the timer has fired | |
| timerSemaphore = xSemaphoreCreateBinary(); | |
| // Use 1st timer of 4 (counted from zero). | |
| // Set 80 divider for prescaler (see ESP32 Technical Reference Manual for more | |
| // info). | |
| timer = timerBegin(0, 80, true); | |
| // Attach onTimer function to our timer. | |
| timerAttachInterrupt(timer, &timerCountPIR, true); | |
| // Set alarm to call onTimer function every second (value in microseconds). | |
| // Repeat the alarm (third parameter) | |
| timerAlarmWrite(timer, 1000000, true); | |
| // Start an alarm | |
| timerAlarmEnable(timer); | |
| } | |
| // | |
| // メインループ | |
| // | |
| int prevSecond = -1; | |
| float prevTemp = -1; | |
| float prevHumi = -1; | |
| float prevPres = -1; | |
| int sameDataCount = 0; | |
| void loop() { | |
| char pirBuf[10]; | |
| float t, h, p; | |
| // 1秒ごとの処理 | |
| if (prevSecond != second()) { | |
| BME280.readMeasurements(); | |
| t = BME280.getTemperature(); | |
| h = BME280.getHumidity(); | |
| p = BME280.getPressure(); | |
| if (t == prevTemp && h == prevHumi && p == prevPres) { | |
| sameDataCount++; | |
| if (sameDataCount > 60) { | |
| Serial.println("same data detected....reset BME280"); | |
| // BME280.writeRegister(regReset, 0xB6); // reset command | |
| Wire.beginTransmission(addrBME280); | |
| Wire.write(regReset); | |
| Wire.write(0xB6); | |
| Wire.endTransmission(); | |
| initBME280(); | |
| sameDataCount = 0; | |
| } | |
| } else { | |
| sameDataCount = 0; | |
| } | |
| prevTemp = t; | |
| prevHumi = h; | |
| prevPres = p; | |
| prevSecond = second(); | |
| } | |
| // | |
| // 1分ごとに処理 | |
| if (prevMinute != minute()) { | |
| pirBuf[0] = 0; // c文字列を空に | |
| if (prevMinute != -1) { // 最初の1回は飛ばす | |
| // Wifi再接続などでループを抜けそこねた場合の処理 | |
| if (pirCounter > 60) pirCounter = 60; | |
| // PIRの値を文字列に変換 | |
| itoa(pirCounter, pirBuf, 10); // air to pirBuffer, in Decimal | |
| // 変換し終わったら素早くカウンタを0にリセット | |
| pirCounter = 0; | |
| // | |
| // 人間の動きっぷりをシリアルへ出力 | |
| Serial.print(" : Moved = "); | |
| Serial.print(pirBuf); | |
| Serial.println(" s/min"); | |
| ambient.set(1, pirBuf); | |
| if (t > -40 && t < 100) { | |
| dtostrf(t, 7, 2, pirBuf); | |
| ambient.set(2, pirBuf); | |
| } | |
| if (h > 0 && h < 100) { | |
| dtostrf(h, 7, 2, pirBuf); | |
| ambient.set(3, pirBuf); | |
| } | |
| if (p > 800 && p < 1100) { | |
| dtostrf(p, 7, 2, pirBuf); | |
| ambient.set(4, pirBuf); | |
| } | |
| ambient.send(); | |
| } | |
| if (WiFi.status() != WL_CONNECTED) { | |
| connectWiFi(); | |
| } | |
| prevMinute = minute(); | |
| } | |
| delay(1000); | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment