Skocz do zawartości

Termostat bojlera, Supla, sterowanie grzałką.


SOYER

Pomocna odpowiedź

Szkic dla zainteresowanych:

#include <SuplaDevice.h>
#include <supla/control/relay.h>
#include <supla/sensor/DS18B20.h>
#include <supla/network/esp_wifi.h>
#include <supla/storage/eeprom.h>
Supla::Eeprom eeprom(0);

#include <supla/storage/storage.h>
#include <supla/control/virtual_relay.h>
#include <supla/sensor/virtual_thermometer.h>
#include <Timers.h>

#define ONE_WIRE D1

Supla::ESPWifi wifi("xxx", "xxx");/////"xx
Timer sekund30;
Supla::Control::Relay *relay1 = nullptr;
Supla::Control::Relay *relay2 = nullptr;
Supla::Control::VirtualRelay *vrelay = nullptr;
Supla::Sensor::DS18B20 *tempBojler = nullptr;

double temp;
int przek1 = 0;
int tempON = 30;
int tempOFF = 65;
int histereza = 3;
int wyslanoON = 0;
int wyslanoOFF = 0;
int stanVirtuala = 0;
int stanGrzalki = 0;

class RelayAsInput : public Supla::Control::VirtualRelay {//temp wył. grzałki
 public:
  explicit RelayAsInput(int step = 1) : step(step) {
  }
  virtual ~RelayAsInput() {}
  void onInit() override {}
  void turnOn(_supla_int_t duration = 0) override {

    (void)(duration);
    int newTemp = temp.getValue() + step;
    if (newTemp > 90) {
      newTemp = 90;
    }
    temp.setValue(newTemp);
    tempOFF = newTemp;
    wyslanoON = 0;
    wyslanoOFF = 0;
    Supla::Storage::ScheduleSave(2000);
  }

  void turnOff(_supla_int_t duration = 0) override {
    (void)(duration);
    int newTemp = temp.getValue() - step;
    if (newTemp <= (tempON +3)) {
      newTemp = tempON +3;
    }
    temp.setValue(newTemp);
    tempOFF = newTemp;
    wyslanoON = 0;
    wyslanoOFF = 0;
    Supla::Storage::ScheduleSave(2000);
  }

  int getValue() {
    return temp.getValue();
  }

  void setValue(int value) {
    temp.setValue(value);
    Supla::Storage::ScheduleSave(2000);
  }

  void onLoadState() override {
    int temperature = 0;
    Supla::Storage::ReadState(reinterpret_cast<unsigned char *>(&temperature),
                              sizeof(int));
    temp.setValue(temperature);
    tempOFF = temperature;
  }

  void onSaveState() override {
    int temperature = temp.getValue();
    Supla::Storage::WriteState(reinterpret_cast<unsigned char *>(&temperature),
                               sizeof(int));
  }

 protected:
  Supla::Sensor::VirtualThermometer temp;
  int step = 1;
};

class RelayAsInput1 : public Supla::Control::VirtualRelay {//////////temp wł. grzałki
 public:
  explicit RelayAsInput1(int step = 1) : step(step) {
  }
  virtual ~RelayAsInput1() {}
  void onInit() override {}
  void turnOn(_supla_int_t duration = 0) override {
    (void)(duration);
    int newTemp = temp.getValue() + step;
    if (newTemp >= (tempOFF -3)) {
      newTemp = tempOFF -3;
    }
    temp.setValue(newTemp);
    tempON = newTemp;
    wyslanoON = 0;
    wyslanoOFF = 0;
    Supla::Storage::ScheduleSave(2000);
  }

  void turnOff(_supla_int_t duration = 0) override {
    (void)(duration);
    int newTemp = temp.getValue() - step;
    if (newTemp < 5) {
      newTemp = 5;
    }
    temp.setValue(newTemp);
    tempON = newTemp;
    wyslanoON = 0;
    wyslanoOFF = 0;
    Supla::Storage::ScheduleSave(2000);
  }

  int getValue() {
    return temp.getValue();
  }

