-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
80 lines (62 loc) · 2.2 KB
/
main.cpp
File metadata and controls
80 lines (62 loc) · 2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <Arduino.h>
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>
#include <string>
#include "nvs_flash.h"
#include "utils.h"
#include "image_array1.h"
#include "image_array2.h"
#include "image_array3.h"
#define LED_BUILTIN 2
BLEServer *imageServer = NULL;
// Services
#define IMAGE_SERVICE_UUID "0000181a-0000-1000-8000-00805f9b34fb" // Environmental Sensing Service (ESS) UUiD: 0x181A
BLEService *imageService = NULL;
// Characteristics
#define IMAGE_CHARACTERISTIC_UUID "0000181a-0000-1000-8000-00805f9b34fc"
BLECharacteristic *imageCharacteristic = NULL;
// Advertising
BLEAdvertising *deviceAdvertising = NULL;
// Other Globals
const std::string DEVICE_NAME = "IoT9_ESP32";
const unsigned char* images[] = {image1, image2, image3};
const unsigned int imageSizes[] = {image1_size, image2_size, image3_size};
class MyServerCallbacks: public BLEServerCallbacks {
void onDisconnect(BLEServer* pServer) {
deviceAdvertising->start();
}
};
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(115200);
// Clean to apply device name change
esp_err_t err = nvs_flash_init();
if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND) {
ESP_ERROR_CHECK(nvs_flash_erase());
err = nvs_flash_init();
}
// Initialize BLE Device
BLEDevice::init(DEVICE_NAME);
imageServer = BLEDevice::createServer();
imageServer->setCallbacks(new MyServerCallbacks());
// Main Service
imageService = imageServer->createService(IMAGE_SERVICE_UUID);
// Main Characteristic
imageCharacteristic = imageService->createCharacteristic(
IMAGE_CHARACTERISTIC_UUID,
BLECharacteristic::PROPERTY_NOTIFY
);
// Start Advertising BLE
imageService->start();
deviceAdvertising = BLEDevice::getAdvertising();
deviceAdvertising->start();
std::string message = "BLE device is now advertising with name: " + DEVICE_NAME;
Serial.println(message.c_str());
}
void loop() {
static unsigned int index = 0;
sendImage(imageCharacteristic, images[index], imageSizes[index], 508, 10);
index = (++index % 3);
delay(4000);
}