BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

Add physical button Arduino Nano Bluetooth

gpio
arduino
hardware
#1

Hello,
i am controlling 2 relays with my arduino nano running blynk via a bluetooth LE module. Now i want to wire a physical button to the arduino to controll one of the relay aswell as with the blynk app. (virtual + physical controll)
I had only found posts for the esp8266 but with other arduino sketches…
I am using the default “Serial_HM10_HC08” Bluetooth Blynk sketch on my arduino nano.

#define BLYNK_USE_DIRECT_CONNECT

// You could use a spare Hardware Serial on boards that have it (like Mega)
#include <SoftwareSerial.h>
SoftwareSerial DebugSerial(2, 3); // RX, TX

#define BLYNK_PRINT DebugSerial
#include <BlynkSimpleSerialBLE.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "AUTHKEY";

void setup()
{
  // Debug console
  DebugSerial.begin(9600);

  // Blynk will work through Serial
  // Do not read or write this serial manually in your sketch
  Serial.begin(9600);
  Blynk.begin(Serial, auth);
}

void loop()
{
  Blynk.run();
}

Can somebody help me to modify to code so i can wire a hardware switch to the arduino and switch the relay with it (and bluetooth aswell)

Thanks,

~ Jan2220

0 Likes

#2

Since they all use the same programming language… what is the problem with testing those code examples?

Also here is an example from the Blynk Sketch Builder…

https://examples.blynk.cc/?board=Arduino%20Uno&shield=ESP8266%20WiFi%20Shield&example=More%2FSync%2FSyncPhysicalButton

We don’t really write code for you, we try to help you learn about Blynk. How to program is for you to learn and there are many other teaching sites out there for that.

1 Like

#3

Hey,
I didnt know that this Sketch Builder existed…
Thanks, now i got a sketch, thats (almost) doing what i want.
The only problem is, that the button only works if the app connects to the arduino once. When i start the arduino, the button is not working, as soon as i connect the app to the arduino once, the button works even when the app is closed…
That is pretty annoying, because the power to the arduino cuts off many times so the arduino reboots and i´ve to connect the app once again to use the button…
Is there a way so the button will always work, even when the app hasnt connected?

Im now uising this sketch:

#define BLYNK_USE_DIRECT_CONNECT

// You could use a spare Hardware Serial on boards that have it (like Mega)
#include <SoftwareSerial.h>
SoftwareSerial DebugSerial(2, 3); // RX, TX

#define BLYNK_PRINT DebugSerial
#include <BlynkSimpleSerialBLE.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "630738becd584d4d8f007e4b9494036a";

const int ledPin = 10;
const int btnPin = 11;


BlynkTimer timer;
void checkPhysicalButton();

int ledState = LOW;
int btnState = LOW;

// Every time we connect to the cloud...
BLYNK_CONNECTED() {
  // Request the latest state from the server
  Blynk.syncVirtual(V2);

  // Alternatively, you could override server state using:
  //Blynk.virtualWrite(V2, ledState);
}

// When App button is pushed - switch the state
BLYNK_WRITE(V2) {
  ledState = param.asInt();
  digitalWrite(ledPin, ledState);
}

void checkPhysicalButton()
{
  if (digitalRead(btnPin) == HIGH && btnState == LOW) {

      // Toggle LED state
    ledState = HIGH;
    digitalWrite(ledPin, ledState);

      // Update Button Widget
    Blynk.virtualWrite(V2, ledState);
    
    btnState = HIGH;
  }

  if (digitalRead(btnPin) == LOW && btnState == HIGH) {

      // Toggle LED state
    ledState = LOW;
    digitalWrite(ledPin, ledState);

      // Update Button Widget
    Blynk.virtualWrite(V2, ledState);
    
    btnState = LOW;
  }
  
}


void setup()
{
  // Debug console
  DebugSerial.begin(9600);

  DebugSerial.println("Waiting for connections...");

  // Blynk will work through Serial,
  // Do not read or write this serial manually in your sketch
  Serial.begin(9600);
  Blynk.begin(Serial, auth);
  
  pinMode(ledPin, OUTPUT);
  pinMode(btnPin, INPUT_PULLUP);
  digitalWrite(ledPin, ledState);
  timer.setInterval(100L, checkPhysicalButton);
  
}

void loop()
{
  Blynk.run();
  timer.run();
}

Thanks.

~Jan

0 Likes

#4

Yah… all the relevant inks are at the top of this page, however they tend to disappear as soon as one adds a topic. Hasn’t been fixed yet.

0 Likes

#5

If you are referring to the App Button, no… but if properly coded, then yes a sketch and physical button should keep running even if the device is not connected to the server at the time

I am not understanding why your Arduino keeps shutting off, nor why it will not automatically reconnect when powered on. EDIT - oh, you are using BT/BLE OK, that is “normal” for now I guess)

Check out this connection example… EDIT - However, it is geared for WiFi use…

0 Likes