Back to Parent

central BLE - tft screen & alarm clock
/*
 * Haunted [Smart] House - RME Spring 2023 Final Project
 * central BLE
 * control tft screen and alarm clock
 */
#include <ArduinoBLE.h>
#include <Adafruit_NeoPixel.h>
#include <Adafruit_GFX.h>     // Core graphics library
#include <Adafruit_ST7789.h>  // Hardware-specific library for ST7789
#include <SPI.h>

#define TFT_CS D10
#define TFT_RST -1  // Or set to -1 and connect to Arduino RESET pin
#define TFT_DC D8

const char* deviceServiceUuid = "19b10000-e8f2-537e-4f6c-d104768a1214";
const char* deviceServiceCharacteristicUuid = "19b10001-e8f2-537e-4f6c-d104768a1214";

#define TFT_COPI D11  // Data out
#define TFT_SCLK D13  // Clock out

Adafruit_ST7789 tft = Adafruit_ST7789(TFT_CS, TFT_DC, TFT_COPI, TFT_SCLK, TFT_RST);

int proximity = -1;
int shock = -1;
int oldShockValue = -1;

void setup() {
  // for screen
  Serial.begin(9600);

  delay(5000);
  Serial.print(F("Hello! ST77xx TFT Test"));

  tft.init(172, 320);  // Init ST7789 172x320
  tft.setSPISpeed(32000000);
  Serial.println(F("Initialized"));

  uint16_t time = millis();
  tft.fillScreen(ST77XX_BLACK);
  time = millis() - time;

  Serial.println(time, DEC);
  delay(500);

  Serial.begin(9600);
  while (!Serial)
    ;

  if (!BLE.begin()) {
    Serial.println("* Starting Bluetooth® Low Energy module failed!");
    while (1)
      ;
  }

  BLE.setLocalName("Nano 33 BLE (Central)");
  BLE.advertise();

  Serial.println("Arduino Nano 33 BLE Sense (Central Device)");
  Serial.println(" ");
}

void loop() {
  connectToPeripheral();
}


void connectToPeripheral() {
  BLEDevice peripheral;

  Serial.println("- Discovering peripheral device...");

  do {
    BLE.scanForUuid(deviceServiceUuid);
    peripheral = BLE.available();
  } while (!peripheral);

  if (peripheral) {
    Serial.println("* Peripheral device found!");
    Serial.print("* Device MAC address: ");
    Serial.println(peripheral.address());
    Serial.print("* Device name: ");
    Serial.println(peripheral.localName());
    Serial.print("* Advertised service UUID: ");
    Serial.println(peripheral.advertisedServiceUuid());
    Serial.println(" ");
    BLE.stopScan();
    controlPeripheral(peripheral);
  }
}

void controlPeripheral(BLEDevice peripheral) {
  Serial.println("- Connecting to peripheral device...");

  if (peripheral.connect()) {
    Serial.println("* Connected to peripheral device!");
    Serial.println(" ");
  } else {
    Serial.println("* Connection to peripheral device failed!");
    Serial.println(" ");
    return;
  }

  Serial.println("- Discovering peripheral device attributes...");
  if (peripheral.discoverAttributes()) {
    Serial.println("* Peripheral device attributes discovered!");
    Serial.println(" ");
  } else {
    Serial.println("* Peripheral device attributes discovery failed!");
    Serial.println(" ");
    peripheral.disconnect();
    return;
  }

  BLECharacteristic shockCharacteristic = peripheral.characteristic(deviceServiceCharacteristicUuid);

  if (!shockCharacteristic) {
    Serial.println("* Peripheral device does not have shock_type characteristic!");
    peripheral.disconnect();
    return;
  } else if (!shockCharacteristic.canWrite()) {
    Serial.println("* Peripheral does not have a writable shock_type characteristic!");
    peripheral.disconnect();
    return;
  }

  while (peripheral.connected()) {
    screen_text();

    // check if a proximity reading is available
    shock = 1;

    if (oldShockValue != shock) {
      oldShockValue = shock;
      Serial.print("* Writing value to shock_type characteristic: ");
      Serial.println(shock);
      shockCharacteristic.writeValue((byte)shock);
      Serial.println("* Writing value to shock_type characteristic done!");
      Serial.println(" ");
    }
  }
  Serial.println("- Peripheral device disconnected!");
}

void screen_text() {
  drawtext1("Lamp under control...", ST77XX_WHITE);
  delay(5000);
  drawtext2("Alarm clock under control...", ST77XX_WHITE);
  delay(5000);
  tft.fillScreen(ST77XX_BLACK);
}

void drawtext1(char* text, uint16_t color) {
  tft.setCursor(0, 20);
  tft.setTextColor(color);
  tft.setTextSize(3);
  tft.setTextWrap(true);
  tft.setRotation(3);
  tft.print(text);
}

void drawtext2(char* text, uint16_t color) {
  tft.setCursor(0, 75);
  tft.setTextColor(color);
  tft.setTextSize(3);
  tft.setTextWrap(true);
  tft.setRotation(3);
  tft.print(text);
}
Click to Expand

Content Rating

Is this a good/useful/informative piece of content to include in the project? Have your say!

0