ESP32 tcp server-client problem
Posted: Sun Nov 24, 2019 8:32 pm
Hello, I try to build a remote sensor with esp32. I have two tasks, first with sensor management and second with TCP server, and with this server I have some troubles. It runs like a server , waits for data from my PC , and I can even read message from PC via but when I read data from the first task and then tried to send it by WiFi, I can't.
MY code:
I also tried to use client.connect(IP,port) method to establish a connection with PC but then I get "Software caused connection abort"- error. And then I still dont know how to handle with it. Any hints?
Code: Select all
wifiServer.begin()
Code: Select all
WiFiClient client=wifiServer.avilable()
Code: Select all
client.available()
MY code
Code: Select all
const char* ssid = "sensor";
const char* password = "sensor1";
const int tcpPort = 56606;
const char* host = "192.168.137.1";
WiFiServer wifiServer(tcpPort);
void setup() {
//Connect to the WiFi network
WiFi.begin(ssid, password);
Serial.println("");
// Wait for connection
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
/*This initializes wifi server*/
wifiServer.begin();
/* create the queue which size can contains 5 elements of Data */
xQueue1 = xQueueCreate(1000, sizeof(Data));
xQueue2 = xQueueCreate(5, sizeof(Commands));
void loop()
{
vTaskDelay(20);
}
<here should be the first task but it works fine and it not related with wifi>
void sendTask( void * parameter )
{
/*basic variables of freertos to keep data transfer status +create a struct dane*/
BaseType_t xStatus;
const TickType_t xTicksToWait = pdMS_TO_TICKS(5);
Data dane;
Commands message;
int j=1;
int allSamples=0;
bool Read_flag=false;
int sampleNumber=0;
/* JSON parser initialization*/
StaticJsonDocument<1900> doc;
for(;;){
WiFiClient client = wifiServer.available();
if(!Read_flag)
{
uint8_t data[100];
if (client)
{ Serial.println(client);
TIMERG0.wdt_wprotect=TIMG_WDT_WKEY_VALUE;
TIMERG0.wdt_feed=1;
TIMERG0.wdt_wprotect=0;
Serial.println("new client");
/*check client is connected */
if (client.connected())
{
if (client.available()) {
int len = client.read(data, 100);
if(len < 100)
{
data[len] = '\0';
}
else
{
data[100] = '\0';
}
Serial.print("client sent: ");
Serial.println((char *)data);
delay(20);
const int capacity = 200;
StaticJsonDocument<capacity> command;
DeserializationError err = deserializeJson(command,data);
if(err)
{
Serial.print(F("deserializeJson() failed with code "));
Serial.println(err.c_str());
}
else
{
serializeJson(command,Serial);
Serial.println();
Read_flag=true;
delay(10);
int accel= command["accel"];
int gyro = command["gyro"];
int magnet = command["magnet"];
int time = command["time"];
Serial.println(accel);
Serial.println(gyro);
Serial.println(time);
setupSensor(accel,gyro,magnet);
delay(100);
Serial.println("sensor range set");
message.measureTime=time;
message.flag=true;
allSamples=time*50;
xStatus =xQueueSendToFront(xQueue2, &message, xTicksToWait);
Serial.println("command sent");
delay(100);
}
}
client.println("I got message");
client.stop();
}
}
}
if(Read_flag)
{
<some boring stuff with Json parser>
xStatus = xQueueReceive( xQueue1, &dane, xTicksToWait );
if(xStatus==pdPASS)
{
serializeJson(doc,Serial);
doc.clear();
sampleNumber=sampleNumber+5;
if(client)
{Serial.println("we have clinet");
if (client.connected())
{client.println("my JSON Stuff");
}
}
}
}
}
I also tried to use client.connect(IP,port) method to establish a connection with PC but then I get "Software caused connection abort"- error. And then I still dont know how to handle with it. Any hints?