22 Async_PET_Check.ino
33 For ESP32 using WiFi along with BlueTooth BLE
44
5- Blynk_Async_ESP32_BT_WF is a library, using AsyncWebServer instead of (ESP8266)WebServer for inclusion of both ESP32
5+ Blynk_Async_ESP32_BT_WF is a library, using AsyncWebServer instead of (ESP8266)WebServer for inclusion of both ESP32
66 Blynk BT/BLE and WiFi libraries. Then select either one or both at runtime.
7-
7+
88 Based on and modified from Blynk library v0.6.1 https://github.com/blynkkk/blynk-library/releases
99 Built by Khoi Hoang https://github.com/khoih-prog/Blynk_Async_ESP32_BT_WF
1010 Licensed under MIT license
1313
1414 Version Modified By Date Comments
1515 ------- ----------- ---------- -----------
16- 1.0.6 K Hoang 25/08/2020 Initial coding to use (ESP)AsyncWebServer instead of (ESP8266)WebServer.
16+ 1.0.6 K Hoang 25/08/2020 Initial coding to use (ESP)AsyncWebServer instead of (ESP8266)WebServer.
1717 Bump up to v1.0.16 to sync with BlynkESP32_BT_WF v1.0.6.
1818 *****************************************************************************************************************************/
1919/* ***************************************************************************************************************************
2020 Example Created by Miguel Alexandre Wisintainer
2121 See https://nina-b302-scanner-presenca.blogspot.com/2020/06/nina-w102-ble-detector-presenca-de-pet.html
2222 Date: 06/06/2020
23-
23+
2424 Important Notes:
2525 1) Sketch is ~0.9MB of code because only 1 instance of Blynk if #define BLYNK_USE_BT_ONLY => true
2626 2) Sketch is very large (~1.3MB code) because 2 instances of Blynk if #define BLYNK_USE_BT_ONLY => false
27- 3) To conmpile, use Partition Scheem with large APP size, such as
27+ 3) To conmpile, use Partition Scheme with large APP size, such as
2828 a) 8MB Flash (3MB APP, 1.5MB FAT) if use EEPROM
2929 b) No OTA (2MB APP, 2MB SPIFFS)
3030 c) No OTA (2MB APP, 2MB FATFS) if use EEPROM
@@ -55,15 +55,18 @@ void set_led(byte status)
5555
5656void noticeAlive (void )
5757{
58- if (USE_BLE)
59- Blynk_BLE.virtualWrite (V0, F (" OK" ));
60- else
61- Blynk_WF.virtualWrite (V0, F (" OK" ));
58+ if (Blynk_WF.connected ())
59+ {
60+ if (USE_BLE)
61+ Blynk_BLE.virtualWrite (V0, F (" OK" ));
62+ else
63+ Blynk_WF.virtualWrite (V0, F (" OK" ));
6264
63- if (NEAR_PET == 0 ) // NOT FOUND PET ON SCAN!!!!! ALERT THE BLYNK
64- Blynk_WF.notify (" Hi, i cant find the PET!!!!" );
65+ if (NEAR_PET == 0 ) // NOT FOUND PET ON SCAN!!!!! ALERT THE BLYNK
66+ Blynk_WF.notify (" Hi, i cant find the PET!!!!" );
6567
66- NEAR_PET = 0 ; // SCAN AGAIN
68+ NEAR_PET = 0 ; // SCAN AGAIN
69+ }
6770}
6871
6972void heartBeatPrint (void )
@@ -74,7 +77,7 @@ void heartBeatPrint(void)
7477 {
7578 set_led (HIGH);
7679 led_ticker.once_ms (111 , set_led, (byte) LOW);
77- Serial.print (F (" B" ));
80+ Serial.print (F (" B" ));
7881 }
7982 else
8083 {
@@ -115,16 +118,17 @@ char BLE_Device_Name[] = "PET-Check-BLE";
115118
116119int scanTime = 5 ; // In seconds
117120BLEScan* pBLEScan;
121+ bool BLE_Initialized = false ;
118122
119123char BLE_Manufacturer_Data[] = " 4c000215fda50693a4e24fb1afcfc6eb07647825271b271bc9" ;
120124
121- class MyAdvertisedDeviceCallbacks : public BLEAdvertisedDeviceCallbacks
125+ class MyAdvertisedDeviceCallbacks : public BLEAdvertisedDeviceCallbacks
122126{
123- void onResult (BLEAdvertisedDevice advertisedDevice)
127+ void onResult (BLEAdvertisedDevice advertisedDevice)
124128 {
125129 char * manufacturerdata = BLEUtils::buildHexData (NULL , (uint8_t *)advertisedDevice.getManufacturerData ().data (), advertisedDevice.getManufacturerData ().length ());
126130 Serial.println (" Advertised Device: " + String (manufacturerdata));
127-
131+
128132 if (strcmp (BLE_Manufacturer_Data, manufacturerdata) == 0 )
129133 {
130134 NEAR_PET++; // just to identify that found the PET near!
@@ -135,12 +139,38 @@ class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks
135139
136140void checkPet (void )
137141{
138- BLEScanResults foundDevices = pBLEScan->start (scanTime, false );
139- Serial.print (F (" Devices found: " ));
140- Serial.println (foundDevices.getCount ());
141- Serial.println (F (" Scan done!" ));
142- // delete results fromBLEScan buffer to release memory
143- pBLEScan->clearResults ();
142+ if (BLE_Initialized)
143+ {
144+ Serial.println (F (" Scanning..." ));
145+
146+ BLEScanResults foundDevices = pBLEScan->start (scanTime, false );
147+ Serial.print (F (" Devices found: " ));
148+ Serial.println (foundDevices.getCount ());
149+ Serial.println (F (" Scan done!" ));
150+ // delete results fromBLEScan buffer to release memory
151+ pBLEScan->clearResults ();
152+ }
153+ }
154+
155+ void init_BLE ()
156+ {
157+ if (Blynk_WF.connected () && !BLE_Initialized)
158+ {
159+ BLEDevice::init (" " );
160+
161+ BLE_Initialized = true ;
162+
163+ // create new scan
164+ pBLEScan = BLEDevice::getScan ();
165+ pBLEScan->setAdvertisedDeviceCallbacks (new MyAdvertisedDeviceCallbacks ());
166+
167+ // active scan uses more power, but get results faster
168+ pBLEScan->setActiveScan (true );
169+
170+ pBLEScan->setInterval (100 );
171+ // less or equal setInterval value
172+ pBLEScan->setWindow (99 );
173+ }
144174}
145175
146176void setup ()
@@ -161,7 +191,7 @@ void setup()
161191#endif
162192
163193 pinMode (WIFI_BLE_SELECTION_PIN, INPUT_PULLUP);
164-
194+
165195 pinMode (LED_BUILTIN, OUTPUT);
166196
167197 if (digitalRead (WIFI_BLE_SELECTION_PIN) == HIGH)
@@ -172,9 +202,27 @@ void setup()
172202#if ESP32_BLE_WF_DEBUG
173203 Serial.println (F (" USE_BLYNK_WM: Blynk_WF begin" ));
174204#endif
175- // Set config portal channel, defalut = 1. Use 0 => random channel from 1-13 to avoid conflict
205+ // Set config portal SSID and Password
206+ Blynk.setConfigPortal (" TestPortal-ESP32" , " TestPortalPass" );
207+ // Set config portal IP address
208+ Blynk.setConfigPortalIP (IPAddress (192 , 168 , 232 , 1 ));
209+
210+ // Set config portal channel, default = 1. Use 0 => random channel from 1-13 to avoid conflict
176211 Blynk_WF.setConfigPortalChannel (0 );
177212
213+ // From v1.0.6, select either one of these to set static IP + DNS
214+ Blynk.setSTAStaticIPConfig (IPAddress (192 , 168 , 2 , 232 ), IPAddress (192 , 168 , 2 , 1 ), IPAddress (255 , 255 , 255 , 0 ));
215+ // Blynk.setSTAStaticIPConfig(IPAddress(192, 168, 2, 232), IPAddress(192, 168, 2, 1), IPAddress(255, 255, 255, 0),
216+ // IPAddress(192, 168, 2, 1), IPAddress(8, 8, 8, 8));
217+ // Blynk.setSTAStaticIPConfig(IPAddress(192, 168, 2, 232), IPAddress(192, 168, 2, 1), IPAddress(255, 255, 255, 0),
218+ // IPAddress(4, 4, 4, 4), IPAddress(8, 8, 8, 8));
219+
220+ // Use this to default DHCP hostname to ESP8266-XXXXXX or ESP32-XXXXXX
221+ // Blynk.begin();
222+ // Use this to personalize DHCP hostname (RFC952 conformed)
223+ // 24 chars max,- only a..z A..Z 0..9 '-' and no '-' as last char
224+ // Blynk.begin("PET-Check-BLE");
225+
178226 Blynk_WF.begin (BLE_Device_Name);
179227#else
180228 // Blynk_WF.begin(auth, ssid, pass);
@@ -187,8 +235,11 @@ void setup()
187235 else
188236 {
189237 USE_BLE = true ;
238+
190239 Serial.println (F (" GPIO14 LOW, Use BLE" ));
240+
191241 Blynk_BLE.setDeviceName (BLE_Device_Name);
242+
192243#if USE_BLYNK_WM
193244 if (Blynk_WF.getBlynkBLEToken () == NO_CONFIG)
194245 {
@@ -219,35 +270,25 @@ void setup()
219270 }
220271 }
221272
222- Serial.println (F (" Scanning..." ));
223-
224- BLEDevice::init (" " );
225- // create new scan
226- pBLEScan = BLEDevice::getScan ();
227- pBLEScan->setAdvertisedDeviceCallbacks (new MyAdvertisedDeviceCallbacks ());
228-
229- // active scan uses more power, but get results faster
230- pBLEScan->setActiveScan (true );
231-
232- pBLEScan->setInterval (100 );
233- // less or equal setInterval value
234- pBLEScan->setWindow (99 );
235-
273+ init_BLE ();
274+
236275 // Important, need to keep constant communication to Blynk Server at least once per ~25s
237276 // Or Blynk will lost and have to (auto)reconnect
238277 timer.setInterval (10000L , noticeAlive);
239278
240- // Scan for Pet every 5.1s
241- timer.setInterval (5100L , checkPet);
279+ // Scan for Pet every 60s ( 5.1s)
280+ timer.setInterval (60000L , checkPet);
242281}
243282
244283void loop ()
245284{
285+ init_BLE ();
286+
246287 if (USE_BLE)
247288 Blynk_BLE.run ();
248289 else
249290 Blynk_WF.run ();
250-
291+
251292 timer.run ();
252293 checkStatus ();
253294}
0 commit comments