Guest User

bt humidity

a guest
May 13th, 2024
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.89 KB | None | 0 0
  1. #include <Arduino.h>
  2.  
  3. #include <BLEDevice.h>
  4. #include <BLEUtils.h>
  5. #include <BLEServer.h>
  6. #include <Adafruit_MAX31865.h>
  7.  
  8. #include <Wire.h>
  9. #include <HIH61xx.h>
  10. #include <AsyncDelay.h>
  11.  
  12. #include "Adafruit_SGP30.h"
  13.  
  14. Adafruit_SGP30 sgp;
  15. bool sgp30_found ;
  16.  
  17. //static const uint8_t SDA = 8;
  18. //static const uint8_t SCL = 9;
  19.  
  20.  
  21. #define SERVICE_UUID "19763624-d8ea-4221-aa30-bbf6f1717140"
  22. #define CHARACTERISTIC_UUID "cb239101-6a43-42c5-8484-db880c08ebab"
  23. #define RREF 4300.0 //PT1000
  24.  
  25.  
  26. BLECharacteristic *pCharacteristic;
  27. Adafruit_MAX31865 max_31865 = Adafruit_MAX31865(10, 11, 12, 13);
  28.  
  29.  
  30.  
  31.  
  32. HIH61xx<TwoWire> hih(Wire);
  33.  
  34. AsyncDelay samplingInterval;
  35.  
  36.  
  37. uint32_t getAbsoluteHumidity(float temperature, float humidity) {
  38. // approximation formula from Sensirion SGP30 Driver Integration chapter 3.15
  39. const float absoluteHumidity = 216.7f * ((humidity / 100.0f) * 6.112f * exp((17.62f * temperature) / (243.12f + temperature)) / (273.15f + temperature)); // [g/m^3]
  40. const uint32_t absoluteHumidityScaled = static_cast<uint32_t>(1000.0f * absoluteHumidity); // [mg/m^3]
  41. return absoluteHumidityScaled;
  42. }
  43.  
  44. void powerUpErrorHandler(HIH61xx<TwoWire>& hih)
  45. {
  46. log_e("Error powering up HIH61xx device");
  47. }
  48.  
  49.  
  50. void readErrorHandler(HIH61xx<TwoWire>& hih)
  51. {
  52. log_e("Error reading from HIH61xx device");
  53. }
  54.  
  55.  
  56. //https://prod-edam.honeywell.com/content/dam/honeywell-edam/sps/siot/en-us/products/sensors/humidity-with-temperature-sensors/common/documents/sps-siot-i2c-comms-humidicon-tn-009061-2-en-ciid-142171.pdf?download=false
  57.  
  58. int temp = 0;
  59. int sgp30_humidity_update_counter = 0;
  60. void setup() {
  61. Serial.begin(115200); // Initialize serial communications with the PC
  62. while (!Serial);
  63. log_d("Total heap: %d", ESP.getHeapSize());
  64. log_d("Free heap: %d", ESP.getFreeHeap());
  65. log_d("Total PSRAM: %d", ESP.getPsramSize());
  66. log_d("Free PSRAM: %d", ESP.getFreePsram());
  67.  
  68. //log_d("SDA: %d ",SDA);
  69. //log_d("SDL: %d ",SDL);
  70.  
  71.  
  72. log_d("MOSI: %d ",MOSI);
  73. log_d("MISO: %d",MISO);
  74. log_d("SCK: %d", SCK);
  75. log_d("SS: %d ",SS);
  76.  
  77.  
  78.  
  79. // Set the handlers *before* calling initialise() in case something goes wrong
  80. Wire.begin();
  81. hih.setPowerUpErrorHandler(powerUpErrorHandler);
  82. hih.setReadErrorHandler(readErrorHandler);
  83. hih.initialise();
  84.  
  85. samplingInterval.start(1, AsyncDelay::MILLIS);
  86.  
  87. if (! sgp.begin()){
  88. Serial.println("Sensor SGP30 not found :(");
  89. sgp30_found=false;
  90. }
  91. else{
  92. sgp30_found=true;
  93. Serial.print("Found SGP30 serial #");
  94. Serial.print(sgp.serialnumber[0], HEX);
  95. Serial.print(sgp.serialnumber[1], HEX);
  96. Serial.println(sgp.serialnumber[2], HEX);
  97. }
  98.  
  99.  
  100. /*MOSI: 11
  101. [ MISO: 13
  102. [ SCK: 12
  103. [ SS: 10 */
  104. // Use software SPI: CS, DI, DO, CLK
  105. max_31865 = Adafruit_MAX31865(10, 11, 12, 13);
  106. max_31865.begin(MAX31865_2WIRE);
  107.  
  108. BLEDevice::init("ESP32_PT1000");
  109. BLEServer *pServer = BLEDevice::createServer();
  110. BLEService *pService = pServer->createService(SERVICE_UUID);
  111.  
  112.  
  113. pCharacteristic = pService->createCharacteristic(
  114. CHARACTERISTIC_UUID,
  115. BLECharacteristic::PROPERTY_READ |
  116. BLECharacteristic::PROPERTY_WRITE
  117. );
  118.  
  119. pService->start();
  120. // BLEAdvertising *pAdvertising = pServer->getAdvertising(); // this still is working for backward compatibility
  121. BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
  122. pAdvertising->addServiceUUID(SERVICE_UUID);
  123. pAdvertising->setScanResponse(true);
  124. pAdvertising->setMinPreferred(0x06); // functions that help with iPhone connections issue
  125. pAdvertising->setMinPreferred(0x12);
  126. BLEDevice::startAdvertising();
  127.  
  128.  
  129. }
  130.  
  131. uint8_t print_fault(){
  132. uint8_t fault = max_31865.readFault();
  133. if (fault) {
  134. Serial.print("Fault 0x"); Serial.println(fault, HEX);
  135. if (fault & MAX31865_FAULT_HIGHTHRESH) {
  136. Serial.println("RTD High Threshold");
  137. }
  138. if (fault & MAX31865_FAULT_LOWTHRESH) {
  139. Serial.println("RTD Low Threshold");
  140. }
  141. if (fault & MAX31865_FAULT_REFINLOW) {
  142. Serial.println("REFIN- > 0.85 x Bias");
  143. }
  144. if (fault & MAX31865_FAULT_REFINHIGH) {
  145. Serial.println("REFIN- < 0.85 x Bias - FORCE- open");
  146. }
  147. if (fault & MAX31865_FAULT_RTDINLOW) {
  148. Serial.println("RTDIN- < 0.85 x Bias - FORCE- open");
  149. }
  150. if (fault & MAX31865_FAULT_OVUV) {
  151. Serial.println("Under/Over voltage");
  152. }
  153. max_31865.clearFault();
  154. }
  155. return fault;
  156. }
  157.  
  158. bool printed = true;
  159. uint16_t rh_T = 0;
  160. uint16_t hih_Temp = 0;
  161.  
  162. void loop() {
  163.  
  164.  
  165. /*if (samplingInterval.isExpired() && !hih.isSampling()) {
  166. hih.start();
  167. printed = false;
  168. samplingInterval.repeat();
  169. Serial.println("Sampling started (using Wire library)");
  170. }*/
  171.  
  172. //hih.process();
  173.  
  174. log_d("ESP32 Probe");
  175.  
  176. //delay(25);
  177.  
  178.  
  179. uint8_t fault = print_fault();
  180. int rtd = max_31865.readRTD();
  181. Serial.print("RTD value: "); Serial.println(rtd);
  182. float ratio = rtd;
  183. ratio /= 32768;
  184. temp = int(max_31865.temperature(1000,RREF)*10); //TODO change to 1000 for pt1000
  185.  
  186. Serial.print("Ratio = "); Serial.println(ratio,8);
  187. Serial.print("Resistance = "); Serial.println(RREF*ratio,8);
  188. Serial.print("Temp_1000 = "); Serial.println(temp/10);
  189. Serial.print("Temp_100 = "); Serial.println(max_31865.temperature(100,RREF));
  190.  
  191. //if (hih.isFinished() ){//&& !printed) {
  192. hih.read();
  193. printed = true;
  194. hih_Temp = hih.getAmbientTemp();
  195. rh_T = hih.getRelHumidity();
  196.  
  197. // Print saved values
  198. Serial.print("--------------------------------------------RH: ");
  199. Serial.print(hih.getRelHumidity() / 100.0);
  200.  
  201. Serial.println(" %");
  202. Serial.print("Ambient: ");
  203. Serial.print(hih.getAmbientTemp() / 100.0);
  204.  
  205. Serial.println(" deg C");
  206. Serial.print("Status: ");
  207. Serial.println(hih.getStatus());
  208.  
  209. // hih.start();
  210. // hih.process();
  211. // }
  212.  
  213.  
  214. if (ratio==0){
  215. Serial.println ("ERROR on sensor PT1000");
  216. temp = 6666;
  217. }
  218.  
  219. if (! sgp.IAQmeasure()) {
  220. Serial.println("SGP30 Measurement failed");
  221.  
  222. }
  223. else{
  224.  
  225. Serial.print("TVOC "); Serial.print(sgp.TVOC); Serial.print(" ppb\t");
  226. Serial.print("eCO2 "); Serial.print(sgp.eCO2); Serial.print(" ppm");
  227. Serial.print(" (");Serial.print(sgp30_humidity_update_counter); Serial.println(") ");
  228.  
  229. sgp.setHumidity(getAbsoluteHumidity(hih_Temp/100.0, rh_T/100.0));
  230.  
  231. }
  232.  
  233.  
  234.  
  235. temp = temp & 0xFFFF;
  236. rh_T = rh_T & 0xFFFF;
  237. hih_Temp = hih_Temp & 0xFFFF;
  238.  
  239. byte frame_end[16]={126,highByte(temp),lowByte(temp),126,highByte(rh_T),lowByte(rh_T),126,
  240. highByte(hih_Temp),lowByte(hih_Temp),126,highByte(sgp.TVOC),lowByte(sgp.TVOC),126,
  241. highByte(sgp.eCO2),lowByte(sgp.eCO2),126};
  242.  
  243. pCharacteristic->setValue(frame_end,16);
  244.  
  245. }
  246.  
Advertisement
Add Comment
Please, Sign In to add comment