WebSocketClientSSL.ino 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * WebSocketClientSSL.ino
  3. *
  4. * Created on: 10.12.2015
  5. *
  6. * note SSL is only possible with the ESP8266
  7. *
  8. */
  9. #include <Arduino.h>
  10. #include <WiFi.h>
  11. #include <WiFiMulti.h>
  12. #include <WiFiClientSecure.h>
  13. #include <WebSocketsClient.h>
  14. WiFiMulti WiFiMulti;
  15. WebSocketsClient webSocket;
  16. #define USE_SERIAL Serial1
  17. void hexdump(const void *mem, uint32_t len, uint8_t cols = 16) {
  18. const uint8_t* src = (const uint8_t*) mem;
  19. USE_SERIAL.printf("\n[HEXDUMP] Address: 0x%08X len: 0x%X (%d)", (ptrdiff_t)src, len, len);
  20. for(uint32_t i = 0; i < len; i++) {
  21. if(i % cols == 0) {
  22. USE_SERIAL.printf("\n[0x%08X] 0x%08X: ", (ptrdiff_t)src, i);
  23. }
  24. USE_SERIAL.printf("%02X ", *src);
  25. src++;
  26. }
  27. USE_SERIAL.printf("\n");
  28. }
  29. void webSocketEvent(WStype_t type, uint8_t * payload, size_t length) {
  30. switch(type) {
  31. case WStype_DISCONNECTED:
  32. USE_SERIAL.printf("[WSc] Disconnected!\n");
  33. break;
  34. case WStype_CONNECTED:
  35. {
  36. USE_SERIAL.printf("[WSc] Connected to url: %s\n", payload);
  37. // send message to server when Connected
  38. webSocket.sendTXT("Connected");
  39. }
  40. break;
  41. case WStype_TEXT:
  42. USE_SERIAL.printf("[WSc] get text: %s\n", payload);
  43. // send message to server
  44. // webSocket.sendTXT("message here");
  45. break;
  46. case WStype_BIN:
  47. USE_SERIAL.printf("[WSc] get binary length: %u\n", length);
  48. hexdump(payload, length);
  49. // send data to server
  50. // webSocket.sendBIN(payload, length);
  51. break;
  52. case WStype_ERROR:
  53. case WStype_FRAGMENT_TEXT_START:
  54. case WStype_FRAGMENT_BIN_START:
  55. case WStype_FRAGMENT:
  56. case WStype_FRAGMENT_FIN:
  57. break;
  58. }
  59. }
  60. void setup() {
  61. // USE_SERIAL.begin(921600);
  62. USE_SERIAL.begin(115200);
  63. //Serial.setDebugOutput(true);
  64. USE_SERIAL.setDebugOutput(true);
  65. USE_SERIAL.println();
  66. USE_SERIAL.println();
  67. USE_SERIAL.println();
  68. for(uint8_t t = 4; t > 0; t--) {
  69. USE_SERIAL.printf("[SETUP] BOOT WAIT %d...\n", t);
  70. USE_SERIAL.flush();
  71. delay(1000);
  72. }
  73. WiFiMulti.addAP("SSID", "passpasspass");
  74. //WiFi.disconnect();
  75. while(WiFiMulti.run() != WL_CONNECTED) {
  76. delay(100);
  77. }
  78. webSocket.beginSSL("192.168.0.123", 81);
  79. webSocket.onEvent(webSocketEvent);
  80. }
  81. void loop() {
  82. webSocket.loop();
  83. }