Newer
Older
IoT_Programming_E6 / cds01.ino
@kaitona kaitona on 28 Jun 2022 1 KB first commit
#include <M5StickCPlus.h>
#include <WiFi.h>
#include <HTTPClient.h>

const char* ssid = "miura2g";
const char* password = "jikken2022";

int PIN = 36;

// +--- 15kΩ抵抗 --+-- CdSセル --+
// |              |             |
// GND           G26           3V3
// https://www.storange.jp/2012/03/arduinocds.html

// 注意点: https://lang-ship.com/reference/unofficial/M5StickC/Peripherals/ADC/
int light = 0;
void setup() {
  M5.begin();
  Serial.begin(115200);
  pinMode(PIN, ANALOG); // PINのモード設定
  // https://lang-ship.com/blog/work/m5stickc-io/

  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED){
    delay(50);
    Serial.print(".");
  }

  delay(1000);
}

void loop() {
  M5.Beep.update();
  light = analogRead(PIN);
  Serial.printf("%04d\n", light ); // 暗 0 〜 4095 明
  if (light < 3000)
  {
    //digitalWrite(36, 1);
    M5.Beep.tone(1000,500);
    Serial.printf("HIGH \n");

  }
  
  char* weatherapi_url = "https://script.google.com/macros/s/AKfycbzlbfiBTz_WOpS3Vb2quIos3nwVvgbyudq-gSI0-p9TRbWDe9XCXDb5pmBcjg-HH20qGA/exec?light=";
  //char buf[weatherapi_url.length()+4];
  char tmp[400];
  snprintf(tmp, 256, "%s%d",weatherapi_url, light);
  HTTPClient http;            // クライアント作成
  http.begin(tmp); // HTTPでサーバに接続
  int httpCode = http.GET();  // ステータスコードを取得
  if (httpCode > 0) {
      Serial.println(httpCode);
    if (httpCode == HTTP_CODE_OK) { // ステータスコードが「成功」(200) なら
      String payload = http.getString();
      Serial.println(payload);
    }
  }
  http.end();
  delay(10000);
}