---
# System prepended metadata

title: Phone controlled switch

---

# Phone controlled switch
 ## 前置作業
   - Research
        - [x] ESP32
        - 接腳位(發現新世界，ESP32可以壓成芯片大小好像也比較剛好，也找到可以模擬的地方，可是變成芯片好像會難很多)
        - 
        - [x] 繼電器
        - [x] 8/21 實際開關電路
        - [x] 8/23 Esp32 練習
        - [ ] ~~PCB(esp32電源[交流電轉直流電裝置])~~
        7/20 放棄PCB 直接用洞洞板和焊接變壓器
        - [x] IDE
        - [x] Code
         [https://iotcircuithub.com/esp32-bluetooth-home-automation/#Code_for_ESP32_Bluetooth_controlled_Relays](https://)
         變壓器是一個Module,Switch 和 Bluetooth都透過ESP32寫入控制變壓器,但共用變數ToggleState
        - [ ] ~~IOS App。~~
            好像是不行，強行必帶MacBook的概念，但我在尋找可以跨越的方法
        - 目前找到VMware解法，但應該等電路那邊用完再說
        - 7/20 正式放棄IOS 
        - [x] 藍芽夠不夠強--經過airpods的測試，感覺隔著一層抽屜在出房門是可以的

   - 買
        - [x] ESP32
        - [x] 繼電器
        - [x] 端子
        - [x] 實體開關
        - [x] 變壓器 110V -> 3.3V
        - [ ] ~~印出PCB~~
        數量少，時間太長 -> 焊接測試
 ## 實作
- [x] 8/23 ESP32 Code
- [x] 8/24 組裝
- [ ] ~~Xcode~~ 
    由於放棄ios就沒用Xcode

## Dump

* 7/1 找到網路上reference，十分有用
* 7/4 把code跟接法弄完(全部用繼電器控也太屌)，也被Notion燒到，發現我真的很爛是不是要來用Notion了
* 7/5 花了大概兩小時把code 接法 模擬器 appcreator 電路板找一找用一用，反而發現很多新的待做事項，事前工作變好多，而且原本先找到Xcode解決方法想說好複雜等到真的要寫的時候再來研究，結果發現PCB電路板那裡好像是更大的一個坑
* 7/11 拆開電燈座看一下，發現位子真的很小，現在面對的問題真的是結合電路板這塊，只有ESP32的芯片，還有電路板電元的部分。最後詢問爸爸後放棄Xcode，他的公司試過這件事但失敗了。
* 8/10 簡略畫了整體架構圖，其實沒有很複雜，只是串並聯要想好
* 8/15 發現開關座長寬大小限制嚴重，物件擺放要重新考慮，甚至要以多層或是雙面
* 8/23 原考慮雙面都要擺放，所以選雙面的洞洞板，但實際焊接發現雙面也不能省去多少面積，還是要預留人工焊接的位置和走線，所以決定這次先以成功為目標，不考慮大小



## Circuit Design
1. Design
![](https://i.imgur.com/WYWOoWV.jpg)
![](https://i.imgur.com/nt9AyJ6.jpg)
 

1. Placement 8/24
![](https://i.imgur.com/muprF5Q.jpg)

2. Soldered
![](https://i.imgur.com/ts8woJI.jpg)

3. Implament
![](https://i.imgur.com/kWXXPwP.jpg)

## Video Demo
* https://drive.google.com/file/d/1zUSiExzuLQkW5JDcJt0_b5v1AAIiWZve/view?usp=sharing

## Code Revised
```


#include <dummy.h>

 /**********************************************************************************
 *  TITLE: Bluetooth + Manual (Switch) control 8 relays using ESP32.
 *  Click on the following links to learn more. 
 *  YouTube Video: https://youtu.be/UxwQ8DHY2zs
 *  Related Blog : https://iotcircuithub.com/esp32-projects/
 *  by Subhajit (Tech StudyCell)
 *  Preferences--> Aditional boards Manager URLs : 
 *  https://dl.espressif.com/dl/package_esp32_index.json, http://arduino.esp8266.com/stable/package_esp8266com_index.json
 *  
 *  Download Board ESP32: https://github.com/espressif/arduino-esp32
 *  Download the libraries
 *  BluetoothSerial: https://github.com/espressif/arduino-esp32/tree/master/libraries/BluetoothSerial
 *  AceButton Library: https://github.com/bxparks/AceButton
 *  Install the libraries at Arduino IDE -> Sketch - Include Library -> Add Zip Library
 **********************************************************************************/


#include "BluetoothSerial.h" 
#include <AceButton.h>
#include "esp_bt_device.h"
using namespace ace_button;

#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run
`make menuconfig` to and enable it
#endif

BluetoothSerial SerialBT;


// define the GPIO connected with Relays and switches
#define RelayPin1 25  //D25
#define RelayPin2 32  //D32
#define RelayPin3 34  //D34


#define SwitchPin1 21  //D21
#define SwitchPin2 18  //D18
#define SwitchPin3 2  //D2


int toggleState_1 = 1; //Define integer to remember the toggle state for relay 1
int toggleState_2 = 1; //Define integer to remember the toggle state for relay 2
int toggleState_3 = 1; //Define integer to remember the toggle state for relay 3


char bt_data; // variable for storing bluetooth data 

ButtonConfig config1;
AceButton button1(&config1);
ButtonConfig config2;
AceButton button2(&config2);
ButtonConfig config3;
AceButton button3(&config3);



void handleEvent1(AceButton*, uint8_t, uint8_t);
void handleEvent2(AceButton*, uint8_t, uint8_t);
void handleEvent3(AceButton*, uint8_t, uint8_t);



void Bluetooth_handle()
{
  bt_data = SerialBT.read();
//  Serial.println(bt_data);
  delay(20);

  switch(bt_data)
      {
        case 'A': 
          digitalWrite(RelayPin1, HIGH);  toggleState_1 = 1; // if 'A' received Turn on Relay1
          digitalWrite(RelayPin1, HIGH);  toggleState_1 = 1; break; // if 'A' received Turn on Relay1
        case 'a': 
          digitalWrite(RelayPin1, LOW); toggleState_1 = 0;
          digitalWrite(RelayPin1, LOW); toggleState_1 = 0; break;// if 'a' received Turn off Relay1
        case 'B': 
          digitalWrite(RelayPin2, LOW);  toggleState_2 = 0;
          digitalWrite(RelayPin2, LOW);  toggleState_2 = 0; break;// if 'B' received Turn on Relay2
        case 'b': 
          digitalWrite(RelayPin2, HIGH); toggleState_2 = 1;
          digitalWrite(RelayPin2, HIGH); toggleState_2 = 1; break;// if 'b' received Turn off Relay2
        case 'C': digitalWrite(RelayPin3, LOW);  toggleState_3 = 0; break; // if 'C' received Turn on Relay3
        case 'c': digitalWrite(RelayPin3, HIGH); toggleState_3 = 1; break; // if 'c' received Turn off Relay3
        default : break;
      }
}

void printDeviceAddress(){
  const uint8_t* point = esp_bt_dev_get_address();

  for (int i = 0; i <6;i++){
    char str[3];
    sprintf(str,"%02X",(int)point[i]);
    Serial.print(str);
    if (i<5){
      Serial.print(":");}
    }
    Serial.println();
}


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

//  btStart();  //Serial.println("Bluetooth On");
  
  SerialBT.begin("ESP32"); //Bluetooth device name
  Serial.println("The device started, now you can pair it with bluetooth!");
  printDeviceAddress();
  delay(5000);

  pinMode(RelayPin1, OUTPUT);
  pinMode(RelayPin2, OUTPUT);
  pinMode(RelayPin3, OUTPUT);


//  pinMode(wifiLed, OUTPUT);

  pinMode(SwitchPin1, INPUT_PULLUP);
  pinMode(SwitchPin2, INPUT_PULLUP);
  pinMode(SwitchPin3, INPUT_PULLUP);


  //During Starting all Relays should TURN OFF
  
  digitalWrite(RelayPin2, toggleState_2);
  digitalWrite(RelayPin1, toggleState_1=0);


  config1.setEventHandler(button1Handler);
  config2.setEventHandler(button2Handler);
  config3.setEventHandler(button3Handler);


  button1.init(SwitchPin1);
  button2.init(SwitchPin2);
  button3.init(SwitchPin3);


  delay(200);
}

void loop()
{  
  if (SerialBT.available()){
   Bluetooth_handle();
 }

  button1.check();
  button2.check();
  button3.check();

}

void button1Handler(AceButton* button, uint8_t eventType, uint8_t buttonState) {
  Serial.println("EVENT1");
  switch (eventType) {
    case AceButton::kEventPressed:
      Serial.println("kEventPressed");
      toggleState_1 = 0;
      digitalWrite(RelayPin1, LOW);
      break;
    case AceButton::kEventReleased:
      Serial.println("kEventReleased");
      toggleState_1 = 1;
      digitalWrite(RelayPin1, HIGH);
      break;
  }
}

void button2Handler(AceButton* button, uint8_t eventType, uint8_t buttonState) {
  Serial.println("EVENT2");
  switch (eventType) {
    case AceButton::kEventPressed:
      Serial.println("kEventPressed");
      toggleState_2 = 0;
      digitalWrite(RelayPin2, LOW);
      break;
    case AceButton::kEventReleased:
      Serial.println("kEventReleased");
      toggleState_2 = 1;
      digitalWrite(RelayPin2, HIGH);
      break;
  }
}

void button3Handler(AceButton* button, uint8_t eventType, uint8_t buttonState) {
  Serial.println("EVENT3");
  switch (eventType) {
    case AceButton::kEventPressed:
      Serial.println("kEventPressed");
      toggleState_3 = 0;
      digitalWrite(RelayPin3, LOW);
      break;
    case AceButton::kEventReleased:
      Serial.println("kEventReleased");
      toggleState_3 = 1;
      digitalWrite(RelayPin3, HIGH);
      break;
  }
}
```

## Reference
* https://iotcircuithub.com/esp32-bluetooth-home-automation/#Code_for_ESP32_Bluetooth_controlled_Relays

