Arduino Uno RFID RC522 Django

Hello, i want to create a system with arduino uno rc522 and django to display the uid's of the cards that will be read by rc522. I tried different methods like serial communication, pyserial libraries etc. but unfortunately, i could not make any success. I would like to listen all the advices. Thank you.

Sorry I couldn't find the code you used for these tries in your post. You might have forgotten to post it. You should also post a link to the RC522 board you're using, a wiring diagram of your setup and the output you got in your tries.

Sorry, at first, i am using this code to get uid of the cards.

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

#define SS_PIN 10
#define RST_PIN 9

MFRC522 rfid(SS_PIN, RST_PIN);

void setup() {
  Serial.begin(9600);
  SPI.begin();
  rfid.PCD_Init();
}

void loop() {
  // Check if a card is present
  if (rfid.PICC_IsNewCardPresent() && rfid.PICC_ReadCardSerial()) {
    // Read the UID of the card
    String uid = "";
    for (byte i = 0; i < rfid.uid.size; i++) {
      uid += String(rfid.uid.uidByte[i] < 0x10 ? "0" : "");
      uid += String(rfid.uid.uidByte[i], HEX);
    }
    Serial.println(uid);
    delay(1000);  // Delay for 1 second before reading the next card
    rfid.PICC_HaltA();
    rfid.PCD_StopCrypto1();
  }
}
After that, at the python site, i was reading the values from serial port. The python script is below;

import requests
import serial

ser = serial.Serial('COM4', 9600)

while True:
if ser.in_waiting > 0:
uid = ser.readline().decode().strip()
print("UID:", uid)

    url = "http://127.0.0.1:8000/rfid-data/"  # Write the url of the Django server
    data = {"uid": uid}

    try:
        response = requests.post(url, data=data)
        if response.status_code == 200:
            print("Published succesfully.")
        else:
            print("Error at publishing:", response.status_code)
    except requests.exceptions.RequestException as e:
        print("Request Error:", e)
Then, i created a django server at local, i can share the views.py, urls.py;
views.py

from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt
import logging

logger = logging.getLogger(name)

@csrf_exempt
def rfid_view(request):
if request.method == 'POST':
uid = request.POST.get('uid')
if uid:
logger.info("Card UID: %s", uid)
return HttpResponse(status=200)
elif request.method == 'GET':
uid = request.GET.get('uid')
if uid is not None: # None check
logger.info("Card UID: %s", uid)
return HttpResponse(status=200)
return HttpResponse(status=400)

urls.py

from django.urls import path
from rfidreader.views import rfid_view

urlpatterns = [
path('rfid-data/', rfid_view, name='rfid_data'),
]

It was my first time using forum, forgive me for making any mistakes.

You shouldn't use the String class on AVR Arduinos (the UNO is using the AVR platform). Try to replace that with C strings (character arrays).

Does the UNO sketch for itself work? Post some output you get!

Setting the code tags for the Python script failed in your post. Please edit the post and set the code tags correctly, as we cannot see indenting and that's important in Python as you know.

I asked you for the output you get. Post a log file excerpt from the server. I'm not a Django expert but it seems from your programming that it automatically assumes JSON input and decodes that. Your Python HTTP client doesn't set a content-type header so I'm a bit confused where the server knows from what type of data it receives.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.