Skip to content

hunchongtan/Simple_Arduino_SwitchBot

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 

Repository files navigation

DIY SwitchBot with ESP32 Wi-Fi Module & Google Firebase

This tutorial will illustrate the working of a DIY SwitchBot using 2 Servo Motors and 1 IR Receiver-Remote.
💪 Beginner | ⌛ 45 minutes | 🎥 Video Demo

Things used in this project

Hardware components

Image Component
Espressif ESP32C3 x 1
IR Receiver Module x 1
Remote Control x 1
SG90 Servo Motor x 2
Resistor 220 ohm x 1
Breadboard x 1
Male/Male Jumper Wires x 9

Software apps and online services

Image App
Arduino IDE
Google Firebase

Story

What is a SwitchBot?

A SwitchBot is a small gadget with a tiny arm that physically presses the switch or button for you. It can help you to switch on or off your switch without the need to walk over physically. Control your lights, appliances, and even blinds remotely. SwitchBot brings automation and convenience to your everyday life, all in a small, easy-to-install package.

In simple terms, a SwitchBot can turn almost any regular switch or button into a smart one!

My Inspiration

I stay in my university hostel and the light switch is a distance away from the bed. I always face the issue of needing to get out of bed, walk over to the light switch and switch off the lights physically.

It was then I found out about the SwitchBot. The SwitchBot is a must-get for any lazy or “efficient” person like me! However, the $40 SwitchBot price tag had me scrolling past right quickly. But then, a spark of inspiration hit. Why spend the big bucks when I could unleash my inner inventor? Here's the thing: with some Arduino know-how, I can build my own smart switch, customise it exactly how I want, and all for a fraction of the cost. Suddenly, my university Acai Bowl budget started looking a little brighter. Plus, the challenge of building something myself? Way more exciting than hitting "add to cart."

Basic Set-Up

Essential Libraries

Download the following libraries:

  • Arduino
  • WiFi
  • FirebaseESP32
  • IRremote
  • ESP32Servo (Normal Servo library does not work as well)

IR Receiver Set-Up

Before typing-out the necessary code, you have to set up a basic circuit for the IR receiver as such: (Reference Link)

Upload this code to Arduino IDE and open the serial monitor to find the key codes for your remote control.

#include <IRremote.h>

const int RECV_PIN = 6; // Please change according to your IR receiver pin.
IRrecv irrecv(RECV_PIN);
decode_results results;

void setup(){
  Serial.begin(9600);
  irrecv.enableIRIn();
  irrecv.blink13(true);
}

void loop(){
  if (irrecv.decode(&results)){
        Serial.println(results.value, HEX);
        irrecv.resume();
  }
}

Now press each key on your remote and write down the hexadecimal codes printed for Plus and Minus key presses.
Note that you will receive a 0XFFFFFFFF code when you press a key continuously.

In my case, my backPlay Button code is 0x52A3D41F, frontPlay button code is 0xD7E84B1B, plus Button code is 0xA3C8EDDB and minus Button code is 0xF076C13B.

Schematics

Connection Schematic Diagram

Note: Due to limations on TinkerCAD, diagram shows an Arduino UNO instead. Just take note of the pinwiring.

A 220 ohm resistor is connected to IR Receiver to prevent short circuit.

Circuit Diagram

Demo

Product Demo

Firebase Demo

Code

In this piece of code, you need to amend the codes that are commented with //.

#include <Arduino.h>
#include <WiFi.h>
#include <FirebaseESP32.h>
#include <IRremote.h>
#include <ESP32Servo.h>

/* Define the WiFi credentials */
#define WIFI_SSID "your_wifi_ssid" // Please indicate your own WIFI_SSID.
#define WIFI_PASSWORD "your_wifi_password" // Please indicate your own WIFI_PASSWORD.

/* Define the API Key */
#define API_KEY "your_api_key" // Please indicate your own API_KEY.
#define DATABASE_URL "your_database_url" // Please indicate your own DATABASE_URL.

/* Define the IR remote button codes */
#define backButton 0x52A3D41F // Please change according to your button code tested from the Set Up procedure.
#define frontButton 0xD7E84B1B // Please change according to your button code tested from the Set Up procedure.
#define plusButton 0xA3C8EDDB // Please change according to your button code tested from the Set Up procedure.
#define minusButton 0xF076C13B // Please change according to your button code tested from the Set Up procedure.

/* Define the pins */
#define RECV_PIN 6  // Please change according to your IR receiver pin.
#define SERVO_TOP_PIN 7 // Please change according to your Top Servo pin.
#define SERVO_BTM_PIN 8 // Please change according to your Btm Servo pin.

