napotkałem na bardzo dziwne zachowanie. Ale najpierw opiszę problem: w trakcie załączania przekaźnika muszę odczytać pewne informacje i dokonać pewnych zapisów na własnym serwerze. Mam do tego własny serwer restAPI, który robi co trzeba. Napisałem więc kod (nieco przydługi, ale skróciłem go tak jak tylko mogłem):
Code: Select all
#include <SPI.h>
#include <SuplaDevice.h>
#include <supla/storage/eeprom.h>
#include <supla/network/esp_wifi.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/actions.h>
#include <supla/control/button.h>
#include <supla/control/relay.h>
//buttons
#define CH1_BUTTON_GPIO 5 //D1
//relays
#define CH1_RELAY_GPIO 13 //D7
//status
#define STATUS_LED_GPIO 14 //D5
#define SVR_ADR "10.1.1.7"
#define SVR_PORT 9001
Supla::ESPWifi wifi;
Supla::Eeprom eeprom;
Supla::LittleFsConfig configSupla;
Supla::EspWebServer suplaServer;
class MyConnect {
public:
MyConnect(const char *_url, const char *_host, const uint16_t _port);
bool checkConnection();
bool openConnection();
bool closeConnection();
const char *getRequest(const char *parameter = nullptr);
protected:
WiFiClient MyClient;
char url[50];
char host[50];
uint16_t port;
int8_t retryCount = 0;
};
MyConnect::MyConnect(const char *_url, const char *_host, const uint16_t _port) {
strcpy(url, _url);
strcpy(host, _host);
port = _port;
}
bool MyConnect::openConnection() {
bool retvalue = false;
retryCount = 3;
do {
retvalue = MyClient.connect(host, port) ? true : false;
delay(100);
retryCount--;
} while (retryCount > 0 && !retvalue);
return retvalue;
}
bool MyConnect::closeConnection() {
MyClient.stop();
return checkConnection();
}
bool MyConnect::checkConnection() {
return MyClient.connected() == 1 ? true : false;
}
namespace Supla {
namespace Control {
class MyRelay : public Relay {
public:
MyRelay(int _pin, const char *_server, uint16_t _port, bool highIsOn = true);
void turnOn(_supla_int_t duration = 0);
private:
int pin;
char server[256];
uint16_t port;
MyConnect *MyConnection = nullptr;
};
MyRelay::MyRelay(int _pin, const char *_server, uint16_t _port, bool highIsOn)
: Relay(_pin,
highIsOn,
SUPLA_BIT_FUNC_POWERSWITCH),
pin(_pin), port(_port) {
strcpy(server, _server);
MyConnection = new MyConnect("", server, port);
}
void MyRelay::turnOn(_supla_int_t duration) {
Serial.println("1");
MyConnection->openConnection();
Serial.println("2");
//Do sth.
MyConnection->closeConnection();
Serial.println("3");
Relay::turnOn(duration);
}
}
}
void setup() {
Serial.begin(115200);
new Supla::Html::DeviceInfo(&SuplaDevice);
new Supla::Html::WifiParameters;
new Supla::Html::ProtocolParameters;
new Supla::Html::StatusLedParameters;
new Supla::Device::StatusLed(STATUS_LED_GPIO, false);
auto relayCH1 = new Supla::Control::MyRelay(CH1_RELAY_GPIO, SVR_ADR, SVR_PORT);
auto relayButtonCH1 = new Supla::Control::Button(CH1_BUTTON_GPIO, true, true);
relayButtonCH1->setHoldTime(3000);
relayButtonCH1->setMulticlickTime(300);
relayButtonCH1->addAction(Supla::TOGGLE, relayCH1, Supla::ON_CLICK_1);
// configure defualt Supla CA certificate
SuplaDevice.setSuplaCACert(suplaCACert);
SuplaDevice.setSupla3rdPartyCACert(supla3rdCACert);
SuplaDevice.begin();
}
void loop() {
SuplaDevice.iterate();
}
Code: Select all
Serial.println("1");
MyConnection->openConnection();Czym może być spowodowane tak dziwne zachowanie przy różnych metodach zainicjowanie tej samej metody?
