167 lines
4.6 KiB
Arduino
167 lines
4.6 KiB
Arduino
#include <Arduino_GFX_Library.h>
|
|
#include <Adafruit_ST7789.h>
|
|
#include <Arduino.h>
|
|
|
|
#include <BLEDevice.h>
|
|
#include <BLEUtils.h>
|
|
#include <BLEServer.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 MONT=0;
|
|
unsigned long YEAR=2026;
|
|
|
|
unsigned long preMil = 0;
|
|
const long interval = 1000;
|
|
|
|
Arduino_DataBus *bus = new Arduino_HWSPI(TFT_DC, -1);
|
|
Arduino_GFX *gfx = new Arduino_ST7789(bus, TFT_RST, 0, true, 240, 240, 0, 0);
|
|
|
|
#define SERVICE_UUID "791cadd7-de87-49f9-8986-94b7ae11940b"
|
|
|
|
#define CHAR_MESSAGE_UUID "3e4eac5b-eaa2-4e0c-a705-dcac4f606e01"
|
|
#define CHAR_TIME_UUID "3e4eac5b-eaa2-4e0c-a705-dcac4f606e02"
|
|
#define CHAR_DATE_UUID "3e4eac5b-eaa2-4e0c-a705-dcac4f606e03"
|
|
|
|
#define deviceName "ESP32-Watch"
|
|
|
|
class CharacteristicCallbacks: 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))) {
|
|
Serial.print("Time Recived: ");
|
|
Serial.println(value);
|
|
}
|
|
if (pCharacteristic->getUUID().equals(BLEUUID(CHAR_DATE_UUID))) {
|
|
Serial.print("Date Recived: ");
|
|
Serial.println(value);
|
|
}
|
|
if (pCharacteristic->getUUID().equals(BLEUUID(CHAR_MESSAGE_UUID))) {
|
|
Serial.print("Message Recived: ");
|
|
Serial.println(value);
|
|
}
|
|
}
|
|
};
|
|
class MyServerCallbacks: public BLEServerCallbacks {
|
|
void onConnect(BLEServer* pServer) {
|
|
Serial.println("Conecction with client");
|
|
BLEDevice::startAdvertising();
|
|
};
|
|
|
|
void onDisconnect(BLEServer* pServer) {
|
|
Serial.println("Disconnection with client");
|
|
}
|
|
};
|
|
|
|
void setup() {
|
|
Serial.begin(115200);
|
|
|
|
gfx->begin();
|
|
gfx->setFont(NULL);
|
|
gfx->fillScreen(ST77XX_BLACK);
|
|
|
|
gfx->setTextColor(ST77XX_GREEN, ST77XX_BLACK);
|
|
|
|
initDevice();
|
|
}
|
|
|
|
void loop() {
|
|
drawText(10,10, 2, "{");
|
|
drawText(30,30,2,"Time: {");
|
|
|
|
gfx->setCursor(50, 50);
|
|
gfx->printf("Hour: %d%c\n", HOUR, ',');
|
|
|
|
gfx->setCursor(50, 70);
|
|
gfx->printf("Min: %d%c\n", MIN, ',');
|
|
|
|
gfx->setCursor(50, 90);
|
|
gfx->printf("Sec: %d%c\n", SEC);
|
|
|
|
drawText(30,110,2,"},");
|
|
|
|
drawText(30,130,2,"Date: {");
|
|
|
|
gfx->setCursor(50, 150);
|
|
gfx->printf("Day: %d%c\n", DAY, ',');
|
|
|
|
gfx->setCursor(50, 170);
|
|
gfx->printf("Mont: %d%c\n", MONT, ',');
|
|
|
|
gfx->setCursor(50, 190);
|
|
gfx->printf("Year: %d%c\n", YEAR);
|
|
|
|
drawText(10,210,2,"}");
|
|
|
|
updateClock();
|
|
}
|
|
void initDevice(){
|
|
BLEDevice::init(deviceName);
|
|
BLEServer *pServer = BLEDevice::createServer();
|
|
pServer->setCallbacks(new MyServerCallbacks());
|
|
BLEService *pService = pServer->createService(SERVICE_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);
|
|
|
|
//Message
|
|
BLECharacteristic *pCharacteristicMessage = pService->createCharacteristic(
|
|
CHAR_MESSAGE_UUID,
|
|
BLECharacteristic::PROPERTY_READ |
|
|
BLECharacteristic::PROPERTY_WRITE);
|
|
pCharacteristicMessage->setCallbacks(new CharacteristicCallbacks());
|
|
pCharacteristicMessage->setValue("Hello World");
|
|
|
|
//Time
|
|
BLECharacteristic *pCharacteristicTime = pService->createCharacteristic(
|
|
CHAR_TIME_UUID,
|
|
BLECharacteristic::PROPERTY_READ |
|
|
BLECharacteristic::PROPERTY_WRITE);
|
|
pCharacteristicTime->setCallbacks(new CharacteristicCallbacks());
|
|
pCharacteristicTime->setValue("TIME");
|
|
|
|
BLECharacteristic *pCharacteristicDate = pService->createCharacteristic(
|
|
CHAR_DATE_UUID,
|
|
BLECharacteristic::PROPERTY_READ |
|
|
BLECharacteristic::PROPERTY_WRITE);
|
|
pCharacteristicDate->setCallbacks(new CharacteristicCallbacks());
|
|
pCharacteristicDate->setValue("Date");
|
|
|
|
pService->start();
|
|
// BLEAdvertising *pAdvertising = pServer->getAdvertising(); // this still is working for backward compatibility
|
|
BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
|
|
pAdvertising->addServiceUUID(SERVICE_UUID);
|
|
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++;
|
|
}
|
|
if (SEC >= 60) {SEC = 0; MIN++;}
|
|
if (MIN >= 60) {MIN = 0; HOUR++;}
|
|
if (HOUR >= 24) {HOUR = 0; DAY++;}
|
|
}
|
|
void drawText(int width, int hight,int size, const char* text) {
|
|
gfx->setCursor(width, hight);
|
|
gfx->setTextSize(size);
|
|
gfx->println(text);
|
|
}
|