WebSocketClientSSLWithCA.ino 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * WebSocketClientSSLWithCA.ino
  3. *
  4. * Created on: 27.10.2019
  5. *
  6. * note SSL is only possible with the ESP8266
  7. *
  8. */
  9. #include <Arduino.h>
  10. #include <ESP8266WiFi.h>
  11. #include <ESP8266WiFiMulti.h>
  12. #include <WebSocketsClient.h>
  13. ESP8266WiFiMulti WiFiMulti;
  14. WebSocketsClient webSocket;
  15. #define USE_SERIAL Serial1
  16. // Can be obtained with:
  17. // openssl s_client -showcerts -connect echo.websocket.org:443 </dev/null
  18. const char ENDPOINT_CA_CERT[] PROGMEM = R"EOF(
  19. -----BEGIN CERTIFICATE-----
  20. MIIEkjCCA3qgAwIBAgIQCgFBQgAAAVOFc2oLheynCDANBgkqhkiG9w0BAQsFADA/MSQwIgYDVQQKExtEaWdpdGFsIFNpZ25hdHVyZSBUcnVzdCBDby4xFzAVBgNVBAMT
  21. DkRTVCBSb290IENBIFgzMB4XDTE2MDMxNzE2NDA0NloXDTIxMDMxNzE2NDA0NlowSjELMAkGA1UEBhMCVVMxFjAUBgNVBAoTDUxldCdzIEVuY3J5cHQxIzAhBgNVBAMT
  22. GkxldCdzIEVuY3J5cHQgQXV0aG9yaXR5IFgzMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAnNMM8FrlLke3cl03g7NoYzDq1zUmGSXhvb418XCSL7e4S0EF
  23. q6meNQhY7LEqxGiHC6PjdeTm86dicbp5gWAf15Gan/PQeGdxyGkOlZHP/uaZ6WA8SMx+yk13EiSdRxta67nsHjcAHJyse6cF6s5K671B5TaYucv9bTyWaN8jKkKQDIZ0
  24. Z8h/pZq4UmEUEz9l6YKHy9v6Dlb2honzhT+Xhq+w3Brvaw2VFn3EK6BlspkENnWAa6xK8xuQSXgvopZPKiAlKQTGdMDQMc2PMTiVFrqoM7hD8bEfwzB/onkxEz0tNvjj
  25. /PIzark5McWvxI0NHWQWM6r6hCm21AvA2H3DkwIDAQABo4IBfTCCAXkwEgYDVR0TAQH/BAgwBgEB/wIBADAOBgNVHQ8BAf8EBAMCAYYwfwYIKwYBBQUHAQEEczBxMDIG
  26. CCsGAQUFBzABhiZodHRwOi8vaXNyZy50cnVzdGlkLm9jc3AuaWRlbnRydXN0LmNvbTA7BggrBgEFBQcwAoYvaHR0cDovL2FwcHMuaWRlbnRydXN0LmNvbS9yb290cy9k
  27. c3Ryb290Y2F4My5wN2MwHwYDVR0jBBgwFoAUxKexpHsscfrb4UuQdf/EFWCFiRAwVAYDVR0gBE0wSzAIBgZngQwBAgEwPwYLKwYBBAGC3xMBAQEwMDAuBggrBgEFBQcC
  28. ARYiaHR0cDovL2Nwcy5yb290LXgxLmxldHNlbmNyeXB0Lm9yZzA8BgNVHR8ENTAzMDGgL6AthitodHRwOi8vY3JsLmlkZW50cnVzdC5jb20vRFNUUk9PVENBWDNDUkwu
  29. Y3JsMB0GA1UdDgQWBBSoSmpjBH3duubRObemRWXv86jsoTANBgkqhkiG9w0BAQsFAAOCAQEA3TPXEfNjWDjdGBX7CVW+dla5cEilaUcne8IkCJLxWh9KEik3JHRRHGJo
  30. uM2VcGfl96S8TihRzZvoroed6ti6WqEBmtzw3Wodatg+VyOeph4EYpr/1wXKtx8/wApIvJSwtmVi4MFU5aMqrSDE6ea73Mj2tcMyo5jMd6jmeWUHK8so/joWUoHOUgwu
  31. X4Po1QYz+3dszkDqMp4fklxBwXRsW10KXzPMTZ+sOPAveyxindmjkW8lGy+QsRlGPfZ+G6Z6h7mjem0Y+iWlkYcV4PIWL1iwBi8saCbGS5jN2p8M+X+Q7UNKEkROb3N6
  32. KOqkqm57TH2H3eDJAkSnh6/DNFu0Qg==
  33. -----END CERTIFICATE-----
  34. )EOF";
  35. void webSocketEvent(WStype_t type, uint8_t * payload, size_t length) {
  36. switch(type) {
  37. case WStype_DISCONNECTED:
  38. USE_SERIAL.printf("[WSc] Disconnected!\n");
  39. break;
  40. case WStype_CONNECTED:
  41. {
  42. USE_SERIAL.printf("[WSc] Connected to url: %s\n", payload);
  43. // send message to server when Connected
  44. webSocket.sendTXT("Connected");
  45. }
  46. break;
  47. case WStype_TEXT:
  48. USE_SERIAL.printf("[WSc] get text: %s\n", payload);
  49. // send message to server
  50. // webSocket.sendTXT("message here");
  51. break;
  52. case WStype_BIN:
  53. USE_SERIAL.printf("[WSc] get binary length: %u\n", length);
  54. hexdump(payload, length);
  55. // send data to server
  56. // webSocket.sendBIN(payload, length);
  57. break;
  58. }
  59. }
  60. void setup() {
  61. USE_SERIAL.begin(115200);
  62. USE_SERIAL.setDebugOutput(true);
  63. USE_SERIAL.println();
  64. USE_SERIAL.println();
  65. USE_SERIAL.println();
  66. for(uint8_t t = 4; t > 0; t--) {
  67. USE_SERIAL.printf("[SETUP] BOOT WAIT %d...\n", t);
  68. USE_SERIAL.flush();
  69. delay(1000);
  70. }
  71. WiFiMulti.addAP("SSID", "passpasspass");
  72. while(WiFiMulti.run() != WL_CONNECTED) {
  73. delay(100);
  74. }
  75. //When using BearSSL, client certificate and private key can be set:
  76. //webSocket.setSSLClientCertKey(clientCert, clientPrivateKey);
  77. //clientCert and clientPrivateKey can be of types (const char *, const char *) , or of types (BearSSL::X509List, BearSSL::PrivateKey)
  78. webSocket.beginSslWithCA("echo.websocket.org", 443, "/", ENDPOINT_CA_CERT);
  79. webSocket.onEvent(webSocketEvent);
  80. }
  81. void loop() {
  82. webSocket.loop();
  83. }