My project is to read virtual pin blynk and send it to node sensor via gateway.
but when i run the program, BLYNK_WRITE not working. Below is my code :
#define BLYNK_PRINT Serial
#include "BLEDevice.h"
#include "BLEScan.h"
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
static BLEUUID serviceUUID1("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
static BLEUUID serviceUUID2("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
static BLEUUID serviceUUID3("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
static BLEUUID serviceUUID4("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
static BLEUUID charUUID("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
char auth[] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
char ssid[] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
char pass[] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
BlynkTimer timer;
int y=0,z=0;
int x1,x2,x3,x4;
int a=0,b=0,c=0,d=0;
String temp1,temp2,temp3,temp4;
unsigned long _time;
static boolean doConnect = false;
static boolean connected = false;
static boolean doScan = false;
static BLERemoteCharacteristic* pRemoteCharacteristic;
static BLEAdvertisedDevice* myDevice;
void connectnode(){
if(y==0 && _time==2){
connectToServer1();
delay(2000);
BLEDevice::getScan()->start(0);
y=1;
a=1;
}
if(y==1 && _time==12){
connectToServer2();
delay(2000);
BLEDevice::getScan()->start(0);
y=2;
b=1;
}
if(y==2 && _time==22){
connectToServer3();
delay(2000);
BLEDevice::getScan()->start(0);
y=3;
c=1;
}
if(y==3 && _time==32){
connectToServer4();
d=1;
delay(2000);
//ESP.restart();
}
if(a==1 && b==1 && c==1 && d==1){
Serial.print(a);
Serial.print(b);
Serial.println(c);
delay(3000);
Blynk.begin(auth, ssid, pass);
while(connected = true){
Blynk.run();
timer.run();
if(x1==1 || x2==1 || x3==1 || x4==1){
connected=false;
}
}
a=0;
b=0;
c=0;
d=0;
y=0;
BLEDevice::getScan()->start(0);
}
}
BLYNK_WRITE(V5)
{
x1 = param.asInt();
Serial.println(param.asInt());
}
BLYNK_WRITE(V6)
{
x2 = param.asInt();
Serial.println(param.asInt());
}
BLYNK_WRITE(V7)
{
x3 = param.asInt();
Serial.println(param.asInt());
}
BLYNK_WRITE(V8)
{
x4 = param.asInt();
Serial.println(param.asInt());
}
static void notifyCallback(
BLERemoteCharacteristic* pBLERemoteCharacteristic,
uint8_t* pData,
size_t length,
bool isNotify) {
Serial.print("Notify callback for characteristic ");
Serial.print(pBLERemoteCharacteristic->getUUID().toString().c_str());
Serial.print(" of data length ");
Serial.println(length);
Serial.print("data: ");
Serial.println((char*)pData);
}
class MyClientCallback : public BLEClientCallbacks {
void onConnect(BLEClient* pclient) {
}
void onDisconnect(BLEClient* pclient) {
connected = false;
Serial.println("onDisconnect");
}
};
bool connectToServer1() {
Serial.print("Forming a connection to ");
Serial.println(myDevice->getAddress().toString().c_str());
BLEClient* pClient = BLEDevice::createClient();
Serial.println(" - Created client");
pClient->setClientCallbacks(new MyClientCallback());
pClient->connect(myDevice);
Serial.println(" - Connected to server");
BLERemoteService* pRemoteService = pClient->getService(serviceUUID1);
if (pRemoteService == nullptr) {
Serial.print("Failed to find our service UUID: ");
Serial.println(serviceUUID1.toString().c_str());
pClient->disconnect();
return false;
}
Serial.println(" - Found our service");
pRemoteCharacteristic = pRemoteService->getCharacteristic(charUUID);
if (pRemoteCharacteristic == nullptr) {
Serial.print("Failed to find our characteristic UUID: ");
Serial.println(charUUID.toString().c_str());
pClient->disconnect();
return false;
}
Serial.println(" - Found our characteristic");
if(pRemoteCharacteristic->canRead()) {
std::string value = pRemoteCharacteristic->readValue();
Serial.print("The characteristic value was: ");
Serial.println(value.c_str());
}
std::string value = pRemoteCharacteristic->readValue();
Serial.print("Received Value: ");
Serial.println(value.c_str());
Serial.println("*********");
String totalString = value.c_str();
temp1 = totalString.substring(0,1);
if(temp1 == "0"){
Blynk.virtualWrite(V1,"Kontak1 : Normal");
}
if(temp1 == "1"){
Blynk.virtualWrite(V1,"Kontak1 : Mati");
}
Serial.print("Node Sensor 1 : ");
Serial.println(x1);
if(x1==1){
pRemoteCharacteristic->writeValue("1");}
if(x1==0){
pRemoteCharacteristic->writeValue("0");}
pClient->disconnect();
}
bool connectToServer2() {
Serial.print("Forming a connection to ");
Serial.println(myDevice->getAddress().toString().c_str());
BLEClient* pClient = BLEDevice::createClient();
Serial.println(" - Created client");
pClient->setClientCallbacks(new MyClientCallback());
pClient->connect(myDevice);
Serial.println(" - Connected to server");
BLERemoteService* pRemoteService = pClient->getService(serviceUUID2);
if (pRemoteService == nullptr) {
Serial.print("Failed to find our service UUID: ");
Serial.println(serviceUUID2.toString().c_str());
pClient->disconnect();
return false;
}
Serial.println(" - Found our service");
pRemoteCharacteristic = pRemoteService->getCharacteristic(charUUID);
if (pRemoteCharacteristic == nullptr) {
Serial.print("Failed to find our characteristic UUID: ");
Serial.println(charUUID.toString().c_str());
pClient->disconnect();
return false;
}
Serial.println(" - Found our characteristic");
if(pRemoteCharacteristic->canRead()) {
std::string value = pRemoteCharacteristic->readValue();
Serial.print("The characteristic value was: ");
Serial.println(value.c_str());
}
std::string value = pRemoteCharacteristic->readValue();
Serial.print("Received Value: ");
Serial.println(value.c_str());
Serial.println("*********");
String totalString = value.c_str();
temp2 = totalString.substring(0,1);
if(temp2 == "0"){
Blynk.virtualWrite(V2,"Kontak2 : Normal");
}
if(temp2 == "1"){
Blynk.virtualWrite(V2,"Kontak2 : Mati");
}
Serial.print("Node Sensor 2 : ");
Serial.println(x1);
if(x2==1){
pRemoteCharacteristic->writeValue("1");}
if(x2==0){
pRemoteCharacteristic->writeValue("0");}
pClient->disconnect();
}
bool connectToServer3() {
Serial.print("Forming a connection to ");
Serial.println(myDevice->getAddress().toString().c_str());
BLEClient* pClient = BLEDevice::createClient();
Serial.println(" - Created client");
pClient->setClientCallbacks(new MyClientCallback());
pClient->connect(myDevice);
Serial.println(" - Connected to server");
BLERemoteService* pRemoteService = pClient->getService(serviceUUID3);
if (pRemoteService == nullptr) {
Serial.print("Failed to find our service UUID: ");
Serial.println(serviceUUID3.toString().c_str());
pClient->disconnect();
return false;
}
Serial.println(" - Found our service");
pRemoteCharacteristic = pRemoteService->getCharacteristic(charUUID);
if (pRemoteCharacteristic == nullptr) {
Serial.print("Failed to find our characteristic UUID: ");
Serial.println(charUUID.toString().c_str());
pClient->disconnect();
return false;
}
Serial.println(" - Found our characteristic");
if(pRemoteCharacteristic->canRead()) {
std::string value = pRemoteCharacteristic->readValue();
Serial.print("The characteristic value was: ");
Serial.println(value.c_str());
}
std::string value = pRemoteCharacteristic->readValue();
Serial.print("Received Value: ");
Serial.println(value.c_str());
Serial.println("*********");
String totalString = value.c_str();
temp3 = totalString.substring(0,1);
if(temp3 == "0"){
Blynk.virtualWrite(V3,"Kontak3 : Normal");
}
if(temp3 == "1"){
Blynk.virtualWrite(V3,"Kontak3 : Mati");
}
Serial.print("Node Sensor 3 : ");
Serial.println(x1);
if(x3==1){
pRemoteCharacteristic->writeValue("1");}
if(x3==0){
pRemoteCharacteristic->writeValue("0");}
pClient->disconnect();
}
bool connectToServer4() {
Serial.print("Forming a connection to ");
Serial.println(myDevice->getAddress().toString().c_str());
BLEClient* pClient = BLEDevice::createClient();
Serial.println(" - Created client");
pClient->setClientCallbacks(new MyClientCallback());
pClient->connect(myDevice);
Serial.println(" - Connected to server");
BLERemoteService* pRemoteService = pClient->getService(serviceUUID4);
if (pRemoteService == nullptr) {
Serial.print("Failed to find our service UUID: ");
Serial.println(serviceUUID4.toString().c_str());
pClient->disconnect();
return false;
}
Serial.println(" - Found our service");
pRemoteCharacteristic = pRemoteService->getCharacteristic(charUUID);
if (pRemoteCharacteristic == nullptr) {
Serial.print("Failed to find our characteristic UUID: ");
Serial.println(charUUID.toString().c_str());
pClient->disconnect();
return false;
}
Serial.println(" - Found our characteristic");
if(pRemoteCharacteristic->canRead()) {
std::string value = pRemoteCharacteristic->readValue();
Serial.print("The characteristic value was: ");
Serial.println(value.c_str());
}
std::string value = pRemoteCharacteristic->readValue();
Serial.print("Received Value: ");
Serial.println(value.c_str());
Serial.println("*********");
String totalString = value.c_str();
temp4 = totalString.substring(0,1);
if(temp4 == "0"){
Blynk.virtualWrite(V4,"Kontak4 : Normal");
}
if(temp4 == "1"){
Blynk.virtualWrite(V4,"Kontak4 : Mati");
}
Serial.print("Node Sensor 4 : ");
Serial.println(x1);
if(x4==1){
pRemoteCharacteristic->writeValue("1");}
if(x4==0){
pRemoteCharacteristic->writeValue("0");}
pClient->disconnect();
}
class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
void onResult(BLEAdvertisedDevice advertisedDevice) {
Serial.print("BLE Advertised Device found: ");
Serial.println(advertisedDevice.toString().c_str());
if (z == 0 && advertisedDevice.haveServiceUUID() && advertisedDevice.isAdvertisingService(serviceUUID1)) {
BLEDevice::getScan()->stop();
myDevice = new BLEAdvertisedDevice(advertisedDevice);
doScan = true;
z=1;
}
if (z == 1 && advertisedDevice.haveServiceUUID() && advertisedDevice.isAdvertisingService(serviceUUID2)) {
BLEDevice::getScan()->stop();
myDevice = new BLEAdvertisedDevice(advertisedDevice);
doScan = true;
z=2;
}
if (z == 2 && advertisedDevice.haveServiceUUID() && advertisedDevice.isAdvertisingService(serviceUUID3)) {
BLEDevice::getScan()->stop();
myDevice = new BLEAdvertisedDevice(advertisedDevice);
doScan = true;
z=3;
}
if (z == 3 && advertisedDevice.haveServiceUUID() && advertisedDevice.isAdvertisingService(serviceUUID4)) {
BLEDevice::getScan()->stop();
myDevice = new BLEAdvertisedDevice(advertisedDevice);
doScan = true;
z=0;
}
}
};
void setup() {
Serial.begin(115200);
BLEDevice::init("");
BLEScan* pBLEScan = BLEDevice::getScan();
pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
pBLEScan->setInterval(1349);
pBLEScan->setWindow(449);
pBLEScan->setActiveScan(true);
pBLEScan->start(5, false);
}
void loop() {
_time = (millis()/1000)%40;
connectnode();
delay(1000);
}