Getting http error code -1

Below is my Arduino code

#include <ArduinoJson.h>
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>

const char* ssid = "YourWiFiSSID"; // Replace with your WiFi SSID
const char* password = "YourWiFiPassword"; // Replace with your WiFi password
const char* apiEndpoint = "http:///api/iotData/"; // Replace with your Django API endpoint

void setup() {
Serial.begin(9600);
connectToWiFi();
}

void loop() {
if (Serial.available() > 0) {
String receivedData = Serial.readStringUntil('\n');

// Check if the received data is not empty
if (!receivedData.isEmpty()) {
  Serial.println("Received Data: " + receivedData);
  processSerialData(receivedData);
}

}
}

void processSerialData(String data) {
DynamicJsonDocument jsonDoc(1024);
DeserializationError error = deserializeJson(jsonDoc, data);

if (error) {
Serial.print("Deserialization error: ");
Serial.println(error.c_str());
return;
}

float temperature = jsonDoc["temperature"].as(); // Ensure float type
float pHValue = jsonDoc["pHValue"].as(); // Ensure float type
int turbidity = jsonDoc["turbidity"].as(); // Ensure integer type

// Print received values (optional)
Serial.print("Temperature: ");
Serial.println(temperature);
Serial.print("pH Value: ");
Serial.println(pHValue);
Serial.print("Turbidity: ");
Serial.println(turbidity);

// Send data to Django API
sendDataToDjangoAPI(temperature, pHValue, turbidity);

// Clear the JSON document
jsonDoc.clear();
}

void sendDataToDjangoAPI(float temperature, float pHValue, int turbidity) {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;

// Construct the JSON payload
String payload = "{\"temperature\":" + String(temperature, 2) +
                 ",\"pHValue\":" + String(pHValue, 2) +
                 ",\"turbidity\":" + String(turbidity) + "}";

// Make the HTTP POST request
WiFiClient client;
http.begin(client, apiEndpoint);
http.addHeader("Content-Type", "application/json");
int httpCode = http.POST(payload);

if (httpCode == HTTP_CODE_OK) {
  Serial.println("Data sent successfully!");
  Serial.println(http.getString());
} else {
  Serial.print("HTTP Error code: ");
  Serial.println(httpCode);
}

// Disconnect
http.end();

}
}

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

Below is the error keeping showing up
23:11:59.090 -> HTTP Error code: -1
23:11:59.121 -> Received Data: 0.004883,"turbidity":628}23:11:59.154 -> Deserialization error: InvalidInput
23:11:59.186 -> Received Data: {"temperature":-127,"pHValue":0.004883,"turbidity":627}23:11:59.249 -> Temperature: -127.00
23:11:59.281 -> pH Value: 0.00
23:11:59.326 -> Turbidity: 627

Can anyone please help me
Please ignore the readings

When you post code, format it using the < CODE > button in the message box... the result will look like this...

#include <ArduinoJson.h>
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>

const char* ssid = "YourWiFiSSID"; // Replace with your WiFi SSID
const char* password = "YourWiFiPassword"; // Replace with your WiFi password
const char* apiEndpoint = "http:///api/iotData/"; // Replace with your Django API endpoint

void setup() {
  Serial.begin(9600);
  connectToWiFi();
}

void loop() {
  if (Serial.available() > 0) {
    String receivedData = Serial.readStringUntil('\n');

    // Check if the received data is not empty
    if (!receivedData.isEmpty()) {
      Serial.println("Received Data: " + receivedData);
      processSerialData(receivedData);
    }

  }
}

void processSerialData(String data) {
  DynamicJsonDocument jsonDoc(1024);
  DeserializationError error = deserializeJson(jsonDoc, data);

  if (error) {
    Serial.print("Deserialization error: ");
    Serial.println(error.c_str());
    return;
  }

  float temperature = jsonDoc["temperature"].as(); // Ensure float type
  float pHValue = jsonDoc["pHValue"].as(); // Ensure float type
  int turbidity = jsonDoc["turbidity"].as(); // Ensure integer type

  // Print received values (optional)
  Serial.print("Temperature: ");
  Serial.println(temperature);
  Serial.print("pH Value: ");
  Serial.println(pHValue);
  Serial.print("Turbidity: ");
  Serial.println(turbidity);

  // Send data to Django API
  sendDataToDjangoAPI(temperature, pHValue, turbidity);

  // Clear the JSON document
  jsonDoc.clear();
}

void sendDataToDjangoAPI(float temperature, float pHValue, int turbidity) {
  if (WiFi.status() == WL_CONNECTED) {
    HTTPClient http;

    // Construct the JSON payload
    String payload = "{\"temperature\":" + String(temperature, 2) +
                     ",\"pHValue\":" + String(pHValue, 2) +
                     ",\"turbidity\":" + String(turbidity) + "}";

    // Make the HTTP POST request
    WiFiClient client;
    http.begin(client, apiEndpoint);
    http.addHeader("Content-Type", "application/json");
    int httpCode = http.POST(payload);

    if (httpCode == HTTP_CODE_OK) {
      Serial.println("Data sent successfully!");
      Serial.println(http.getString());
    } else {
      Serial.print("HTTP Error code: ");
      Serial.println(httpCode);
    }

    // Disconnect
    http.end();
  }
}

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

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.