  void setValue(int value) {
    temp.setValue(value);
    Supla::Storage::ScheduleSave(2000);
  }

  void onLoadState() override {
    int temperature = 0;
    Supla::Storage::ReadState(reinterpret_cast<unsigned char *>(&temperature),
                              sizeof(int));
    temp.setValue(temperature);
    tempON = temperature;
  }

  void onSaveState() override {
    int temperature = temp.getValue();
    Supla::Storage::WriteState(reinterpret_cast<unsigned char *>(&temperature),
                               sizeof(int));
  }

 protected:
  Supla::Sensor::VirtualThermometer temp;
  int step = 1;
};

void setup() {
  Serial.begin(9600);
   sekund30.begin(14898);
// Replace the falowing GUID with value that you can retrieve from https://www.supla.org/arduino/get-guid
  char GUID[SUPLA_GUID_SIZE] = {xx};

  // Replace the following AUTHKEY with value that you can retrieve from: https://www.supla.org/arduino/get-authkey
  char AUTHKEY[SUPLA_AUTHKEY_SIZE] = {xx};

  relay1 =   new Supla::Control::Relay(D5, false);
  relay2 =   new Supla::Control::Relay(D6, false);
  vrelay = new Supla::Control::VirtualRelay();
  tempBojler = new Supla::Sensor::DS18B20(ONE_WIRE);
  new RelayAsInput1();
  new RelayAsInput();
  
       dsSTART();
  SuplaDevice.begin(GUID,              // Global Unique Identifier 
                    "svrxx.supla.org",  // SUPLA server address//////////xx.supla.org
                    "xxxx",   // Email address used to login to Supla Cloud/////////////xx
                    AUTHKEY);          // Authorization key
}

void loop() {
   SuplaDevice.iterate();
   ds();
   relayOnOff();
}

void ds(){
  if(sekund30.available()){
    temp = tempBojler->getChannel()->getValueDouble();
  }
}
void dsSTART(){
  temp = tempBojler->getChannel()->getValueDouble();
}
void relayOnOff(){
  stanV();
  if(temp>=tempOFF){
    przek1=0;
  }
  else if(temp<tempON){
    przek1=1; 
  }
 if(!vrelay->isOn()){
  if((przek1 == 1) && (wyslanoON==0)){
   relay2->turnOn();
   wyslanoON=1;
   wyslanoOFF=0;
  }
  else if((przek1==0) && (wyslanoOFF==0)){
   relay2->turnOff();
   wyslanoON=0;
   wyslanoOFF=1;
  }
}
}
void stanV(){
    int r2 = relay2->isOn();
    if(r2 != stanGrzalki){
      stanGrzalki = r2;
      wyslanoON=0;
      wyslanoOFF=0;
    }
    int vr = vrelay->isOn();
    if(vr != stanVirtuala){
      stanVirtuala = vr;
      wyslanoON=0;
      wyslanoOFF=0;
    }
    if(vrelay->isOn()){
      if(relay2->isOn()){
        przek1=1; 
      }
      else if(!relay2->isOn()){
        przek1=0; 
      }
    }
}

 

Link do komentarza
Share on other sites

Dołącz do dyskusji, napisz odpowiedź!

Jeśli masz już konto to zaloguj się teraz, aby opublikować wiadomość jako Ty. Możesz też napisać teraz i zarejestrować się później.
Uwaga: wgrywanie zdjęć i załączników dostępne jest po zalogowaniu!

Anonim
Dołącz do dyskusji! Kliknij i zacznij pisać...

×   Wklejony jako tekst z formatowaniem.   Przywróć formatowanie

  Dozwolonych jest tylko 75 emoji.

×   Twój link będzie automatycznie osadzony.   Wyświetlać jako link

×   Twoja poprzednia zawartość została przywrócona.   Wyczyść edytor

×   Nie możesz wkleić zdjęć bezpośrednio. Prześlij lub wstaw obrazy z adresu URL.

×
×
  • Utwórz nowe...

Ważne informacje

Ta strona używa ciasteczek (cookies), dzięki którym może działać lepiej. Więcej na ten temat znajdziesz w Polityce Prywatności.