Troubleshooting HTTP POST Error on ESP32 with ADXL335 Accelerometer

Hey guys @Middleware & OS @MCU, MPU & Firmware , I am attempting to set up an ESP32 to collect vibration data using an ADXL335 accelerometer and send it to a server for predictive maintenance analysis. The data should be transmitted over Wi-Fi, I have configured the ESP32 to connect to a Wi-Fi network, set up the ADXL335 accelerometer to read vibration data, wrote code to send the sensor data to a server using HTTP POST requests. But still getting the error
Error on sending POST: -1
Error on sending POST: -1
This is my code
#include <WiFi.h>
#include <HTTPClient.h>
// Wi-Fi credentials
const char* ssid = "TurboNet";
const char* password = "your_PASSWORD";
// Server URL
const char* serverName = "http://your-server.com/post-data";
// ADXL335 pins
const int xPin = A0;
const int yPin = A1;
const int zPin = A2;
void setup() {
    Serial.begin(115200);
    // Connect to Wi-Fi
    WiFi.begin(ssid, password);
    while (WiFi.status() != WL_CONNECTED) {
        delay(1000);
        Serial.println("Connecting to WiFi...");
    }
#include <WiFi.h>
#include <HTTPClient.h>
// Wi-Fi credentials
const char* ssid = "TurboNet";
const char* password = "your_PASSWORD";
// Server URL
const char* serverName = "http://your-server.com/post-data";
// ADXL335 pins
const int xPin = A0;
const int yPin = A1;
const int zPin = A2;
void setup() {
    Serial.begin(115200);
    // Connect to Wi-Fi
    WiFi.begin(ssid, password);
    while (WiFi.status() != WL_CONNECTED) {
        delay(1000);
        Serial.println("Connecting to WiFi...");
    }
Solution:
Thanks, it worked after correcting the url I forgot to add : after https
Jump to solution
4 Replies
Enthernet Code
Enthernet Code3mo ago
    Serial.println("Connected to WiFi");
}
void loop() {
    // Read vibration sensor data
    int xValue = analogRead(xPin);
    int yValue = analogRead(yPin);
    int zValue = analogRead(zPin);
    if (WiFi.status() == WL_CONNECTED) {
        HTTPClient http;
        // Specify content-type header
        http.begin(serverName);
        http.addHeader("Content-Type", "application/x-www-form-urlencoded");
        // Prepare data for HTTP POST
        String httpRequestData = "xValue=" + String(xValue) + "&yValue=" + String(yValue) + "&zValue=" + String(zValue);
        // Send HTTP POST request
        int httpResponseCode = http.POST(httpRequestData);
        if (httpResponseCode > 0) {
            String response = http.getString();
            Serial.println(httpResponseCode); // Print return code
            Serial.println(response); // Print response
        } else {
            Serial.print("Error on sending POST: ");
            Serial.println(httpResponseCode);
        }
        // Free resources
        http.end();
    } else {
        Serial.println("Error in WiFi connection");
    }
    delay(10000); // Send a request every 10 seconds
}
    Serial.println("Connected to WiFi");
}
void loop() {
    // Read vibration sensor data
    int xValue = analogRead(xPin);
    int yValue = analogRead(yPin);
    int zValue = analogRead(zPin);
    if (WiFi.status() == WL_CONNECTED) {
        HTTPClient http;
        // Specify content-type header
        http.begin(serverName);
        http.addHeader("Content-Type", "application/x-www-form-urlencoded");
        // Prepare data for HTTP POST
        String httpRequestData = "xValue=" + String(xValue) + "&yValue=" + String(yValue) + "&zValue=" + String(zValue);
        // Send HTTP POST request
        int httpResponseCode = http.POST(httpRequestData);
        if (httpResponseCode > 0) {
            String response = http.getString();
            Serial.println(httpResponseCode); // Print return code
            Serial.println(response); // Print response
        } else {
            Serial.print("Error on sending POST: ");
            Serial.println(httpResponseCode);
        }
        // Free resources
        http.end();
    } else {
        Serial.println("Error in WiFi connection");
    }
    delay(10000); // Send a request every 10 seconds
}
wafa_ath
wafa_ath3mo ago
It is a connection problem. check your server URL make sure it's accecssible, and verify your ESP32 is connected to Wi-Fi then try again..
Dark AI
Dark AI3mo ago
@Enthernet Code have you checked if your wifi is properly connected and if the Serial Monitor is open at the correct baud rate (115200 as per your setup)
Solution
Enthernet Code
Enthernet Code3mo ago
Thanks, it worked after correcting the url I forgot to add : after https
Want results from more Discord servers?
Add your server