TCP socket Server using esp8266 Arduino Core
Posted: Fri Mar 11, 2016 8:51 am
Greetings,
I've been trying to setup a tcp socket server with the arduino ide (using the esp8266 arduino core) and the WifiManager library to make an easy setup on different networks. The code is simple enough, I set up the server and wait for a client. If a client connects and sends data to the server that data must be sent through the serial port. Here's the code:
After a full day more or less, the esp8266 becomes unresponsive. Either it completely disconnects from the network or in some cases (the ones that are weirder) remains connected to the network but doesn't respond to client messages. It was suggested to me that using yield() within loop() could help. But from the documentation I understand that the internal processes of the esp are done at the end of each loop() iteration. I would really appreciate any help on the matter.
I've been trying to setup a tcp socket server with the arduino ide (using the esp8266 arduino core) and the WifiManager library to make an easy setup on different networks. The code is simple enough, I set up the server and wait for a client. If a client connects and sends data to the server that data must be sent through the serial port. Here's the code:
Code: Select all
#include <ESP8266WiFi.h> //https://github.com/esp8266/Arduino
//needed for library
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h> //https://github.com/tzapu/WiFiManager
WiFiServer server(4998);
WiFiClient client;
char data[1500];
int ind = 0;
void setup() {
// put your setup code here, to run once:
Serial.begin(38400);
//WiFiManager
//Local intialization. Once its business is done, there is no need to keep it around
WiFiManager wifiManager;
//reset saved settings
//wifiManager.resetSettings();
//set custom ip for portal
//wifiManager.setAPConfig(IPAddress(10,0,1,1), IPAddress(10,0,1,1), IPAddress(255,255,255,0));
//fetches ssid and pass from eeprom and tries to connect
//if it does not connect it starts an access point with the specified name
//here "AutoConnectAP"
//and goes into a blocking loop awaiting configuration
wifiManager.autoConnect("AutoConnectAP");
//or use this for auto generated name ESP + ChipID
//wifiManager.autoConnect();
//if you get here you have connected to the WiFi
Serial.println("Connection established");
server.begin();
Serial.println("Server started");
Serial.setDebugOutput(true);
}
void loop() {
// put your main code here, to run repeatedly:
if(!client.connected())
{
//try to connect to a new client
client = server.available();
}
else
{
if(client.available() > 0)
{
while(client.available())
{
data[ind] = client.read();
ind++;
}
client.flush();
//Serial.println(data);
for(int j=0;j < ind; j++)
{
Serial.print(data[j]);
}
ind = 0;
client.print("OK!");
}
}
}
After a full day more or less, the esp8266 becomes unresponsive. Either it completely disconnects from the network or in some cases (the ones that are weirder) remains connected to the network but doesn't respond to client messages. It was suggested to me that using yield() within loop() could help. But from the documentation I understand that the internal processes of the esp are done at the end of each loop() iteration. I would really appreciate any help on the matter.