LoginSignup

This article is a Private article. Only a writer and users who know the URL can access it.
Please change open range to public in publish setting if you want to share this article with other users.

More than 1 year has passed since last update.

M5StickCでIoT

Posted at

明るさを測定

明るさを測定して、画面に表示します。(暗くなるほど数値が大きくなります。)

配線

M5StickCの5Vー10kΩの抵抗ーCdSセルーM5StickCのGND
の流れができるような配線をします。

その後、10kΩの抵抗とCdSセルが繋がっている列からM5StickCのG36ピンに配線します。
a.png

プログラム

#include <M5StickC.h>

int PIN = 36;

void setup() {
  M5.begin();
  pinMode(PIN, ANALOG);

  M5.Lcd.setRotation(3);
  M5.Lcd.setTextSize(2);
  M5.Lcd.drawCentreString("Hello World", 0, 20, 2);
  delay(5000);
}

void loop() {
  int val=analogRead(PIN);
  M5.Lcd.fillScreen(BLACK);
  M5.Lcd.drawCentreString(String(val), 0, 20, 2);
  delay(500);
}

明るさを測定して、サーバーに送る

IoTサーバーのアカウントを作成

doyolabのIoTサーバーを使うために自分のjindaiメールでここからアカウントを作ってください。
jindaiメールにユーザーkeyが送られてくるので、大切に保管してください。(写真ではなく、メモなどテキストで残しておきましょう)

プログラム

#include <M5StickC.h>

#include <doyolab_IoT.h>
#include <WiFi.h>


//使用環境に応じて変更-----------------------------------------------------
const char* ssid     = "";//繋げたいネットワーク
const char* password = "";//繋げたいネットワークのパスワード
String user_key = "";//自分のユーザーkey
String sub_id = "";//端末の名前など識別できるに文字
//-------------------------------------------------------------------

//doyolab_IoTインスタンス化-->conn1とよぶ
doyolab_IoT conn1(user_key,sub_id);


int PIN = 36;
void setup(){
    Serial.begin(115200);
    M5.begin();
    M5.Lcd.setRotation(3); // 横向きにする
    pinMode(PIN, ANALOG);

    // text print
    M5.Lcd.fillScreen(BLACK);
    M5.Lcd.setTextColor(WHITE);
    M5.Lcd.setTextSize(2);
    M5.Lcd.println("Boot Complete");

    WiFi.begin(ssid, password);
    M5.Lcd.println("WiFi connecting");
    while ((!(WiFi.status() == WL_CONNECTED))){
      delay(300);
      M5.Lcd.print(".");
    }
    M5.Lcd.fillScreen(BLACK);
    M5.Lcd.setCursor(0, 10);
    M5.Lcd.println("WiFi connected");
    delay(5000);

}

void loop(){
    //必要に応じて変更-->センサーなどから値を取得(例では直接変数に代入)-------
  int val=analogRead(PIN);
//  float fdata=1.5;
//  String tdata="test";
  M5.update();
  M5.Lcd.fillScreen(BLACK);
  M5.Lcd.setCursor(0, 10);
  M5.Lcd.println("Sending");
  M5.Lcd.drawCentreString(String(val), 0, 20, 2);
  //-----------------------------
  //引数はintデータ、floatデータ、テキストデータの順番
  //intデータ、floatデータはStringに変換する
  String ret=conn1.send_data(String(val),"","");

  M5.update();
  M5.Lcd.fillScreen(BLACK);
  M5.Lcd.setCursor(0, 10);
  M5.Lcd.setTextSize(2.0);
  M5.Lcd.println(ret);
  delay(10000); // 間隔を設定 1秒は1000 1分は60000 間隔はサーバー負荷を減らすため1分以上でお願いします。

}

データを確認

ここからデータを確認します。
ユーザーkeyと自分で設定したsubidが必要になります。

0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up