Как отправить значения из nodeMCU на сайт django
Я работаю над nodeMCU с датчиком влажности почвы и хочу показать его на моем сайте django. Я очень близок к этому, но когда я отправляю данные с nodeMCU на сервер django, я вижу http 200 запрос с обеих сторон (со стороны nodeMCU и со стороны dajngo). Но главная проблема в том, что я не могу показать значения в реальном времени на моей веб-странице dajngo. У кого-нибудь есть знания об этом, пожалуйста, поделитесь, возможно, это будет очень полезно для меня.
Проект urls.py
from django.contrib import admin
from django.urls import path,include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('SIS_APP.urls')),
#1.nodeMCU hit data on this url
path('update', include('SIS_APP.urls')),
]
App urls.py
from django.contrib import admin
from django.urls import path
from . import views
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.index, name='index'),
#2.nodeMCU hit data on this url
path('update', views.update, name='update'),
]
views.py
from django.shortcuts import render
from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt
# Create your views here.
def index(request):
return render (request, 'index.html')
#this function call when nodeMCU hit this url
@csrf_exempt
def update(request):
value = request.POST.get("httpResponseCode")
#this value show "None" on webpage
return HttpResponse (value)
Nodemcu.ino
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
const char* ssid = "****";
const char* password = "*****";
// Domain Name with full URL Path for HTTP POST Request
const char* serverName = "http://machine_ip_here:8000/update";
// THE DEFAULT TIMER IS SET TO 10 SECONDS FOR TESTING PURPOSES
// For a final application, check the API call limits per hour/minute to avoid getting
blocked/banned
unsigned long lastTime = 0;
// Set timer to 10 minutes (600000)
//unsigned long timerDelay = 600000;
// Timer set to 10 seconds (10000)
unsigned long timerDelay = 1000;
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
Serial.println("Connecting");
while(WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to WiFi network with IP Address: ");
Serial.println(WiFi.localIP());
Serial.println("Timer set to 10 seconds (timerDelay variable), it will take 10 seconds before
publishing the first reading.");
// Random seed is a number used to initialize a pseudorandom number generator
randomSeed(analogRead(0));
}
void loop() {
//Send an HTTP POST request every 10 seconds
if ((millis() - lastTime) > timerDelay) {
//Check WiFi connection status
if(WiFi.status()== WL_CONNECTED){
WiFiClient client;
HTTPClient http;
// Your Domain name with URL path or IP address with path
http.begin(client, serverName);
// Specify content-type header
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
// Data to send with HTTP POST
String httpRequestData = String(random(40));
// Send HTTP POST request
int httpResponseCode = http.POST(httpRequestData);
/*
// If you need an HTTP request with a content type: application/json, use the following:
http.addHeader("Content-Type", "application/json");
// JSON data to send with HTTP POST
String httpRequestData = "{String(random(40))}";
// Send HTTP POST request
int httpResponseCode = http.POST(httpRequestData);*/
Serial.print("HTTP Response code1: ");
Serial.println(httpResponseCode);
// Free resources
http.end();
}
else {
Serial.println("WiFi Disconnected");
}
lastTime = millis();
}
}