This is the ESP32 code I have. I have generated a .pem file using
openssl s_client -showcerts -connect hidden-thicket-03510.herokuapp.com:443
But still I have a problem connecting to the server using WiFisecureClient.
Can you please tell me what I have done wrong? My guess is that I have done something wrong when attaching the certificate.
Thanks for the help
#include <WiFiClientSecure.h>
#include <WebSocketClient.h>
#include <ArduinoJson.h>
const char* ssid = "##";
const char* password = "###";
char path[] = "/";
char host[] = "https://hidden-thicket-03510.herokuapp.com";
WebSocketClient webSocketClient;
WiFiClientSecure client;
const char* test_root_ca=
"-----BEGIN CERTIFICATE-----
"
"MIIDxTCCAq2gAwIBAgIQAqxcJmoLQJuPC3nyrkYldzANBgkqhkiG9w0BAQUFADBs
"
"MQswCQYDVQQGEwJVUzEVMBMGA1UEChMMRGlnaUNlcnQgSW5jMRkwFwYDVQQLExB3
"
"d3cuZGlnaWNlcnQuY29tMSswKQYDVQQDEyJEaWdpQ2VydCBIaWdoIEFzc3VyYW5j
"
"ZSBFViBSb290IENBMB4XDTA2MTExMDAwMDAwMFoXDTMxMTExMDAwMDAwMFowbDEL
"
"MAkGA1UEBhMCVVMxFTATBgNVBAoTDERpZ2lDZXJ0IEluYzEZMBcGA1UECxMQd3d3
"
"LmRpZ2ljZXJ0LmNvbTErMCkGA1UEAxMiRGlnaUNlcnQgSGlnaCBBc3N1cmFuY2Ug
"
"RVYgUm9vdCBDQTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAMbM5XPm
"
"+9S75S0tMqbf5YE/yc0lSbZxKsPVlDRnogocsF9ppkCxxLeyj9CYpKlBWTrT3JTW
"
"PNt0OKRKzE0lgvdKpVMSOO7zSW1xkX5jtqumX8OkhPhPYlG++MXs2ziS4wblCJEM
"
"xChBVfvLWokVfnHoNb9Ncgk9vjo4UFt3MRuNs8ckRZqnrG0AFFoEt7oT61EKmEFB
"
"Ik5lYYeBQVCmeVyJ3hlKV9Uu5l0cUyx+mM0aBhakaHPQNAQTXKFx01p8VdteZOE3
"
"hzBWBOURtCmAEvF5OYiiAhF8J2a3iLd48soKqDirCmTCv2ZdlYTBoSUeh10aUAsg
"
"EsxBu24LUTi4S8sCAwEAAaNjMGEwDgYDVR0PAQH/BAQDAgGGMA8GA1UdEwEB/wQF
"
"MAMBAf8wHQYDVR0OBBYEFLE+w2kD+L9HAdSYJhoIAu9jZCvDMB8GA1UdIwQYMBaA
"
"FLE+w2kD+L9HAdSYJhoIAu9jZCvDMA0GCSqGSIb3DQEBBQUAA4IBAQAcGgaX3Nec
"
"nzyIZgYIVyHbIUf4KmeqvxgydkAQV8GK83rZEWWONfqe/EW1ntlMMUu4kehDLI6z
"
"eM7b41N5cdblIZQB2lWHmiRk9opmzN6cN82oNLFpmyPInngiK3BD41VHMWEZ71jF
"
"hS9OMPagMRYjyOfiZRYzy78aG6A9+MpeizGLYAiJLQwGXFK3xPkKmNEVX58Svnw2
"
"Yzi9RKR/5CYrCsSXaQ3pjOLAEFe4yHYSkVXySGnYvCoCWw9E1CAx2/S6cCZdkGCe
"
"vEsXCS+0yx5DaMkHJ8HSXPfqIbloEpw8nL+e/IBcm2PN7EeqJSdnoDfzAIJ9VNep
"
"+OkuE6N36B9K
"
"-----END CERTIFICATE-----
";
int timer=0;
void connnect() {
if (client.connect(host,443)) {
Serial.println("Connected");
} else {
Serial.println("Connection failed.");
}
webSocketClient.path = path;
webSocketClient.host = host;
if (webSocketClient.handshake(client)) {
Serial.println("Handshake successful");
} else {
Serial.println("Handshake failed.");
}
}
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
delay(5000);
client.setCACert(test_root_ca);
connnect();
if (client.connected()) {
webSocketClient.sendData("Info to be echoed back");
}
}
void loop() {
String data;
if (client.connected()) {
webSocketClient.getData(data);
Serial.println(data);
int data_len = data.length() + 1;
char char_array[data_len];
data.toCharArray(char_array, data_len);
StaticJsonDocument<1200> doc;
const char* a=doc["message"];
if (data_len > 1) {
Serial.print("Received data: ");
Serial.println(a);
}
} else {
Serial.println("Client disconnected.");
connnect();
}
delay(3000);
}
If it would be of any help, this is the server code running on Heroku:
var WebSocketServer = require('websocket').server;
var http = require('http');
var server = http.createServer(function(request, response) {
console.log((new Date()) + ' Received request for ' + request.url);
response.writeHead(404);
response.end();
});
let port =process.env.PORT || 5000;
server.listen(port, function() {
console.log((new Date()) + ' Server is listening on port 5000');
});
wsServer = new WebSocketServer({
httpServer: server
});
const clients={}
wsServer.on('request', request=> {
var connection = request.accept(null, request.origin);
console.log((new Date()) + ' Connection accepted.');
const clientId=guid();
clients[clientId]={
"connection":connection
};
// connection.sendUTF("JSON.stringify(payload)")
connection.on('open',()=>{console.log("opened")})
connection.on('message', message => {
console.log(message);
var a=JSON.stringify({'message':'sdaed'})
connection.send(a)
});
connection.on('close', (reasonCode, description) =>{
console.log((new Date()) + ' Peer ' + connection.remoteAddress + ' disconnected.');
});
const payload={
"method":"connect",
"clientId":clientId
}
});
const guid=()=> {
const s4=()=> Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1);
return `${s4() + s4()}-${s4()}-${s4()}-${s4()}-${s4() + s4() + s4()}`;
}
The Serial output is,
Connected
Waiting...
Waiting...
Waiting...
Handshake failed.
Client disconnected.
Connected
Waiting...
Waiting...
Waiting...
Handshake failed.
Client disconnected.
Connected
Waiting...
Waiting...
Waiting...
Waiting...
Waiting...
Handshake failed.
question from:https://stackoverflow.com/questions/65942740/problem-connecting-to-websocket-server-deployed-to-heroku-from-esp32