/* Define the Firebase user credentials */
#define USER_EMAIL "your_user_email" // Please indicate your own USER_EMAIL.
#define USER_PASSWORD "your_user_password" // Please indicate your own USER_PASSWORD.

FirebaseData fbdo;
FirebaseAuth auth;
FirebaseConfig config;

IRrecv irrecv(RECV_PIN);
decode_results results;
Servo top;
Servo btm;

void setup() {
  /* Initalise Serial Monitor */
  Serial.begin(9600); // Please change according to the baud value stated on the top right of Serial Monitor.

  /* Start the IR Receiver */
  irrecv.enableIRIn();
  irrecv.blink13(true);

  /* Attach the servo to its pin and Initalise it to the 90 degrees position */
  servo.attach(SERVO_PIN);
  servo.write(90);
  
  /* Connect to WiFi */
  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  Serial.print("Connecting to Wi-Fi");
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(300);
  }
  Serial.println("\nConnected to WiFi");

  /* Initialise Firebase */
  Serial.printf("Firebase Client v%s\n\n", FIREBASE_CLIENT_VERSION);
  /* Assign the api key (required) */
  config.api_key = API_KEY;
  /* Assign the user sign in credentials */
  auth.user.email = USER_EMAIL;
  auth.user.password = USER_PASSWORD;
  /* Assign the RTDB URL (required) */
  config.database_url = DATABASE_URL;
  /* Comment or pass false value when WiFi reconnection will control by your code or third party library e.g. WiFiManager */
  Firebase.reconnectNetwork(true);
  /* Since v4.4.x, BearSSL engine was used, the SSL buffer need to be set. */
  /* Large data transmission may require larger RX buffer, otherwise connection issue or data read time out can be occurred. */
  fbdo.setBSSLBufferSize(4096 /* Rx buffer size in bytes from 512 - 16384 */, 1024 /* Tx buffer size in bytes from 512 - 16384 */);
  Firebase.begin(&config, &auth);
  Firebase.setDoubleDigits(5);
  /* Check Firebase connection */
  if (Firebase.ready()) {
    Serial.println("Connected to Firebase!");
  } else {
    Serial.println("Failed to connect to Firebase!");
  }
}

void loop() {
  if (irrecv.decode(&results)){
      Serial.println(results.value, HEX);
      /* Under State-key branch, add and initalise keys (on & off) with the values 0 to Firebase. */

      /* Left Switch Functions */
      if (results.value == plusButton) {
        Serial.println("Plus button pressed!");
        /* Rotate the servo motor 150 degrees */
        top.write(150);
        /* Delay for 1 second */
        delay(1000);
        /* Stop the servo motor */
        top.write(90);
        /* Change on-key value to 1 */
        Firebase.set(fbdo, "State/Left", 0);
      }
      if (results.value == minusButton) {
        Serial.println("Minus button pressed!");
        /* Rotate the servo motor 30 degrees */
        btm.write(30);
        /* Delay for 1 second */
        delay(1000);
        /* Stop the servo motor */
        btm.write(90);
        /* Change off-key value to 1 */
        Firebase.set(fbdo, "State/Left", 1);
      }

      /* Right Switch Functions */
      if (results.value == frontButton) {
        Serial.println("Front button pressed!");
        /* Rotate the servo motor 30 degrees */
        top.write(30);
        /* Delay for 1 second */
        delay(1000);
        /* Stop the servo motor */
        top.write(90);
        /* Change on-key value to 1 */
        Firebase.set(fbdo, "State/Right", 0);
      }
      if (results.value == backButton) {
        Serial.println("Back button pressed!");
        /* Rotate the servo motor 150 degrees */
        btm.write(150);
        /* Delay for 1 second */
        delay(1000);
        /* Stop the servo motor */
        btm.write(90);
        /* Change off-key value to 1 */
        Firebase.set(fbdo, "State/Right", 1);
      }

      /* Receive the next value */
      irrecv.resume();
  }
}

Physical Set Up

Ideally, the code is for a top-btm servo motor set up for 2 switches as shown in this example online:

Due to space constraints above the switch, I have gone for a top-btm servo motor set up for just the light switch.

Troubleshooting Tips

  • Tweak the angle of rotation (0 - 180) based on the amount of strength needed to turn the switch on/off.
  • The IR Receiver can be shorted easily. Ensure the resistor is connected. To check if it is working, run the IR Receiver Set Up code in the Basic Set Up section.
  • Firmly secure the servo motor with Bluetac or any adhesive.
  • Make sure the cable connected is a data cable.
  • Use a portable battery pack to power the circuit.

About

Small, simple Arduino project using Google Firebase

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages