So i've got a setup running very nicely with 2-4 ESP-E12
On the Master ESP i'm creating a SoftAP and a Server. Its sends at random times a messaged to the other slave ESP's
MASTER CODE
extern "C" {
#include "user_interface.h"
}
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <EEPROM.h>
#include <string.h>
#include <SoftwareSerial.h>
#define MAX_SRV_CLIENTS 6
WiFiServer server(23);
WiFiClient serverClients[MAX_SRV_CLIENTS];
WiFiClient client;
char Name[32] = "HELLOWORLD"; // Standard name part
char ID[8] ; // buffer of ID of the ESP8266 ID
IPAddress myIP;
int connected = 0 ;
const char *ssid = "SOFTAP";
const char *password = "123456789";
void setup() {
Serial.begin(9600);
itoa(ESP.getChipId(), ID, 10); // get ID of ESP
// Convert ID to constant Char
for (int i = 0; i < 8; i ++)
{
Name[i + 12] = ID[i];
}
wifi_station_disconnect();
wifi_set_opmode(0x02); // SETTING IT TO AP MODE
const char *ssid = Name;
WiFi.softAP(ssid, password, 8, 0);
myIP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(myIP);
server.begin();
server.setNoDelay(false);
Serial.println("TCP Server Setup done");
delay(5000);
}
void loop() {
uint8_t i;
//check if there are any new clients
if (server.hasClient()) {
connected = 0;
for (i = 0; i < MAX_SRV_CLIENTS; i++) {
//find free/disconnected spot
if (!serverClients[i] || !serverClients[i].connected()) {
if (serverClients[i]) serverClients[i].stop();
serverClients[i] = server.available();
Serial.print("New client: "); Serial.println(i);
serverClients[i].print("00000010");
continue;
}
connected ++;
}
Serial.print("Connected clients = ");
Serial.println(connected);
//no free/disconnected spot so reject
WiFiClient serverClient = server.available();
serverClient.stop();
}
// Check for Data each time only 1 byte is expected and needed to be send
if (Serial.available() > 0)
{
byte temp_input = Serial.read();
Serial.flush();
String byte_message = "";
byte_message += bitRead(temp_input, 7);
byte_message += bitRead(temp_input, 6);
byte_message += bitRead(temp_input, 5);
byte_message += bitRead(temp_input, 4);
byte_message += bitRead(temp_input, 3);
byte_message += bitRead(temp_input, 2);
byte_message += bitRead(temp_input, 1);
byte_message += bitRead(temp_input, 0); // TAKES THE 10001011 version of the Byte
for (i = 0; i < MAX_SRV_CLIENTS; i++) {
if (serverClients[i] && serverClients[i].connected()) {
serverClients[i].print(byte_message);
delay(1);
}
}
}
}
SLAVE CODE
extern "C" {
#include "user_interface.h"
}
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include<WiFiUdp.h> // libary for UDp connections
const char* host = "192.168.4.1";
const int port = 23;
const char *ssid = "";
const char *password = "";
int status = 6;
WiFiClient client;
void fpm_wakup_cb_func1(void) {
wifi_fpm_close(); // disable force sleep function
//wifi_set_opmode(STATION_MODE); // set station mode
//wifi_station_connect();
// Serial.println("Woken up...");
}
// LIGHT SLEEP FUNCTION
void sleepNow(int MILI) {
// Serial.println("Going to sleep...");
float sleepTime = millis();
wifi_fpm_set_sleep_type(LIGHT_SLEEP_T); // light sleep
wifi_fpm_open(); // enable force sleep
wifi_fpm_set_wakeup_cb(fpm_wakup_cb_func1); // Set wakeup callback
wifi_fpm_do_sleep(MILI * 1000);
delay(MILI +1);
}
void setup() {
Serial.begin(9600);
WiFi.disconnect();
delay(10);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
if (!client.connect(host, port))
{
Serial.println("Failed Connection to Server");
status = 1;
}
}
void loop()
{
status = WiFi.status();
if (status == 3 && client.available() < 1) // if there is no data ready for Module go to sleep
{
Serial.println("GO To SLEEP");
sleepNow(10);
Serial.println("WAKING UP");
}
else
{
// function to reconnect
}
if (!client.connect(host, port))
{
Serial.println("Failed Connection to Server");
status = 1;
}
else if (client.available() >0)
{
String TEMP_Message = "";
while (client.available() > 0)
{
TEMP_Message += char(client.read());
}
Serial.println("GOT message = " + TEMP_Message);
client.flush();
}
}
Everything works perfect aslong as my sleepNOW function stays under the 15 miliseconds
I would like to have my slaves sleep for 100 mili and then check if there is something available for them
the problem is that whenever i increase the time the slave sleeps , the master stays in a loop or something for the sending to that node ater the client.print function
. You would say 100 mili shouldn't be a problem however if the Slave sleeps for 100 MILI the lag in my master is about 2 seconds before it continous from the client.print method.
Any thought on how i could make sure my master doesn't get stuck at the sending part when my slave nodes sleep ?
Kind Regards,
Orcanbull
Orcanbull
Yes i can help you , Yes you can send me a PM with a question - No i won't make the code for you