Advertisement
hilmanzhy

Untitled

Sep 30th, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.92 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2. #include <WebSocketClient.h>
  3.  
  4. const char* ssid = "vascomm_ngalam";
  5. const char* password = "baksomalang";
  6. char path[] = "";
  7. char host[] = "192.168.172.19";
  8. char port = 5005;
  9.  
  10. WebSocketClient webSocketClient;
  11.  
  12. // Use WiFiClient class to create TCP connections
  13. WiFiClient client;
  14.  
  15. void setup() {
  16. Serial.begin(115200);
  17. delay(10);
  18.  
  19. // We start by connecting to a WiFi network
  20.  
  21. Serial.println();
  22. Serial.println();
  23. Serial.print("Connecting to ");
  24. Serial.println(ssid);
  25.  
  26. WiFi.begin(ssid, password);
  27.  
  28. while (WiFi.status() != WL_CONNECTED) {
  29. delay(500);
  30. Serial.print(".");
  31. }
  32.  
  33. Serial.println("");
  34. Serial.println("WiFi connected");
  35. Serial.println("IP address: ");
  36. Serial.println(WiFi.localIP());
  37.  
  38. delay(5000);
  39.  
  40.  
  41. // Connect to the websocket server
  42. if (client.connect(host, 5005)) {
  43. Serial.println(host);
  44. Serial.println("Server Connected");
  45. } else {
  46. Serial.println("Connection failed.");
  47. while(1) {
  48. // Hang on failure
  49. }
  50. }
  51.  
  52. // Handshake with the server
  53. webSocketClient.path = path;
  54. webSocketClient.host = host;
  55. int a;
  56. a:
  57. if (webSocketClient.handshake(client)) {
  58. Serial.println("Handshake successful");
  59. } else {
  60. Serial.println("Handshake failed.");
  61. while(1) {
  62. goto a;
  63. // Hang on failure
  64. }
  65. }
  66.  
  67. }
  68.  
  69.  
  70. void loop() {
  71. String data;
  72.  
  73. if (client.connected()) {
  74.  
  75. webSocketClient.getData(data);
  76. if (data.length() > 0) {
  77. Serial.print("Received data: ");
  78. Serial.println(data);
  79. }
  80.  
  81. // capture the value of analog 1, send it along
  82. pinMode(1, INPUT);
  83. data = String(analogRead(1));
  84.  
  85. webSocketClient.sendData(data);
  86.  
  87. } else {
  88. Serial.println("Client disconnected.");
  89. while (1) {
  90. // Hang on disconnect.
  91. }
  92. }
  93.  
  94. // wait to fully let the client disconnect
  95. delay(3000);
  96.  
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement