Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

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 3 years have passed since last update.

遠隔スイッチ

Last updated at Posted at 2021-11-15

#遠隔でデバイスを操る

M5StickCを2台使って、離れた場所でスイッチを押すと、インターネット経由でLEDがつくということをやってみましょう。

##ボタン側のデバイス

#include <M5StickC.h>

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


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

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

void setup(){
    Serial.begin(115200);
    M5.begin();
    M5.Lcd.setRotation(3); // 横向きにする
   
    // 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(){
  M5.update();
  if ( M5.BtnA.wasPressed() ) {
    String ret=conn1.turn_switch();//スイッチを切り替える(現在の状態(0 or 1)と逆の状態にする)
    M5.Lcd.fillScreen(BLACK);
    M5.Lcd.setCursor(0, 10);
    M5.Lcd.setTextSize(2);
    M5.Lcd.println("status:"+ret);
    
  }
 
}

##受信側のデバイス

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

//switch機能を利用する前に下記のURLでスイッチの登録をしてください。
//https://doyolab.net/appli/iot/to_make_switch

//設定--------------------------------------
const char* ssid     = "";//繋げたいネットワーク
const char* password = "";//繋げたいネットワークのパスワード
String user_key="";自分のユーザーkey
String sub_id="switch";//登録したスイッチ名
//-----------------------------------------

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

void setup() {
    Serial.begin(115200);
    M5.begin();
    M5.Lcd.setRotation(3); // 横向きにする
    pinMode(GPIO_NUM_10, OUTPUT);//内蔵のLED
    digitalWrite(GPIO_NUM_10, HIGH);//使用でHIGHがオフ、LOWがオンなので注意
  
    // 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() {
  //2秒ごとにスイッチの状態(0 or 1)を調べる
  String ret=conn1.switch_status();//スイッチの現在の状態(0 or 1)
  Serial.println(ret);
    M5.Lcd.fillScreen(BLACK);
    M5.Lcd.setCursor(0, 10);
    M5.Lcd.setTextSize(2);
    M5.Lcd.println("receive:"+ret);
    if(ret=="1"){
      digitalWrite(GPIO_NUM_10, LOW);
    }else{
      digitalWrite(GPIO_NUM_10, HIGH);
    }
    delay(2000);
}
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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?