... | ... |
@@ -2,79 +2,79 @@ |
2 | 2 |
Based on Neil Kolban example for IDF: https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLE%20Tests/SampleScan.cpp |
3 | 3 |
Ported to Arduino ESP32 by Evandro Copercini |
4 | 4 |
*/ |
5 |
- |
|
5 |
+#ifndef ARDUINO_ESP32_DEV |
|
6 |
+ error "Board Must be /ESP32 Wrover Module/ OR /????/" |
|
7 |
+#endif //ARDUINO_ESP32_DEV |
|
6 | 8 |
#include <Arduino.h> |
7 |
-#include <sstream> |
|
8 |
- |
|
9 |
-#include <BLEDevice.h> |
|
10 |
-#include <BLEUtils.h> |
|
11 |
-#include <BLEScan.h> |
|
12 |
-#include <BLEAdvertisedDevice.h> |
|
13 |
- |
|
14 |
-#include "soc/soc.h" |
|
15 |
-#include "soc/rtc_cntl_reg.h" |
|
16 | 9 |
|
17 |
-#include "serial.h" |
|
10 |
+#include "soc/soc.h" // Disable brownout problems |
|
11 |
+#include "soc/rtc_cntl_reg.h" // Disable brownout problems |
|
18 | 12 |
|
19 |
-class MyAdvertisedDeviceCallbacks : public BLEAdvertisedDeviceCallbacks |
|
20 |
-{ |
|
21 |
- void onResult(BLEAdvertisedDevice advertisedDevice) |
|
22 |
- { |
|
23 |
- uint8_t process_entry = 0; |
|
24 |
- if( true == gNoFilter ) { |
|
25 |
- process_entry = 1; |
|
26 |
- } else if( 0 == strncmp( advertisedDevice.getAddress().toString().c_str(), gFilter, strlen(gFilter) ) ) { |
|
27 |
- process_entry = 1; |
|
28 |
- } |
|
29 |
- if(0 != process_entry) { |
|
30 |
- Serial.printf("MAC Address : %s\r\n", advertisedDevice.getAddress().toString().c_str()); |
|
31 |
- { |
|
32 |
- uint8_t* mdp = (uint8_t*)advertisedDevice.getPayload(); |
|
33 |
- char *pHex = BLEUtils::buildHexData(nullptr, mdp, advertisedDevice.getPayloadLength()); |
|
34 |
- Serial.printf("Payload : %s\r\n",pHex); |
|
35 |
- free(pHex); |
|
36 |
- } |
|
37 |
- if (advertisedDevice.haveManufacturerData()) { |
|
38 |
- std::string md = advertisedDevice.getManufacturerData(); |
|
39 |
- uint8_t* mdp = (uint8_t*)advertisedDevice.getManufacturerData().data(); |
|
40 |
- char *pHex = BLEUtils::buildHexData(nullptr, mdp, md.length()); |
|
41 |
- Serial.printf("ManufacturerData : %s\r\n", pHex); |
|
42 |
- free(pHex); |
|
43 |
- } |
|
44 |
- if (advertisedDevice.haveServiceUUID()) { |
|
45 |
- Serial.printf("Service UUID : %s\r\n", advertisedDevice.getServiceUUID().toString().c_str()); |
|
46 |
- } |
|
47 |
- Serial.printf("--------------------------------------------------\r\n"); |
|
48 |
- } |
|
49 |
- pollSerial(); |
|
50 |
- } |
|
51 |
-}; |
|
13 |
+// My Includes |
|
14 |
+#include <dummy.h> |
|
15 |
+#include "my_constants.h" |
|
16 |
+//#include "my_draw.h" |
|
17 |
+#include "my_wifi.h" |
|
18 |
+#include "my_sd.h" |
|
19 |
+#include "my_ntp.h" |
|
20 |
+#include "my_serial.h" |
|
21 |
+#include "my_ota.h" |
|
22 |
+#include "my_mqtt.h" |
|
23 |
+#include "my_http.h" |
|
24 |
+#include "my_ble.h" |
|
52 | 25 |
|
53 | 26 |
void setup() |
54 | 27 |
{ |
55 | 28 |
WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0); //disable brownout detector |
56 | 29 |
Serial.begin(115200); |
57 |
- Serial.println("ESP32 BLE Scanner"); |
|
58 |
- BLEDevice::init(""); |
|
30 |
+ Serial.setDebugOutput(true); |
|
31 |
+ Serial.println(String(APPNAME)); |
|
32 |
+ |
|
33 |
+ sd_set(); |
|
34 |
+ WiFiConnect(); |
|
35 |
+ |
|
36 |
+ if( WiFi.status() == WL_CONNECTED ) { |
|
37 |
+ setClock(); |
|
38 |
+#ifdef USE_MQTT |
|
39 |
+ sprintf(clientID,clientIDFmt,WiFi.macAddress().c_str()); |
|
40 |
+ mqtt_connect(); |
|
41 |
+#endif //USE_MQTT |
|
42 |
+ } |
|
43 |
+ Serial.println("BuildDate:["+String(compile_date)+"]"); |
|
44 |
+ ble_set(); |
|
45 |
+ http_set(); |
|
59 | 46 |
} |
60 | 47 |
|
61 | 48 |
void loop() |
62 | 49 |
{ |
63 |
- // put your main code here, to run repeatedly: |
|
64 |
- BLEScan *pBLEScan = BLEDevice::getScan(); //create new scan |
|
65 |
- pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks()); |
|
66 |
- pBLEScan->setActiveScan(true); //active scan uses more power, but get results faster |
|
67 |
- pBLEScan->setInterval(gInterval); |
|
68 |
- pBLEScan->setWindow(gWindow); |
|
69 |
- Serial.printf("Start BLE scan for %d seconds...\r\n", gScanTime); |
|
70 |
- Serial.printf("setInterval %d\r\n", gInterval); |
|
71 |
- Serial.printf("setWindow %d\r\n", gWindow); |
|
72 |
- if( 0 != strcmp(gFilter,"none")) { |
|
73 |
- Serial.printf("Filter on [%s]...\r\n", gFilter); |
|
74 |
- } else { |
|
75 |
- Serial.printf("No Filter\r\n"); |
|
50 |
+ unsigned long time; |
|
51 |
+ time = millis(); |
|
52 |
+ if (time - timeSinceLastModeSwitch > UPDATE_PERIOD) { |
|
53 |
+ if( 0 == upgradeInProgress ) { |
|
54 |
+ countMeasures++; |
|
55 |
+ getTimeString(); |
|
56 |
+ getBLEScan(); |
|
57 |
+ sd_write(); |
|
58 |
+ } |
|
59 |
+ } else if( time > MAX_RUNNING_TIME ) { |
|
60 |
+ //Restart the board if running time is > MAX_RUNNING_TIME |
|
61 |
+ #ifdef USE_MQTT |
|
62 |
+ mqtt_reconnect(); |
|
63 |
+ String MQTTMsg = "{\"time\": \""+String(acTimeStringTZ)+"\", \"MAC\": \""+WiFi.macAddress()+"\", \"status\": \"Triggered reboot\"}"; |
|
64 |
+ if (!client.publish(mqtt_topic, MQTTMsg.c_str())) { |
|
65 |
+ Serial.println("ERROR: MQTT not sent!"); |
|
66 |
+ } |
|
67 |
+ #endif //USE_MQTT |
|
68 |
+ Serial.println("Triggered reboot"); |
|
69 |
+ ESP.restart(); |
|
70 |
+ } else if(WiFi.status() != WL_CONNECTED) { |
|
71 |
+ //Reconnect WiFi if not connected |
|
72 |
+ WiFiConnect(); |
|
73 |
+ #ifdef USE_MQTT |
|
74 |
+ mqtt_connect(); |
|
75 |
+ #endif //USE_MQTT |
|
76 | 76 |
} |
77 |
- BLEScanResults foundDevices = pBLEScan->start(gScanTime); |
|
78 |
- |
|
79 | 77 |
pollSerial(); |
78 |
+ server.handleClient(); |
|
79 |
+ delay(LOOP_DELAY); |
|
80 | 80 |
} |