2
2
Async_PET_Check.ino
3
3
For ESP32 using WiFi along with BlueTooth BLE
4
4
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
6
6
Blynk BT/BLE and WiFi libraries. Then select either one or both at runtime.
7
-
7
+
8
8
Based on and modified from Blynk library v0.6.1 https://github.com/blynkkk/blynk-library/releases
9
9
Built by Khoi Hoang https://github.com/khoih-prog/Blynk_Async_ESP32_BT_WF
10
10
Licensed under MIT license
13
13
14
14
Version Modified By Date Comments
15
15
------- ----------- ---------- -----------
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.
17
17
Bump up to v1.0.16 to sync with BlynkESP32_BT_WF v1.0.6.
18
18
*****************************************************************************************************************************/
19
19
/* ***************************************************************************************************************************
20
20
Example Created by Miguel Alexandre Wisintainer
21
21
See https://nina-b302-scanner-presenca.blogspot.com/2020/06/nina-w102-ble-detector-presenca-de-pet.html
22
22
Date: 06/06/2020
23
-
23
+
24
24
Important Notes:
25
25
1) Sketch is ~0.9MB of code because only 1 instance of Blynk if #define BLYNK_USE_BT_ONLY => true
26
26
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
28
28
a) 8MB Flash (3MB APP, 1.5MB FAT) if use EEPROM
29
29
b) No OTA (2MB APP, 2MB SPIFFS)
30
30
c) No OTA (2MB APP, 2MB FATFS) if use EEPROM
@@ -55,15 +55,18 @@ void set_led(byte status)
55
55
56
56
void noticeAlive (void )
57
57
{
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" ));
62
64
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!!!!" );
65
67
66
- NEAR_PET = 0 ; // SCAN AGAIN
68
+ NEAR_PET = 0 ; // SCAN AGAIN
69
+ }
67
70
}
68
71
69
72
void heartBeatPrint (void )
@@ -74,7 +77,7 @@ void heartBeatPrint(void)
74
77
{
75
78
set_led (HIGH);
76
79
led_ticker.once_ms (111 , set_led, (byte) LOW);
77
- Serial.print (F (" B" ));
80
+ Serial.print (F (" B" ));
78
81
}
79
82
else
80
83
{
@@ -115,16 +118,17 @@ char BLE_Device_Name[] = "PET-Check-BLE";
115
118
116
119
int scanTime = 5 ; // In seconds
117
120
BLEScan* pBLEScan;
121
+ bool BLE_Initialized = false ;
118
122
119
123
char BLE_Manufacturer_Data[] = " 4c000215fda50693a4e24fb1afcfc6eb07647825271b271bc9" ;
120
124
121
- class MyAdvertisedDeviceCallbacks : public BLEAdvertisedDeviceCallbacks
125
+ class MyAdvertisedDeviceCallbacks : public BLEAdvertisedDeviceCallbacks
122
126
{
123
- void onResult (BLEAdvertisedDevice advertisedDevice)
127
+ void onResult (BLEAdvertisedDevice advertisedDevice)
124
128
{
125
129
char * manufacturerdata = BLEUtils::buildHexData (NULL , (uint8_t *)advertisedDevice.getManufacturerData ().data (), advertisedDevice.getManufacturerData ().length ());
126
130
Serial.println (" Advertised Device: " + String (manufacturerdata));
127
-
131
+
128
132
if (strcmp (BLE_Manufacturer_Data, manufacturerdata) == 0 )
129
133
{
130
134
NEAR_PET++; // just to identify that found the PET near!
@@ -135,12 +139,38 @@ class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks
135
139
136
140
void checkPet (void )
137
141
{
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
+ }
144
174
}
145
175
146
176
void setup ()
@@ -161,7 +191,7 @@ void setup()
161
191
#endif
162
192
163
193
pinMode (WIFI_BLE_SELECTION_PIN, INPUT_PULLUP);
164
-
194
+
165
195
pinMode (LED_BUILTIN, OUTPUT);
166
196
167
197
if (digitalRead (WIFI_BLE_SELECTION_PIN) == HIGH)
@@ -172,9 +202,27 @@ void setup()
172
202
#if ESP32_BLE_WF_DEBUG
173
203
Serial.println (F (" USE_BLYNK_WM: Blynk_WF begin" ));
174
204
#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
176
211
Blynk_WF.setConfigPortalChannel (0 );
177
212
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
+
178
226
Blynk_WF.begin (BLE_Device_Name);
179
227
#else
180
228
// Blynk_WF.begin(auth, ssid, pass);
@@ -187,8 +235,11 @@ void setup()
187
235
else
188
236
{
189
237
USE_BLE = true ;
238
+
190
239
Serial.println (F (" GPIO14 LOW, Use BLE" ));
240
+
191
241
Blynk_BLE.setDeviceName (BLE_Device_Name);
242
+
192
243
#if USE_BLYNK_WM
193
244
if (Blynk_WF.getBlynkBLEToken () == NO_CONFIG)
194
245
{
@@ -219,35 +270,25 @@ void setup()
219
270
}
220
271
}
221
272
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
+
236
275
// Important, need to keep constant communication to Blynk Server at least once per ~25s
237
276
// Or Blynk will lost and have to (auto)reconnect
238
277
timer.setInterval (10000L , noticeAlive);
239
278
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);
242
281
}
243
282
244
283
void loop ()
245
284
{
285
+ init_BLE ();
286
+
246
287
if (USE_BLE)
247
288
Blynk_BLE.run ();
248
289
else
249
290
Blynk_WF.run ();
250
-
291
+
251
292
timer.run ();
252
293
checkStatus ();
253
294
}
0 commit comments