245 lines
6.9 KiB
Arduino
245 lines
6.9 KiB
Arduino
#include <Arduino_GFX_Library.h>
|
|
#include <Adafruit_ST7789.h>
|
|
#include <Arduino.h>
|
|
|
|
#include <BLEDevice.h>
|
|
#include <BLEUtils.h>
|
|
#include <BLEServer.h>
|
|
|
|
#include <string.h>
|
|
|
|
#define TFT_DC D7
|
|
#define TFT_RST D9
|
|
|
|
unsigned long SEC=0;
|
|
unsigned long MIN=0;
|
|
unsigned long HOUR=0;
|
|
|
|
unsigned long DAY=0;
|
|
unsigned long MONTH=0;
|
|
unsigned long YEAR=2026;
|
|
|
|
String message = "";
|
|
|
|
unsigned long preMil = 0;
|
|
const long interval = 1000;
|
|
|
|
bool connected = false;
|
|
bool timeChanged = false;
|
|
|
|
Arduino_DataBus *bus = new Arduino_HWSPI(TFT_DC, -1);
|
|
Arduino_GFX *gfx = new Arduino_ST7789(bus, TFT_RST, 0, true, 240, 240, 0, 0);
|
|
|
|
const uint16_t SERVICE_TD_UUID = 0x4d43;
|
|
|
|
const uint16_t CHAR_MESSAGE_UUID = 0xb331;
|
|
const uint16_t CHAR_TIME_UUID = 0x3c4b;
|
|
const uint16_t CHAR_DATE_UUID = 0x6ecf;
|
|
|
|
|
|
const uint16_t SERVICE_NOTIFICATION_UUID = 0xa7fa;
|
|
|
|
const uint16_t CHAR_NOTIF_APP_UUID = 0x375f;
|
|
const uint16_t CHAR_NOTIF_TITLE_UUID = 0x6e40;
|
|
const uint16_t CHAR_NOTIF_TEXT_UUID = 0x2eef;
|
|
|
|
#define deviceName "ESP32-Watch"
|
|
|
|
class CharacteristicCallbacksServiceTD: public BLECharacteristicCallbacks {
|
|
void onWrite(BLECharacteristic *pCharacteristic) override {
|
|
String value = pCharacteristic->getValue();
|
|
|
|
if (value.length() == 0) return;
|
|
|
|
//Time characteristic check
|
|
if (pCharacteristic->getUUID().equals(BLEUUID(CHAR_TIME_UUID))) {
|
|
|
|
sscanf(value.c_str(), "%d %d %d", &HOUR, &MIN, &SEC);
|
|
|
|
Serial.print("Time Recived: ");
|
|
Serial.println(value);
|
|
}
|
|
if (pCharacteristic->getUUID().equals(BLEUUID(CHAR_DATE_UUID))) {
|
|
|
|
sscanf(value.c_str(), "%d %d %d", &DAY, &MONTH, &YEAR);
|
|
|
|
Serial.print("Date Recived: ");
|
|
Serial.println(value);
|
|
}
|
|
if (pCharacteristic->getUUID().equals(BLEUUID(CHAR_MESSAGE_UUID))) {
|
|
Serial.print("Message Recived: ");
|
|
message = value;
|
|
Serial.println(value);
|
|
}
|
|
}
|
|
};
|
|
|
|
class CharacteristicCallbacksNotification: public BLECharacteristicCallbacks {
|
|
void onWrite(BLECharacteristic *pCharacteristic) override {
|
|
String value = pCharacteristic->getValue();
|
|
|
|
if (value.length() == 0) return;
|
|
|
|
//Notification characteristic check
|
|
if (pCharacteristic->getUUID().equals(BLEUUID(CHAR_NOTIF_APP_UUID))) {
|
|
Serial.print("Notification App: ");
|
|
Serial.println(value);
|
|
}
|
|
|
|
if (pCharacteristic->getUUID().equals(BLEUUID(CHAR_NOTIF_TITLE_UUID))) {
|
|
Serial.print("Notification Title: ");
|
|
Serial.println(value);
|
|
}
|
|
|
|
if (pCharacteristic->getUUID().equals(BLEUUID(CHAR_NOTIF_TEXT_UUID))) {
|
|
Serial.print("Notification Text: ");
|
|
Serial.println(value);
|
|
}
|
|
}
|
|
};
|
|
|
|
class MyServerCallbacks: public BLEServerCallbacks {
|
|
void onConnect(BLEServer* pServer) {
|
|
Serial.println("Conecction with client");
|
|
connected = true;
|
|
BLEDevice::stopAdvertising();
|
|
};
|
|
|
|
void onDisconnect(BLEServer* pServer) {
|
|
Serial.println("Disconnection with client");
|
|
connected = false;
|
|
BLEDevice::startAdvertising();
|
|
}
|
|
};
|
|
|
|
void setup() {
|
|
Serial.begin(115200);
|
|
|
|
gfx->begin();
|
|
gfx->setFont(NULL);
|
|
gfx->fillScreen(ST77XX_BLACK);
|
|
|
|
gfx->setTextColor(ST77XX_GREEN, ST77XX_BLACK);
|
|
|
|
initDevice();
|
|
timeChanged = true;
|
|
}
|
|
|
|
void loop() {
|
|
mainScreen();
|
|
}
|
|
|
|
void mainScreen(){
|
|
gfx->setTextSize(2);
|
|
gfx->setCursor(20, 20);
|
|
if (connected){
|
|
gfx->print("Connected: true ");
|
|
}else {
|
|
gfx->print("Connected: flase");
|
|
}
|
|
|
|
if (timeChanged) {
|
|
gfx->setCursor(20, 50);
|
|
gfx->printf("Time: %02d:%02d:%02d\n", HOUR, MIN, SEC);
|
|
|
|
gfx->setCursor(20, 80);
|
|
gfx->printf("Date: %02d.%02d.%04d\n", DAY, MONTH, YEAR);
|
|
|
|
gfx->setCursor(20, 110);
|
|
gfx->printf("message: %s", message);
|
|
|
|
timeChanged = false;
|
|
}
|
|
|
|
updateClock();
|
|
}
|
|
|
|
void initDevice(){
|
|
BLEDevice::init(deviceName);
|
|
BLEServer *pServer = BLEDevice::createServer();
|
|
pServer->setCallbacks(new MyServerCallbacks());
|
|
|
|
BLEService *pServiceNotif = pServer->createService(SERVICE_NOTIFICATION_UUID);
|
|
BLEService *pServiceTD = pServer->createService(SERVICE_TD_UUID);
|
|
|
|
BLESecurity *pSecurity = new BLESecurity();
|
|
pSecurity->setAuthenticationMode(ESP_LE_AUTH_BOND);
|
|
pSecurity->setCapability(ESP_IO_CAP_NONE);
|
|
pSecurity->setInitEncryptionKey(ESP_BLE_ENC_KEY_MASK | ESP_BLE_ID_KEY_MASK);
|
|
|
|
//---------------ServiceNotificationChar---------------------------------
|
|
BLECharacteristic *pCharacteristicApp = pServiceNotif->createCharacteristic(
|
|
CHAR_NOTIF_APP_UUID,
|
|
BLECharacteristic::PROPERTY_READ |
|
|
BLECharacteristic::PROPERTY_WRITE);
|
|
pCharacteristicApp->setCallbacks(new CharacteristicCallbacksNotification());
|
|
pCharacteristicApp->setValue("App");
|
|
|
|
BLECharacteristic *pCharacteristicTitle = pServiceNotif->createCharacteristic(
|
|
CHAR_NOTIF_TITLE_UUID,
|
|
BLECharacteristic::PROPERTY_READ |
|
|
BLECharacteristic::PROPERTY_WRITE);
|
|
pCharacteristicTitle->setCallbacks(new CharacteristicCallbacksNotification());
|
|
pCharacteristicTitle->setValue("Title");
|
|
|
|
BLECharacteristic *pCharacteristicText = pServiceNotif->createCharacteristic(
|
|
CHAR_NOTIF_TEXT_UUID,
|
|
BLECharacteristic::PROPERTY_READ |
|
|
BLECharacteristic::PROPERTY_WRITE);
|
|
pCharacteristicText->setCallbacks(new CharacteristicCallbacksNotification());
|
|
pCharacteristicText->setValue("Text");
|
|
|
|
//---------------ServiceChar--------------------------
|
|
BLECharacteristic *pCharacteristicMessage = pServiceTD->createCharacteristic(
|
|
CHAR_MESSAGE_UUID,
|
|
BLECharacteristic::PROPERTY_READ |
|
|
BLECharacteristic::PROPERTY_WRITE);
|
|
pCharacteristicMessage->setCallbacks(new CharacteristicCallbacksServiceTD());
|
|
pCharacteristicMessage->setValue("Hello World");
|
|
|
|
//Time
|
|
BLECharacteristic *pCharacteristicTime = pServiceTD->createCharacteristic(
|
|
CHAR_TIME_UUID,
|
|
BLECharacteristic::PROPERTY_READ |
|
|
BLECharacteristic::PROPERTY_WRITE);
|
|
pCharacteristicTime->setCallbacks(new CharacteristicCallbacksServiceTD());
|
|
pCharacteristicTime->setValue("TIME");
|
|
|
|
BLECharacteristic *pCharacteristicDate = pServiceTD->createCharacteristic(
|
|
CHAR_DATE_UUID,
|
|
BLECharacteristic::PROPERTY_READ |
|
|
BLECharacteristic::PROPERTY_WRITE);
|
|
pCharacteristicDate->setCallbacks(new CharacteristicCallbacksServiceTD());
|
|
pCharacteristicDate->setValue("Date");
|
|
|
|
pServiceTD->start();
|
|
Serial.println(ESP.getFreeHeap());
|
|
pServiceNotif->start();
|
|
Serial.println(ESP.getFreeHeap());
|
|
|
|
BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
|
|
pAdvertising->setScanResponse(true);
|
|
pAdvertising->setMinPreferred(0x12);
|
|
BLEDevice::startAdvertising();
|
|
Serial.println("Characteristic defined! Now you can read it in your phone!");
|
|
}
|
|
|
|
void updateClock(){
|
|
unsigned long curMil = millis();
|
|
|
|
if (curMil - preMil >= interval) {
|
|
preMil = curMil;
|
|
SEC++;
|
|
timeChanged = true;
|
|
}
|
|
if (SEC >= 60) {SEC = 0; MIN++; }
|
|
if (MIN >= 60) {MIN = 0; HOUR++; timeChanged = true;}
|
|
if (HOUR >= 24) {HOUR = 0; DAY++; timeChanged = true;}
|
|
}
|
|
|
|
void drawText(int width, int hight,int size, const char* text) {
|
|
gfx->setCursor(width, hight);
|
|
gfx->setTextSize(size);
|
|
gfx->println(text);
|
|
}
|