Arduino UNO R4 WiFiで遠隔操作をする

Arduino

はじめに

ここでは、スマホからArduino UNO R4 WiFiに内蔵されているLEDの点灯を遠隔操作する方法について述べます。

Arduino Cloudにログイン

まず、Arduinoの公式ページにログインします。

右上にある「Cloud」をクリックします。

Devicesの設定

Home画面のメニューからDevicesを選択します。

「ADD DEVICE」をクリックします。

「Arduino board」をクリックします。しばらく待つと次の画面が現れます。

「CONFIGURE」をクリックします。

しばらく待ちます。すると、次のような画面が現れます。

Device Nameを入力して、「NEXT」をクリックします。

しばらく待ちます。すると次の画面が現れます。

「DONE」をクリックします。次のような画面が現れます。

Thingsの設定

上の図に示したボタンをクリックします。

「ADD」をクリックします。

変数名を「Led」、変数の型を「Boolean」、権限を「Read & Write」に、更新頻度を「On Changes」に設定し、「ADD VARIABLE」をクリックします。

画面右側にあるNetworkの欄のConfigureボタンをクリックします。

SSIDとPASSを入力して、「SAVE」をクリックします。なお、デフォルトではThingsの名前がUntitleになっていますので「LED」に変更しました。

プログラムの作成

上の図に示した箇所をクリックします。

「Sketches」をクリックします。

上の図の赤線で囲った部分にプログラムのテンプレートができていますので、これを開きます。

このテンプレートに必要な文を追記します。以下に示します。

/* 
  Sketch generated by the Arduino IoT Cloud Thing "Untitled"
  https://create.arduino.cc/cloud/things/ee257533-2d45-4306-a0db-4e90f9e5c800 

  Arduino IoT Cloud Variables description

  The following variables are automatically generated and updated when changes are made to the Thing

  bool led;

  Variables which are marked as READ/WRITE in the Cloud Thing will also have functions
  which are called when their values are changed from the Dashboard.
  These functions are generated with the Thing and added at the end of this sketch.
*/

#include "thingProperties.h"

void setup() {
  // Initialize serial and wait for port to open:
  Serial.begin(9600);
  // This delay gives the chance to wait for a Serial Monitor without blocking if none is found
  delay(1500); 

  // Defined in thingProperties.h
  initProperties();

  // Connect to Arduino IoT Cloud
  ArduinoCloud.begin(ArduinoIoTPreferredConnection);
  
  /*
     The following function allows you to obtain more information
     related to the state of network and IoT Cloud connection and errors
     the higher number the more granular information you’ll get.
     The default is 0 (only errors).
     Maximum is 4
 */
  setDebugMessageLevel(2);
  ArduinoCloud.printDebugInfo();
  pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
  ArduinoCloud.update();
  // Your code here 
  digitalWrite(LED_BUILTIN, led);
}

/*
  Since Led is READ_WRITE variable, onLedChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onLedChange()  {
  // Add your code here to act upon Led change
  Serial.print("Led status changed:");
  Serial.println(led);
}

追記したのは以下の4行です。

pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, led);
Serial.print("Led status changed:");
Serial.println(led);

プログラムが書けたら、Arduinoへアップロードします。

Dashboardsの設定

上の図の赤線で囲った部分をクリックします。

「Dashboards」をクリックします。

「Untitled」を「LED」(何でもよい)に変更します。名前の変更は下の図の赤線で囲った部分をクリックするとできます。

いま名前を変更してできた「LED」の行をクリックします。

右上にある「EDIT」をクリックします。

中央上部にある「ADD」をクリックします。

「Switch」をクリックします。

画面右側にある「Link Variable」をクリックします。

中央の欄にある「Led」をクリックします。

画面右側にある「LINK VARIABLE」をクリックします。

「DONE」をクリックします。

同様の手順で「LED」を追加します。

画面右上にある「DONE」をクリックします。

動作の方法

Switchの下のON-OFFボタンをクリックしてください。LEDがついたり消えたりすると思います。

スマホでArduino Cloudにログインしてください。Dashboardsまでたどり着いたらパソコンと同じ画面が出てきます。同様に、Switchの下のボタンをクリックしてみてください。LEDがついたり消えたりするはずです。これで、Arduinoを遠隔操作できることがわかりました。

おわりに

かなり手間がかかりましたが、Arduino CloudからArduinoを遠隔操縦できることがわかりました。

コメント

タイトルとURLをコピーしました