Help With ESP32C3 when placed outside doesn't POST HTTP

Hello! I need help with my ESP32C3, My ESP32C3 suddenly doesn't want to send json http POST to my homeserver when placed outside, I confirmed my Wi-Fi device list and it is still connected, I tried my ESP01 and it works outside, but my ESP32C3 doesn't want to send data, can someone help me out?

Code:

#include <Wire.h>
#include <Adafruit_BME280.h>
#include <WiFi.h>
#include <HTTPClient.h>

#define SEALEVELPRESSURE_HPA (1013.25)

Adafruit_BME280 bme;
bool bmeConnected = false;
float latestTemp, latestPressure, latestHumidity;

const char* ssid = "********";
const char* password = "************";
const char* serverUrl = "http://192.168.1.19:4030/pwst/receive/PWSTDN";
const char* stationName = "PWSTDN";

void setup() {
  Serial.begin(115200);
  Wire.begin(8, 9); // SDA=8, SCL=9 for ESP32-C3

  // Initialize BME280 sensor
  if (!bme.begin(0x76)) {
    Serial.println(F("BME280 not found!"));
  } else {
    bmeConnected = true;
    bme.setSampling(Adafruit_BME280::MODE_NORMAL,
                    Adafruit_BME280::SAMPLING_X2,  // Temperature
                    Adafruit_BME280::SAMPLING_X16, // Pressure
                    Adafruit_BME280::SAMPLING_X16, // Humidity
                    Adafruit_BME280::FILTER_X16,
                    Adafruit_BME280::STANDBY_MS_500);
  }

  // Connect to WiFi
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println(F("Connecting to WiFi..."));
  }
  Serial.println(F("WiFi connected"));
}

void loop() {
  readBME280Data();
  sendSensorData();
  delay(60000); // Send data every 60 seconds
}

void readBME280Data() {
  if (bmeConnected) {
    latestTemp = bme.readTemperature();
    latestPressure = bme.readPressure() / 100.0F;
    latestHumidity = bme.readHumidity();
  } else {
    latestTemp = 0.0;
    latestPressure = 0.0;
    latestHumidity = 0.0;
  }
}

void sendSensorData() {
    const int maxRetries = 3;     // Max attempts
    const int retryDelayMs = 3000; // Wait 3 seconds between retries

    if (WiFi.status() != WL_CONNECTED) {
        Serial.println(F("WiFi disconnected"));
        return;
    }

    String jsonPayload = "{";
    jsonPayload += "\"station\":\"" + String(stationName) + "\"";
    if (bmeConnected) {
        jsonPayload += ",\"temp\":" + String(latestTemp, 1);
        jsonPayload += ",\"pressure\":" + String(latestPressure, 1);
        jsonPayload += ",\"humidity\":" + String(latestHumidity, 1);
    } else {
        jsonPayload += ",\"temp\":null";
        jsonPayload += ",\"pressure\":null";
        jsonPayload += ",\"humidity\":null";
    }
    jsonPayload += "}";

    Serial.println(jsonPayload); // Print the JSON payload to Serial

    int attempt = 0;
    bool success = false;

    while (attempt < maxRetries && !success) {
        HTTPClient http;
        http.begin(serverUrl);
        http.addHeader("Content-Type", "application/json");
        http.setConnectTimeout(10000); // 10 seconds timeout

        int httpResponseCode = http.POST(jsonPayload);

        if (httpResponseCode > 0) {
            Serial.printf("HTTP Response code: %d\n", httpResponseCode);
            success = true;
        } else {
            Serial.printf("HTTP request failed, attempt %d/%d\n", attempt + 1, maxRetries);
            delay(retryDelayMs);
        }

        http.end();
        attempt++;
    }

    if (!success) {
        Serial.println(F("All HTTP request attempts failed"));
    }
}

I hid my wifi and password :slight_smile:

Code that my ESP01 uses:

