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 ![]()
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);
}
}