Arduino RFID-RC522 transmit UID to Waspmote using UART, then send UID to Raspberry Pi with DRAGINO LoRa shield through LoRa P2P, then insert record into MySQL.

It is quite complex solution as the environmental constrains.
My DB server is in a lab and the distance from the door to the lab is quite far, so the WiFi cannot reach. So the only fast and cheap solution is to use LoRa, lucky we brought them.

Arduino RFID-RC522 read UID

The code & connection diagram for RFID-RC522 of Arduino is very easy to find in the internet, has plenty of them.

UART communication between Arduino & Waspmote

Since we only need to transmit the UID to Waspmote, does not require feedback, so we only need to connect the TX pin of Arudino to the RX pin of the Waspmote. However, the primary serial port is used by the LoRa, so we need to use the auxiliary serial port, Waspmote does have 2 auxiliary serial ports, thanks to that. Just need to modify the Radio P2P example - Sending packets of the Waspmote LoRa example and add a few line of code, as below.

#include "WaspUART.h"

WaspUART uart = WaspUART();

void setup()
{
  USB.ON();
  USB.println(F("Radio P2P example - Sending packets\n"));

  // module setup
  error = radioModuleSetup();
  
  // Check status
  if (error == 0)
  {
    USB.println(F("Module configured OK"));     
  }
  else 
  {
    USB.println(F("Module configured ERROR"));     
  }  

  uart.setBaudrate(9600);
  uart.setUART(1);
  uart.beginUART();
  Utils.setMuxAux1();
  beginSerial(9600,1);
}

void loop()
{
  uart.readBuffer(sizeof(uart._buffer));
  if (uart._length > 0)
  {
    for (int i = 0; i < 8; i++)
    {
      uid[i * 2 + 1] = uart._buffer[i];
    }
    
    // Send packet
    error = LoRaWAN.sendRadio(uid);
    
    // Check status
    if (error == 0)
    {
      USB.println(F("--> Packet sent OK"));
    }
    else 
    {
      USB.print(F("Error waiting for packets. error = "));  
      USB.println(error, DEC);   
    }
  }
  delay(1000);
}
#
#
#

Waspmote send UID to Raspberry Pi with DRAGINO LoRa shield through LoRa P2P

To setup, I mainly followed the instructions from Use Lora/GPS HAT + RaspberryPi to set up a Lora Node.

  1. Use 'raspi-config' to ensure that SPI can be used on RPi;

  2. Use 'git clone git://git.drogon.net/wiringPi' to install the GPIO access library written in C for the BCM2835 used in the Raspberry Pi;

  3. Get the single channel LoRa Gateway source code from HERE.

  4. Edit the 'main.cpp'.

Since I am not going to send any data to the internet, I have made quite modifications, I removed all the codes that used to communicate to the TTN network and debug printings. But, I didn't change any of the logic, the only thing I add, I will discuss it next.

Raspberry Pi insert record into MySQL once received.

Since the code using for receiving is written in C, so it will be quite troublesome to do SQL operations. My solution is to use python script to do the insertion. In order for C to execute Python script, you need to do the following.

  1. Run sudo apt-get install python-dev If you don't have Python.h, which usually not necessary.

  2. Modify the code.

    #include <Python.h>
    
        #add the code snippet after successfully received in the receivePkt(char *payload) function
        FILE *cp = fopen("insertUID.py", "r");
        Py_Initialize();
        char *argv[2];
        argv[0] = "insert";
        argv[1] = payload;
        PySys_SetArgv(2, argv);
        int rc = PyRun_SimpleFile(cp, "insertUID.py");
        fclose(cp);
        Py_Finalize(); 
  3. Write a python script to do the insertion.

    import mysql.connector
    import sys
    
    cnx = mysql.connector.connect(host='10.228.240.101', user='root', database='e-fire_register')
    cursor = cnx.cursor()
    
    uid_insert = "INSERT INTO UserActivity (UID) VALUES (%s)"
    uid_data = (sys.argv[1],)
    
    cursor.execute(uid_insert, uid_data)
    
    cnx.commit()
    cursor.close()
    cnx.close()
  4. Modify the Makefile to include the Python.h in order to build successfully.

CC=g++
CFLAGS=-c -Wall
LIBS=-lwiringPi
LIBS_Py=-I/usr/include/python2.7 -lpython2.7

all: single_chan_pkt_fwd

single_chan_pkt_fwd: base64.o main.o
    $(CC) main.o base64.o $(LIBS) $(LIBS_Py) -o single_chan_pkt_fwd

main.o: main.cpp
    $(CC) $(CFLAGS) main.cpp $(LIBS_Py) 

base64.o: base64.c
    $(CC) $(CFLAGS) base64.c

clean:
    rm *.o single_chan_pkt_fwd    

TODO: The Waspmote V1.2 above does have a SPI interface, so it may be possible that the Waspmote could read from the RFID-RC522 directly.


HiroshiFuu
49 声望7 粉丝

引用和评论

0 条评论