Wireless Switch Presser Using ESP8266

by Hermawan in Circuits > Microcontrollers

1265 Views, 1 Favorites, 0 Comments

Wireless Switch Presser Using ESP8266

2ebc7547-5ef8-4faa-b6ad-4de453d7622d.jpg

Spice up the boring stuff! Creating a wireless button to press a "one-way" switch, making the button can be presses anywhere with less effort (You can add some components and modify the codes to make it works for a "two-way" switch).

The wireless button in this project requires 2 ESP8266, a servo, a push button, a 18650 3.7 Li-ion battery, and a 12v power supply adaptor.

When the push button is pressed, the servo that attached with another ESP8266 will press the expected switch, then put the ESP8266 with battery into "Deep-sleep" to greatly reduce the battery consumption.


Let's get connected with LinkedIn :D

Supplies

Picture1.png
  • Two ESP8266
  • a small breadboard
  • a battery holder
  • a 18650 3.7 Li-ion battery
  • a power supply adaptor 12V
  • Two 47uF capacitor
  • a 5V Regulator
  • a Push button
  • a DC jack connector
  • a servo motor
  • a printed circuit board (PCB)
  • a solder station and solder wires
  • OPTIONAL - 3d Printer

Install ESP8266 Add-on in Arduino IDE

2.jpg
  1. In your Arduino IDE, click File > Preferences
  2. Copy http://arduino.esp8266.com/stable/package_esp8266com_index.json into the "Additional Boards Manager URLs", if you already have another URL, simply put a comma. Then, click "OK":
  3. Go to Tools > Boards > Boards Manager
  4. Search for ESP8266 and press Install
  5. Go to Tools > Boards > ESP8266 Boards. Choose NodeMCU 1.0

Checking the Mac Address of the ESP8266

3.jpg
  • Copy and run the code below:
#include <ESP8266WiFi.h>


void setup(){
  Serial.begin(115200);
  Serial.println();
  Serial.print("ESP8266 Board MAC Address:  ");
  Serial.println(WiFi.macAddress());
}
 
void loop(){
}

Take note of the mac address, it will be used receive a signal from another ESP8266.

Code the ESP8266 That Attached With a Servo

5.jpg
  • Connect the servo with the ESP8266 GPIO 2 (pin D4). The brown cable connected to the ground pin while the red cable connected to the 3v pin.


  • Copy and execute the code below:
#include <ESP8266WiFi.h>
#include <espnow.h>
#include <Servo.h>

Servo servo;

unsigned long lastTime = 0;
unsigned long timerDelay = 2000;


typedef struct struct_message {
  int b;
} struct_message;

// Create a struct_message called myData
struct_message myData;

// Callback function that will be executed when data is received
void OnDataRecv(uint8_t * mac, uint8_t *incomingData, uint8_t len) {
  memcpy(&myData, incomingData, sizeof(myData));
  Serial.print("Bytes received: ");
  Serial.println(len);
  Serial.print("Char: ");
  Serial.println(myData.b);
  Serial.println();

  if (myData.b == 1) {
    servo.write(145); //adjust this number if you want to change the degree of the servo
  } else {
    servo.write(180);
  }
}

void setup() {

  servo.attach(2); //Gpio-2 of nodemcu with pwm pin of servo motor
  servo.write(180);

  // Initialize Serial Monitor
  Serial.begin(115200);

  // Set device as a Wi-Fi Station
  WiFi.mode(WIFI_STA);

  // Init ESP-NOW
  if (esp_now_init() != 0) {
    Serial.println("Error initializing ESP-NOW");
    return;
  }

  // Once ESPNow is successfully Init, we will register for recv CB to
  // get recv packer info
  esp_now_set_self_role(ESP_NOW_ROLE_SLAVE);
  esp_now_register_recv_cb(OnDataRecv);

}

void loop() {
}

Code the ESP8266 With a Push Button

