I am trying to make a simple data collection with two ESP32. The server uses a BME 280 to formulate data:
/* ESP32 + BME280 Svarar på fråga om temperatur resp RF */ #include <SPI.h> #include <WiFi.h> #include <Wire.h> #include <ESPAsyncWebServer.h> #include <Adafruit_BME280.h> #include <Adafruit_Sensor.h> Adafruit_BME280 bme; // I2C char ssid[] = ""; char pass[] = ""; AsyncWebServer server(80); IPAddress local_IP(192, 168, 0, 205); // Suntak UTE IPAddress gateway(192, 168, 0, 1); IPAddress subnet(255, 255, 255, 0); String getAnswer() { String temp = "0"; String RH = "0"; String svar; delay(50); bme.begin(0x76); temp = bme.readTemperature(); delay(50); RH = bme.readHumidity(); svar = temp + ";" + RH; return svar; } void setup() { Serial.begin(19200); // only for debug if (!bme.begin(0x76)) { Serial.println("Could not find a valid BME280 sensor, check wiring!"); while (1); } if (!WiFi.config(local_IP, gateway, subnet)) { Serial.println("STA Failed to configure"); } WiFi.begin(ssid, pass); while (WiFi.status() != WL_CONNECTED) { // Serial.print("."); delay(500); } //Serial.print("Local IP: "); Serial.println(WiFi.localIP()); // Serial.println("Connected to wifi"); server.on("/GetStatus", HTTP_GET, [](AsyncWebServerRequest * request) { request->send(200, "text/plain", getAnswer()); }); server.begin(); } void loop () { }
The client uses HTTPClient to receive:
#include <WiFi.h> #include <HTTPClient.h> char ssid[] = ""; char pass[] = ""; IPAddress ip(192, 168, 0, 201); // IP address ofthis client IPAddress gateway(192, 168, 0, 1); // gateway of your network IPAddress subnet(255, 255, 255, 0); // subnet mask of your network void setup() { Serial.begin(19200); WiFi.config(ip, gateway, subnet); WiFi.begin(ssid, pass); while (WiFi.status() != WL_CONNECTED) { //Serial.print("."); delay(50); } Serial.println("Connected to wifi"); } void loop () { HTTPClient http; http.begin("http://192.168.0.205/GetStatus"); int httpCode = http.GET(); if (httpCode > 0) { String payload = http.getString(); Serial.println(httpCode); Serial.println(payload); } else { Serial.println("Error on HTTP request"); } http.end(); delay(10000); }
The problem is that the client crashes in a inpredictable manner. Sometimes it workes 50 times in a row, sometimes it crashes a couple of times and then works two times and then crashes etc. I can not find a pattern in it. To my mind it looks as some kind of memory problem but I need help to find out what it is.
Hi.
When you say it crashes, what do you actually mean? What happens when it crashes? Can you describe?
Regards,
Sara
Hi Sara.
PROBLEM SOLVED!
It used to crash, reboot, now and then, but now it has been sending and receiveing every 10 second for 2,5 hours without any problems.
I wrote a line to check WiFI status prior to every hppt call, that did the trick. It seems that the WiFi connection can’t be trusted even if the connection has started in a normal way.
Sorry to bother you with such a trivial problem.
Regards
Anders