I'm currently using an ESP32 under Arduino IDE to generate and send 33 bytes of data via WiFi (TCP Packets). The ESP32 is configured as an Access Point, so I'm connecting from my PC to this AP and the data is sent when the ESP32 receives an 'b'. The problem is that after like 2 mins, the sending stops while my code is still running (I print something on the Serial port to see that). I checked Wireshark and I noticed that the ESP32 sends randomly a packet containing FIN flag (so I guess it stops the communication without any reason).
#include <WiFi.h>
#include <SPI.h>
#include <WiFiClient.h>
#include <WiFiAP.h>
#include <WebServer.h>
#include <ESPmDNS.h>
#include <ArduinoJson.h>
#define JSON_BUFFER_SIZE 1024
#define PI 3.1415926535
// Set these to your desired credentials.
const char *ssid = "WiFi AP";
const char *password = "abcd1234";
unsigned long long timing,n=0;
WiFiServer server(80);
unsigned long startMillis; //some global variables available anywhere in the program
unsigned long currentMillis;
byte mess[]={0xA0,0,1,1,1,2,2,2,3,3,3,4,4,4,5,5,5,6,6,6,7,7,7,8,8,8,10,10,15,15,50,50,0xC0};
int val_finale;
int sin1,sin2;
int f1=20;
int Fe=250;
float Te=1.0/Fe;
size_t wifi_latency = 0;
IPAddress local_ip(192, 168, 4, 1);
IPAddress network_gateway(192, 168, 4, 1);
IPAddress subnet_mask(255, 255, 255, 0);
WebServer web_server(80);
WiFiClient tcp_client;
bool streaming_enabled = false;
uint8_t channel_setting_buffer[8] = {0};
void get_system_info()
{
DynamicJsonDocument json_document(JSON_BUFFER_SIZE);
JsonObject json_object = json_document.to<JsonObject>();
json_object["board_connected"] = true;
json_object["heap"] = ESP.getFreeHeap();
json_object["ip"] = WiFi.softAPIP().toString();
json_object["latency"] = wifi_latency;
json_object["mac"] = WiFi.softAPmacAddress();
json_object["name"] = "OpenBCI-FFFF";
json_object["num_channels"] = 8;
json_object["version"] = "v2.0.5";
String json_string = "";
serializeJson(json_document, json_string);
web_server.send(200, "text/json", json_string);
}
size_t gain_from_channel(uint8_t channel_index)
{
return 1;
}
void get_board_info()
{
DynamicJsonDocument json_document(JSON_BUFFER_SIZE);
JsonObject json_object = json_document.to<JsonObject>();
json_object["board_connected"] = true;
json_object["board_type"] = "cyton";
JsonArray gains = json_object.createNestedArray("gains");
for (size_t channel_index = 0; channel_index < 8; channel_index++) gains.add(gain_from_channel(channel_index));
json_object["num_channels"] = 8;
String json_string = "";
serializeJson(json_document, json_string);
web_server.send(200, "text/json", json_string);
}
uint8_t digit_from_char(char digit_char)
{
return digit_char - '0';
}
void process_command()
{
DynamicJsonDocument json_document(JSON_BUFFER_SIZE);
deserializeJson(json_document, web_server.arg(0));
JsonObject json_object = json_document.as<JsonObject>();
String command = json_object["command"];
String return_message = "";
bool streaming_state = streaming_enabled;
streaming_enabled = false;
delayMicroseconds(50);
if (command[0] == '~')
{
uint8_t sampling_rate = digit_from_char(command[1]);
return_message = "Success: Sample rate is now ";
return_message += "250";
return_message += "Hz";
}
else if (command == "b") streaming_state = true;
else if (command == "s") streaming_state = false;
streaming_enabled = streaming_state;
web_server.send(200, "text/plain", return_message);
}
void start_streaming()
{
streaming_enabled = true;
web_server.send(200);
}
void stop_streaming()
{
streaming_enabled = false;
web_server.send(200);
}
void get_tcp_config()
{
DynamicJsonDocument json_document(JSON_BUFFER_SIZE);
JsonObject json_object = json_document.to<JsonObject>();
json_object["connected"] = (tcp_client.connected() != 0) ? true : false;
json_object["delimiter"] = false;
json_object["ip_address"] = tcp_client.remoteIP().toString();
json_object["output"] = "raw";
json_object["port"] = tcp_client.remotePort();
json_object["latency"] = wifi_latency;
String json_string = "";
serializeJson(json_document, json_string);
web_server.send(200, "text/json", json_string);
}
IPAddress ip_from_string(String ip_string)
{
IPAddress ip_address(0, 0, 0, 0);
ip_address.fromString(ip_string);
return ip_address;
}
void set_tcp_config()
{
streaming_enabled = false;
DynamicJsonDocument json_document(JSON_BUFFER_SIZE);
deserializeJson(json_document, web_server.arg(0));
JsonObject json_object = json_document.as<JsonObject>();
String tcp_client_ip = json_object["ip"];
wifi_latency = json_object["latency"];
uint16_t tcp_client_port = json_object["port"];
tcp_client.stop();
tcp_client.connect(ip_from_string(tcp_client_ip), tcp_client_port);
tcp_client.setNoDelay(1);
get_tcp_config();
}
void stop_tcp_connection()
{
streaming_enabled = false;
tcp_client.stop();
get_tcp_config();
}
void invalid_request()
{
web_server.send(404, "text/plain", "Invalid Request!");
}
void switch_raw_output()
{
web_server.send(200, "text/plain", "Output mode configured to raw");
}
void gen_sinus(unsigned char *mess, int n)
{
sin1 = (int)(10000*sin(2*PI*f1*n*Te)+10000);
mess[1]=n;
mess[2]=((unsigned char)(sin1>>16));
mess[3]=((unsigned char)(sin1>>8));
mess[4]=((unsigned char)sin1);
}
void setup() {
Serial.begin(115200);
Serial.println();
Serial.println("Configuring access point...");
WiFi.mode(WIFI_AP);
WiFi.softAP(ssid, password);
delay(250);
WiFi.softAPConfig(local_ip, network_gateway, subnet_mask);
delay(250);
MDNS.begin("openbci");
web_server.on("/all", HTTP_GET, get_system_info);
web_server.on("/board", HTTP_GET, get_board_info);
web_server.on("/command", HTTP_POST, process_command);
web_server.on("/stream/start", HTTP_GET, start_streaming);
web_server.on("/stream/stop", HTTP_GET, start_streaming);
web_server.on("/output/raw", HTTP_GET, switch_raw_output);
web_server.on("/tcp", HTTP_GET, get_tcp_config);
web_server.on("/tcp", HTTP_POST, set_tcp_config);
web_server.on("/tcp", HTTP_DELETE, stop_tcp_connection);
web_server.onNotFound(invalid_request);
MDNS.addService("http", "tcp", 80);
web_server.begin();
}
void loop() {
if(streaming_enabled == true)
{
gen_sinus(mess,n);
n++;
tcp_client.write(mess,33);
//Serial.println(n);
delay(10);
}
web_server.handleClient();
}
How can I solve this ? PS: I dont know much about communication protocols.