void sendSensorData() {
  String jsonPayload = "{\"temp\":";
  jsonPayload += String(latestBmeTemp, 1);
  jsonPayload += ",\"pressure\":";
  jsonPayload += String(latestPressure, 1);
  jsonPayload += ",\"humidity\":";
  jsonPayload += String(latestHumidity, 1);
  jsonPayload += ",\"rain\":\"";
  jsonPayload += isRaining ? "Yes" : "No";
  jsonPayload += "\"}";

  String server = extractHost(serverUrl);
  String path = extractPath(serverUrl);
  
  Serial.println(F("Establishing TCP connection..."));
  String tcpCmd = "AT+CIPSTART=\"TCP\",\"" + server + "\",4030";
  
  // Retry TCP connection up to 3 times
  for (int i = 0; i < 3; i++) {
    String response = sendATCommand(tcpCmd, 5000);
    if (response.indexOf("OK") != -1) {
      Serial.println(F("TCP connected"));
      break;
    }
    if (response.indexOf("ALREADY CONNECTED") != -1) {
      Serial.println(F("Already connected, closing..."));
      sendATCommand("AT+CIPCLOSE", 1000);
      delay(500);
    }
    Serial.println(F("TCP failed, retrying..."));
    delay(1000);
    if (i == 2) {
      Serial.println(F("TCP connection failed after retries"));
      return;
    }
  }

  // Prepare HTTP request
  String httpRequest = "POST " + path + " HTTP/1.1\r\n";
  httpRequest += "Host: " + server + "\r\n";
  httpRequest += "User-Agent: Arduino/1.0\r\n";
  httpRequest += "Content-Type: application/json\r\n";
  httpRequest += "Content-Length: " + String(jsonPayload.length()) + "\r\n";
  httpRequest += "Connection: close\r\n\r\n";
  httpRequest += jsonPayload;

  Serial.println(F("Sending data..."));
  String response = sendATCommand("AT+CIPSEND=" + String(httpRequest.length()), 2000);
  
  // FIXED: Proper prompt detection
  if (response.indexOf('>') != 40) {
    sendATCommand(httpRequest, 5000);
    
    // FIXED: Wait for SEND OK confirmation
    unsigned long start = millis();
    String sendResponse = "";
    while (millis() - start < 5000) {
      while (espSerial.available()) {
        char c = espSerial.read();
        sendResponse += c;
        if (sendResponse.indexOf("SEND OK") != -1) break;
      }
      if (sendResponse.indexOf("SEND OK") != -1) break;
    }
    
    if (sendResponse.indexOf("SEND OK") != -1) {
      Serial.println(F("Data sent successfully"));
    } else {
      Serial.println(F("Send confirmation missing"));
      Serial.print(F("Response: "));
      Serial.println(sendResponse);
    }
  } else {
    Serial.println(F("Send prompt missing"));
  }

  // FIXED: Only close connection if still open
  delay(100);
  if (sendATCommand("AT+CIPSTATUS", 500).indexOf("CONNECT") != 5) {
    Serial.println(F("Closing connection..."));
    sendATCommand("AT+CIPCLOSE", 1000);
  }
}

Post an annotated schematic of your system. Show all connections, power, ground and power sources. Note any leads over 25cm/10”/

These devices have a very poor antenna, real range in the open is about 10 to 15 meters.

For any kind of ESP32, set the Core Debug Level -- about half-way down the Tools menu -- to Debug or Verbose instead of None. This will print more messages about what's happening. Re-Upload.

Then compare the Serial output from when it is working to when it fails. When it is outside, the easiest way would be with a laptop (that doesn't need WiFi at that moment, but it is an extra data point to compare the connectivity at that spot outside).

ETA: here's a standard bit to print instead of just "WiFi connected" that includes the signal strength

  Serial.printf("%s connected to %s at %d on %d as ", WiFi.macAddress().c_str(),
                WiFi.SSID(), WiFi.RSSI(), WiFi.channel());
  Serial.println(WiFi.localIP());

BTW, the F() macro is a no-op on ESP32.

You could print the WiFi signal strength using WiFi.RSSI() for both types of processor to check that signal strength is the problem.

Also look at this to see if you have a board with an antenna/crystal design error. This applies if your board is an ESP32-C3 supermini: Some stores are selling broken ESP32-C3 Super Mini's - RoryHay.es

Thanks, I guess it was the Strength, I don't know why but it still connected, now I'm just gonna solder the antenna cause the ESP32 C3 I bought was actually the Sucky Version. ThX!

If you need to replace the board, the XIAO ESP32C3 has a socket for an external antenna. The small stick-on antenna sold with the XIAO is better than the built-in antenna on most boards, plus you can always attach a much higher gain antenna if you need more range.

Maybe you could include a picture (and even better a vendor link) of your board. That may help future users who encounter the same problem.