Jutro postaram się wrzucić jakiś film.
Szkic:
Code: Select all
#include <Wire.h>
#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 <OneWire.h>
#include <DallasTemperature.h>
#include <Timers.h>
Supla::ESPWifi wifi("xxx", "xxx);/////
#define ONE_WIRE D1
OneWire oneWire0(D1);
DallasTemperature t1(&oneWire0);
Timer sekund30;
Supla::Control::Relay *relay1 = nullptr;
Supla::Control::Relay *relay2 = nullptr;
Supla::Control::VirtualRelay *vrelay = nullptr;
int 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] = {xxx};
// Replace the following AUTHKEY with value that you can retrieve from: https://www.supla.org/arduino/get-authkey
char AUTHKEY[SUPLA_AUTHKEY_SIZE] = {xxx};
relay1 = new Supla::Control::Relay(D5, false);
relay2 = new Supla::Control::Relay(D6, false);
vrelay = new Supla::Control::VirtualRelay();
new Supla::Sensor::DS18B20(ONE_WIRE);
new RelayAsInput1();
new RelayAsInput();
t1.begin();
dsSTART();
SuplaDevice.begin(GUID, // Global Unique Identifier
"svrxx.supla.org", // SUPLA server address//////////
"xxx", // Email address used to login to Supla Cloud/////////////
AUTHKEY); // Authorization key
}
void loop() {
SuplaDevice.iterate();
ds();
relayOnOff();
}
void ds(){
if(sekund30.available()){
t1.requestTemperatures();
temp = (t1.getTempCByIndex(0));
sekund30.restart();
}
}
void dsSTART(){
t1.requestTemperatures();
temp = (t1.getTempCByIndex(0));
}
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;
}
}Np. chciałbym wiedzieć jak pobierać wartość temp z ds z suplowej klasy, a nie samemu go dodatkowo czytać.
