Hi, I am trying to do RGB lightning control using blynk and BLE human presence detector using ESP32 (NodeMCU-32S)
Whenever my phone is near to the ESP32 it would turn on the RGB Strip
However it would disconnect after few BLE trigger, and wouldn’t reconnect back.
Here’s my sketch:
#define BLYNK_DEBUG
#define BLYNK_PRINT Serial
#define BLYNK_NO_BUILTIN // Disable built-in analog & digital pin operations
#define BLYNK_NO_FLOAT // Disable float operations
#define BLYNK_MAX_READBYTES 1024
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <FastLED.h>
#include <ArduinoOTA.h>
#include <ESPmDNS.h>
#include <WiFiUdp.h>
#include <BLEDevice.h>
#define NUM_LEDS 60
#define DATA_PIN 16
CRGB leds[NUM_LEDS];
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "***";
char ssid[] = "***";
char pass[] = "***";
int lastColor[3];
bool away = true;
BlynkTimer timer;
//BLE
static String pServerAddress;
BLEScan* pBLEScan;
BLEClient* pClient;
bool deviceFound = false;
String knownDevices[] = {"OnePlus 3"};
class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
/**
Called for each advertising BLE server.
*/
void onResult(BLEAdvertisedDevice advertisedDevice) {
// Serial.print("BLE Advertised Device found: ");
// Serial.println(advertisedDevice.toString().c_str());
pServerAddress = advertisedDevice.getName().c_str();
bool known = false;
for (int i = 0; i < (sizeof(knownDevices) / sizeof(knownDevices[0])); i++) {
if (strcmp(pServerAddress.c_str(), knownDevices[i].c_str()) == 0) known = true;
}
if (known) {
// Serial.print("Device found: ");
// Serial.println(advertisedDevice.getRSSI());
if (advertisedDevice.getRSSI() > -70) deviceFound = true;
else deviceFound = false;
// Serial.println(pServerAddress.c_str());
advertisedDevice.getScan()->stop();
}
}
};
//Multicore ?
static int taskCore = 0;
void coreTask( void * pvParameters ) {
while (true) {
// Serial.println("Restarting scan.");
deviceFound = false;
BLEScanResults scanResults = pBLEScan->start(30);
if (deviceFound) {
if (away == true) {
Serial.println("Device found: Turning LED On");
Blynk.virtualWrite(V1, 1);
Blynk.syncVirtual(V1);
away = false;
}
delay(2000);
}
else {
if (away == false) {
Serial.println("Device out of range: Turning LED Off");
Blynk.virtualWrite(V1, 0);
Blynk.syncVirtual(V1);
}
away = true;
delay(1000);
}
}
}
void reconnect(){
if(!Blynk.connected()){
Blynk.connect();
}
}
void setup()
{
// Debug console
Serial.begin(9600);
FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS);
Blynk.begin(auth, ssid, pass);
timer.setInterval(100, reconnect);
ArduinoOTA.setHostname("esp32");
ArduinoOTA.setPassword("***");
ArduinoOTA
.onStart([]() {
String type;
if (ArduinoOTA.getCommand() == U_FLASH)
type = "sketch";
else // U_SPIFFS
type = "filesystem";
// NOTE: if updating SPIFFS this would be the place to unmount SPIFFS using SPIFFS.end()
Serial.println("Start updating " + type);
})
.onEnd([]() {
Serial.println("\nEnd");
})
.onProgress([](unsigned int progress, unsigned int total) {
Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
})
.onError([](ota_error_t error) {
Serial.printf("Error[%u]: ", error);
if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
else if (error == OTA_END_ERROR) Serial.println("End Failed");
});
ArduinoOTA.begin();
BLEDevice::init("");
pClient = BLEDevice::createClient();
Serial.println(" - Created client");
pBLEScan = BLEDevice::getScan();
pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
pBLEScan->setActiveScan(true);
xTaskCreatePinnedToCore(
coreTask, /* Function to implement the task */
"coreTask", /* Name of the task */
10000, /* Stack size in words */
NULL, /* Task input parameter */
1, /* Priority of the task */
NULL, /* Task handle. */
taskCore); /* Core where the task should run */
}
BLYNK_WRITE(V0) {
fill_solid(leds, NUM_LEDS, CRGB(param[0].asInt(), param[1].asInt(), param[2].asInt()));
Blynk.virtualWrite(V1, 1);
FastLED.show();
for (int i = 0; i < 3; i++) {
lastColor[i] = param[i].asInt();
}
}
BLYNK_WRITE(V1) {
int on = param.asInt();
if (on == 0) {
FastLED.clear();
FastLED.show();
} else {
fill_solid(leds, NUM_LEDS, CRGB(lastColor[0], lastColor[1], lastColor[2]));
FastLED.show();
}
}
BLYNK_WRITE(V2) {
FastLED.setBrightness(param.asInt());
FastLED.show();
}
void loop()
{
Blynk.run();
ArduinoOTA.handle();
timer.run();
}
Serial output:
[2091] Connected to WiFi
[2091] IP: 192.168.0.194
[2091]
___ __ __
/ _ )/ /_ _____ / /__
/ _ / / // / _ \/ '_/
/____/_/\_, /_//_/_/\_\
/___/ v0.5.2 on Arduino
[2161] Connecting to blynk-cloud.com:80
[2262] <[02|00|01|00] ***
[2312] >[00|00|01|00|C8]
[2312] Ready (ping: 50ms).
[2379] <[11|00|02|00]Hver[00]0.5.2[00]h-beat[00]10[00]buff-in[00]1024[00]dev[00]Arduino[00]build[00]May 5 2018 14:29:12[00]
- Created client
[2847] >[00|00|02|00|C8]
Device found: Turning LED On
[7769] <[14|00|03|00|06]vw[00]1[00]1
[7838] <[10|00|04|00|04]vr[00]1
[7931] >[14|00|04|00|06]
[7931] >vw[00]1[00]1
Device out of range: Turning LED Off
[15755] <[14|00|05|00|06]vw[00]1[00]0
[15824] <[10|00|06|00|04]vr[00]1
[15920] >[14|00|06|00|06]
[15921] >vw[00]1[00]0
Device found: Turning LED On
[23256] <[14|00|07|00|06]vw[00]1[00]1
[23324] <[10|00|08|00|04]vr[00]1
[23497] >[14|00|08|00|06]
[23497] >vw[00]1[00]1
Device out of range: Turning LED Off
[25830] <[14|00|09|00|06]vw[00]1[00]0
[25899] <[10|00|0A|00|04]vr[00]1
[26055] >[14|00|0A|00|06]
[26056] >vw[00]1[00]0
Device found: Turning LED On
[28411] <[14|00|0B|00|06]vw[00]1[00]1
[28479] <[10|00|0C|00|04]vr[00]1
[28616] >[14|00|0C|00|06]
[28617] >vw[00]1[00]1
Device out of range: Turning LED Off
[30885] <[14|00|0D|00|06]vw[00]1[00]0
[30953] <[10|00|0E|00|04]vr[00]1
[31074] >[14|00|0E|00|06]
[31074] >vw[00]1[00]0
Device found: Turning LED On
[34264] <[14|00|0F|00|06]vw[00]1[00]1
[34333] <[10|00|10|00|04]vr[00]1
[34455] >[14|00|10|00|06]
[34456] >vw[00]1[00]1
Device out of range: Turning LED Off
[39444] <[14|00|11|00|06]vw[00]1[00]0
[39512] <[10|00|12|00|04]vr[00]1
[39677] >[14|00|12|00|06]
[39678] >vw[00]1[00]0
Device found: Turning LED On
[41722] <[14|00|13|00|06]vw[00]1[00]1
[41791] <[10|00|14|00|04]vr[00]1
[41928] >[14|00|14|00|06]
[41929] >vw[00]1[00]1
[51792] <[06|00|15|00|00]
[51841] >[00|00|15|00|C8]
Device out of range: Turning LED Off
[60133] <[14|00|16|00|06]vw[00]1[00]0
[60201] <[10|00|17|00|04]vr[00]1
Device found: Turning LED On
[61507] <[14|00|18|00|06]vw[00]1[00]1
[61575] <[10|00|19|00|04]vr[00]1
Device out of range: Turning LED Off
[63781] <[14|00|1A|00|06]vw[00]1[00]0
[63849] <[10|00|1B|00|04]vr[00]1
Device found: Turning LED On
[64955] <[14|00|1C|00|06]vw[00]1[00]1
[65023] <[10|00|1D|00|04]vr[00]1
Device out of range: Turning LED Off
Device out of range: Turning LED Off
[94987] <[14|00|1E|00|06]vw[00]1[00]0
[95055] <[10|00|1F|00|04]vr[00]1
Device found: Turning LED On
[102386] <[14|00] [00|06]vw[00]1[00]1
[102386] Cmd error
Device out of range: Turning LED Off