WebSocketClientSSL.ino 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 <ESP8266WiFi.h>
  11. #include <ESP8266WiFiMulti.h>
  12. #include <WebSocketsClient.h>
  13. #include <Hash.h>
  14. ESP8266WiFiMulti WiFiMulti;
  15. WebSocketsClient webSocket;
  16. #define USE_SERIAL Serial1
  17. void webSocketEvent(WStype_t type, uint8_t * payload, size_t length) {
  18. switch(type) {
  19. case WStype_DISCONNECTED:
  20. USE_SERIAL.printf("[WSc] Disconnected!\n");
  21. break;
  22. case WStype_CONNECTED:
  23. {
  24. USE_SERIAL.printf("[WSc] Connected to url: %s\n", payload);
  25. // send message to server when Connected
  26. webSocket.sendTXT("Connected");
  27. }
  28. break;
  29. case WStype_TEXT:
  30. USE_SERIAL.printf("[WSc] get text: %s\n", payload);
  31. // send message to server
  32. // webSocket.sendTXT("message here");
  33. break;
  34. case WStype_BIN:
  35. USE_SERIAL.printf("[WSc] get binary length: %u\n", length);
  36. hexdump(payload, length);
  37. // send data to server
  38. // webSocket.sendBIN(payload, length);
  39. break;
  40. }
  41. }
  42. void setup() {
  43. // USE_SERIAL.begin(921600);
  44. USE_SERIAL.begin(115200);
  45. //Serial.setDebugOutput(true);
  46. USE_SERIAL.setDebugOutput(true);
  47. USE_SERIAL.println();
  48. USE_SERIAL.println();
  49. USE_SERIAL.println();
  50. for(uint8_t t = 4; t > 0; t--) {
  51. USE_SERIAL.printf("[SETUP] BOOT WAIT %d...\n", t);
  52. USE_SERIAL.flush();
  53. delay(1000);
  54. }
  55. WiFiMulti.addAP("SSID", "passpasspass");
  56. //WiFi.disconnect();
  57. while(WiFiMulti.run() != WL_CONNECTED) {
  58. delay(100);
  59. }
  60. webSocket.beginSSL("192.168.0.123", 81);
  61. webSocket.onEvent(webSocketEvent);
  62. }
  63. void loop() {
  64. webSocket.loop();
  65. }