4 czujniki SHT30 na jednym ESP32

User avatar
veeroos
Posts: 1081
Joined: Sun Mar 20, 2022 9:30 am
Location: Głogów
Has thanked: 1 time
Been thanked: 9 times

Post

Dlatego, że jeden z kolegów mnie poprosił o napisanie kodu na odczyt 4 czujników SHT30, dlatego też zostawię działający kod tutaj dla "potomnych", może się komuś kiedyś przyda. Kod wykorzystuje dostępne 2 interfejsy komunikacyjne i2c

Code: Select all

  #define STATUS_LED_GPIO 2
#define BUTTON_CFG_RELAY_GPIO 0

#include <Wire.h>
#include <SHT31.h> //   https://github.com/RobTillaart/SHT31
#include <SuplaDevice.h>
#include <supla/network/esp_wifi.h>
#include <supla/control/button.h>
#include <supla/device/status_led.h>
#include <supla/storage/littlefs_config.h>
#include <supla/network/esp_web_server.h>
#include <supla/network/html/device_info.h>
#include <supla/network/html/protocol_parameters.h>
#include <supla/network/html/status_led_parameters.h>
#include <supla/network/html/wifi_parameters.h>
#include <supla/device/supla_ca_cert.h>
#include <supla/events.h>
#include <supla/sensor/therm_hygro_meter.h>

SHT31 sht[4] = {
  SHT31(0x44, &Wire), 
  SHT31(0x45, &Wire), 
  SHT31(0x44, &Wire1), 
  SHT31(0x45, &Wire1) 
};

#include <supla/storage/eeprom.h>
Supla::Eeprom eeprom;

Supla::ESPWifi wifi;
Supla::LittleFsConfig configSupla;

Supla::Device::StatusLed statusLed(STATUS_LED_GPIO, true); // inverted state
Supla::EspWebServer suplaServer;

class Czujnik_SHT : public Supla::Sensor::ThermHygroMeter {
  public:
    explicit Czujnik_SHT(uint8_t czujnik_sht) : czujnik_sht(czujnik_sht){
    }
  double getTemp() override {
   odczytaj_czujniki();
   return temperature;
  }

  double getHumi() override {
    return humidity;
  }

  void onInit() override {
    sht[czujnik_sht].begin();
    channel.setNewValue(getTemp(), getHumi());
  }
  private:
    void odczytaj_czujniki(){
      sht[czujnik_sht].read();
      temperature = sht[czujnik_sht].getTemperature();
      humidity = sht[czujnik_sht].getHumidity();
    }
  protected:
    int8_t czujnik_sht;
    double temperature = TEMPERATURE_NOT_AVAILABLE;
    double humidity = HUMIDITY_NOT_AVAILABLE;
}; Czujnik_SHT *czujniki[4];

Supla::Html::DeviceInfo htmlDeviceInfo(&SuplaDevice);
Supla::Html::WifiParameters htmlWifi;
Supla::Html::ProtocolParameters htmlProto;
Supla::Html::StatusLedParameters htmlStatusLed;


void setup() {

  Serial.begin(115200);
  Wire.begin();
  Wire1.begin(33,32);
  auto buttonCfgRelay = new Supla::Control::Button(BUTTON_CFG_RELAY_GPIO, true, true);
  buttonCfgRelay->configureAsConfigButton(&SuplaDevice);
 
  for (int i = 0; i < 4; ++i){
    czujniki[i] = new Czujnik_SHT(i); 
  }
  // configure defualt Supla CA certificate
  SuplaDevice.setSuplaCACert(suplaCACert);
  SuplaDevice.setSupla3rdPartyCACert(supla3rdCACert);

  SuplaDevice.begin();
}

void loop() {
 
  SuplaDevice.iterate();
  
} 
Zamel Mew-01, Zamel PEW-01, Zamel SBW-01, Zamel THW-01, Zamel SRW-01, Zamel mSLW-02, Auraton BOX + Indor Sensor, Airly Gate By Veeroos, Zigbee to Supla Gateway + sensors, Zamel SGW-01 and more

https://github.com/v33r005
nebraska
Posts: 204
Joined: Thu Dec 08, 2022 3:00 pm
Been thanked: 1 time

Post

Kolega @ veeroos wykonał kawal dobrej roboty.
Wrzucam schemat, żeby inni wiedzieli jak to podłączyć. Wszystko śmiga.
Dziękuje.
You do not have the required permissions to view the files attached to this post.

Return to “Projekty użytkowników”