TANI-LabのTLP-A100を使用してAlexaをトリガーにESP32でモーターを操作する方法を紹介します。

## AlexaとESP32でモーターを操作するプロジェクト


<iframe width="923" height="1641" src="https://www.youtube.com/embed/fiy8IQm_EbE" title="" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>
---

### 参考資料
- [ESP32でのAlexa対応に関する記事](https://kohacraft.com/archives/202006201124.html)
- TANI-Lab の[Amazon ストア](https://www.amazon.co.jp/stores/TANI-Lab/page/600E7AC4-3B7D-4A6B-ABF8-59FFD17CC265?ref_=ast_bln)
- TANI-Lab TLP-A100 [Amazonのページ TLP-A100](https://amzn.to/401hocV)
- TANI-Lab TLP-P120 [Amazonのページ TLP-P120](https://amzn.to/3ZLa5ER)

---

### 回路
![IMG_5354.jpeg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3958059/884efaae-65f8-0b4f-c10c-f05c46bae178.jpeg)

PD対応ACアダプタ -> TLP-A100.Type-C.IN
TLP-A100.Type-C.OUT -> ESP32.MicroUSB
TLP-A100.Type-C.外部出力端子(VOUT) -> TLP-P120.VIN
TLP-A100.Type-C.外部出力端子(GND) -> TLP-P120.GND
TLP-P120.VIN -> ドライバー.MotVIN
TLP-P120.GND -> ドライバー.MotGND

ESP32 
PIN 25  ->  1kΩ->LED1 -> GND
PIN 26  ->  1kΩ->LED2 -> GND
PIN 27  ->  1kΩ->LED3 -> GND
PIN 14  ->  ドライバー.DIR
PIN 12  ->  ドライバー.STEP
PIN 3V3 ->  ドライバー.VIO
PIN GND ->  ドライバー.VGND 

---

### プログラム

使用したプログラムは以下の通りです。詳細はファイルをご参照ください。

```cpp
#include <WiFi.h>
#include <Espalexa.h>

// prototypes
boolean connectWifi();

//callback functions
void firstLightChanged(uint8_t brightness);
void SecondLightChanged(uint8_t brightness);
void MotorChanged(uint8_t brightness);

// Change this!!
const char* ssid = "WIFI-SSID";
const char* password = "WIFI-PW";

boolean wifiConnected = false;

Espalexa espalexa;


const int LED_BLUE = 25;
const int LED_WHITE = 26;
const int LED_RED = 27;
const int PIN_MT_DIR = 14;
const int PIN_MT_STEP = 12;
byte MOT_STEP;
byte MOT_RUN;

void setup()
{
  Serial.begin(115200);

  pinMode(LED_BLUE, OUTPUT);
  digitalWrite(LED_BLUE, LOW);
  pinMode(LED_WHITE, OUTPUT);
  digitalWrite(LED_WHITE, LOW);
  pinMode(LED_RED, OUTPUT);
  digitalWrite(LED_RED, LOW);
  pinMode( PIN_MT_DIR, OUTPUT );
  pinMode( PIN_MT_STEP, OUTPUT );
  MOT_STEP=0;
  MOT_RUN=0;
  // Initialise wifi connection
  wifiConnected = connectWifi();
  
  if(wifiConnected){
    
    // Define your devices here. 
    espalexa.addDevice("LED1", firstLightChanged,0); //simplest definition, default state off
    espalexa.addDevice("LED2", SecondLightChanged); //simplest definition, default state off
    espalexa.addDevice("MT1", MotorChanged); //simplest definition, default state off

    espalexa.begin();
    
  } else
  {
    while (1) {
      Serial.println("Cannot connect to WiFi. Please check data and reset the ESP.");
      delay(2500);
    }
  }
}
 
void loop()
{

   espalexa.loop();
  if ( MOT_RUN ){
    if ( MOT_STEP ){
      MOT_STEP=false;
      digitalWrite( PIN_MT_STEP, HIGH );
    }else{
      MOT_STEP=true;
      digitalWrite( PIN_MT_STEP, LOW );
    }
  }
   delay(1);
}

//our callback functions
void firstLightChanged(uint8_t brightness) {
    Serial.print("Device 1 changed to ");
    
    //do what you need to do here
    showLED( brightness );

    //EXAMPLE
    if (brightness) {
      Serial.print("ON, brightness ");
      Serial.print(brightness);
    }
    else  {
      Serial.println("OFF");
    }
}

void SecondLightChanged(uint8_t brightness) {
    Serial.print("Device 2 changed to ");
    
    //do what you need to do here

    //EXAMPLE
    if (brightness) {
      digitalWrite(LED_WHITE, HIGH);
      Serial.print("ON, brightness ");
      Serial.print(brightness);
    }
    else  {
      digitalWrite(LED_WHITE, LOW);
      Serial.println("OFF");
    }
}

void MotorChanged(uint8_t brightness) {
  MOT_RUN=brightness;
}


// connect to wifi – returns true if successful or false if not
boolean connectWifi(){
  boolean state = true;
  int i = 0;
  
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  Serial.println("");
  Serial.println("Connecting to WiFi");

  // Wait for connection
  Serial.print("Connecting...");
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
    if (i > 20){
      state = false; break;
    }
    i++;
  }
  Serial.println("");
  if (state){
    Serial.print("Connected to ");
    Serial.println(ssid);
    Serial.print("IP address: ");
    Serial.println(WiFi.localIP());
  }
  else {
    Serial.println("Connection failed.");
  }
  return state;
}

void showLED( int value )
{
  if( value == 0 )
  {

      digitalWrite(LED_BLUE, LOW);
      digitalWrite(LED_WHITE, LOW);
      digitalWrite(LED_RED, LOW);
 
      return;
  }
  
  int num = (value-3)/63;
  switch( num ){
    case 0 :
 
      digitalWrite(LED_RED, LOW);
      digitalWrite(LED_BLUE, LOW);
      digitalWrite(LED_WHITE, LOW);

      break;
            
    case 1 :

      digitalWrite(LED_RED, LOW);
      digitalWrite(LED_BLUE, LOW);
      digitalWrite(LED_WHITE, LOW);

      break;
            
    case 2 :
 
      digitalWrite(LED_RED, LOW);
      digitalWrite(LED_BLUE, HIGH);
      digitalWrite(LED_WHITE, LOW);

      break;
            
    case 3 :
 
      digitalWrite(LED_RED, LOW);
      digitalWrite(LED_BLUE, HIGH);
      digitalWrite(LED_WHITE, HIGH);

      break;
            
    case 4 :

      digitalWrite(LED_RED, HIGH);
      digitalWrite(LED_BLUE, HIGH);
      digitalWrite(LED_WHITE, HIGH);
 
      break;

    default :
 
      digitalWrite(LED_BLUE, LOW);

      break;
  }
  return;
}

```