I’m using an esp32 to connect to an wifi network. I am also using an static ip an next to the acces point i can connect to the wifi network. If i go further from the acces point at a point where my laptop and smartphone don’t have an problem connecting to the wifi network the esp32 does have a problem. Wifi.status() returns code 3 that says WL_CONNECTED. Only while pinging the IP or trying to access an webpage I received times out. Any reasons for that?
Here is the code i’m using. The wifi part is important, the rest of the code for communication and NTC measurement to degree is not relevant for this post.
#include <WiFi.h>
WiFiServer wifiServer(80);
String Wifi_read;
// Wifi settings
const char *ssid = "NETWORK_NAME";
const char *password = "NETWORK_PASSWORD";
IPAddress local_IP(10,0,0,93);
IPAddress gateway(10,0,0,138);
IPAddress subnet(255,255,255,0);
int Wifi_led = 2;
int Wifi_status;
// given data
const int ADC_resolution = 4095;
const float VCC = 3.3;
//Settings
int Average_readings = 299; // Starting at 0
//Timers
unsigned long currenttime;
unsigned long nexttime = 0;
int interval = 125;
unsigned long Wifi_nexttime = 0;
int Wifi_interval = 125;
unsigned long check_wifi = 10000;
// NTC_1 variables
int NTC_1 = 34;
const int Resistor_1 = 9986;
float ADC_value_1;
float Voltage_1;
float NTC_R_raw_1;
float NTC_R_avg_1;
float NTC_array_1[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0, 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, 0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0,};
int Array_2 = 0;
float T02;
int T02_low;
int T02_high;
float Delta_t;
// values voor lookup table
float value[] =
{32869, 31229, 29680, 28217, 26834, 25527, 24291, 23122, 22015, 20968, // 0 t/m 9 degree
19977, 19038, 18149, 17306, 16507, 15750, 15032, 14350, 13703, 13090, // 10 t/m 19 degree
12507, 11953, 11427, 10927, 10452, 10000, 9570, 9161, 8772, 8402, // 20 t/m 29 degree
8049, 7713, 7393, 7088, 6798, 6521, 6256, 6004, 5764, 5534, // 30 t/m 39 degree
5315, 5106, 4906, 4715, 4533, 4358, 4191, 4032, 3879, 3733, // 40 t/m 49 degree
3594, 3460, 3332, 3210, 3092, 2980, 2872, 2768, 2669, 2574, // 50 t/m 59 degree
2483, 2396, 2312, 2231, 2154, 2080, 2009, 1940, 1874, 1811, // 60 t/m 69 degree
1750, 1692, 1636, 1582, 1530, 1480, 1431, 1385, 1340, 1298, // 70 t/m 79 degree
1256, 1216, 1178, 1141, 1105, 1071, 1038, 1006, 975, 945, // 80 t/m 89 degree
916, 889, 862, 836, 811, 787, 764, 741, 720, 699, // 80 t/m 89 degree
678, 659, 640, 622, 604, 587, 570, 554, 539, 524, // 90 t/m 99 degree
509, 495, 481, 468, 455, 443, 431, 419, 408, 397, // 100 t/m 109 degree
386, 376, 366, 357, 347, 338}; // 110 t/m 115 degree
int value_number;
void setup() {
Serial.begin(115200);
pinMode(Wifi_led, OUTPUT);
if (!WiFi.config(local_IP, gateway, subnet))
{ Serial.println("STA Failed to configure"); }
WiFi.begin(ssid, password);
wifiServer.begin();
}
void loop() {
WiFiClient client = wifiServer.available();
currenttime = millis();
Wifi_indication();
Wifi_labview();
if (currenttime >= nexttime) {
nexttime += interval;
Temps();
Serial.println(WiFi.status());
}
}
void Temps() {
Temps_T01();
Wifi_labview();
Temps_T02();
Wifi_labview();
Temps_Delta_t();
Wifi_labview();
}
void Temps_T01() {
// T01
// Reading the analog value
ADC_value_1 = analogRead(NTC_1); // Reading the ADC value
Voltage_1 = (ADC_value_1 * VCC) / ADC_resolution;
NTC_R_raw_1 = Resistor_1 / (VCC - Voltage_1) * Voltage_1;
// Smoothing the resistance value
NTC_array_1[Array_1] = NTC_R_raw_1;
Array_1 += 1;
if (Array_1 > Average_readings) {Array_1 = 0;}
NTC_R_avg_1 = 0;
for (int i = 0; i < Average_readings;) {
NTC_R_avg_1 += NTC_array_1[i];
i++;
}
NTC_R_avg_1 = NTC_R_avg_1 / Average_readings;
if (NTC_array_1[Average_readings] == 0 or NTC_R_raw_1 <= 500) { NTC_R_avg_1 = 0; }
// making an temperatur with decimals
value_number = 0;
while (value[value_number] >= NTC_R_avg_1) {
T01_low = value_number;
value_number++;
}
T01_high = value_number;
T01 = T01_low + (1 - ((T01_high - T01_low) / (value[T01_low] - value[T01_high]) * (NTC_R_avg_1 - value[T01_high])));
}
void Temps_T02() {
// T02
// Reading the analog value
ADC_value_2 = analogRead(NTC_2); // Reading the ADC value
Voltage_2 = (ADC_value_2 * VCC) / ADC_resolution;
NTC_R_raw_2 = Resistor_2 / (VCC - Voltage_2) * Voltage_2;
// Smoothing the resistance value
NTC_array_2[Array_2] = NTC_R_raw_2;
Array_2 += 1;
NTC_R_avg_2 = 0;
if (Array_2 > Average_readings) {Array_2 = 0;}
for (int i = 0; i < Average_readings;) {
NTC_R_avg_2 += NTC_array_2[i];
i++;
}
NTC_R_avg_2 = NTC_R_avg_2 / Average_readings;
if (NTC_array_2[Average_readings] == 0 or NTC_R_raw_2 <= 500) { NTC_R_avg_2 = 0; }
// making an temperatur with decimals
value_number = 0;
while (value[value_number] >= NTC_R_avg_2) {
T02_low = value_number;
value_number++;
}
T02_high = value_number;
T02 = T02_low + (1 - ((T02_high - T02_low) / (value[T02_low] - value[T02_high]) * (NTC_R_avg_2 - value[T02_high])));
}
void Temps_Delta_t() {
//Delta T
Delta_t = T01 - T02;
}
void Wifi_indication() {
if (WiFi.status() != WL_CONNECTED) {
if (currenttime >= Wifi_nexttime) {
Wifi_nexttime += Wifi_interval;
if (Wifi_status == HIGH)
{ Wifi_status = LOW; }
else
{ Wifi_status = HIGH; }
delay(100);
}
if (currenttime > check_wifi) {
WiFi.disconnect();
WiFi.begin(ssid, password);
check_wifi = currenttime + 30000;
Serial.println("Restarting wifi connection");
}
}
else
{ Wifi_status = HIGH; }
digitalWrite(Wifi_led, Wifi_status);
}
void Wifi_labview() {
WiFiClient client = wifiServer.available();
if (client.connected()) {
while (client.available() > 0) {
char a = client.read();
Wifi_read += a;
}
}
if (Wifi_read == "Read") {
client.print("T01: ");
client.print(T01);
client.print("\t");
client.print("T02: ");
client.print(T02);
client.print("\t");
client.print("DeltaT: ");
client.println(Delta_t);
}
Wifi_read = "";
}