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