0

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?

Tim
  • 2,510
  • 1
  • 22
  • 26
Nero515
  • 21
  • 6
  • 4
    Check your server name, it's currently pointing at `127.0.0.1` or 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. – Tim May 15 '21 at 13:36
  • @Tim Okay but the Django server is set on the local machine so if I understand this correctly this one should be fine. This is the output after starting server - Starting development server at http://127.0.0.1:8000/ – Nero515 May 15 '21 at 14:08
  • And what does "local" mean? 127.0.0.1 is shorthand for "this computer" - that's what localhost means. It will only get you to your Django server when used on your Django server. It won't get you anywhere on and ESP8266. You need to use the actual IP address of the Django server, like @Tim said. – romkey May 15 '21 at 14:39
  • `Django` (or any other server) with `127.0.0.1` can be accessed only by programs on the same computer - it can be useful for security reason. Other computers may access it only if `Django` use `0.0.0.0` (to listening requests from all network card in computer) or IP of network card which is used to connect from `ESP` to this computer. (PL: `127.0.0.1` pozwala na dostęp do Django tylko programom, które działają na tym samym komputerze co Django. `Django` używając `0.0.0.0` nasłuchuje zgłoszeń na wszystkich swoich kartach sieciowych - czyli zgłoszeń ze wszystkich komputerów w lokalnej sieci) – furas May 15 '21 at 16:15

1 Answers1

0

Guys up there get me back on the right track. They were right that I was giving the wrong address because 127.0.0.1 is for application on local host. So first of all I needed to change the address to 0.0.0.0 to allow connection to the server to other devices connected to the net.

python manage.py runserver 0.0.0.0:8000

then I needed to check IP of my computer because it was hosting the server so this supposed to be IP I should connect. I checked the IP using (for Linux):

ip addr show

then I needed to add addresses to Django settings in settings.py

ALLOWED_HOSTS ["0.0.0.0", "your_machine_IP"]

then I only needed to change the code for ESP82666 to connect to proper IP:

String serverName = "http://your_machine_IP:8000/data/";

And it worked perfectly! Thank everyone to contribute finally I can move forward with my project.

Nero515
  • 21
  • 6