Hello is it possible to autoconnect bluetooth on Android phone to hc05 on arduino uno when i startup Blynk
Me program works ok but if i start up Blynk i have to start bluetooth on my phone .
Is there a way to do that automatish ?
And turn the bluetooth on my phone off as i close the Blynk again.
Or when i push V1 bluetooth turn on and when i push V2 turn bluetoooth off when time is done.
I used a program example from Jamin and change it a bit.
So i can use it to control 4 relays whit some time between them.
special thanks to Jamin !
#define BLYNK_USE_DIRECT_CONNECT
// You could use a spare Hardware Serial on boards that have it (like Mega)
#include <SoftwareSerial.h>
SoftwareSerial DebugSerial(0, 1); // RX, TX
//#include <ArduinoOTA.h>
#define BLYNK_PRINT DebugSerial
#include <BlynkSimpleSerialBLE.h>
#include <SimpleTimer.h>
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "************************1";
SimpleTimer timer;
int CountdownRemainReset;
int CountdownRemain;
int CountdownTimer;
long tm;
long tm2;
int t1;
int t2;
int Ba =0;
int Ba2 =0;
int Bb =0;
int Bb2 =1;
const int RelayPin = 8;
const int RelayPin1 = 9;
const int RelayPin2 = 10;
const int RelayPin3 = 11;
//-----------------------------------------------------------------------------------
void setup() {
Serial.begin(9600);
Blynk.begin(Serial, auth);
while (Blynk.connect() == false) {}
pinMode(RelayPin, OUTPUT);
pinMode(RelayPin1, OUTPUT);
pinMode(RelayPin2, OUTPUT);
pinMode(RelayPin3, OUTPUT);
digitalWrite(RelayPin, HIGH);
digitalWrite(RelayPin1, HIGH);
digitalWrite(RelayPin2, HIGH);
digitalWrite(RelayPin3, HIGH);
CountdownTimer = timer.setInterval(1000, CountdownTimerFunction);
timer.disable(CountdownTimer); // disable it on boot
}
//-------------------- Get the progress (0-100%)---------------------------------------
void CountdownProgress(int seconds){
int progress = map(seconds,CountdownRemainReset,0,0,100); // make sure to invert it
Blynk.virtualWrite(4,progress + String("%"));
}
//--------------------------------------------------------------------------------------
void CountdownTimerFunction() {
// Serial.println ("");
if (t1 == 0){
tm = millis();
t1 = 1;
}
if ((millis() - tm > 5000) && (Ba == 1)){
digitalWrite(RelayPin1, LOW);
}
if ((millis() - tm > 5000) && (Bb == 1)){
digitalWrite(RelayPin3, LOW);
}
/*
Serial.println ("------------");
Serial.println (t1);
Serial.println (tm);
Serial.println (millis());
Serial.println (Ba);
Serial.println (Bb);
*/
CountdownRemain--; // remove 1 every second
CountdownShowFormatted(CountdownRemain);
CountdownProgress(CountdownRemain);
if (!CountdownRemain) { // check if CountdownRemain == 0/FALSE/LOW
timer.disable(CountdownTimer); // if 0 stop timer
Blynk.virtualWrite(1, LOW); // reset START/STOP button status
Blynk.virtualWrite(2, LOW); // reset START/STOP button status
Blynk.virtualWrite(0, "TIMER COMPLETE");
// BLYNK_CONNECTED() {
// Blynk.syncVirtual(0);
// }
Blynk.virtualWrite(6, 255); // LED for timer completed
Blynk.virtualWrite(5, 30); // Timer LED status light off
digitalWrite(RelayPin, HIGH);
digitalWrite(RelayPin1, HIGH);
digitalWrite(RelayPin2, HIGH);
digitalWrite(RelayPin3, HIGH);
/*
Serial.println ("gggggggg");
Serial.println (t1);
Serial.println (tm);
Serial.println (millis());
Serial.println (Ba);
Serial.println (Bb);
Serial.println ("************");
Serial.println (CountdownRemain);
*/
if (Ba == 1){
Bb2 = 0;
}
if (Bb == 1){
Ba2 = 0;
}
Ba = 0;
Bb = 0;
t1 = 0;
}
else {
Blynk.virtualWrite(6, 0); // LED for timer completed
}
}
//------------------ Button Widget (Switch Type): Start/Pause Timer------------------------
BLYNK_WRITE(1) {
Ba = 1;
Ba2 = 1;
if ((param.asInt()) && (Bb == 0) && (Bb2 == 1)) {
if (CountdownRemain) { // check if there is a time set or not
timer.enable(CountdownTimer);
Blynk.virtualWrite(5, 255); // Timer LED status light on
digitalWrite(RelayPin, LOW);
//Blynk.virtualWrite(1, LOW); // if CountdownRemain is set to 0, then dont start hte timer.
//Blynk.virtualWrite(0, "COUNTDOWN TIME NOT SET"); // if CountdownRemain is set to 0, then tell the user
}
else {
CountdownRemain = 30;
CountdownShowFormatted(CountdownRemain);
timer.enable(CountdownTimer);
Blynk.virtualWrite(5, 255); // Timer LED status light on
digitalWrite(RelayPin, LOW);
/*
Serial.println ("------------");
Serial.println (t1);
Serial.println (tm);
Serial.println (millis());
Serial.println (t1);
*/
//Blynk.virtualWrite(1, LOW); // if CountdownRemain is set to 0, then dont start hte timer.
//Blynk.virtualWrite(0, "COUNTDOWN TIME NOT SET"); // if CountdownRemain is set to 0, then tell the user
}
/*
Serial.println ("------tttttttt------");
Serial.println (t1);
Serial.println (tm);
Serial.println (millis());
*/
}
else {
timer.disable(CountdownTimer);
Blynk.virtualWrite(5, 0); // Timer LED status light off
digitalWrite(RelayPin, HIGH);
digitalWrite(RelayPin1, HIGH);
digitalWrite(RelayPin2, HIGH);
digitalWrite(RelayPin3, HIGH);
t1 = 0;
Ba = 0;
}
}
//----------------- Button Widget (Switch Type): Start/Pause Timer----------------
BLYNK_WRITE(2) {
Bb = 1;
Bb2 = 1;
if ((param.asInt()) && (Ba == 0 && (Ba2 == 1))) {
if (CountdownRemain) { // check if there is a time set or not
timer.enable(CountdownTimer);
Blynk.virtualWrite(5, 255); // Timer LED status light on
digitalWrite(RelayPin2, LOW);
}
else {
CountdownRemain = 25;
CountdownShowFormatted(CountdownRemain);
timer.enable(CountdownTimer);
Blynk.virtualWrite(5, 255); // Timer LED status light on
digitalWrite(RelayPin2, LOW);
}
}
else {
timer.disable(CountdownTimer);
Blynk.virtualWrite(5, 0); // Timer LED status light off
digitalWrite(RelayPin, HIGH);
digitalWrite(RelayPin1, HIGH);
digitalWrite(RelayPin2, HIGH);
digitalWrite(RelayPin3, HIGH);
t1 = 0;
Bb = 0;
}
}
//---------------------- Button Widget (Momentary): Reset Timer--------------------------------
BLYNK_WRITE(9) {
CountdownRemain = CountdownRemainReset; // reset to original start time
CountdownShowFormatted(CountdownRemain);
}
//------------------------ Slider Widget (60-180): Set Timer (mins)----------------------------
BLYNK_WRITE(3) {
if (timer.isEnabled(CountdownTimer)) { // only update if timer not running
Blynk.virtualWrite(3, param.asInt() ); // if running, refuse to let use change slider
} else {
CountdownRemainReset = param.asInt() * 1 + 1; // + 1 set the timer to 1:00:00 instead of 00:59:59
CountdownRemain = param.asInt() * 1;
CountdownShowFormatted(CountdownRemain);
}
}
//----------------------------------------------------------------------------------------------
void CountdownShowFormatted(int seconds) {
long days = 0;
long hours = 0;
long mins = 0;
long secs = 0;
String secs_o = ":";
String mins_o = ":";
String hours_o = ":";
secs = seconds; // set the seconds remaining
mins = secs / 60; //convert seconds to minutes
hours = mins / 60; //convert minutes to hours
days = hours / 24; //convert hours to days
secs = secs - (mins * 60); //subtract the coverted seconds to minutes in order to display 59 secs max
mins = mins - (hours * 60); //subtract the coverted minutes to hours in order to display 59 minutes max
hours = hours - (days * 24); //subtract the coverted hours to days in order to display 23 hours max
if (secs < 10) {
secs_o = ":0";
}
if (mins < 10) {
mins_o = ":0";
}
if (hours < 10) {
hours_o = ":0";
}
Blynk.virtualWrite(0, mins_o + mins + secs_o + secs);
}
//--------------------------------------------------------------------------------------
void loop() {
Blynk.run();
timer.run();
}