I'm trying to get very basic sockets communications going between two 8266 WiFi nodes. It works sporadically. There seems to be no real problem with the basic configuration of the two 8266 nodes, as the setup code is all copied from my working application that has 7 8266s all talking to each other over a tree-structured network where three of the 8266s are both APs and Stations, the rest are just Stations.
But, with the below code, most of the time the "server" messages do make it to the "client" correctly, and they are "reflected" back, but rarely make it back to the server.
What am I doing wrong here? The SERVER define controls whether the code is built for the server node or client node. I am running both on NODEMCUs.
#include <ESP8266WiFi.h>
#define SERVER
#if defined(SERVER)
WiFiServer server = WiFiServer(8000);
#endif
uint8_t Cnt = 123;
uint8_t State = 0;
WiFiClient client = WiFiClient();
IPAddress Netmask = IPAddress(255, 255, 255, 0);
char *STASSID = "TP-LINK_A47DC2";
char *STAPass = "79517515";
IPAddress STAGatewayIP = IPAddress(10, 0, 0, 1);
IPAddress STAIP = IPAddress(10, 0, 0, 50);
char *APSSID = "WiFiNetwork";
char *APPass = "WiFiPassword";
IPAddress APIP = IPAddress(192, 168, 0, 1);
IPAddress ClientIP = IPAddress(192, 168, 0, 2);
char s[80];
int i = 0;
uint32_t timer = 0;
#if defined(SERVER)
void ServerSetup()
{
boolean success = false;
while (!success)
{
// Disconnect, if connected
WiFi.disconnect();
// Set Mode
if (!(success = WiFi.mode(WiFiMode_t::WIFI_AP_STA)))
continue;
// Configure STA
if (!(success = WiFi.config(STAIP, STAGatewayIP, Netmask, STAGatewayIP)))
continue;
// Configure AP
if (!(success = WiFi.softAPConfig(APIP, STAGatewayIP, Netmask)))
continue;
// Start/Connect AP + STA
WiFi.persistent(true);
if (!(success = WiFi.setAutoConnect(true)))
continue;
if (!(success = WiFi.begin(STASSID, STAPass)))
continue;
if (!(success = WiFi.setAutoReconnect(true)))
continue;
if (!(success = WiFi.softAP(APSSID, APPass, 1)))
continue;
}
server.begin();
Serial.printf("\n\nServer Ready\n");
}
void ServerLoop()
{
switch (State)
{
case 0:
client = server.available();
if (client)
{
Serial.printf("Server: Wrote: %03d ", Cnt);
client.printf("\r\n%03d\r\n", Cnt);
timer = millis();
State = 1;
}
break;
case 1:
client = server.available();
while (client.available())
{
char c = client.read();
if (c == '\r')
continue;
else if (c == '\n')
{
if (i > 0)
{
int val = atoi(s);
Serial.printf("Read: %03d => ", val);
if (val == Cnt)
{
Serial.printf("Match\n");
Cnt++;
State = 0;
}
else
{
Serial.printf("No Match\n");
Cnt++;
client.stop();
State = 0;
}
}
i = 0;
s[i] = '\0';
}
else
{
s[i++] = c;
s[i] = '\0';
Serial.print(c);
}
}
if (millis() - timer > 3000)
{
client.stop();
Serial.printf("Timeout\n");
Cnt++;
State = 0;
}
break;
}
}
#else
void ClientSetup()
{
boolean success = false;
while (!success)
{
// Disconnect, if connected
WiFi.disconnect();
// Set Mode
if (!(success = WiFi.mode(WiFiMode_t::WIFI_STA)))
continue;
// Configure STA
if (!(success = WiFi.config(ClientIP, APIP, Netmask, APIP)))
continue;
WiFi.persistent(true);
if (!(success = WiFi.setAutoConnect(true)))
continue;
// Connect STA
if (!(success = WiFi.begin(APSSID, APPass)))
continue;
if (!(success = WiFi.setAutoReconnect(true)))
continue;
}
client.connect(APIP, 8000);
Serial.printf("\n\nClient Ready\n");
}
void ClientLoop()
{
if (!client.connected())
{
//client.stop();
client.connect(APIP, 8000);
}
else
{
char s[80];
int i = 0;
while (client.available())
{
char c = client.read();
if (c == '\r')
continue;
else if (c == '\n')
{
if (i > 0)
{
int val = atoi(s);
Serial.printf("Client: Wrote: %03d\n", val);
client.printf("\r\n%03d\r\n", val);
}
i = 0;
s[i] = '\0';
}
else
{
s[i++] = c;
s[i] = '\0';
}
}
}
}
#endif
void setup()
{
Serial.begin(115200);
#if defined(SERVER)
ServerSetup();
#else
ClientSetup();
#endif
}
void loop()
{
#if defined(SERVER)
ServerLoop();
#else
ClientLoop();
#endif
}
Regards,
Ray L.