Django receive POST data from NodeMCU ESP8266HTTPClient


Hi I’m new to Django framework and I am on a project on which to send http.POST data from NodeMCU to the web application using the Django Framework and the sent data is a string ID of the RFID card.

I want to render the POST data sent by the NodeMCU to the textarea in the template below. I dont know how to handle http.POST sent by the NodeMCU using the Django Framework.

I tested the views functions and it rendered data to the templates. I don’t if the data is successfully sent or received.

Arduino version 1.8.13

Django version 3.1.4

Any help would be appreciated

 from django.shortcuts import render

# Create your views here.

def card_reg(request):
    id = request.POST.get('UIDresult', 'whatever')
    context = {
        'id': id
    return render(request, 'nodemcuapp/card_reg.html', context)


      {% extends 'nodemcuapp/main.html' %}

{%block content%}
 <div class="container">
      <div class="center" style="margin: 0 auto; width:495px; border-style: solid; border-color: #f2f2f2;">
        <h3 align="center" class="text-primary">Registration Form</h3>
        <form action=" " method="POST" >
          <div class="form-group">
            <label class="text-primary">ID</label>
              <textarea class="form-control" name="id" id="getUID" placeholder="Please Tag your Card to display ID" rows="1" cols="1" required>{{id}}</textarea>

            <label class="text-primary">Name</label>
              <input class="form-control" id="div_refresh" name="name" type="text"  placeholder="" required>

          <div class="form-group">
            <label class="text-primary">Email Address</label>
              <input class="form-control" name="email" type="text" placeholder="" required>
            <div class="row">
              <div class="col-md-7">
                <label class="text-primary">Mobile Number</label>
                  <input class="form-control" name="mobile" type="text"  placeholder="" required>

                <div class="col-md-5">
                    <label class="text-primary">Select</label>
                  <select class="custom-select mr-sm-2" id="inlineFormCustomSelect">
                    <option selected>Gender</option>
                    <option value="Male">Male</option>
                    <option value="Female">Female</option>
          <div class="form-group">
            <button type="submit" class="btn btn-primary form-control">Save</button>


from django.urls import path
from . import views

urlpatterns = [
    path('', views.home, name='home'),
    path('user_data', views.user_data, name='user_data'),
    path('card_registration/', views.card_reg, name='card_reg'),
    path('read_card', views.card_read, name='card_read'),


#include <ESP8266WebServer.h>
#include <ESP8266HTTPClient.h>
#include <SPI.h>
#include <MFRC522.h>

#define SS_PIN D2  //--> SDA / SS is connected to pinout D2
#define RST_PIN D1  //--> RST is connected to pinout D1
MFRC522 mfrc522(SS_PIN, RST_PIN);  //--> Create MFRC522 instance.

#define ON_Board_LED 2  //--> Defining an On Board LED, used for indicators when the process of connecting to a wifi router

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

ESP8266WebServer server(80);  //--> Server on port 80

int readsuccess;
byte readcard[4];
char str[32] = "";
String StrUID;

void setup() {
  Serial.begin(115200); //--> Initialize serial communications with the PC
  SPI.begin();      //--> Init SPI bus
  mfrc522.PCD_Init(); //--> Init MFRC522 card


  WiFi.begin(ssid, password); //--> Connect to your WiFi router
  digitalWrite(ON_Board_LED, HIGH); //--> Turn off Led On Board

  while (WiFi.status() != WL_CONNECTED) {
    digitalWrite(ON_Board_LED, LOW);
    digitalWrite(ON_Board_LED, HIGH);
  digitalWrite(ON_Board_LED, HIGH); 
  Serial.print("Successfully connected to : ");
  Serial.print("IP address: ");

  Serial.println("Please tag a card to display UID !");

void loop() {
  // put your main code here, to run repeatedly
  readsuccess = getid();
  if(readsuccess) {  
  digitalWrite(ON_Board_LED, LOW);
    HTTPClient http;    //Declare object of class HTTPClient
    String UIDresultSend, postData;
    UIDresultSend = StrUID;
    //Post Data
    postData = "UIDresult=" + UIDresultSend;
    http.begin("");  //Specify request destination
    http.addHeader("Content-Type", "application/x-www-form-urlencoded"); //Specify content-type header
    int httpCode = http.POST(postData);   //Send the request
    String payload = http.getString();    //Get the response payload
    Serial.println(httpCode);   //Print HTTP return code
    Serial.println(payload);    //Print request response payload
    http.end();  //Close connection
  digitalWrite(ON_Board_LED, HIGH);

int getid() {  
  if(!mfrc522.PICC_IsNewCardPresent()) {
    return 0;
  if(!mfrc522.PICC_ReadCardSerial()) {
    return 0;
  for(int i=0;i<4;i++){
    readcard[i]=mfrc522.uid.uidByte[i]; //storing the UID of the tag in readcard
    array_to_string(readcard, 4, str);
    StrUID = str;
  Serial.print("THE UID OF THE SCANNED CARD IS : ");
  return 1;

void array_to_string(byte array[], unsigned int len, char buffer[]) {
    for (unsigned int i = 0; i < len; i++)
        byte nib1 = (array[i] >> 4) & 0x0F;
        byte nib2 = (array[i] >> 0) & 0x0F;
        buffer[i*2+0] = nib1  < 0xA ? '0' + nib1  : 'A' + nib1  - 0xA;
        buffer[i*2+1] = nib2  < 0xA ? '0' + nib2  : 'A' + nib2  - 0xA;
    buffer[len*2] = '\0';


You need to create a to make it work on your for this follow up the instructions below.

from django import forms

class SaveForm(forms.Form):
    UIDresult = forms.CharField(label='UIDresult', max_length=200)

from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt
# Note please import here ....

def card_registrations(request):
    form = SaveForm(request.POST)
    context = {
        id: form.cleaned_data.get('UIDresult'),
    return render(request, 'nodemcuapp/card_reg.html', context)

Answered By – Eddwin Paz

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published