スマホでロボットアームを動かす

 

 

スマホでロボットアームを動かす

 

続き

今回は、Bluetoothスマホとのやり取りです。

 

またまた、チュートリアルを参考にさせてもらいます。

「Chapter 13 Bluetooth」その中でも"Sketch_13.3_BluetoothToLed"がドンピシャリ

少し書き直すだけでロボットアームが動かせそうです。

 

チュートリアルに従って進めていきます。

先ずは、ESP32との通信が出来るようにgoogle playからSerial Bluetooth Terminalをスマホにインストール。このアプリでやり取りします。

 

Serial Bluetooth Terminalのアイコン

ブレッドボードはそのままで行けそうです。

 

次に、プログラムを書き込んで実行

/**********************************************************************
  Filename    : SerialToLED
  Description : The phone controls esp32's led via bluetooth.
                When the phone sends "LED_on," ESP32's LED lights turn on.
                When the phone sends "LED_off," ESP32's LED lights turn off.
  Auther      : www.freenove.com
  Modification: 2020/07/11
**********************************************************************/
#include "BluetoothSerial.h"
#include "string.h"
#define LED 2
BluetoothSerial SerialBT;
char buffer[20];
static int count = 0;
void setup() {
  pinMode(LED, OUTPUT);
  SerialBT.begin("ESP32test"); //Bluetooth device name
  Serial.begin(1115200);
  Serial.println("\nThe device started, now you can pair it with bluetooth!");
}

void loop() {
  while(SerialBT.available())
  {
    buffer[count] = SerialBT.read();
    count++;
  }
  if(count>0){
    Serial.print(buffer);
    if(strncmp(buffer,"led_on",6)==0){
      digitalWrite(LED,HIGH);
    }
    if(strncmp(buffer,"led_off",7)==0){
      digitalWrite(LED,LOW);
    }
    count=0;
    memset(buffer,0,20);
  }
}

ただただ、指示通りに進めるだけ、シリアルモニタを開いて、各種設定そして"led_on"を送信。

 

Serial Bluetooth Terminalの画面

"led_on"を書き入れて送信をタップするだけ。

 

はい、LEDが点灯しました。

 

びっくりするほど簡単です。30行ほどのプログラムでBluetoothが使えるなんてESP32

おそるべし

 

次回は、"Sketch_13.3_BluetoothToLed"を少し改造してハンドの開閉をコントロールできるようにしていきます。

 

また、更新します。

 

mckeechan.hatenadiary.jp