Open amprodes opened 5 years ago
Hi... need help this is not working, server as client get disconnected always, please help
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <WebSocketsServer.h>
#include <WebSocketsClient.h>
#include <Hash.h>
#ifndef APSSID
#define APSSID "wifiSSID"
#define APPSK "*******"
#endif
WebSocketsServer webSocket(81); // Create a websocket server on port 81
WebSocketsClient webSocketCli;
const char *ssid = APSSID;
const char *password = APPSK;
void event(const char * payload, size_t length) {
Serial.printf("got message: %s\n", payload);
}
void startWiFi() {
Serial.println();
Serial.print("Setting WIFI_AP … ");
Serial.println(WiFi.mode(WIFI_AP) ? "Ready" : "Failed!");
Serial.print("Setting soft-AP … ");
Serial.println(WiFi.softAP(ssid, password) ? "Ready" : "Failed!");
Serial.print("Soft-AP IP address =");
Serial.println(WiFi.softAPIP());
}
void startWebSocket() {
webSocket.begin();
webSocket.onEvent(webSocketEvent);
}
void startWebSocketClient() {
webSocketCli.begin("192.168.4.1", 81, "/");
webSocketCli.onEvent(webSocketEventCli);
webSocketCli.setReconnectInterval(5000);
webSocketCli.enableHeartbeat(15000, 3000, 2);
}
void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t lenght) { // When a WebSocket message is received
switch (type) {
case WStype_DISCONNECTED:
Serial.printf("[%u] Disconnected!\n", num);
break;
case WStype_CONNECTED: {
IPAddress ip = webSocket.remoteIP(num);
Serial.printf("\"[%u] Connected from %d.%d.%d.%d url: %s\n \"", num, ip[0], ip[1], ip[2], ip[3], payload);
// send message to client
webSocket.sendTXT(num, " Conectado");
}
break;
case WStype_TEXT:
Serial.printf("[%u] get Text: %s\n", num, payload);
webSocket.sendTXT(num, payload);
break;
case WStype_ERROR:
Serial.printf("[%u] get Error: %s\n", num, payload);
break;
case WStype_FRAGMENT_TEXT_START:
case WStype_FRAGMENT_BIN_START:
case WStype_FRAGMENT:
case WStype_FRAGMENT_FIN:
case WStype_BIN:
default:
break;
}
}
void webSocketEventCli(WStype_t type, uint8_t * payload, size_t length) {
switch(type) {
case WStype_DISCONNECTED:
Serial.printf("[WSc] Disconnected!\n");
break;
case WStype_CONNECTED:
{
Serial.printf("[WSc] Connected to url: %s\n", payload);
webSocketCli.sendTXT("Clien from Esp8266 is Connected");
}
break;
case WStype_TEXT:
Serial.printf("[WSc] get text: %s\n", payload);
webSocketCli.sendTXT("got message too");
break;
case WStype_BIN:
Serial.printf("[WSc] get binary length: %u\n", length);
hexdump(payload, length);
break;
case WStype_PING:
Serial.printf("[WSc] get ping\n");
break;
case WStype_PONG:
Serial.printf("[WSc] get pong\n");
break;
}
}
void setup() {
Serial.begin(115200);
delay(5000);
Serial.println();
startWiFi();
startWebSocket();
startWebSocketClient();
for(uint8_t t = 4; t > 0; t--) {
Serial.printf("[SETUP] BOOT WAIT %d...\n", t);
Serial.flush();
delay(1000);
}
}
void loop() {
webSocket.loop();
webSocketCli.loop();
delay(50); // Give the ESP time to handle network...
webSocketCli.sendTXT("...");
if (Serial.available()) {
Serial.setTimeout(100);
String s = Serial.readStringUntil('\n');
//Serial.write(s.c_str());
Serial.printf(s.c_str());
if (s.startsWith("node: ")) {
String data;
int end = s.indexOf('\r');
if (end > 0) {
data = s.substring(6, end);
} else {
data = s.substring(6);
}
webSocketCli.sendTXT(data.c_str());
}
}
}
Could you attach a log?
yes it can, only local loop back connection is not supported (TCP stack limitation).
see #136 and #401 for more details and example code