Guest User

Untitled

a guest
Jan 19th, 2018
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.65 KB | None | 0 0
  1. #include <BLEUtils.h>
  2. #include <BLEScan.h>
  3. #include <BLEAdvertisedDevice.h>
  4. #include <BLEDevice.h>
  5. #include <BLEServer.h>
  6. #include <BLE2902.h>
  7. #include <DHT.h>
  8.  
  9.  
  10. #define DHTPIN 4 // what pin we're connected to
  11. #define DHTTYPE DHT11 // DHT 11
  12.  
  13. #define LEDR 23
  14. #define LEDG 18
  15. #define LEDB 19
  16.  
  17. //BLE
  18. #define SERVICE_UUID "00000001-0000-1000-8000-00805f9b34fb"
  19. #define TEMP_HUMIDITY_CHARACTERISTIC_UUID "00001001-0000-1000-8000-00805f9b34fb"
  20.  
  21.  
  22. long previousMillis = 0;
  23. long interval = 1000;
  24. bool deviceConnected = false;
  25.  
  26. DHT dht(DHTPIN, DHTTYPE);
  27. BLECharacteristic *pCharacteristic1;
  28.  
  29. int old_t = 0;
  30. int old_h = 0;
  31.  
  32.  
  33. void setLED(int f) {
  34. if (f == 0) {
  35. ledcWrite(1, 0);
  36. ledcWrite(2, 0);
  37. ledcWrite(3, 255);
  38. } else {
  39. ledcWrite(1, 0);
  40. ledcWrite(2, 255);
  41. ledcWrite(3, 0);
  42. }
  43. }
  44.  
  45.  
  46. class MyServerCallbacks: public BLEServerCallbacks
  47. {
  48. void onConnect(BLEServer* pServer) {
  49. deviceConnected = true;
  50. Serial.println("Connected.");
  51. setLED(1);
  52. };
  53.  
  54. void onDisconnect(BLEServer* pServer) {
  55. deviceConnected = false;
  56. Serial.println("Disconnected.");
  57. setLED(0);
  58. }
  59. };
  60.  
  61.  
  62. class MyCallbacks: public BLECharacteristicCallbacks
  63. {
  64. void onWrite(BLECharacteristic *pCharacteristic) {
  65. Serial.println("onWrite");
  66. std::string value = pCharacteristic->getValue();
  67.  
  68. if (value.length() > 0) {
  69. Serial.println("*********");
  70. Serial.print("New value: ");
  71. for (int i = 0; i < value.length(); i++) {
  72. Serial.print(value[i]);
  73. }
  74. Serial.println();
  75. Serial.println("*********");
  76. }
  77. }
  78.  
  79. void onRead(BLECharacteristic* pCharacteristic) {
  80. Serial.println("onRead");
  81. }
  82. };
  83.  
  84.  
  85. //----- main -----//
  86.  
  87. void setup() {
  88. Serial.begin(115200);
  89.  
  90. ledcAttachPin(LEDR, 1); // assign RGB led pins to channels
  91. ledcAttachPin(LEDG, 2);
  92. ledcAttachPin(LEDB, 3);
  93.  
  94. ledcSetup(1, 12000, 8); // 12 kHz PWM, 8-bit resolution
  95. ledcSetup(2, 12000, 8);
  96. ledcSetup(3, 12000, 8);
  97.  
  98. setLED(0);
  99.  
  100. Serial.println("DHT11 test!");
  101. dht.begin();
  102.  
  103. Serial.println("Starting BLE work!");
  104. BLEDevice::init("RL_ESP32_BLE_DHT");
  105. BLEServer *pServer = BLEDevice::createServer();
  106. pServer->setCallbacks(new MyServerCallbacks());
  107.  
  108. BLEService *pService = pServer->createService(SERVICE_UUID);
  109.  
  110.  
  111. pCharacteristic1 = pService->createCharacteristic(
  112. TEMP_HUMIDITY_CHARACTERISTIC_UUID,
  113. BLECharacteristic::PROPERTY_READ |
  114. BLECharacteristic::PROPERTY_NOTIFY |
  115. BLECharacteristic::PROPERTY_INDICATE
  116. );
  117. pCharacteristic1->setValue("-----");
  118. pCharacteristic1->addDescriptor(new BLE2902());
  119. pCharacteristic1->setCallbacks(new MyCallbacks());
  120.  
  121.  
  122. pService->start();
  123. BLEAdvertising *pAdvertising = pServer->getAdvertising();
  124. pAdvertising->start();
  125.  
  126. Serial.println("Now you can read it in your phone!");
  127. delay(1000);
  128. }
  129.  
  130. void loop() {
  131. if (millis() - previousMillis > interval) {
  132. readTemperatureAndHumidity();
  133. previousMillis = millis();
  134. }
  135. }
  136.  
  137. void readTemperatureAndHumidity() {
  138. // Reading temperature or humidity takes about 250 milliseconds!
  139. // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  140. float h = dht.readHumidity();
  141. // Read temperature as Celsius (the default)
  142. float t = dht.readTemperature();
  143. // Read temperature as Fahrenheit (isFahrenheit = true)
  144. float f = dht.readTemperature(true);
  145.  
  146. // Check if any reads failed and exit early (to try again).
  147. if (isnan(h) || isnan(t) || isnan(f)) {
  148. Serial.println("Failed to read from DHT sensor!");
  149. return;
  150. }
  151.  
  152. // Compute heat index in Fahrenheit (the default)
  153. float hif = dht.computeHeatIndex(f, h);
  154. // Compute heat index in Celsius (isFahreheit = false)
  155. float hic = dht.computeHeatIndex(t, h, false);
  156.  
  157.  
  158. if (int(t) != old_t || int(h) != old_h) {
  159. Serial.print("Humidity: ");
  160. Serial.print(h);
  161. Serial.print(" %\t");
  162. Serial.print("Temperature: ");
  163. Serial.print(t);
  164. Serial.print(" *C ");
  165. Serial.print(f);
  166. Serial.print(" *F\t");
  167. Serial.print("Heat index: ");
  168. Serial.print(hic);
  169. Serial.print(" *C ");
  170. Serial.print(hif);
  171. Serial.println(" *F");
  172.  
  173. old_t = int(t);
  174. old_h = int(h);
  175. String notify_msg = String(int(t)) + "," + String(int(h));
  176. pCharacteristic1->setValue(notify_msg.c_str());
  177. pCharacteristic1->notify();
  178. }
  179. }
Add Comment
Please, Sign In to add comment