I am trying to get a socket server to send polling data to a client. I can get the first reading to it, but then it sends the same reading over and over again, failing to update.
netcat output:
localhost:~> netcat -v 192.168.1.249 8080
Connection to 192.168.1.249 8080 port [tcp/http-alt] succeeded!
68.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.8368.83^C
expected output:
68.83
68.82
68.89
etc..
my code:
#include <Adafruit_AHT10.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <ESPmDNS.h>
const char *ssid = "";
const char *password = "";
WiFiServer server2(8080);
//static ip config
// Current time
unsigned long currentTime = millis();
// Previous time
unsigned long previousTime = 0;
// Define timeout time in milliseconds (example: 2000ms = 2s)
const long timeoutTime = 2000;
// Set your Static IP address
IPAddress local_IP(192, 168, 1, 249); //cameras are .253 and .252 2 cameras
// Set your Gateway IP address
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);
IPAddress primaryDNS(1, 1, 1, 2); //optional
IPAddress secondaryDNS(1, 0, 0, 2); //optional
const int led = 13;
Adafruit_AHT10 aht;
void setup() {
Serial.begin(115200);
Serial.println("Adafruit AHT10 demo!");
if (! aht.begin()) {
Serial.println("Could not find AHT10? Check wiring");
while (1) delay(10);
}
Serial.println("AHT10 found");
pinMode(led, OUTPUT);
digitalWrite(led, 0);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.println("");
// Configures static IP address
if (!WiFi.config(local_IP, gateway, subnet, primaryDNS, secondaryDNS)) {
Serial.println("STA Failed to configure");
}
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
if (MDNS.begin("esp32")) {
Serial.println("MDNS responder started");
}
//begin socket server
server2.begin();
Serial.println("Socket server started");
}
void loop() {
sensors_event_t humidity, temp;
aht.getEvent(&humidity, &temp);// populate temp and humidity objects with fresh data
//Serial.print("Temperature in Celsius:\t\t"); Serial.print(temp.temperature);
//Serial.print("\nTemperature in Fahrenheit:\t"); Serial.print(temp.temperature * 1.8 + 32);
//Serial.print("\nHumidity:\t\t\t"); Serial.print(humidity.relative_humidity); Serial.println("% rH");
delay(25);
WiFiClient client = server2.available();
//Serial.print(client);
//socket server
if (client) {
Serial.print("boo");
//client.print(temp.temperature * 1.8 + 32);
while (client.connected() > 0) {
client.print(temp.temperature * 1.8 + 32);
delay(10);
}
client.stop();
Serial.println("Client disconnected");
}
}
How do I get my socket server to send updated polling information and not just the same one over and over again?