You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi, so I have an issue when attempting to communicate between two ESP8266s each part of a NodeMCU 1.0 dev kit. I have one configured as a web socket client which reads serial input, and sends it as binary data to the other, which is configured as a web socket server and writes to serial the data it receives. The program works fine when it has just started, but soon shows problems like delays in writing the received data, the first byte is written instantly but the rest are delayed by a half second or so, or the first byte is the only written data at all.
I am using Script Communicator and a UART to send test data, so for example I am currently sending '001 002 003 004 055' as test data and the output is either '001 002 003 004 055', '001' followed by '002 003 004 055' after a delay, or just '001'. Rarely also there will be no output at all.
I have attached the code below. I am running in Async mode and using the examples provided in this git as a base
Server Code:
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <WebSocketsServer.h>
#include <Hash.h>
WebSocketsServer webSocket = WebSocketsServer(81); // create a websocket server on port 81
#define USE_SERIAL Serial1
uint8_t * blank;
void setup() {
delay(1000);
Serial.begin(57600);
Serial1.begin(57600);
startWifi();
webSocket.begin();
webSocket.onEvent(webSocketEvent);
}
void loop() {
delay(0);
}
void startWifi() { // Start a WebSocket server
Serial.println();
Serial.print("Configuring access point...");
WiFi.softAP("esptest");
IPAddress myIP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(myIP);
}
void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t length) {
switch(type) {
case WStype_DISCONNECTED:
USE_SERIAL.printf("[%u] Disconnected!\n", num);
break;
case WStype_CONNECTED:
{
IPAddress ip = webSocket.remoteIP(num);
USE_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, "Connected");
}
break;
case WStype_TEXT:
USE_SERIAL.printf("[%u] get Text: %s\n", num, payload);
// send message to client
//webSocket.sendTXT(num, "confirm");
// send data to all connected clients
// webSocket.broadcastTXT("message here");
break;
case WStype_BIN:
/*for (int i = 0; i < length; i++) {
Serial.print(payload[i]);
}*/
Serial.write(payload, (length/4 + (length % 4 != 0)));
break;
}
}
Client code:
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <WebSocketsClient.h>
#include <Hash.h>
int incomingByte = 0;
uint8_t * buf;
size_t buf_len;
WebSocketsClient webSocket;
#define USE_SERIAL Serial1
const char* ssid = "esptest";
const char * host = "192.168.4.1";
void webSocketEvent(WStype_t type, uint8_t * payload, size_t length) {
switch(type) {
case WStype_DISCONNECTED:
//USE_SERIAL.printf("[WSc] Disconnected!\n");
break;
case WStype_CONNECTED: {
//USE_SERIAL.printf("[WSc] Connected to url: %s\n", payload);
// send message to server when Connected
//webSocket.sendTXT("Connected");
}
break;
case WStype_TEXT:
//USE_SERIAL.printf("[WSc] get text: %s\n", payload);
// send message to server
//webSocket.sendBIN(buf, buf_len);
break;
case WStype_BIN:
break;
}
}
void setup() {
Serial.begin(57600);
Serial1.begin(57600);
delay(10);
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
// server address, port and URL
webSocket.begin(host, 81, "/");
// event handler
webSocket.onEvent(webSocketEvent);
// try ever 5000 again if connection has failed
webSocket.setReconnectInterval(5000);
}
void loop() {
if (Serial.available() > 0) {
incomingByte = Serial.read();
buf = (uint8_t *) &incomingByte;
buf_len = sizeof(incomingByte);
delay(0);
webSocket.sendBIN(buf, buf_len);
delay(0);
}
}
Any help would be appreciated. Thank you.
The text was updated successfully, but these errors were encountered:
Hi, so I have an issue when attempting to communicate between two ESP8266s each part of a NodeMCU 1.0 dev kit. I have one configured as a web socket client which reads serial input, and sends it as binary data to the other, which is configured as a web socket server and writes to serial the data it receives. The program works fine when it has just started, but soon shows problems like delays in writing the received data, the first byte is written instantly but the rest are delayed by a half second or so, or the first byte is the only written data at all.
I am using Script Communicator and a UART to send test data, so for example I am currently sending '001 002 003 004 055' as test data and the output is either '001 002 003 004 055', '001' followed by '002 003 004 055' after a delay, or just '001'. Rarely also there will be no output at all.
I have attached the code below. I am running in Async mode and using the examples provided in this git as a base
Server Code:
Client code:
Any help would be appreciated. Thank you.
The text was updated successfully, but these errors were encountered: