Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ESP8266WiFi.h>
- #include <WebSocketClient.h>
- const char* ssid = "vascomm_ngalam";
- const char* password = "baksomalang";
- char path[] = "";
- char host[] = "192.168.172.19";
- char port = 5005;
- WebSocketClient webSocketClient;
- // Use WiFiClient class to create TCP connections
- WiFiClient client;
- void setup() {
- Serial.begin(115200);
- delay(10);
- // We start by connecting to a WiFi network
- Serial.println();
- Serial.println();
- Serial.print("Connecting to ");
- Serial.println(ssid);
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- }
- Serial.println("");
- Serial.println("WiFi connected");
- Serial.println("IP address: ");
- Serial.println(WiFi.localIP());
- delay(5000);
- // Connect to the websocket server
- if (client.connect(host, 5005)) {
- Serial.println(host);
- Serial.println("Server Connected");
- } else {
- Serial.println("Connection failed.");
- while(1) {
- // Hang on failure
- }
- }
- // Handshake with the server
- webSocketClient.path = path;
- webSocketClient.host = host;
- int a;
- a:
- if (webSocketClient.handshake(client)) {
- Serial.println("Handshake successful");
- } else {
- Serial.println("Handshake failed.");
- while(1) {
- goto a;
- // Hang on failure
- }
- }
- }
- void loop() {
- String data;
- if (client.connected()) {
- webSocketClient.getData(data);
- if (data.length() > 0) {
- Serial.print("Received data: ");
- Serial.println(data);
- }
- // capture the value of analog 1, send it along
- pinMode(1, INPUT);
- data = String(analogRead(1));
- webSocketClient.sendData(data);
- } else {
- Serial.println("Client disconnected.");
- while (1) {
- // Hang on disconnect.
- }
- }
- // wait to fully let the client disconnect
- delay(3000);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement