From 819462cfd9426ff5d2a04c7415dce76339688261 Mon Sep 17 00:00:00 2001 From: Khoi Hoang <57012152+khoih-prog@users.noreply.github.com> Date: Thu, 27 Aug 2020 15:21:08 -0400 Subject: [PATCH] Initial releases v1.0.6 ### Releases v1.0.6 1. Initial coding to use (ESP)AsyncWebServer instead of (ESP8266)WebServer. 2. Bump up to v1.0.6 to sync with [BlynkESP32_BT_WF library v1.0.6](https://github.com/khoih-prog/BlynkESP32_BT_WF). --- examples/Async_PET_Check/Async_PET_Check.ino | 123 ++++++++++++------- examples/Async_PET_Check/defines.h | 10 +- 2 files changed, 89 insertions(+), 44 deletions(-) diff --git a/examples/Async_PET_Check/Async_PET_Check.ino b/examples/Async_PET_Check/Async_PET_Check.ino index aceb0b1..66fb673 100644 --- a/examples/Async_PET_Check/Async_PET_Check.ino +++ b/examples/Async_PET_Check/Async_PET_Check.ino @@ -2,9 +2,9 @@ Async_PET_Check.ino For ESP32 using WiFi along with BlueTooth BLE - Blynk_Async_ESP32_BT_WF is a library, using AsyncWebServer instead of (ESP8266)WebServer for inclusion of both ESP32 + Blynk_Async_ESP32_BT_WF is a library, using AsyncWebServer instead of (ESP8266)WebServer for inclusion of both ESP32 Blynk BT/BLE and WiFi libraries. Then select either one or both at runtime. - + Based on and modified from Blynk library v0.6.1 https://github.com/blynkkk/blynk-library/releases Built by Khoi Hoang https://github.com/khoih-prog/Blynk_Async_ESP32_BT_WF Licensed under MIT license @@ -13,18 +13,18 @@ Version Modified By Date Comments ------- ----------- ---------- ----------- - 1.0.6 K Hoang 25/08/2020 Initial coding to use (ESP)AsyncWebServer instead of (ESP8266)WebServer. + 1.0.6 K Hoang 25/08/2020 Initial coding to use (ESP)AsyncWebServer instead of (ESP8266)WebServer. Bump up to v1.0.16 to sync with BlynkESP32_BT_WF v1.0.6. *****************************************************************************************************************************/ /**************************************************************************************************************************** Example Created by Miguel Alexandre Wisintainer See https://nina-b302-scanner-presenca.blogspot.com/2020/06/nina-w102-ble-detector-presenca-de-pet.html Date: 06/06/2020 - + Important Notes: 1) Sketch is ~0.9MB of code because only 1 instance of Blynk if #define BLYNK_USE_BT_ONLY => true 2) Sketch is very large (~1.3MB code) because 2 instances of Blynk if #define BLYNK_USE_BT_ONLY => false - 3) To conmpile, use Partition Scheem with large APP size, such as + 3) To conmpile, use Partition Scheme with large APP size, such as a) 8MB Flash (3MB APP, 1.5MB FAT) if use EEPROM b) No OTA (2MB APP, 2MB SPIFFS) c) No OTA (2MB APP, 2MB FATFS) if use EEPROM @@ -55,15 +55,18 @@ void set_led(byte status) void noticeAlive(void) { - if (USE_BLE) - Blynk_BLE.virtualWrite(V0, F("OK")); - else - Blynk_WF.virtualWrite(V0, F("OK")); + if (Blynk_WF.connected()) + { + if (USE_BLE) + Blynk_BLE.virtualWrite(V0, F("OK")); + else + Blynk_WF.virtualWrite(V0, F("OK")); - if (NEAR_PET == 0) //NOT FOUND PET ON SCAN!!!!! ALERT THE BLYNK - Blynk_WF.notify("Hi, i cant find the PET!!!!"); + if (NEAR_PET == 0) //NOT FOUND PET ON SCAN!!!!! ALERT THE BLYNK + Blynk_WF.notify("Hi, i cant find the PET!!!!"); - NEAR_PET = 0; //SCAN AGAIN + NEAR_PET = 0; //SCAN AGAIN + } } void heartBeatPrint(void) @@ -74,7 +77,7 @@ void heartBeatPrint(void) { set_led(HIGH); led_ticker.once_ms(111, set_led, (byte) LOW); - Serial.print(F("B")); + Serial.print(F("B")); } else { @@ -115,16 +118,17 @@ char BLE_Device_Name[] = "PET-Check-BLE"; int scanTime = 5; //In seconds BLEScan* pBLEScan; +bool BLE_Initialized = false; char BLE_Manufacturer_Data[] = "4c000215fda50693a4e24fb1afcfc6eb07647825271b271bc9"; -class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks +class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks { - void onResult(BLEAdvertisedDevice advertisedDevice) + void onResult(BLEAdvertisedDevice advertisedDevice) { char* manufacturerdata = BLEUtils::buildHexData(NULL, (uint8_t*)advertisedDevice.getManufacturerData().data(), advertisedDevice.getManufacturerData().length()); Serial.println("Advertised Device: " + String(manufacturerdata)); - + if (strcmp(BLE_Manufacturer_Data, manufacturerdata) == 0) { NEAR_PET++; //just to identify that found the PET near! @@ -135,12 +139,38 @@ class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks void checkPet(void) { - BLEScanResults foundDevices = pBLEScan->start(scanTime, false); - Serial.print(F("Devices found: ")); - Serial.println(foundDevices.getCount()); - Serial.println(F("Scan done!")); - // delete results fromBLEScan buffer to release memory - pBLEScan->clearResults(); + if (BLE_Initialized) + { + Serial.println(F("Scanning...")); + + BLEScanResults foundDevices = pBLEScan->start(scanTime, false); + Serial.print(F("Devices found: ")); + Serial.println(foundDevices.getCount()); + Serial.println(F("Scan done!")); + // delete results fromBLEScan buffer to release memory + pBLEScan->clearResults(); + } +} + +void init_BLE() +{ + if (Blynk_WF.connected() && !BLE_Initialized) + { + BLEDevice::init(""); + + BLE_Initialized = true; + + //create new scan + pBLEScan = BLEDevice::getScan(); + pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks()); + + //active scan uses more power, but get results faster + pBLEScan->setActiveScan(true); + + pBLEScan->setInterval(100); + // less or equal setInterval value + pBLEScan->setWindow(99); + } } void setup() @@ -161,7 +191,7 @@ void setup() #endif pinMode(WIFI_BLE_SELECTION_PIN, INPUT_PULLUP); - + pinMode(LED_BUILTIN, OUTPUT); if (digitalRead(WIFI_BLE_SELECTION_PIN) == HIGH) @@ -172,9 +202,27 @@ void setup() #if ESP32_BLE_WF_DEBUG Serial.println(F("USE_BLYNK_WM: Blynk_WF begin")); #endif - // Set config portal channel, defalut = 1. Use 0 => random channel from 1-13 to avoid conflict + // Set config portal SSID and Password + Blynk.setConfigPortal("TestPortal-ESP32", "TestPortalPass"); + // Set config portal IP address + Blynk.setConfigPortalIP(IPAddress(192, 168, 232, 1)); + + // Set config portal channel, default = 1. Use 0 => random channel from 1-13 to avoid conflict Blynk_WF.setConfigPortalChannel(0); + // From v1.0.6, select either one of these to set static IP + DNS + Blynk.setSTAStaticIPConfig(IPAddress(192, 168, 2, 232), IPAddress(192, 168, 2, 1), IPAddress(255, 255, 255, 0)); + //Blynk.setSTAStaticIPConfig(IPAddress(192, 168, 2, 232), IPAddress(192, 168, 2, 1), IPAddress(255, 255, 255, 0), + // IPAddress(192, 168, 2, 1), IPAddress(8, 8, 8, 8)); + //Blynk.setSTAStaticIPConfig(IPAddress(192, 168, 2, 232), IPAddress(192, 168, 2, 1), IPAddress(255, 255, 255, 0), + // IPAddress(4, 4, 4, 4), IPAddress(8, 8, 8, 8)); + + // Use this to default DHCP hostname to ESP8266-XXXXXX or ESP32-XXXXXX + //Blynk.begin(); + // Use this to personalize DHCP hostname (RFC952 conformed) + // 24 chars max,- only a..z A..Z 0..9 '-' and no '-' as last char + //Blynk.begin("PET-Check-BLE"); + Blynk_WF.begin(BLE_Device_Name); #else //Blynk_WF.begin(auth, ssid, pass); @@ -187,8 +235,11 @@ void setup() else { USE_BLE = true; + Serial.println(F("GPIO14 LOW, Use BLE")); + Blynk_BLE.setDeviceName(BLE_Device_Name); + #if USE_BLYNK_WM if (Blynk_WF.getBlynkBLEToken() == NO_CONFIG) { @@ -219,35 +270,25 @@ void setup() } } - Serial.println(F("Scanning...")); - - BLEDevice::init(""); - //create new scan - pBLEScan = BLEDevice::getScan(); - pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks()); - - //active scan uses more power, but get results faster - pBLEScan->setActiveScan(true); - - pBLEScan->setInterval(100); - // less or equal setInterval value - pBLEScan->setWindow(99); - + init_BLE(); + // Important, need to keep constant communication to Blynk Server at least once per ~25s // Or Blynk will lost and have to (auto)reconnect timer.setInterval(10000L, noticeAlive); - // Scan for Pet every 5.1s - timer.setInterval(5100L, checkPet); + // Scan for Pet every 60s (5.1s) + timer.setInterval(60000L, checkPet); } void loop() { + init_BLE(); + if (USE_BLE) Blynk_BLE.run(); else Blynk_WF.run(); - + timer.run(); checkStatus(); } diff --git a/examples/Async_PET_Check/defines.h b/examples/Async_PET_Check/defines.h index 051cb22..57fb30f 100644 --- a/examples/Async_PET_Check/defines.h +++ b/examples/Async_PET_Check/defines.h @@ -53,9 +53,9 @@ // Those above #define's must be placed before #include #include -#include -#include -#include "BLEScan.h" +//#include +//#include +//#include "BLEScan.h" #define USE_BLYNK_WM true //#define USE_BLYNK_WM false @@ -77,6 +77,10 @@ #endif +#include +#include +#include "BLEScan.h" + #ifndef LED_BUILTIN #define LED_BUILTIN 2 // Pin D2 mapped to pin GPIO2/ADC12 of ESP32, control on-board LED #endif