Skip to content

Instantly share code, notes, and snippets.

@TakehikoShimojima
Created October 25, 2019 01:57
Show Gist options
  • Save TakehikoShimojima/126ad63efcbfbd874bc7153788373674 to your computer and use it in GitHub Desktop.
Save TakehikoShimojima/126ad63efcbfbd874bc7153788373674 to your computer and use it in GitHub Desktop.
/*
* ESP32でSi7021を読み、Ambientに送り、DeepSleepする
*/
#ifdef ARDUINO_M5Stack_Core_ESP32
#include <M5Stack.h>
#endif
#include <WiFi.h>
#include "Adafruit_Si7021.h"
#include <Ambient.h>
#define TIME_TO_SLEEP 60 /* Time ESP32 will go to sleep (in seconds) */
const char* ssid = "ssid";
const char* password = "password";
WiFiClient client;
Ambient ambient;
unsigned int channelId = 100; // AmbientのチャネルID
const char* writeKey = "writeKey"; // ライトキー
Adafruit_Si7021 sensor = Adafruit_Si7021();
void setup() {
#ifdef ARDUINO_M5Stack_Core_ESP32
M5.begin();
#endif
unsigned long starttime = millis();
Serial.begin(115200);
while (!Serial) ;
WiFi.begin(ssid, password); // Wi-Fiネットワークに接続する
while (WiFi.status() != WL_CONNECTED) { // 接続したか調べる
delay(0);
Serial.print(".");
}
Serial.println("WiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP()); // ローカルIPアドレスをプリントする
ambient.begin(channelId, writeKey, &client); // チャネルIDとライトキーを指定してAmbientの初期化
if (!sensor.begin()) {
Serial.println("Did not find Si7021 sensor!");
while (true) ;
}
float temp = sensor.readTemperature();
float humid = sensor.readHumidity();
Serial.printf("temp: %.2f, humid: %.2f\r\n", temp, humid);
ambient.set(1, temp); // Ambientのデータ1に温度をセットする
ambient.set(2, humid); // データ2に湿度をセットする
ambient.send(); // Ambientに送信する
#ifdef ARDUINO_M5Stack_Core_ESP32
M5.Lcd.writecommand(ILI9341_DISPOFF);
M5.Lcd.setBrightness(0);
#endif
// 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