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
Other
234 stars 124 forks source link

Not working on esp8266 #305

Closed marcoratto closed 1 year ago

marcoratto commented 1 year ago

Hi, I tried the samples switch: Nodejs - it works fine Python - it works fine Esp8266 - it doesn't work

I did a debug on the tcp connection and I found some differences: Nodejs and python uses http websocket with deflate compression and frame masked, Esp8266 uses https websocket, without compression and without frame masked.

Could someone help me to understand how to fix the connection on esp8266 device ?

Thanks in advance

sivar2311 commented 1 year ago

Hi @marcoratto and a happy new year! Which sketch do you use and what error do you have?

marcoratto commented 1 year ago

I tested the following Arduino Sample: https://github.com/sinricpro/esp8266-esp32-sdk/blob/master/examples/Switch/Switch/Switch.ino

sivar2311 commented 1 year ago

Ok, what errors do you have exactly?

sivar2311 commented 1 year ago

Please uncomment Line 18 (//#define ENABLE_DEBUG) to enable serial debug output and post the content of your serial monitor

kakopappa commented 1 year ago

Hi @marcoratto

Just tried the switch sketch with an ESP8266, it can connect and receive commands from the server.

Could you please enable debug logs and change the Arduino IDE ->Tools -> Debug Port -> Serial (for additional Websocket library logs) and share the logs from Arduino Serial monitor?

If you have connection issues, you can try connecting the ESP via a Mobile Hotspot

image

#define ENABLE_DEBUG

#ifdef ENABLE_DEBUG
       #define DEBUG_ESP_PORT Serial
       #define NODEBUG_WEBSOCKETS
       #define NDEBUG
#endif 

#include <Arduino.h>
#ifdef ESP8266 
       #include <ESP8266WiFi.h>
#endif 
#ifdef ESP32   
       #include <WiFi.h>
#endif

#include "SinricPro.h"
#include "SinricProSwitch.h"

#define WIFI_SSID         ""    
#define WIFI_PASS         ""
#define APP_KEY           ""
#define APP_SECRET      ""
#define SWITCH_ID         ""
#define BAUD_RATE         9600                // Change baudrate to your need

bool onPowerState(const String &deviceId, bool &state) {
  Serial.printf("Device %s turned %s (via SinricPro) \r\n", deviceId.c_str(), state?"on":"off");
  return true; // request handled properly
}

// setup function for WiFi connection
void setupWiFi() {
  Serial.printf("\r\n[Wifi]: Connecting");
  WiFi.begin(WIFI_SSID, WIFI_PASS);

  while (WiFi.status() != WL_CONNECTED) {
    Serial.printf(".");
    delay(250);
  }
  Serial.printf("connected!\r\n[WiFi]: IP-Address is %s\r\n", WiFi.localIP().toString().c_str());
}

// setup function for SinricPro
void setupSinricPro() {
  // add device to SinricPro
  SinricProSwitch& mySwitch = SinricPro[SWITCH_ID];

  // set callback function to device
  mySwitch.onPowerState(onPowerState);

  // setup SinricPro
  SinricPro.onConnected([](){ Serial.printf("Connected to SinricPro\r\n"); }); 
  SinricPro.onDisconnected([](){ Serial.printf("Disconnected from SinricPro\r\n"); });
  //SinricPro.restoreDeviceStates(true); // Uncomment to restore the last known state from the server.
  SinricPro.begin(APP_KEY, APP_SECRET);
}

// main setup function
void setup() {
  Serial.begin(BAUD_RATE); Serial.printf("\r\n\r\n");
  setupWiFi();
  setupSinricPro();
}

void loop() {
  SinricPro.handle();
}

image

marcoratto commented 1 year ago

Hi, this is the log using SSL connection:

11:35:56.515 -> connected with ***, channel * 11:35:56.515 -> dhcp client start... 11:35:56.947 -> ip:192.168.1.116,mask:255.255.255.0,gw:192.168.1.1 11:35:56.980 -> 11:35:56.980 -> 11:35:56.980 -> Connected to 11:35:56.980 -> IP address: 192.168.1.116 11:35:56.980 -> 11:35:56.980 -> sleep disable 11:35:59.966 -> Connecting to MQTT broker OK 11:35:59.966 -> Topics subscribed to the Broker 11:35:59.966 -> Status update on Broker 11:36:02.984 -> [SinricPro]: Device "" does not exist. Creating new device 11:36:03.017 -> [SinricPro:add()]: Adding device with id "". 11:36:03.017 -> [SinricProDevice::sendEvent]: The event could not be sent. No connection to the SinricPro server. 11:36:03.017 -> Connecting to SIRINC ws.sinric.pro:443 11:36:03.017 -> OK 11:36:06.467 -> pm open,type:0 0 11:36:09.022 -> [SinricPro:Websocket]: Connecting to WebSocket Server using SSL (ws.sinric.pro) 11:36:09.022 -> [SinricPro:Websocket]: headers: 11:36:09.022 -> appkey: 11:36:09.055 -> deviceids:* 11:36:09.055 -> restoredevicestates:false 11:36:09.055 -> ip:192.168.1.116 11:36:09.055 -> mac:** 11:36:09.055 -> platform:ESP8266 11:36:09.055 -> SDKVersion:2.10.3 11:36:09.055 -> [SinricPro:Websocket]: Connecting to ws.sinric.pro:443 11:36:09.055 -> [WS-Client] Websocket Version: 2.3.6 11:36:09.055 -> [WS-Client] connect wss... 11:36:10.017 -> [WS-Client] connected to ws.sinric.pro:443. 11:36:10.017 -> [WS-Client][sendHeader] sending header... 11:36:10.017 -> [WS-Client][sendHeader] handshake GET / HTTP/1.1 11:36:10.017 -> Host: ws.sinric.pro:443 11:36:10.017 -> Connection: Upgrade 11:36:10.017 -> Upgrade: websocket 11:36:10.017 -> Sec-WebSocket-Version: 13 11:36:10.017 -> Sec-WebSocket-Key: **** 11:36:10.017 -> Sec-WebSocket-Protocol: arduino 11:36:10.017 -> appkey:* 11:36:10.050 -> deviceids:* 11:36:10.050 -> restoredevicestates:false 11:36:10.050 -> ip:192.168.1.116 11:36:10.050 -> mac:** 11:36:10.050 -> platform:ESP8266 11:36:10.050 -> SDKVersion:2.10.3 11:36:10.050 -> User-Agent: arduino-WebSocket-Client 11:36:10.050 -> 11:36:10.050 -> [write] n: 413 t: 14552 11:36:10.083 -> [WS-Client][sendHeader] sending header... Done (71968us). 11:36:11.112 -> [WS-Client][handleHeader] RX: HTTP/1.1 101 Switching Protocols 11:36:12.140 -> [WS-Client][handleHeader] RX: Server: nginx 11:36:13.135 -> [WS-Client][handleHeader] RX: Date: Tue, 03 Jan 2023 10:36:10 GMT 11:36:14.164 -> [WS-Client][handleHeader] RX: Connection: upgrade 11:36:15.193 -> [WS-Client][handleClientData] header response timeout.. disconnecting! 11:36:15.193 -> [WS-Client] client disconnected. 11:36:15.193 -> [SinricPro:Websocket]: disconnected 11:36:15.193 -> Disconnected from SinricPro 11:36:16.222 -> [WS-Client] connect wss... 11:36:17.151 -> [WS-Client] connected to ws.sinric.pro:443. 11:36:17.151 -> [WS-Client][sendHeader] sending header... 11:36:17.151 -> [WS-Client][sendHeader] handshake GET / HTTP/1.1 11:36:17.151 -> Host: ws.sinric.pro:443 11:36:17.151 -> Connection: Upgrade 11:36:17.151 -> Upgrade: websocket 11:36:17.151 -> Sec-WebSocket-Version: 13 11:36:17.151 -> Sec-WebSocket-Key: **** 11:36:17.184 -> Sec-WebSocket-Protocol: arduino 11:36:17.184 -> appkey:* 11:36:17.184 -> deviceids:***** 11:36:17.184 -> restoredevicestates:false 11:36:17.184 -> ip:192.168.1.116 11:36:17.184 -> mac:** 11:36:17.184 -> platform:ESP8266 11:36:17.184 -> SDKVersion:2.10.3 11:36:17.184 -> User-Agent: arduino-WebSocket-Client 11:36:17.184 -> 11:36:17.184 -> [write] n: 413 t: 21693 11:36:17.217 -> [WS-Client][sendHeader] sending header... Done (72093us). 11:36:17.250 -> duration is 12770 11:36:17.250 -> distance is 219

kakopappa commented 1 year ago

Likely you don’t have enough memory to maintain two connections in esp8266

Try using a ESP32 or disable Sinric Pro SSL using

define SINRICPRO_NOSSL

Add above to top of the file

On Tue, 3 Jan 2023 at 5:41 PM marcoratto @.***> wrote:

Hi, this is the log using SSL connection:

11:35:56.515 -> connected with

*, channel * 11:35:56.515 -> dhcp client start... 11:35:56.947 -> ip:192.168.1.116,mask:255.255.255.0,gw:192.168.1.1 11:35:56.980 -> 11:35:56.980 -> 11:35:56.980 -> Connected to *** 11:35:56.980 -> IP address: 192.168.1.116 11:35:56.980 -> 11:35:56.980 -> sleep disable 11:35:59.966 -> Connecting to MQTT broker OK 11:35:59.966 -> Topics subscribed to the Broker 11:35:59.966 -> Status update on Broker 11:36:02.984 -> [SinricPro]: Device "*" does not exist. Creating new device 11:36:03.017 -> [SinricPro:add()]: Adding device with id "

". 11:36:03.017 -> [SinricProDevice::sendEvent]: The event could not be sent. No connection to the SinricPro server. 11:36:03.017 -> Connecting to SIRINC ws.sinric.pro:443 http://ws.sinric.pro:443 11:36:03.017 -> OK 11:36:06.467 -> pm open,type:0 0 11:36:09.022 -> [SinricPro:Websocket]: Connecting to WebSocket Server using SSL (ws.sinric.pro http://ws.sinric.pro) 11:36:09.022 -> [SinricPro:Websocket]: headers: 11:36:09.022 -> appkey: 11:36:09.055 -> deviceids:* 11:36:09.055 -> restoredevicestates:false 11:36:09.055 -> ip:192.168.1.116 11:36:09.055 -> mac:** 11:36:09.055 -> platform:ESP8266 11:36:09.055 -> SDKVersion:2.10.3 11:36:09.055 -> [SinricPro:Websocket]: Connecting to ws.sinric.pro:443 11:36:09.055 -> [WS-Client] Websocket Version: 2.3.6 11:36:09.055 -> [WS-Client] connect wss... 11:36:10.017 -> [WS-Client] connected to ws.sinric.pro:443. 11:36:10.017 -> [WS-Client][sendHeader] sending header... 11:36:10.017 -> [WS-Client][sendHeader] handshake GET / HTTP/1.1 11:36:10.017 -> Host: ws.sinric.pro:443 11:36:10.017 -> Connection: Upgrade 11:36:10.017 -> Upgrade: websocket 11:36:10.017 -> Sec-WebSocket-Version: 13 11:36:10.017 -> Sec-WebSocket-Key: **** 11:36:10.017 -> Sec-WebSocket-Protocol: arduino 11:36:10.017 -> appkey:* 11:36:10.050 -> deviceids:* 11:36:10.050 -> restoredevicestates:false 11:36:10.050 -> ip:192.168.1.116 11:36:10.050 -> mac:** 11:36:10.050 -> platform:ESP8266 11:36:10.050 -> SDKVersion:2.10.3 11:36:10.050 -> User-Agent: arduino-WebSocket-Client 11:36:10.050 -> 11:36:10.050 -> [write] n: 413 t: 14552 11:36:10.083 -> [WS-Client][sendHeader] sending header... Done (71968us). 11:36:11.112 -> [WS-Client][handleHeader] RX: HTTP/1.1 101 Switching Protocols 11:36:12.140 -> [WS-Client][handleHeader] RX: Server: nginx 11:36:13.135 -> [WS-Client][handleHeader] RX: Date: Tue, 03 Jan 2023 10:36:10 GMT 11:36:14.164 -> [WS-Client][handleHeader] RX: Connection: upgrade 11:36:15.193 -> [WS-Client][handleClientData] header response timeout.. disconnecting! 11:36:15.193 -> [WS-Client] client disconnected. 11:36:15.193 -> [SinricPro:Websocket]: disconnected 11:36:15.193 -> Disconnected from SinricPro 11:36:16.222 -> [WS-Client] connect wss... 11:36:17.151 -> [WS-Client] connected to ws.sinric.pro:443. 11:36:17.151 -> [WS-Client][sendHeader] sending header... 11:36:17.151 -> [WS-Client][sendHeader] handshake GET / HTTP/1.1 11:36:17.151 -> Host: ws.sinric.pro:443 11:36:17.151 -> Connection: Upgrade 11:36:17.151 -> Upgrade: websocket 11:36:17.151 -> Sec-WebSocket-Version: 13 11:36:17.151 -> Sec-WebSocket-Key: **** 11:36:17.184 -> Sec-WebSocket-Protocol: arduino 11:36:17.184 -> appkey:* 11:36:17.184 -> deviceids:***** 11:36:17.184 -> restoredevicestates:false 11:36:17.184 -> ip:192.168.1.116 11:36:17.184 -> mac:** 11:36:17.184 -> platform:ESP8266 11:36:17.184 -> SDKVersion:2.10.3 11:36:17.184 -> User-Agent: arduino-WebSocket-Client 11:36:17.184 -> 11:36:17.184 -> [write] n: 413 t: 21693 11:36:17.217 -> [WS-Client][sendHeader] sending header... Done (72093us). 11:36:17.250 -> duration is 12770 11:36:17.250 -> distance is 219

— Reply to this email directly, view it on GitHub https://github.com/sinricpro/esp8266-esp32-sdk/issues/305#issuecomment-1369618440, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABZAZZULP6TA4AEGGXZIBOLWQP67JANCNFSM6AAAAAATOBMTJI . You are receiving this because you commented.Message ID: @.***>

sivar2311 commented 1 year ago

I wonder about [WS-Client][handleClientData] header response timeout.. disconnecting!

Does the ESP have a weak WiFi connection? Are AppKey, AppSecret and DeviceID correct?

marcoratto commented 1 year ago

Hi, Just an update: In my sketch file I removed all the code for managing the HC-SR04 ultrasonic sensor. I leave only the code for sinric, mqtt and blynk and now it works fine.

Thanks to everyone for helping me to understand where it was the problem.

marcoratto commented 1 year ago

Solved.