Skip to content

Issue with WSS and ESP 32 #152

Description

@Titus002

I have an Issue with WSS.
I am Trying to connect to an API for robotic lawn Mowers with an ESP 32. For that I made a get Request to an authentication API which provides me an authentication token. (His part works fine)

Now I need to use this Token to connect to the Websocket Sever via Websocket Secure, but this doesn't work.
Here is the Documentation of the API:

I added Headers which are Requestet from the API and set the SSL certificate which I got with: openssl s_client -showcerts -connect wss://ws.openapi.husqvarna.dev/v1
Iam not getting an SSL ERROR the Websocket just doesn't connects, and getCloseReason gives me 1002.
Maybe someone could help me, that would be awesome.

Here ist my Code:
`#include <Arduino.h>
#include <WiFi.h>
#include <HTTPClient.h>
#include <WiFiClientSecure.h>
#include <config.h>
#include <ArduinoJson.h>
#include <authentificationApi.h>
#include
#include
#include <connectApi.h>
#include <ArduinoWebsockets.h>

String provider;
String token_type;
String access_token;

AuthentificationApi authentificationApi(urlAuthentificationServer, rootCACertificateAuthentificationServer, applicationKey, applicationSecret);

using namespace websockets;
WebsocketsClient client;

//Time set for HTTP
void setClock(){
configTime(0, 0, "pool.ntp.org");
Serial.print(F("Waiting for NTP time sync: "));
time_t nowSecs = time(nullptr);
while (nowSecs < 8 * 3600 * 2) {
delay(500);
Serial.print(F("."));
yield();
nowSecs = time(nullptr);
}
Serial.println();
struct tm timeinfo;
gmtime_r(&nowSecs, &timeinfo);
Serial.print(F("Current time: "));
Serial.print(asctime(&timeinfo));
}

// WiFi Connection
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
Serial.println("Connecting to WIFI: " + String(ssid));
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println();
Serial.println("WiFi connected");
}

void loop() {
// get Data from Authentification API
std::map<String, String> authentificationList = authentificationApi.getAuthentification();
Serial.println(authentificationList["accessToken"]);
Serial.println(authentificationList["scope"]);
Serial.println(authentificationList["expires_in"]);
Serial.println(authentificationList["provider"]);
Serial.println(authentificationList["user_id"]);
Serial.println(authentificationList["token_type"]);

provider = authentificationList["provider"];
token_type =authentificationList["token_type"];
access_token = authentificationList["accessToken"];

client.setCACert(ssl_cert0); //set SSL certificate
client.addHeader("Authorization: Bearer ", access_token); //set Headers so the API allows Connection
client.addHeader("Authorization-Provider: ", "husqvarna");
client.addHeader("X-Api-Key: ", "xxxxx-xxx-xxxxxx");
bool connected = client.connect("wss://ws.openapi.husqvarna.dev/v1");
Serial.println(client.getCloseReason()); //get Close reason gives me 1002
if(client.available()){
Serial.println("Client is avalable");
}

if (connected) {
Serial.println("Connected");
} else {
Serial.println("Connection failed.");
}

client.poll();

Serial.println("Waiting 1000000s before the next round...");
delay(1000000);
}

`

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions