Supla a połączenie przez WiFiClientSecure

KaDet
Posty: 20
Rejestracja: pt lis 27, 2020 5:06 pm

Cześć,
mam problem - wydaje się, ze podobny jak w viewtopic.php?t=4704. Jednakże u mnie zastosowane tam rozwiązanie nie ma miejsca, gdyż od razu miałem inna nazwę lokalnego obiektu. Szukajka nie znalazła mi żadnego tematu związanego z moim problemem (przytoczony link został odnaleziony przez Google) stąd pytanie.

W czym leży problem? - mam kawałek kodu, którego zadaniem jest obsłużenie dwu przycisków. Jeden obsługiwany jest przez Supla i steruje przekaźnikiem, drugi po kliknięciu ma wysłać żądanie do Supli o zmianę stanu innego urządzenia. I w obsłudze tegoz wysyłania jest problem. Próbuję nawiązać połączenie za pomocą WiFiClientSecure. Niestety są dwa możliwe stany - albo timeout, albo wyjątek. Po wykomentowaniu SuplaDevice.iterate(); połączenie jest nawiązywane i żądanie wysyłane. Czy wiecie może jak rozwiązać taki problem? Poniżej kod - funkcja send2supla

Uprzedzając pytania dlaczego tak skomplikowany kod - na potrzeby niniejszego pytani mocno uprościłem urządzenie - w rzeczywistości jest
bardziej skomplikowane.

Funkcja send2supla

Kod: Zaznacz cały

void send2Supla(String hAddress, int hPort, String dirLink) {
  if (dirLink==""){
    Serial.println("Empty link to Supla");
    return;
  }
  WiFiClientSecure myclient;
  myclient.setInsecure();
  if (myclient.connect(hAddress, hPort)) {
    Serial.println("Connected to: "+hAddress);
    String ToSend = String("GET ") + dirLink + " HTTP/1.1\r\n" +
                    "Host: " + hAddress + "\r\n" +
                    "User-Agent: SuplaButtonESP8266\r\n" +
                    "Connection: close\r\n\r\n";

    Serial.println();
    Serial.println("---------------------------");
    Serial.println(ToSend);
    Serial.println("---------------------------");
    Serial.println();

    myclient.println(ToSend);

    Serial.println("End of connection with: "+hAddress);
    
  } else {
    Serial.println("Can't connect to "+hAddress);
  }
}

Cały kod:

Kod: Zaznacz cały

#include <FS.h> 
#include <SPI.h>
#include <SuplaDevice.h>
#include <supla/network/esp_wifi.h>
#include <supla/control/button.h>
#include <supla/control/light_relay.h>
#include <ESP8266HTTPClient.h>
#include <WiFiManager.h>
#include <SwitchPack.h>

#define RELAY_2_GPIO 14 //D5

#define BUTTON_2_GPIO 0 //D3

#define MULTIKEY_GPIO 13 //D7

#define REACTION_TIME 1000 
#define LONGPRESS_TIME 1500
#define WIFIMANAGER_TIME 7000
#define WIFI_MANAGER_GPIO BUTTON_2_GPIO

#define APNAME "TwojAP"


Supla::ESPWifi wifi; //WiFi Manager zarzadza połaczeniem
WiFiManager wifiManager;
DoubleClick multikey(MULTIKEY_GPIO, PULLUP, REACTION_TIME); //obsługa kliknięć przyciskiem


void setup() {
  Serial.begin(9600);
  multikey.begin();

  Serial.println("");
  Serial.println("Starting module"); 
  wifiManager.setDarkMode(true);
  wifiManager.setCaptivePortalEnable(true);
  //starting wifiManager
  bool wifiConnected = wifiManager.autoConnect(APNAME);
  if(!wifiConnected) {
        Serial.println("Autoconnect: Failed to connect");
        Serial.println("Autoconnect: Resetting ESP......");
        Serial.println("");
        Serial.println("");
        ESP.restart();
        delay(500);
  } else {
        //if you get here you have connected to the WiFi    
        Serial.println("Autoconnect: connected");
        Serial.println("local ip");
        Serial.println(WiFi.localIP());        
        //swicth off AP mode - name of ESP will hide       
        WiFi.softAPdisconnect(true);
  }; 

  char GUID[SUPLA_GUID_SIZE] = {TWOJ_GUID};
  char AUTHKEY[SUPLA_AUTHKEY_SIZE] =  {TWOJ_GUID};

  Supla::Control::Relay *relay_2 = new Supla::Control::LightRelay(RELAY_2_GPIO);
  Supla::Control::Button *relay_2_button = new Supla::Control::Button(BUTTON_2_GPIO, true, true);
  relay_2_button->addAction(Supla::TOGGLE, *relay_2, Supla::ON_PRESS);

  SuplaDevice.begin(GUID,               // Global Unique Identifier 
                    "twoj.serwer.org",  // SUPLA server address
                    "twoj.email",   // Email address used to login to Supla Cloud
                    AUTHKEY);          // Authorization key
  Serial.println("End of setup()");
}

void loop() {

  String host = "twoj.server.org";
  int httpsPort = 443; 
  String directLinkOne = "/twoj/link";
 
  int value =multikey.clickCount();    
  if (value==1){
        Serial.println("One Press");
        if (WiFi.status() == WL_CONNECTED) {
            send2Supla(host, httpsPort, directLinkOne);
        }
  }
     
  SuplaDevice.iterate();
   
}

void send2Supla(String hAddress, int hPort, String dirLink) {
  if (dirLink==""){
    Serial.println("Empty link to Supla");
    return;
  }
  WiFiClientSecure myclient;
  myclient.setInsecure();
  if (myclient.connect(hAddress, hPort)) {
    Serial.println("Connected to: "+hAddress);
    String ToSend = String("GET ") + dirLink + " HTTP/1.1\r\n" +
                    "Host: " + hAddress + "\r\n" +
                    "User-Agent: SuplaButtonESP8266\r\n" +
                    "Connection: close\r\n\r\n";

    Serial.println();
    Serial.println("---------------------------");
    Serial.println(ToSend);
    Serial.println("---------------------------");
    Serial.println();

    myclient.println(ToSend);

    Serial.println("End of connection with: "+hAddress);
    
  } else {
    Serial.println("Can't connect to "+hAddress);
  }
}
elmaya
Posty: 1482
Rejestracja: śr cze 27, 2018 5:48 pm
Lokalizacja: El Saucejo - Sevilla

It would be better if you use "Action Trigger" instead of "directlink". viewtopic.php?t=9503
KaDet
Posty: 20
Rejestracja: pt lis 27, 2020 5:06 pm

Thank you so much!!! I was hoping for this kind of answer. I was stuck on version 2.3.5 so I did not even know about the existence of ActionTrigger :). I changed the code and everything works as it should.
ODPOWIEDZ

Wróć do „Pomoc”