티스토리 뷰

Also advertising!

 

 

 

#include <WiFiNINA.h>
#include <time.h>
#include <WiFiUdp.h>
#include <NTPClient.h>
#include <ArduinoBLE.h>
#include "BConstants.h"

#define PIN_Tx0 0
#define PIN_Rx0 1
#define PIN_UARTCONTROL 2
#define PIN_LED 2
#define PIN_IRRx0 3
#define PIN_IRTx0 4
#define PIN_SWITCHIN 5
#define PIN_RELAYCONTROL 6
#define PIN_DRIVER1 7
#define PIN_DRIVER2 8
#define PIN_DRIVER3 9
#define PIN_DRIVER5 10
#define PIN_Rx1 11
#define PIN_Tx1 12

#define PIN_ADC0 A0
#define PIN_ADC1 A1
#define PIN_ADC2 A2

#define NTP_PACKET_SIZE 48

const char WiFi_SSID[] = SECRET_SSID;
const char WiFi_PW[] = SECRET_WIFIPW;
const int TimeZone = 3;

int status = WL_IDLE_STATUS;

byte NTPPacketBuffer[NTP_PACKET_SIZE];

int Count1 = 0;
WiFiClient client;

int ServerStatus = WL_IDLE_STATUS;
WiFiServer server(80);


const char* deviceServiceUuid = "19B10000-E8F2-537E-4F6C-D104768A1214";
const char* deviceServiceCharacteristicUuid = "19B10001-E8F2-537E-4F6C-D104768A1214";

int ValueNow = -1;
int PreValue = -1;

void setup(){
  Serial.begin(9600);
  while(!Serial);
  
  pinMode(PIN_LED, OUTPUT);
  
  if(!BLE.begin()){
    Serial.print("[?] BLE failed");
    while(1);
  }
  
  BLE.setLocalName("B-Nano33 (Central)"); 
  BLE.advertise();
  
  Serial.println("Started BLE LED Peripheral");
}

void loop(){
  connectToPeripheral();
}

void connectToPeripheral(){
  BLEDevice peripheral;
  
  Serial.print(" Discovering peripheral devices ...");
  
  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.print("[!] Connected to peripheral device");
    Serial.println(" ");
  }else{
    Serial.println("[?] Connection failed to peripheral device");
    Serial.println(" ");
    return;
  }
  
  Serial.println("Discovering periphraal device attributes ...");
  if(peripheral.discoverAttributes()){
    Serial.println("[!] Discoverd peripheral device attributers");
  }else{
    Serial.println("[?] Discovery failed in peripheral device attributes");
    peripheral.disconnect();
    return;
  }
  
  BLECharacteristic anCharacterestic = peripheral.characteristic(deviceServiceCharacteristicUuid);
  if(!anCharacterestic){
    Serial.println("Peripheral device doesn't have the characteristic");
    peripheral.disconnect();
    return;
  }else if(!anCharacterestic.canWrite()){
    Serial.println("Peripheral device doesn't have a writable the characteristic");
    peripheral.disconnect();
    return;
  }
  
  while(peripheral.connected()){
    ValueNow = DetectValue();
    if(ValueNow != PreValue){
      PreValue = ValueNow;
      Serial.print("Writing theValue to peripheral : ");
      Serial.println(ValueNow);
      anCharacterestic.writeValue((byte)ValueNow);
    }
  }
  
  Serial.println("Disconnected peripheral device");
}

int DetectValue(){
  int aValue = random(100, 1000);
  return aValue;
}

 

반응형

'SWDesk > Firmware' 카테고리의 다른 글

[Nano 33 IoT] WiFi Client Example(1)  (0) 2021.11.12
[Nano 33 IoT] BLE Peripheral Example(2) - ScanResp  (0) 2021.11.11
[Nano 33 IoT] BLE Peripheral Example(1)  (0) 2021.11.07
[Nano 33 IoT] Configuration  (0) 2021.11.06
[Arduino] NULL Test  (0) 2021.11.02