Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <BLEUtils.h>
- #include <BLEScan.h>
- #include <BLEAdvertisedDevice.h>
- #include <BLEDevice.h>
- #include <BLEServer.h>
- #include <BLE2902.h>
- #include <DHT.h>
- #define DHTPIN 4 // what pin we're connected to
- #define DHTTYPE DHT11 // DHT 11
- #define LEDR 23
- #define LEDG 18
- #define LEDB 19
- //BLE
- #define SERVICE_UUID "00000001-0000-1000-8000-00805f9b34fb"
- #define TEMP_HUMIDITY_CHARACTERISTIC_UUID "00001001-0000-1000-8000-00805f9b34fb"
- long previousMillis = 0;
- long interval = 1000;
- bool deviceConnected = false;
- DHT dht(DHTPIN, DHTTYPE);
- BLECharacteristic *pCharacteristic1;
- int old_t = 0;
- int old_h = 0;
- void setLED(int f) {
- if (f == 0) {
- ledcWrite(1, 0);
- ledcWrite(2, 0);
- ledcWrite(3, 255);
- } else {
- ledcWrite(1, 0);
- ledcWrite(2, 255);
- ledcWrite(3, 0);
- }
- }
- class MyServerCallbacks: public BLEServerCallbacks
- {
- void onConnect(BLEServer* pServer) {
- deviceConnected = true;
- Serial.println("Connected.");
- setLED(1);
- };
- void onDisconnect(BLEServer* pServer) {
- deviceConnected = false;
- Serial.println("Disconnected.");
- setLED(0);
- }
- };
- class MyCallbacks: public BLECharacteristicCallbacks
- {
- void onWrite(BLECharacteristic *pCharacteristic) {
- Serial.println("onWrite");
- std::string value = pCharacteristic->getValue();
- if (value.length() > 0) {
- Serial.println("*********");
- Serial.print("New value: ");
- for (int i = 0; i < value.length(); i++) {
- Serial.print(value[i]);
- }
- Serial.println();
- Serial.println("*********");
- }
- }
- void onRead(BLECharacteristic* pCharacteristic) {
- Serial.println("onRead");
- }
- };
- //----- main -----//
- void setup() {
- Serial.begin(115200);
- ledcAttachPin(LEDR, 1); // assign RGB led pins to channels
- ledcAttachPin(LEDG, 2);
- ledcAttachPin(LEDB, 3);
- ledcSetup(1, 12000, 8); // 12 kHz PWM, 8-bit resolution
- ledcSetup(2, 12000, 8);
- ledcSetup(3, 12000, 8);
- setLED(0);
- Serial.println("DHT11 test!");
- dht.begin();
- Serial.println("Starting BLE work!");
- BLEDevice::init("RL_ESP32_BLE_DHT");
- BLEServer *pServer = BLEDevice::createServer();
- pServer->setCallbacks(new MyServerCallbacks());
- BLEService *pService = pServer->createService(SERVICE_UUID);
- pCharacteristic1 = pService->createCharacteristic(
- TEMP_HUMIDITY_CHARACTERISTIC_UUID,
- BLECharacteristic::PROPERTY_READ |
- BLECharacteristic::PROPERTY_NOTIFY |
- BLECharacteristic::PROPERTY_INDICATE
- );
- pCharacteristic1->setValue("-----");
- pCharacteristic1->addDescriptor(new BLE2902());
- pCharacteristic1->setCallbacks(new MyCallbacks());
- pService->start();
- BLEAdvertising *pAdvertising = pServer->getAdvertising();
- pAdvertising->start();
- Serial.println("Now you can read it in your phone!");
- delay(1000);
- }
- void loop() {
- if (millis() - previousMillis > interval) {
- readTemperatureAndHumidity();
- previousMillis = millis();
- }
- }
- void readTemperatureAndHumidity() {
- // Reading temperature or humidity takes about 250 milliseconds!
- // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
- float h = dht.readHumidity();
- // Read temperature as Celsius (the default)
- float t = dht.readTemperature();
- // Read temperature as Fahrenheit (isFahrenheit = true)
- float f = dht.readTemperature(true);
- // 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;
- }
- // Compute heat index in Fahrenheit (the default)
- float hif = dht.computeHeatIndex(f, h);
- // Compute heat index in Celsius (isFahreheit = false)
- float hic = dht.computeHeatIndex(t, h, false);
- if (int(t) != old_t || int(h) != old_h) {
- Serial.print("Humidity: ");
- Serial.print(h);
- Serial.print(" %\t");
- Serial.print("Temperature: ");
- Serial.print(t);
- Serial.print(" *C ");
- Serial.print(f);
- Serial.print(" *F\t");
- Serial.print("Heat index: ");
- Serial.print(hic);
- Serial.print(" *C ");
- Serial.print(hif);
- Serial.println(" *F");
- old_t = int(t);
- old_h = int(h);
- String notify_msg = String(int(t)) + "," + String(int(h));
- pCharacteristic1->setValue(notify_msg.c_str());
- pCharacteristic1->notify();
- }
- }
Add Comment
Please, Sign In to add comment