1

I have a challenge sending data from esp32 to my django server. I am using HttpClient and my site is hosted on ngrok. I have a sensor conneted to an Arduino Uno and the Arduino send sensor values to esp32 using serrial communication. I need to display these values on my django website. I had thought of using the Ethernet shield instead of esp32 but still facing te same problem, In the Ethernet shield configuratio, i need a port but my ngrok url does not have one or maybe it has but i dont know how to use it.

views.py

def post_entry(request):

    if request.method == 'POST':
        post_data = request.body
        
    return HttpResponse('Hey there im working')

The esp32 code:

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

const char* ssid = "";
const char* password = "";

void setup(){
    Serial.begin(115200);
    delay(1000);

    WiFi.mode(WIFI_STA); //Optional
    WiFi.begin(ssid, password);
    Serial.println("\nConnecting");

    while(WiFi.status() != WL_CONNECTED){
        Serial.print(".");
        delay(100);
    }

    Serial.println("\nConnected to the WiFi network");
    Serial.print("Local ESP32 IP: ");
    Serial.println(WiFi.localIP());
}

void loop(){
  if(WiFi.status()== WL_CONNECTED){   //Check WiFi connection status
  
   HTTPClient http;   
  
   http.begin("my-url-created-from-ngrok/post-entry/");  //Specify destination for HTTP request
   http.addHeader("Content-Type", "application/json");             //Specify content-type header
  
   int httpResponseCode = http.POST("POSTING from ESP32");   //Send the actual POST request
  
   if(httpResponseCode>0){
  
    String response = http.getString();                       //Get the response to the request
  
    Serial.println(httpResponseCode);   //Print return code
    Serial.println(response);           //Print request answer
  
   }else{
  
    Serial.print("Error on sending POST: ");
    Serial.println(httpResponseCode);
  
   }
  
   http.end();  //Free resources
  
 }else{
  
    Serial.println("Error in WiFi connection");   
  
 }
  
  delay(10000);  //Send a request every 10 seconds
  
}

im getting this erro on the serial monitor

Error on sending POST: -1

  • Error code -1 means that connection refused, check your URL and protocol (HTTP or HTTPS). BTW, if you are setting the content type to "application/json", then send a proper json object, not a random string "POSTING from ESP32". – hcheung Apr 12 '23 at 05:34
  • I changed the host and used python anywhere, now te challenge is on processing the data on my django view, how do i do that – Nolulamo Tshuma Apr 23 '23 at 19:49
  • Your original post is the problem with connectivity (error code -1). If you changed your code, and if it get connected now, edit your post and provide an update to your code, and explain the problem you are facing. on "on processing the data on my django view, how do i do that"? there are plenty of tutorials online to teach you that. – hcheung Apr 24 '23 at 01:11

0 Answers0