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
236 stars 125 forks source link

sinricpro with ESPAsyncWebServer #166

Closed smartswitchsio closed 3 years ago

smartswitchsio commented 3 years ago

How to use with ESPAsyncWebServer, kindly guide me to use with this web sockets server

kakopappa commented 3 years ago

https://techtutorialsx.com/2018/08/14/esp32-async-http-web-server-websockets-introduction/

On Fri, 26 Mar 2021 at 5:35 PM Rajasekar @.***> wrote:

How to use with ESPAsyncWebServer, kindly guide me to use with this web sockets server

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/sinricpro/esp8266-esp32-sdk/issues/166, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABZAZZRNPU5GO5PIIKAKZEDTFRPORANCNFSM4Z3EKW7Q .

smartswitchsio commented 3 years ago

Thanks for the reply, i am having to trouble to connect sinricpro using ESPAsyncWebServer, Kindly give basic example to connect with ESPAsyncWebServer

sivar2311 commented 3 years ago

Hi @smartswitchsio

~~There is no basic example. Right now, the SDK works with websockets library in normal mode. Switching to AsyncWebSocket is not (yet) implemented. Maybe this will become available in future.~~

sivar2311 commented 3 years ago

~~Update: As long as AsyncWebsocket does not support wss / SSL, this will not be implemented in the SinricPro SDK Please note https://github.com/Links2004/arduinoWebSockets#limitations-for-async~~

sivar2311 commented 3 years ago

Hi @smartswitchsio

I had to revert my last comments.

AsyncWebServer can be used without any issues.

smartswitchsio commented 3 years ago

please provide example (switch with relay) to use AsyncWebServer

sivar2311 commented 3 years ago

The library is about communication with SinricPro. You need to implement your own web server.

sivar2311 commented 3 years ago

Ok... here is an example how it could be done:


#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 <ESPAsyncWebServer.h>

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

#define WIFI_SSID         ""    
#define WIFI_PASS         ""

#define APP_KEY           ""      // Should look like "de0bxxxx-1x3x-4x3x-ax2x-5dabxxxxxxxx"
#define APP_SECRET        ""      // Should look like "5f36xxxx-x3x7-4x3x-xexe-e86724a9xxxx-4c4axxxx-3x3x-x5xe-x9x3-333d65xxxxxx"

#define SWITCH_ID         ""      // Should look like "5dc1564130xxxxxxxxxxxxxx"
#define SWITCH_PIN        LED_BUILTIN   

#define BAUD_RATE         115200                // Change baudrate to your need

SinricProSwitch &mySwitch = SinricPro[SWITCH_ID];
AsyncWebServer server(80);

bool myPowerState = false;

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

void webinterface(AsyncWebServerRequest *request) {
  if (!request->hasParam("state")) {
    String currentStateStr = myPowerState ? "on" : "off";
    String response = "<html><body>Switch is " + currentStateStr + " (<a href=\"/" SWITCH_ID "?state=on\">on</a> <a href=\"/" SWITCH_ID "?state=off\">off</a> <a href=\"/" SWITCH_ID "?state=flip\">flip</a>)</body></html>";
    request->send(200, "text/html", response);
  } else {
    AsyncWebParameter* param = request->getParam("state");
    bool newState;
    if (param->value() == "on") newState = true;
    if (param->value() == "off") newState = false;
    if (param->value() == "flip") newState = !myPowerState;
    if (newState != myPowerState) mySwitch.sendPowerStateEvent(myPowerState);
    myPowerState = newState;
    digitalWrite(SWITCH_PIN, myPowerState);
    request->redirect("/" SWITCH_ID);
  }
}

// setup function for WiFi connection
void setupWiFi() {
  Serial.printf("\r\n[Wifi]: Connecting");
  WiFi.begin(WIFI_SSID, WIFI_PASS);
  while (!WiFi.isConnected()) { Serial.printf("."); delay(100); };
  Serial.printf("connected!\r\n[WiFi]: IP-Address is %s\r\n", WiFi.localIP().toString().c_str());
}

// setup function for SinricPro
void setupSinricPro() {
  mySwitch.onPowerState(onPowerState);

  SinricPro.onConnected([](){ Serial.printf("Connected to SinricPro\r\n"); }); 
  SinricPro.onDisconnected([](){ Serial.printf("Disconnected from SinricPro\r\n"); });
  SinricPro.begin(APP_KEY, APP_SECRET);
}

void setupWebServer() {
  server.on("/" SWITCH_ID, HTTP_GET, webinterface);
  server.begin();
  Serial.printf("Open you web browser and navigate to http://%s/%s\r\n", WiFi.localIP().toString().c_str(), SWITCH_ID);
}

// main setup function
void setup() {
  pinMode(SWITCH_PIN, OUTPUT); // define LED GPIO as output

  Serial.begin(BAUD_RATE); Serial.printf("\r\n\r\n");
  setupWiFi();
  setupSinricPro();
  setupWebServer();
}

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

I think, this should give you a good starting point.

smartswitchsio commented 3 years ago

HI bro, Thanks for your example code.

stale[bot] commented 3 years ago

This issue has gone quiet. Spooky quiet. We currently close issues after 14 days of inactivity. It’s been at least 7 days since the last update here. If we missed this issue or if you want to keep it open, please reply here. As a friendly reminder, the best way to fix this or any other problem is to provide a detailed error description including a serial log. Thanks for being a part of the SinricPro community!

smartswitchsio commented 3 years ago

This above given example using two different websockets library, kindly give example only with AsyncWebSocket library.

sivar2311 commented 3 years ago

Sorry, the SDK is fixed to ArduinoWebsockets...

So it is not possible to use another one, except to rewrite the complete sdk.

sivar2311 commented 3 years ago

But it should be possible to use any websockets you want to use, regardless which one is used ineternally in the sdk.

smartswitchsio commented 3 years ago

Please give simple switch example only with AsyncWebSocket library

sivar2311 commented 3 years ago

I cant give you, because i dont have one. 2 questions:

  1. Do you want to use SinricPro without this library?
  2. What is your intention in using AsyncWebsockets?
smartswitchsio commented 3 years ago

I am using ESP8266 with AsyncWebsockets library to on/off relay with custom features. if i use SinricPro library with my program web page not loading. it's rebooting the device. I am very happy, if you give example to use only AsyncWebsockets library.

sivar2311 commented 3 years ago

If AsyncWebserver is running without issues, i cannot see why AsyncWebsocket is not.

Did you do any debugging to find out what is causing the crash?

smartswitchsio commented 3 years ago

This is the below error getting

ets Jan 8 2013,rst cause:4, boot mode:(3,7)

wdt reset load 0x4010f000, len 3584, room 16 tail 0 chksum 0xb0 csum 0xb0 v2843a5ac ~ld

sivar2311 commented 3 years ago

This is a boot message... not an error message and no debug information. The only reason i can see is the reboot reason "watchdog" (wdt). So, this is going to be completeley off-topic (original topic was about ESPAsyncWebserver).

Please do the 4 following steps:

  1. Close this issue because AsyncWebserver issue was solved...see my post above
  2. Open a new issue
  3. Share your code (create a git repository or use gist.github.com) so I will be able to reproduce the error you have
  4. Provide detailed informations (debug logs etc etc.)