sinricpro / esp8266-esp32-sdk

Library for https://sinric.pro - simple way to connect your device to Alexa, Google Home, SmartThings and cloud
https://sinric.pro
227 stars 121 forks source link

Problem with SinricPro on ESP8266 #360

Closed decaffeinatedcoffee closed 6 months ago

decaffeinatedcoffee commented 6 months ago

I'm creating a project that consists in my own API with its own APP and SinricPro, its a smart light switch, the code was working fine some time ago but now sinric just takes too long to connect and after connected all my code stops working and only sinric related part works, i can't find a solution for this right now

on setup after wifi connected i have

   SinricProSwitch& mySwitch = SinricPro[srID];   
   mySwitch.onPowerState(onPowerState);                
   SinricPro.begin(srkey, srscrt);  
   SinricPro.onConnected([](){ Serial.println("[NETWORK] Connected to SinricPro"); }); 
   SinricPro.onDisconnected([](){ Serial.println("[NETWORK] Disconnected from SinricPro"); });

i also have SinricPro.handle(); on loop and im using millis instead of delay on my whole code

i can't understand why it takes too long to connect and after this makes my code stop working

also the whole code can be found here https://github.com/decaffeinatedcoffee/GeminiPro/tree/main/MCU

sivar2311 commented 6 months ago

Does this also happen if you use a pure example sketch, e.g. the switch example?

decaffeinatedcoffee commented 6 months ago

Does this also happen if you use a pure example sketch, e.g. the switch example?

the switch example worked fine

decaffeinatedcoffee commented 6 months ago

Okay, for some reason the problem is a conflict with Http requests, when i run setup without http requests sinric connects fine, also it still makes all the http on loop stop, what could cause http and sinricpro conflict?

sivar2311 commented 6 months ago

I will try to reproduce it. Unfortunately, your code is a bit "messy". Therefore I first have to clean up the code to understand what's intended to do and what happens where.

sivar2311 commented 6 months ago

Oh wait. One thing just popped into my head! You are using WiFiClientSecure! SinricPro also uses an SSL connection by default. It is known that the ESP can only handle one SSL connection at a time. SinricPro runs over WebSocket and has a persistent (SSL) connection! Try to switch to a simple WiFiClient or use #define SINRICPRO_NOSSL before #include SinricPro.h.

decaffeinatedcoffee commented 6 months ago

Oh wait. One thing just popped into my head! You are using WiFiClientSecure! SinricPro also uses an SSL connection by default. It is known that the ESP can only handle one SSL connection at a time. SinricPro runs over WebSocket and has a persistent (SSL) connection! Try to switch to a simple WiFiClient or use #define SINRICPRO_NOSSL before #include SinricPro.h.

OMG you are a genius it worked fine! thank you so much

decaffeinatedcoffee commented 6 months ago

okay just one more thing now sometimes i get a problem with sinric websocket where it just never connect:

here is the logs

[SinricPro:Websocket]: Connecting to WebSocket Server (ws.sinric.pro)
[SinricPro:Websocket]: headers: 
appkey:XXXXXXXXXXXXXXXXXXX
deviceidsXXXXXXXXXXXXXXXXX
restoredevicestates:false
ip:192.168.0.26
mac:48:55:19:0B:48:50
platform:ESP8266
SDKVersion:3.0.1
[SinricPro:Websocket]: disconnected
[SinricPro:Websocket]: disconnected

and in dashboard i get

image

sivar2311 commented 6 months ago

A debug log could help here.

To activate debugging, insert this code block before #include <SinricPro.h>:

#define DEBUG_ESP_PORT Serial
#define NODEBUG_WEBSOCKETS
#define NDEBUG
decaffeinatedcoffee commented 6 months ago

A debug log could help here.

To activate debugging, insert this code block before #include <SinricPro.h>:

#define DEBUG_ESP_PORT Serial
#define NODEBUG_WEBSOCKETS
#define NDEBUG

Yep debug log only drops this


[SinricPro:Websocket]: Connecting to WebSocket Server (ws.sinric.pro)
[SinricPro:Websocket]: headers: 
appkey:XXXXXXXXXXXXXXXXXXX
deviceidsXXXXXXXXXXXXXXXXX
restoredevicestates:false
ip:192.168.0.26
mac:48:55:19:0B:48:50
platform:ESP8266
SDKVersion:3.0.1
[SinricPro:Websocket]: disconnected
[SinricPro:Websocket]: disconnected```
sivar2311 commented 6 months ago

The connection is managed and established by SinricPro.handle() not by SinricPro.begin(). While SinricPro.handle() is called periodically the underlying WebSocket client tries to connect. Regarding to your code (SinricPro.onConnected([]() { Serial.println("[NETWORK] Connected to SinricPro"); });) you should see "[NETWORK] Connected to SinricPro" after a few seconds.

decaffeinatedcoffee commented 6 months ago

The connection is managed and established by SinricPro.handle() not by SinricPro.begin(). While SinricPro.handle() is called periodically the underlying WebSocket client tries to connect. Regarding to your code (SinricPro.onConnected([]() { Serial.println("[NETWORK] Connected to SinricPro"); });) you should see "[NETWORK] Connected to SinricPro" after a few seconds.

yep but its not happening i only get

[SinricPro:Websocket]: disconnected

every few seconds

sivar2311 commented 6 months ago

I assume that it doesn't happen with the pure switch example?

decaffeinatedcoffee commented 6 months ago

yes the example works fine, btw ill test example with the #define SINRICPRO_NOSSL line since it started after this

sivar2311 commented 6 months ago

Then the cause lies somewhere in your code.

I suggest breaking it down into individual functional parts and activating them one by one. This is the best way to find the problematic part.

decaffeinatedcoffee commented 6 months ago

okay the example works ill do some tests on my code

sivar2311 commented 6 months ago

I'll see if I can find out something tomorrow. It's already too late for today and your code is unfortunately written very complicated to perform a quick analysis.

decaffeinatedcoffee commented 6 months ago

ok, apparently sinric cant connect while the esp is using the http to send info to my own API

sivar2311 commented 6 months ago

ok, apparently sinric cant connect while the esp is using the http to send info to my own API

It's a limitation of the ESP, not SinricPro.

Can you describe in words what the sketch is supposed to do? (It's very hard to read from the code). Maybe together we can find a better way to handle things that leads to a stable connection.