Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Arduino.h>
- #include <BLEDevice.h>
- #include <BLEUtils.h>
- #include <BLEServer.h>
- #include <Adafruit_MAX31865.h>
- #include <Wire.h>
- #include <HIH61xx.h>
- #include <AsyncDelay.h>
- #include "Adafruit_SGP30.h"
- Adafruit_SGP30 sgp;
- bool sgp30_found ;
- //static const uint8_t SDA = 8;
- //static const uint8_t SCL = 9;
- #define SERVICE_UUID "19763624-d8ea-4221-aa30-bbf6f1717140"
- #define CHARACTERISTIC_UUID "cb239101-6a43-42c5-8484-db880c08ebab"
- #define RREF 4300.0 //PT1000
- BLECharacteristic *pCharacteristic;
- Adafruit_MAX31865 max_31865 = Adafruit_MAX31865(10, 11, 12, 13);
- HIH61xx<TwoWire> hih(Wire);
- AsyncDelay samplingInterval;
- uint32_t getAbsoluteHumidity(float temperature, float humidity) {
- // approximation formula from Sensirion SGP30 Driver Integration chapter 3.15
- const float absoluteHumidity = 216.7f * ((humidity / 100.0f) * 6.112f * exp((17.62f * temperature) / (243.12f + temperature)) / (273.15f + temperature)); // [g/m^3]
- const uint32_t absoluteHumidityScaled = static_cast<uint32_t>(1000.0f * absoluteHumidity); // [mg/m^3]
- return absoluteHumidityScaled;
- }
- void powerUpErrorHandler(HIH61xx<TwoWire>& hih)
- {
- log_e("Error powering up HIH61xx device");
- }
- void readErrorHandler(HIH61xx<TwoWire>& hih)
- {
- log_e("Error reading from HIH61xx device");
- }
- //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
- int temp = 0;
- int sgp30_humidity_update_counter = 0;
- void setup() {
- Serial.begin(115200); // Initialize serial communications with the PC
- while (!Serial);
- log_d("Total heap: %d", ESP.getHeapSize());
- log_d("Free heap: %d", ESP.getFreeHeap());
- log_d("Total PSRAM: %d", ESP.getPsramSize());
- log_d("Free PSRAM: %d", ESP.getFreePsram());
- //log_d("SDA: %d ",SDA);
- //log_d("SDL: %d ",SDL);
- log_d("MOSI: %d ",MOSI);
- log_d("MISO: %d",MISO);
- log_d("SCK: %d", SCK);
- log_d("SS: %d ",SS);
- // Set the handlers *before* calling initialise() in case something goes wrong
- Wire.begin();
- hih.setPowerUpErrorHandler(powerUpErrorHandler);
- hih.setReadErrorHandler(readErrorHandler);
- hih.initialise();
- samplingInterval.start(1, AsyncDelay::MILLIS);
- if (! sgp.begin()){
- Serial.println("Sensor SGP30 not found :(");
- sgp30_found=false;
- }
- else{
- sgp30_found=true;
- Serial.print("Found SGP30 serial #");
- Serial.print(sgp.serialnumber[0], HEX);
- Serial.print(sgp.serialnumber[1], HEX);
- Serial.println(sgp.serialnumber[2], HEX);
- }
- /*MOSI: 11
- [ MISO: 13
- [ SCK: 12
- [ SS: 10 */
- // Use software SPI: CS, DI, DO, CLK
- max_31865 = Adafruit_MAX31865(10, 11, 12, 13);
- max_31865.begin(MAX31865_2WIRE);
- BLEDevice::init("ESP32_PT1000");
- BLEServer *pServer = BLEDevice::createServer();
- BLEService *pService = pServer->createService(SERVICE_UUID);
- pCharacteristic = pService->createCharacteristic(
- CHARACTERISTIC_UUID,
- BLECharacteristic::PROPERTY_READ |
- BLECharacteristic::PROPERTY_WRITE
- );
- pService->start();
- // BLEAdvertising *pAdvertising = pServer->getAdvertising(); // this still is working for backward compatibility
- BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
- pAdvertising->addServiceUUID(SERVICE_UUID);
- pAdvertising->setScanResponse(true);
- pAdvertising->setMinPreferred(0x06); // functions that help with iPhone connections issue
- pAdvertising->setMinPreferred(0x12);
- BLEDevice::startAdvertising();
- }
- uint8_t print_fault(){
- uint8_t fault = max_31865.readFault();
- if (fault) {
- Serial.print("Fault 0x"); Serial.println(fault, HEX);
- if (fault & MAX31865_FAULT_HIGHTHRESH) {
- Serial.println("RTD High Threshold");
- }
- if (fault & MAX31865_FAULT_LOWTHRESH) {
- Serial.println("RTD Low Threshold");
- }
- if (fault & MAX31865_FAULT_REFINLOW) {
- Serial.println("REFIN- > 0.85 x Bias");
- }
- if (fault & MAX31865_FAULT_REFINHIGH) {
- Serial.println("REFIN- < 0.85 x Bias - FORCE- open");
- }
- if (fault & MAX31865_FAULT_RTDINLOW) {
- Serial.println("RTDIN- < 0.85 x Bias - FORCE- open");
- }
- if (fault & MAX31865_FAULT_OVUV) {
- Serial.println("Under/Over voltage");
- }
- max_31865.clearFault();
- }
- return fault;
- }
- bool printed = true;
- uint16_t rh_T = 0;
- uint16_t hih_Temp = 0;
- void loop() {
- /*if (samplingInterval.isExpired() && !hih.isSampling()) {
- hih.start();
- printed = false;
- samplingInterval.repeat();
- Serial.println("Sampling started (using Wire library)");
- }*/
- //hih.process();
- log_d("ESP32 Probe");
- //delay(25);
- uint8_t fault = print_fault();
- int rtd = max_31865.readRTD();
- Serial.print("RTD value: "); Serial.println(rtd);
- float ratio = rtd;
- ratio /= 32768;
- temp = int(max_31865.temperature(1000,RREF)*10); //TODO change to 1000 for pt1000
- Serial.print("Ratio = "); Serial.println(ratio,8);
- Serial.print("Resistance = "); Serial.println(RREF*ratio,8);
- Serial.print("Temp_1000 = "); Serial.println(temp/10);
- Serial.print("Temp_100 = "); Serial.println(max_31865.temperature(100,RREF));
- //if (hih.isFinished() ){//&& !printed) {
- hih.read();
- printed = true;
- hih_Temp = hih.getAmbientTemp();
- rh_T = hih.getRelHumidity();
- // Print saved values
- Serial.print("--------------------------------------------RH: ");
- Serial.print(hih.getRelHumidity() / 100.0);
- Serial.println(" %");
- Serial.print("Ambient: ");
- Serial.print(hih.getAmbientTemp() / 100.0);
- Serial.println(" deg C");
- Serial.print("Status: ");
- Serial.println(hih.getStatus());
- // hih.start();
- // hih.process();
- // }
- if (ratio==0){
- Serial.println ("ERROR on sensor PT1000");
- temp = 6666;
- }
- if (! sgp.IAQmeasure()) {
- Serial.println("SGP30 Measurement failed");
- }
- else{
- Serial.print("TVOC "); Serial.print(sgp.TVOC); Serial.print(" ppb\t");
- Serial.print("eCO2 "); Serial.print(sgp.eCO2); Serial.print(" ppm");
- Serial.print(" (");Serial.print(sgp30_humidity_update_counter); Serial.println(") ");
- sgp.setHumidity(getAbsoluteHumidity(hih_Temp/100.0, rh_T/100.0));
- }
- temp = temp & 0xFFFF;
- rh_T = rh_T & 0xFFFF;
- hih_Temp = hih_Temp & 0xFFFF;
- byte frame_end[16]={126,highByte(temp),lowByte(temp),126,highByte(rh_T),lowByte(rh_T),126,
- highByte(hih_Temp),lowByte(hih_Temp),126,highByte(sgp.TVOC),lowByte(sgp.TVOC),126,
- highByte(sgp.eCO2),lowByte(sgp.eCO2),126};
- pCharacteristic->setValue(frame_end,16);
- }
Advertisement
Add Comment
Please, Sign In to add comment