2

Hello i wrote this sketch that upload a temperature reading of a DHT22 sensor to a phpMyAdmin server in localHost, the sketch work fine, the data been upload every 5 min to the server than ESP go t sleep, but every time the code has been executed i have the printout in the console of this error which i don't understand what mean.

[  1937][E][WiFiClient.cpp:517] flush(): fail on fd 48, errno: 11, "No more processes"

here below my sketch: the sketch include the timer every 5 min to wakeup esp read the temperature and post to the server.

#include <Arduino.h>
#include <DHT.h>
#include <Adafruit_Sensor.h>
#include <WiFi.h>
#include <HTTPClient.h>
#include <Wire.h>

// FOR T sensor
#define TsensorPin 4
#define DHTTYPE DHT22
DHT dht(TsensorPin, DHTTYPE);
String sensorName = "DHT22";
String sensorLocation = "Casa1";
float h;
float t;
float f;
float hif;
float hic;

// for wifi connection
const char* ssid     = "Captain Spiko";
const char* password = "123456789d";

// server data
const char* serverName = "http://192.168.50.158/esp_web/post_esp.php";
String apiKeyValue = "123456789";

// for awake and sleep
#define uS_TO_S_FACTOR 1000000  
#define TIME_TO_SLEEP  300  
RTC_DATA_ATTR int bootCount = 0;

void print_wakeup_reason(){
  esp_sleep_wakeup_cause_t wakeup_reason;

  wakeup_reason = esp_sleep_get_wakeup_cause();

  switch(wakeup_reason)
  {
    case ESP_SLEEP_WAKEUP_EXT0 : Serial.println("Wakeup caused by external signal using RTC_IO"); break;
    case ESP_SLEEP_WAKEUP_EXT1 : Serial.println("Wakeup caused by external signal using RTC_CNTL"); break;
    case ESP_SLEEP_WAKEUP_TIMER : Serial.println("Wakeup caused by timer"); break;
    case ESP_SLEEP_WAKEUP_TOUCHPAD : Serial.println("Wakeup caused by touchpad"); break;
    case ESP_SLEEP_WAKEUP_ULP : Serial.println("Wakeup caused by ULP program"); break;
    default : Serial.printf("Wakeup was not caused by deep sleep: %d\n",wakeup_reason); break;
  }
}

void readTemperature(){
  
  h = dht.readHumidity();
  t = dht.readTemperature();
  f = dht.readTemperature(true); // in F
  
  // Compute heat index in Fahrenheit (the default)
  hif = dht.computeHeatIndex(f, h);
  // Compute heat index in Celsius (isFahreheit = false)
  hic = dht.computeHeatIndex(t, h, false);

  Serial.print(F("Humidity: "));
  Serial.print(h);
  Serial.print(F("%  Temperature: "));
  Serial.print(t);
  Serial.print(F("°C "));
  Serial.print(f);
  Serial.print(F("°F  Heat index: "));
  Serial.print(hic);
  Serial.print(F("°C "));
  Serial.print(hif);
  Serial.println(F("°F"));
}

void activateSleep(){
  Serial.println("Going to sleep now");
  delay(1000);
  Serial.flush(); 
  esp_deep_sleep_start();
}

bool setupWifi(){
  WiFi.begin(ssid, password);
  Serial.println("Connecting");
  while(WiFi.status() != WL_CONNECTED) { 
    delay(500);
    Serial.print(".");
  } 
  Serial.println("");
  Serial.print("Connected to WiFi network with IP Address: ");
  Serial.println(WiFi.localIP());
 
  if (WiFi.status() == WL_CONNECTED){
    return true;
  } else {
    return false;
  }



}

void postData(){
  if(WiFi.status()== WL_CONNECTED){
    WiFiClient client;
    HTTPClient http;
    http.begin(client, serverName);
    http.addHeader("Content-Type", "application/x-www-form-urlencoded");
    // Prepare your HTTP POST request data
    String httpRequestData = "api_key=" + apiKeyValue + "&sensor=" + sensorName
                          + "&location=" + sensorLocation + "&value1=" + String(h)
                          + "&value2=" + String(t) + "&value3=" + String(f) + "";
    Serial.print("httpRequestData: ");
    Serial.println(httpRequestData);
    int httpResponseCode = http.POST(httpRequestData);
    if (httpResponseCode>0) {
      Serial.print("HTTP Response code: ");
      Serial.println(httpResponseCode);
    }
    else {
      Serial.print("Error code: ");
      Serial.println(httpResponseCode);
    }
    http.end();
  }else {
    Serial.println("WiFi Disconnected");
  }
}