6.jpg
  • Create a circuit similar to the picture above:
  1. 1 leg of the push button connected to 3V and RST pin
  2. another leg connected to ground


  • Copy and execute the code below (REMEMBER to replace the mac address with your ESP8266's mac address). If you find an error while executing the code, remove the wire that connected to the RST pin. After executing, put the wire back.
#include <ESP8266WiFi.h>
#include <espnow.h>

int buttonState = 0;

// REPLACE WITH RECEIVER MAC Address
uint8_t broadcastAddress[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};

// Structure example to send data
// Must match the receiver structure
typedef struct struct_message {
  int b;
} struct_message;

// Create a struct_message called myData
struct_message myData;

unsigned long lastTime = 0;
unsigned long timerDelay = 500;  // send readings timer

// Callback when data is sent
void OnDataSent(uint8_t *mac_addr, uint8_t sendStatus) {
  Serial.print("Last Packet Send Status: ");
  if (sendStatus == 0) {
    Serial.println("Delivery success");
  }
  else {
    Serial.println("Delivery fail");
  }
}

void setup() {

  // Init Serial Monitor
  Serial.begin(115200);

  // Set device as a Wi-Fi Station
  WiFi.mode(WIFI_STA);

  // Init ESP-NOW
  if (esp_now_init() != 0) {
    Serial.println("Error initializing ESP-NOW");
    return;
  }

  // Once ESPNow is successfully Init, we will register for Send CB to
  // get the status of Trasnmitted packet
  esp_now_set_self_role(ESP_NOW_ROLE_CONTROLLER);
  esp_now_register_send_cb(OnDataSent);

  // Register peer
  esp_now_add_peer(broadcastAddress, ESP_NOW_ROLE_SLAVE, 1, NULL, 0);
}

void loop() {
  if ((millis() - lastTime) > timerDelay) {

    // Set values to send
    myData.b = 1;

    // Send message via ESP-NOW
    esp_now_send(broadcastAddress, (uint8_t *) &myData, sizeof(myData));
    delay(2000);
    
    myData.b = 0;
    esp_now_send(broadcastAddress, (uint8_t *) &myData, sizeof(myData));

    lastTime = millis();

    ESP.deepSleep(0);
  }
}

Checking the Communication Between the ESP8266

  • Turn on both ESP8266
  • Press the push button to check if the ESP is connected

If the servo move when you press the button, we will move on the next step. If it does not move, check the mac address and try again.

The next phase is to supply the ESP8266 with battery.

Turning on ESP8266 With Battery

In this step, we will supply power to the ESP8266 that attached with the push button

Make sure the battery produce voltage around 3.3V to 5V.

  • Put the battery into the battery holder
  • Connect the positive (+) wire to VIN pin
  • Connect the negative (-) wire to ground (G pin)

The ESP8266 should be turned on by now. Press the button and see the blue LED flash to indicate the ESP8266 is properly functioning.

Soldering a PCB to Supply Power to ESP8266 With Servo

Nodemcu-ESP8266-Power-supply-circuit-diagram-640x303.png

The diagram circuit (Fahad, 2019) above provide a clear instruction on how we should connect and solder our circuit. In this step, we will power the microcontroller with an adaptor so later on we could easily place the servo and the microcontroller besides the expected switch.

If you need more detailed explanation regarding the pictures and soldering go to the website or watch the tutorial (updated tutorial: tutorial).


References:

Fahad, E. (2019, May 7). Power supply for Nodemcu esp8266 WiFi module, circuit diagram. Electronic Clinic. https://www.electroniclinic.com/power-supply-for-nodemcu-esp8266-wifi-module/

(OPTIONAL) Design 3D Case for the Remote

7.jpg

In this step, we will use 3D printer to print the case of the remote so it looks good. You could design you own design using other software. For my design, I used Tinkercad. If you want to have the same design, you could download the file that I attached.

  • Design your own case or simply download my design
  • Download Ultimaker Cura software to slice and configure the design
  • Print and wait for your case

When the case is completed, place the battery, push button and microcontroller to its place (make sure to maintain the connection by soldering them together). I used external pin socket to attached the battery and push button wires within the case after that I connect the pin socket with the ESP and finished

Downloads

Finishing

In this step, we have a fully functional remote and switch presser. Now we simply attach the servo and the microcontroller besides the expected switch and plug the adaptor. Press the button in the remote and the servo will now be your wireless finger.