Hey there.
I'm having some trouble with sending data from three ESP8266 that run in station mode to another ESP8266 which is configured as a soft access point.
Basically I have three 'nodes' which are supposed to read data from a sensor (SHT21) every five minutes and then pass that data on to a 'master'. Reading data from the sensor is no problem. But: Although it seems that the connection is fine and data is transmitted, there is no data coming in on the master side. Or at least there is no output.
Maybe someone is able to tell me what is going on. Thanks!
This is the code running on the MASTER:
#include <ESP8266WiFi.h>
/*****************************************************/
const char* WIFI_SSID = "MyWiFi";
const char* WIFI_PASSWORD = "MyPassword";
const String MASTER_ID = "8";
/*****************************************************/
const int WIFI_CHANNEL = 4;
const int SERVER_PORT = 80;
IPAddress local_IP(192,168,1,1);
IPAddress gateway(192,168,0,1);
IPAddress subnet(255,255,255,0);
WiFiServer WiFi_Server(SERVER_PORT);
WiFiClient WiFi_Client;
char message[50]={};
void setup()
{
Serial.begin(9600);
/* Setup ESP8266 in soft-AP mode*/
WiFi.mode(WIFI_AP);
Serial.println();
Serial.print("Setting soft-AP configuration ... ");
Serial.println(WiFi.softAPConfig(local_IP, gateway, subnet) ? "Ready" : "Failed!");
Serial.print("Setting soft-AP ... ");
Serial.println(WiFi.softAP(WIFI_SSID,WIFI_PASSWORD,WIFI_CHANNEL) ? "Ready" : "Failed!");
Serial.print("Soft-AP IP address = ");
Serial.println(WiFi.softAPIP());
/* Starting the Server */
WiFi_Server.begin();
Serial.println("Server online.");
delay(1000);
}
void loop()
{
WiFi_Client = WiFi_Server.available();
WiFi_Client.setNoDelay(true);
int i = 0;
String data = MASTER_ID + ",";
if (WiFi_Client.available() > 0)
{
#if 1
WiFi_Client.setTimeout(1000);
data = MASTER_ID + "," + WiFi_Client.readStringUntil('\n');
Serial.println(data);
#else
while (WiFi_Client.available() > 0)
{
message[i] = client.read();
i = i + 1;
}
for (int j=0; j<=i; j++)
{
data = data + message[j];
}
Serial.println(data);
#endif
}
}
This is the code running on the NODES:
#include <ESP8266WiFi.h>
#include <Wire.h>
/*****************************************************/
const char* WIFI_SSID = "MyWiFi";
const char* WIFI_PASSWORD = "MyPassword";
const String NODE_ID = "1";
/*****************************************************/
IPAddress server_ip(192,168,1,1);
const int SERVER_PORT = 80;
WiFiClient client;
int read_buffer[3] = {};
int wifitimeout = 0;
int first = 0;
int second = 0;
int third = 0;
float T = 0.0;
float RH = 0.0;
void setup()
{
Wire.begin(5,4);
Serial.begin(9600);
/* Setup ESP8266 in station mode */
Serial.println();
Serial.println("Starting up...");
WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_SSID,WIFI_PASSWORD);
Serial.println();
Serial.print("Waiting for wireless network.");
while(WiFi.status() != WL_CONNECTED)
{
Serial.print(".");
delay(1000);
if (wifitimeout == 20)
{
Serial.println("Could not connect to WiFi Network.");
Serial.println("Device will restart in 10 seconds.");
ESP.deepSleep(10000000);
}
wifitimeout++;
}
Serial.println();
Serial.println("WiFi connection established!");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
client.connect(server_ip,SERVER_PORT);
delay(1000);
}
void loop()
{
/* Fetch temperature data from sensor */
temp_data_request();
delay(250);
float temperature = temp_data();
/* Fetch humidity data from sensor */
humi_data_request();
delay(250);
float humidity = humi_data();
/* Format data prior to sending */
String data1 = String(temperature);
String data2 = String (humidity);
String data = NODE_ID + ',' + data1 + ',' + data2;
/* Send data to master. */
if (client.connect(server_ip,SERVER_PORT))
{
Serial.println();
Serial.println("Sending data...");
Serial.print(temperature);
Serial.println(" degrees Celsius");
Serial.print(humidity);
Serial.println("% relative humidity.");
Serial.println();
client.println(data);
Serial.println("Data sent.");
}
/* Put ESP8266 into deep sleep for five minutes */
ESP.deepSleep(300000000);
}
void user_reg_request()
{
Wire.beginTransmission(0x40);
Wire.write(0xE7);
Wire.endTransmission();
return;
}
void temp_data_request()
{
Wire.beginTransmission(0x40);
Wire.write(0xF3);
Wire.endTransmission();
return;
}
void humi_data_request()
{
Wire.beginTransmission(0x40);
Wire.write(0xF5);
Wire.endTransmission();
return;
}
float temp_data()
{
int i = 0;
Wire.requestFrom(0x40,3);
delay(500);
while (Wire.available()>2)
{
first=(int)Wire.read();
second=(int)Wire.read();
third =(int)Wire.read();
}
int first_byte = first * 256;
int sec_byte = second;
int sT= first_byte+sec_byte;
T = -46.85 + (175.72 * (sT / (pow(2,16))));
return T;
}
float humi_data()
{
int i=0;
Wire.requestFrom(0x40,3);
delay(500);
while (Wire.available()>2)
{
first=(int)Wire.read();
second=(int)Wire.read();
third=(int)Wire.read();
}
int first_byte = first * 256;
int sec_byte = second;
int sRH = first_byte + sec_byte;
RH = -6 + (125 * (sRH / (pow(2,16))));
return RH;
}
void user_data()
{
Wire.requestFrom(64,1);
while (Wire.available())
{
Serial.print("available data: ");
int read_user=Wire.read();
Serial.println(read_user);
return;
}
}
(Please don't mind the I²C stuff. I know it's not very good/elegant but it works and I will take care of it once I figured out why the master receives no data. ;))