WebSocketClientAVR.ino 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. * WebSocketClientAVR.ino
  3. *
  4. * Created on: 10.12.2015
  5. *
  6. */
  7. #include <Arduino.h>
  8. #include <SPI.h>
  9. #include <Ethernet.h>
  10. #include <WebSocketsClient.h>
  11. // Enter a MAC address for your controller below.
  12. // Newer Ethernet shields have a MAC address printed on a sticker on the shield
  13. byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
  14. // Set the static IP address to use if the DHCP fails to assign
  15. IPAddress ip(192, 168, 0, 177);
  16. WebSocketsClient webSocket;
  17. void webSocketEvent(WStype_t type, uint8_t * payload, size_t length) {
  18. switch(type) {
  19. case WStype_DISCONNECTED:
  20. Serial.println("[WSc] Disconnected!\n");
  21. break;
  22. case WStype_CONNECTED:
  23. {
  24. Serial.print("[WSc] Connected to url: ");
  25. Serial.println((char *)payload);
  26. // send message to server when Connected
  27. webSocket.sendTXT("Connected");
  28. }
  29. break;
  30. case WStype_TEXT:
  31. Serial.print("[WSc] get text: ");
  32. Serial.println((char *)payload);
  33. // send message to server
  34. // webSocket.sendTXT("message here");
  35. break;
  36. case WStype_BIN:
  37. Serial.print("[WSc] get binary length: ");
  38. Serial.println(length);
  39. // hexdump(payload, length);
  40. // send data to server
  41. // webSocket.sendBIN(payload, length);
  42. break;
  43. }
  44. }
  45. void setup()
  46. {
  47. // Open serial communications and wait for port to open:
  48. Serial.begin(115200);
  49. while (!Serial) {}
  50. // start the Ethernet connection:
  51. if (Ethernet.begin(mac) == 0) {
  52. Serial.println("Failed to configure Ethernet using DHCP");
  53. // no point in carrying on, so do nothing forevermore:
  54. // try to congifure using IP address instead of DHCP:
  55. Ethernet.begin(mac, ip);
  56. }
  57. webSocket.begin("192.168.0.123", 8011);
  58. webSocket.onEvent(webSocketEvent);
  59. }
  60. void loop()
  61. {
  62. webSocket.loop();
  63. }