Hello, first of all thanks for helping me out!
I'm working on a small project using a ESP8266 module (which will act as input for an Arduino which does more stuff).
One problem I have is when comparing the received strings with a certain string, this doesn't work. I guess it's some rookie mistake, would be nice if some one had some tips...
In "checkNewData()" you can see how I read the data, in "doJob()" you find the comparisons...(Data is received properly in the Serial monitor and printed normally). I also tried C++ syntax with == instead of strcmp, this also didn't work.
#include <stdio.h>
#include "ESP8266WiFi.h"
#define MAX_CLIENTS 10
#define MAX_LINE_LENGTH 64
#define PORT 80
#define AP_SSID "HIDDEN"
#define PASSWORD "ALSOHIDDEN"
#define LED_OUTPUT 2
//#define PUMPE_OUTPUT 0
const char* ssid = AP_SSID;
const char* password = PASSWORD;
WiFiServer wifiServer(PORT);
int led = 2;
//int pumpe = PUMPE_OUTPUT;
WiFiClient* clients[MAX_CLIENTS] = {NULL};
char buffers[MAX_CLIENTS][MAX_LINE_LENGTH] = {0};
int newData[MAX_CLIENTS] = {0};
void setup() {
pinMode(led, OUTPUT);
//pinMode(pumpe, OUTPUT);
Serial.begin(115200);
delay(1000);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(1000);
Serial.println("Connecting..");
}
Serial.print("Connected to WiFi. IP:");
Serial.println(WiFi.localIP());
wifiServer.begin();
}
void checkNewClients()
{
WiFiClient newClient = wifiServer.available();
if (newClient)
{
Serial.println("New client connected");
// Find the first unused space
for (int i=0; i < MAX_CLIENTS; ++i)
{
if (clients[i] == NULL)
{
clients[i] = new WiFiClient(newClient);
break;
}
}
}
}
void checkNewData()
{
memset(newData, 0, MAX_CLIENTS);
for (int i = 0 ; i < MAX_CLIENTS ; ++i)
{
if (clients[i] != NULL && clients[i]->available())
{
newData[i] = 1;
memset(buffers[i], 0, MAX_LINE_LENGTH); //reset the old buffer
int index = 0;
while (clients[i]->available())
{
char current = clients[i]->read();
if(index < MAX_LINE_LENGTH - 1)//-1 to leave space for '\0'
buffers[i][index] = current;
index++;
}
buffers[i][index] = '\0';
//DEBUG
Serial.println(buffers[i]);
//END DEBUG
}
}
}
void doJob()
{
for (int i=0 ; i < MAX_CLIENTS ; ++i)
{
if(newData[i])
{
if(strcmp(buffers[i], "LED OFF") == 0) //THIS DOESN'T WORK
{
digitalWrite(led, LOW); // Turn the LED off
Serial.println("LED OFF");
}
if(strcmp(buffers[i], "LED ON") == 0) //THIS DOESN'T WORK
{
digitalWrite(led, HIGH); // Turn the LED on
Serial.println("LED ON");
}
}
}
}
void checkDisconnections()
{
for (int i=0 ; i<MAX_CLIENTS ; ++i)
{
if (clients[i])
{
if(!clients[i]->connected())
{
clients[i]->stop();
//delete(clients[i]); --> I should expect this is necessary, but Arduino crashes
clients[i] = NULL; //--> This should be a memory leak, as the object clients[i] still exists..., but as said delete results in a crash
if(clients[i] == NULL)
Serial.println("Client was deleted");
else
Serial.println("Client was not deleted");
}
}
}
}
void doWifiJob()
{
checkNewClients();
checkNewData();
doJob();
checkDisconnections();
}
void loop()
{
doWifiJob();
delay(1000);
}
The second problem I have is being unsure about a potential memory leak:
In checkNewClients(), a new client is created and added to the list of clients if a TCP connection is set up. In checkDisconnections() I want to remove the clients which disconnected, making space for other clients. Now I thought I must remove the WiFiClient object there from memory with "delete"...but if I try this, this results in a crash on runtime when a client disconnects (Stack status is printed in Serial Monitor and ESP8266 reboots). Please advise what to do...when I just set the nullpointer it works of course, but it feels wrong to me.