/// @brief  Set up
void setup() {
  
  Serial.begin(115200);
  delay(1000);
  ++bootCount; // tiene il conto dei restart
  Serial.println("Boot number: " + String(bootCount));
  print_wakeup_reason();
  if (setupWifi() == true){
    esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);// set up next wake up after 30 sec
    Serial.println(F("DHTxx test!"));
    dht.begin();
  };
  
  

}

/// @brief loop func
void loop() {
  readTemperature();
  postData();
  activateSleep();
}

6
  • at which device and in which program are you seeing the error?
    – jsotola
    Commented Mar 6, 2023 at 3:44
  • I’m using VS code with platformio, I see this error on the esp32 serial print out, the one that post the data to the server . Commented Mar 6, 2023 at 5:08
  • FD probably means File Descriptor and 48 is the number. If it's Linux-like, basically everything is treated as a file: files, sockets, processes, ... Commented Apr 17, 2023 at 17:29
  • If setupWifi() == false, then dht.begin(); is never called but it still enters loop() and tries to read temperatures. Not sure if that's good or eventually runs into undefined behavior. Commented Apr 17, 2023 at 17:58
  • Also, I don't get why esp_sleep_enable_timer_wakeup() is only activated if it was possible to connect to the Wifi. If it was not possible to connect to the Wifi, you still goto into deep sleep. But you don't want to wake up after 30 seconds then? Commented Apr 17, 2023 at 18:02

3 Answers 3

2

I think you ran into the issue described on Github issues for WifiClient.

A user has loooked into this and found that the error message does not match the problem:

errno 11 (EAGAIN) happens when you go to read (via BSD recv function) bytes from a stream with MSG_DONTWAIT flag and there are no bytes to be read. The ESP error string "No more processes" is misleading. It's really "No data available, try again later." or maybe "No data to process".

It seems that there's no fix for the package yet as of today, but another user has posted a derived class that fixes the problem:

File wifiFix.h:

#pragma once

class WiFiClientFixed : public WiFiClient {
public:
  void flush() override;
};

File wifiFix.cpp:

#include <WiFi.h>
#include <lwip/sockets.h>
#include "wifiFix.h"

#define WIFI_CLIENT_FLUSH_BUFFER_SIZE    (1024)

void WiFiClientFixed::flush() {
  int res;
  size_t a = available(), toRead = 0;
  if (!a) {
      return;//nothing to flush
  }
  auto *buf = (uint8_t *) malloc(WIFI_CLIENT_FLUSH_BUFFER_SIZE);
  if (!buf) {
      return;//memory error
  }
  while (a) {
      // override broken WiFiClient flush method, ref https://github.com/espressif/arduino-esp32/issues/6129#issuecomment-1237417915
      res = read(buf, min(a, (size_t)WIFI_CLIENT_FLUSH_BUFFER_SIZE));
      if (res < 0) {
          log_e("fail on fd %d, errno: %d, \"%s\"", fd(), errno, strerror(errno));
          stop();
          break;
      }
      a -= res;
  }
  free(buf);
}

File main.cpp:

#include "wifiFix.h"
WiFiClient* wifi = new WiFiClientFixed();

Finally, instead of http.begin(url);, use http.begin(*wifi, url);

0

In Platformio.ini I changed config to "platform = [email protected]" from "platform = espressif32". It solved this issue for me

1
  • How does this resolve the problem, i dont get the direct coloration?
    – Mike
    Commented Jun 19, 2023 at 18:17
0

This bug is solved in Espressif Arduino Core 2.0.14 which is out since last month. If you're using PlatformIO add this to your platformio.ini file:

platform = https://github.com/tasmota/platform-espressif32/releases/download/2023.10.05/platform-espressif32.zip

This is not from the official PlatformIO branch but from Tasmota, a trusted source in the PlatformIO community. See the github site: https://github.com/tasmota/platform-espressif32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.