פיתוח ESP32 – שימוש ב BLE
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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
#include <BLEDevice.h> #include <BLEServer.h> #include <BLEUtils.h> #include <BLE2902.h> #define potPin 36 //connect the potentiometer to gpio36 bool oldDeviceConnected = false; int sensorRead = 0; uint8_t value = 0; //the set value function only accepts unsigned 8 bit integers /* Define the UUID for our Custom Service */ #define serviceID BLEUUID((uint16_t)0x1700) /* Define our custom characteristic along with it's properties */ BLECharacteristic customCharacteristic( BLEUUID((uint16_t)0x1A00), BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_NOTIFY ); /* This function handles the server callbacks */ bool deviceConnected = false; class ServerCallbacks: public BLEServerCallbacks { void onConnect(BLEServer* MyServer) { deviceConnected = true; }; void onDisconnect(BLEServer* MyServer) { deviceConnected = false; } }; BLEServer *MyServer; BLEService *customService; uint8_t buf[10]; void setup() { buf[0]='V'; buf[1]='a'; buf[2]='l'; buf[3]=':'; buf[4]=' '; buf[5]=0; buf[6]=0; Serial.begin(115200); // Create and name the BLE Device BLEDevice::init("MyESP32"); /* Create the BLE Server */ MyServer = BLEDevice::createServer(); MyServer->setCallbacks(new ServerCallbacks()); // Set the function that handles Server Callbacks /* Add a service to our server */ customService = MyServer->createService(BLEUUID((uint16_t)0x1700)); // A random ID has been selected /* Add a characteristic to the service */ customService->addCharacteristic(&customCharacteristic); //customCharacteristic was defined above /* Add Descriptors to the Characteristic*/ customCharacteristic.addDescriptor(new BLE2902()); //Add this line only if the characteristic has the Notify property BLEDescriptor VariableDescriptor(BLEUUID((uint16_t)0x2901)); /*```````````````````````````````````````````````````````````````*/ VariableDescriptor.setValue("Temperature -40-60°C"); /* Use this format to add a hint for the user. This is optional. */ customCharacteristic.addDescriptor(&VariableDescriptor); /*```````````````````````````````````````````````````````````````*/ /* Configure Advertising with the Services to be advertised */ MyServer->getAdvertising()->addServiceUUID(serviceID); // Start the service customService->start(); // Start the Server/Advertising MyServer->getAdvertising()->start(); Serial.println("Waiting for a Client to connect..."); sensorRead = analogRead(potPin); //read the value of the potentiometer value = map(sensorRead,0,4095,0,255); //change the value to a range of 0-255 so that it can fit in a single byte value =random(0, 255); Serial.println(value); if (deviceConnected) { /* Set the value */ customCharacteristic.setValue(&value,6); // This is a value of a single byte customCharacteristic.notify(); // Notify the client of a change } else { // Start the Server/Advertising MyServer->getAdvertising()->start(); delay(50); } } int i=48; void loop() { sensorRead = analogRead(potPin); //read the value of the potentiometer value = map(sensorRead,0,4095,0,255); //change the value to a range of 0-255 so that it can fit in a single byte value =random(0, 255); buf[5]=value; if (deviceConnected) { /* Set the value */ customCharacteristic.setValue(&buf[0],6); // This is a value of a single byte customCharacteristic.notify(); // Notify the client of a change Serial.println(value); } // disconnecting if (!deviceConnected && oldDeviceConnected) { delay(500); // give the bluetooth stack the chance to get things ready MyServer->startAdvertising(); // restart advertising Serial.println("start advertising"); oldDeviceConnected = deviceConnected; } if (deviceConnected && !oldDeviceConnected) { // do stuff here on connecting customCharacteristic.setValue(&value,1); // This is a value of a single byte customCharacteristic.notify(); // Notify the client of a change i++; if (i>=57)i=48; Serial.println("on connecting"); delay(25); oldDeviceConnected = deviceConnected; } delay(50); } |
פיתוח ESP32 – שימוש ב Eddystone-TLM
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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
/* Create a BLE server that will send periodic eddystone frames. The design of creating the BLE server is: 1. Create a BLE Server 2. Create advertising data 3. Start advertising. 4. wait 5. Stop advertising. 6. deep sleep */ #include "sys/time.h" #include "BLEDevice.h" #include "BLEServer.h" #include "BLEUtils.h" #include "esp_sleep.h" #define GPIO_DEEP_SLEEP_DURATION 2 // sleep 2 seconds and then wake up RTC_DATA_ATTR static time_t last; // remember last boot in RTC Memory RTC_DATA_ATTR static uint32_t bootcount; // remember number of boots in RTC Memory #ifdef __cplusplus extern "C" { #endif uint8_t temprature_sens_read(); //uint8_t g_phyFuns; #ifdef __cplusplus } #endif // See the following for generating UUIDs: // https://www.uuidgenerator.net/ BLEAdvertising *pAdvertising; struct timeval now; void setBeacon() { char beacon_data[22]; uint16_t beconUUID = 0xFEAA; uint16_t volt = 3300; // 3300mV = 3.3V uint16_t temp = (uint16_t)((float)23.00); uint32_t tmil = now.tv_sec*10; uint8_t temp_farenheit; float temp_celsius; temp_farenheit= temprature_sens_read(); temp_celsius = ( temp_farenheit - 32 ) / 1.8; temp = (uint16_t)(temp_celsius); BLEAdvertisementData oAdvertisementData = BLEAdvertisementData(); oAdvertisementData.setFlags(0x06); // GENERAL_DISC_MODE 0x02 | BR_EDR_NOT_SUPPORTED 0x04 oAdvertisementData.setCompleteServices(BLEUUID(beconUUID)); beacon_data[0] = 0x20; // Eddystone Frame Type (Unencrypted Eddystone-TLM) beacon_data[1] = 0x00; // TLM version beacon_data[2] = (volt>>8); // Battery voltage, 1 mV/bit i.e. 0xCE4 = 3300mV = 3.3V beacon_data[3] = (volt&0xFF); // beacon_data[4] = (temp&0xFF); // Beacon temperature beacon_data[5] = (temp>>8); // beacon_data[6] = ((bootcount & 0xFF000000) >> 24); // Advertising PDU count beacon_data[7] = ((bootcount & 0xFF0000) >> 16); // beacon_data[8] = ((bootcount & 0xFF00) >> 8); // beacon_data[9] = (bootcount&0xFF); // beacon_data[10] = ((tmil & 0xFF000000) >> 24); // Time since power-on or reboot beacon_data[11] = ((tmil & 0xFF0000) >> 16); // beacon_data[12] = ((tmil & 0xFF00) >> 8); // beacon_data[13] = (tmil&0xFF); // oAdvertisementData.setServiceData(BLEUUID(beconUUID), std::string(beacon_data, 14)); pAdvertising->setScanResponseData(oAdvertisementData); } void setup() { Serial.begin(115200); gettimeofday(&now, NULL); Serial.printf("start ESP32 %d\n",bootcount++); Serial.printf("deep sleep (%lds since last reset, %lds since last boot)\n",now.tv_sec,now.tv_sec-last); last = now.tv_sec; // Create the BLE Device BLEDevice::init("ESP32"); // Create the BLE Server BLEServer *pServer = BLEDevice::createServer(); pAdvertising = pServer->getAdvertising(); setBeacon(); // Start advertising pAdvertising->start(); Serial.println("Advertizing started..."); delay(100); pAdvertising->stop(); Serial.printf("enter deep sleep\n"); esp_deep_sleep(1000000LL * GPIO_DEEP_SLEEP_DURATION); Serial.printf("in deep sleep\n"); } void loop() { } |