Rewriting code for universel 3 variable bluetooth connection
Posted: Sun Oct 13, 2019 8:09 pm
Dear forum
i am quite new in ESP32 world, have bought a "Learn ESP 32 book" from Random nerd tutorials. but i need a more general purpose
Bluetooth than the included BLE -> DHT -> OLED tutorial.
So far i have rewritten the the code, to get rid of the DHT## in the Program. it worked down to the OLD line
Which i have changed to
Of cause it did not work, while dhtService are defined else where, and properly are a complex in it self
but what do i have to do to get the program working?
i have printed all libraries, but have a lot of troubles in reading them
full program attached, i have divided the program in tree parts it is the middle part which is of interest
*************************************************************************************************
Code i think is in trouble
******************************************************************************************************
i am quite new in ESP32 world, have bought a "Learn ESP 32 book" from Random nerd tutorials. but i need a more general purpose
Bluetooth than the included BLE -> DHT -> OLED tutorial.
So far i have rewritten the the code, to get rid of the DHT## in the Program. it worked down to the OLD line
Code: Select all
BLEService *dhtService = pServer->createService(SERVICE_UUID);
Code: Select all
BLEService *bilservice = pServer->createService(SERVICE_UUID);
but what do i have to do to get the program working?
i have printed all libraries, but have a lot of troubles in reading them
full program attached, i have divided the program in tree parts it is the middle part which is of interest
Code: Select all
/*********
Rui Santos
Complete project details at http://randomnerdtutorials.com
Alle variable i forhold til de tre bluetooth værdier tempC tempF og Humidity er ændret
*********/
#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
#include <BLE2902.h>
#include "DHT.h"
/*
//Default Temperature is in Celsius
//Comment the next line for Temperature in Fahrenheit
#define temperatureCelsius
*/
//BLE server name
#define bleServerName "dhtESP32"
/*
// Uncomment one of the lines below for whatever DHT sensor type you're using!
#define DHTTYPE DHT11 // DHT 11
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
//#define DHTTYPE DHT22 // DHT 22 (AM230
2), AM2321
*/
// See the following for generating UUIDs:
// https://www.uuidgenerator.net/
#define SERVICE_UUID "91bad492-b950-4226-aa2b-4ede9fa42f59"
#ifdef temperatureCelsius
BLECharacteristic Avar("cba1d466-344c-4be3-ab3f-189f80dd7518", BLECharacteristic::PROPERTY_NOTIFY);
BLEDescriptor LeftRight(BLEUUID((uint16_t)0x2902));
#else
BLECharacteristic Bvar("f78ebbff-c8b7-4107-93de-889a6a06d408", BLECharacteristic::PROPERTY_NOTIFY);
BLEDescriptor Hastighed(BLEUUID((uint16_t)0x2901));
#endif
BLECharacteristic Flag("ca73b3ba-39f6-4ab3-91ae-186dc9577d99", BLECharacteristic::PROPERTY_NOTIFY);
BLEDescriptor FlagReg(BLEUUID((uint16_t)0x2903));
/*
// DHT Sensor
const int DHTPin = 14;
// Initialize DHT sensor.
DHT dht(DHTPin, DHTTYPE);
*/
bool deviceConnected = false;
BLEService *bilservice;
//Setup callbacks onConnect and onDisconnect
class MyServerCallbacks: public BLEServerCallbacks {
void onConnect(BLEServer* pServer) {
deviceConnected = true;
};
void onDisconnect(BLEServer* pServer) {
deviceConnected = false;
}
};
void setup() {
/*
// Start DHT sensor
dht.begin();
*/
// Start serial communication
Serial.begin(115200);
Code i think is in trouble
Code: Select all
// Create the BLE Device
BLEDevice::init(bleServerName);
// Create the BLE Server
BLEServer *pServer = BLEDevice::createServer();
pServer->setCallbacks(new MyServerCallbacks());
// Create the BLE Service
/*
BLEService *dhtService = pServer->createService(SERVICE_UUID);
*/
BLEService *bilservice = pServer->createService(SERVICE_UUID);
// Create BLE Characteristics and Create a BLE Descriptor
// Descriptor bluetooth.com/specifications/gatt/viewer?
// attributeXmlFile=org.bluetooth.descriptor.gatt.client_characteristic_configuration.xml
#ifdef temperatureCelsius
bilService->addCharacteristic(&Avar);
LeftRight.setValue("Højre/Venstre");
Avar.addDescriptor(new BLE2902());
#else
bilService->addCharacteristic(&Bvar);
Hastighed.setValue("Hastighed");
Bvar.addDescriptor(new BLE2902());
#endif
bilService->addCharacteristic(&Flag);
FlagReg.setValue("Flag indhold");
Flag.addDescriptor(new BLE2902());
// Start the service
/*
* dhtService->start();
*/
bilService->start();
Code: Select all
// Start advertising
pServer->getAdvertising()->start();
Serial.println("Waiting a client connection to notify...");
}
void loop() {
if (deviceConnected) {
// Read temperature as Celsius (the default)
float t = analogRead(32);
// Read temperature as Fahrenheit (isFahrenheit = true)
float f = analogRead(33);
// Read humidity
float h = analogRead(34);
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
//Notify temperature reading from DHT sensor
#ifdef temperatureCelsius
static char temperatureCTemp[7];
dtostrf(t, 6, 2, temperatureCTemp);
//Set temperature Characteristic value and notify connected client
Avar.setValue(temperatureCTemp);
Avar.notify();
Serial.print("Temperature Celsius: ");
Serial.print(t);
Serial.print(" *C");
#else
static char temperatureFTemp[7];
dtostrf(f, 6, 2, temperatureFTemp);
//Set temperature Characteristic value and notify connected client
Bvar.setValue(temperatureFTemp);
Bvar.notify();
Serial.print("Temperature Fahrenheit: ");
Serial.print(f);
Serial.print(" *F");
#endif
//Notify humidity reading from DHT
static char humidityTemp[7];
dtostrf(h, 6, 2, humidityTemp);
//Set humidity Characteristic value and notify connected client
Flag.setValue(humidityTemp);
Flag.notify();
Serial.print(" - Humidity: ");
Serial.print(h);
Serial.println(" %");
delay(10000);
}
}