I am new to Django and even newer to ESP8266. I'm trying to send information from ESP8266 to my Django server using the POST method. The information looks like this:
Temperature=24.60&Humiditi=30.30&Soil=0.00&Water=0&Light=602.00
So my ESP8266 code looks like this:
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
#define POMPA1 D0
const char* ssid = "****";
const char* password = "*****";
String serverName = "http://127.0.0.1:8000/data/";
int POMP1 = HIGH;
char output;
String output_str;
String payload;
String server_output = "rain";
unsigned long lastTime = -20000;
unsigned long currentTime = 0;
unsigned long remeberedTime = 0;
int timeDelay = 20000;
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
pinMode(POMPA1, OUTPUT);
WiFi.begin(ssid, password);
Serial.println("Connecting");
while (WiFi.status() != WL_CONNECTED) {
delay(2000);
Serial.print(".");
Serial.println("");
Serial.print("Connected to WiFi network with IP Adress: ");
Serial.println(WiFi.localIP());
Serial.print("DNS IP: ");
Serial.println(WiFi.dnsIP());
}
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
}
void loop() { // run over and over
if (Serial.available()) {
output_str = Serial.readString();
Serial.println(output_str);
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
http.begin(serverName);
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
int httpCode = http.POST(output_str);
payload = http.getString();
Serial.println(httpCode);
Serial.println(payload);
http.end();
}
else {
Serial.println("Server Disconnected");
}
}
Serial.println(payload);
if (payload=="pompa1"){
currentTime = millis();
Serial.println("Let's rain");
if (currentTime >= lastTime + timeDelay) {
remeberedTime = currentTime;
lastTime = currentTime + 10000;
}
if (millis() - remeberedTime <= 10000){
digitalWrite(POMPA1, LOW);
}
else {
digitalWrite(POMPA1, HIGH);
}
}
delay(5000);
}
And the output in the console is :
-> Connected to WiFi network with IP Adress: 192.168.0.101
-> DNS IP: 192.168.0.1
So that's why I know that there is no problem with the server connection. Next here are my urls:
urlpatterns =[
path("", data, name="main-page"),
path("data/", get_data, name="get-data")
]
And here are my views:
def data(request):
if request.method=="GET":
return HttpResponse("<h1>Tutaj będą twoje wyniki</h1>")
def get_data(request):
if request.method=="POST":
print(request)
return HttpResponse("<h1>Tutaj są twoje wyniki </h1>")
For now, I just want to somehow get the information from ESP to the Django server so next, I can save them in my database. Does someone know where the problem is?
127.0.0.1or itself. This should have the IP address of your django server (likely 192.168.0.X). You will also need to check that the ALLOWED_HOSTS setting includes the same IP address else django will block the request.Django(or any other server) with127.0.0.1can be accessed only by programs on the same computer - it can be useful for security reason. Other computers may access it only ifDjangouse0.0.0.0(to listening requests from all network card in computer) or IP of network card which is used to connect fromESPto this computer. (PL:127.0.0.1pozwala na dostęp do Django tylko programom, które działają na tym samym komputerze co Django.Djangoużywając0.0.0.0nasłuchuje zgłoszeń na wszystkich swoich kartach sieciowych - czyli zgłoszeń ze wszystkich komputerów w lokalnej sieci)