softAPgetStationNum() doesn't return to zero when client dis
Posted: Mon Dec 09, 2019 4:31 am
I have this code and I use:
To know how many devices are connected to the access point, it will not return to zero after the client is disconnected
But if the PC is connected and then disconnected, it can detect it and print zero
AP:
Client:
Where is the problem?
Code: Select all
Serial.printf ("Docking stations =% d \ n", WiFi.softAPgetStationNum ());
To know how many devices are connected to the access point, it will not return to zero after the client is disconnected
But if the PC is connected and then disconnected, it can detect it and print zero
AP:
Code: Select all
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <ArduinoJson.h>
#define led0 4 //D2
DynamicJsonBuffer jsonBuffer;
const char *ssid = "ssid";
const char *password = "password";
int sensorValue0 = 0;
String sensor_values;
ESP8266WebServer server(80);
WiFiClient client;
void handleSentVar() {
if (server.hasArg("sensor_reading"))
{
sensor_values = server.arg("sensor_reading");
Serial.print("**********************sensorValue:");
Serial.println(sensor_values);
}
JsonObject& root = jsonBuffer.parseObject(sensor_values);
sensorValue0 = root["sensor0_reading"].as<int>();
jsonBuffer.clear();
Serial.print("sensorValue0:");
Serial.println(sensorValue0);
toggle_leds();
server.send(200, "text/html", "Data received");
}
void setup() {
Serial.begin(9600);
WiFi.softAP(ssid, password);
IPAddress myIP = WiFi.softAPIP();
pinMode(led0, OUTPUT);
//toggle_leds(); //turn off all leds as all the sensor values are zero
server.on("/data/", HTTP_GET, handleSentVar); // when the server receives a request with /data/ in the string //then run the handleSentVar function
server.begin();
}
void loop() {
server.handleClient();
Serial.printf("Stations connected = %d\n", WiFi.softAPgetStationNum());
delay(1000);
}
void toggle_leds()
{
if (sensorValue0 == 0){ digitalWrite(led0, LOW);
Serial.println("++++++++++++++++off+++++++++++++");}
if (sensorValue0 == 1){ digitalWrite(led0, HIGH);
Serial.println("------------on-------------");}
}
Client:
Code: Select all
#include <ESP8266WiFi.h>
#define btn0 5 //D1
const char *ssid = "ssid";
const char *password = "password";
int sensorValue0 = 0; //sensor value, I'm usingg 0/1 button state
void setup() {
Serial.begin(9600);
delay(10);
pinMode(btn0, INPUT);
// set the ESP8266 to be a WiFi-client
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
}
void loop() {
Serial.print("btn");
Serial.println(digitalRead(btn0));
if(digitalRead(btn0) == LOW) sensorValue0 = 1;
if(digitalRead(btn0) == HIGH) sensorValue0 = 0;
// Use WiFiClient class to create TCP connections
WiFiClient client;
const char * host = "192.168.4.1"; //default IP address
const int httpPort = 80;
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
return;
}
// We now create a URI for the request. Something like /data/?sensor_reading=123
String url = "/data/";
url += "?sensor_reading=";
url += "{\"sensor0_reading\":\"sensor0_value\"}";
url.replace("sensor0_value", String(sensorValue0));
// This will send the request to the server
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n");
unsigned long timeout = millis();
while (client.available() == 0) {
if (millis() - timeout > 5000) {
Serial.println(">>> Client Timeout !");
client.stop();
return;
}
}
}
Where is the problem?