rfid id20la + mysql + python + php + arduino

Python

Video is ready, Click Here to View ×


Send data from arduino to databse with rfid php and python programming
Stathis Votsis
stathisvotsis@gmail.com
Farsala Larisa Greece
23/4/15
tel: 6974090755
Send me e-mail for details of installing and programming

9 thoughts on “rfid id20la + mysql + python + php + arduino

  1. For php connection.php

    <?php
    $dbhost = 'localhost'; /////The name of local server
    $dbuser = 'root'; ////The name of user
    $dbpass = 'your_password'; /// No password
    $db = 'evotsis_rfidproducts'; ///The name of database

    $conn = mysql_connect($dbhost, $dbuser, $dbpass); ////Setting up connection to server
    mysql_select_db($db); ///select the database
    ?>

  2. For php index.php

    <?php
    header("refresh: 3;");
    include 'includes/connection.php'; /////Get folder includes with connection.php

    $query = "SELECT * FROM products"; //select everything from database
    $result = mysql_query($query);
    while($item = mysql_fetch_array($result)){
    echo "<h3>" . $item['name'] . "</h3>";
    echo "<h3>" . $item['quantity'] . "</h3>";
    }//display the data like as products exists
    ?>
    Αρχείο connection.php μέσα στο φάκελο includes για σύνδεση στη βάση δεδομένων
    <?php
    $dbhost = 'localhost'; /////The name of local server
    $dbuser = 'root'; ////The name of user
    $dbpass = 'your_password'; /// password
    $db = 'rfidproducts'; ///The name of database

    $conn = mysql_connect($dbhost, $dbuser, $dbpass); ////Setting up connection to server
    mysql_select_db($db); ///select the database
    ?>

  3. For arduino:

    int RFIDResetPin = 13;

    //Register your RFID tags here
    char tag1[13] = "750048114D61";
    char tag2[13] = "7500483CF8F9";
    char tag3[13] = "75004831E4E8";
    char tag4[13] = "75004817AA80";
    char tag5[13] = "7500485398F6";

    void setup(){
    Serial.begin(9600);

    pinMode(RFIDResetPin, OUTPUT);
    digitalWrite(RFIDResetPin, HIGH);

    //ONLY NEEDED IF CONTROLING THESE PINS – EG. LEDs
    pinMode(2, OUTPUT);
    pinMode(3, OUTPUT);
    pinMode(4, OUTPUT);
    pinMode(5, OUTPUT);
    pinMode(6, OUTPUT);
    pinMode(7, OUTPUT);
    pinMode(8, OUTPUT);
    pinMode(9, OUTPUT);
    pinMode(10, OUTPUT);
    pinMode(11, OUTPUT);
    }

    void loop(){

    char tagString[13];
    int index = 0;
    boolean reading = false;

    while(Serial.available()){

    int readByte = Serial.read(); //read next available byte

    if(readByte == 2) reading = true; //begining of tag
    if(readByte == 3) reading = false; //end of tag

    if(reading && readByte != 2 && readByte != 10 && readByte != 13){
    //store the tag
    tagString[index] = readByte;
    index ++;
    }
    }

    checkTag(tagString); //Check if it is a match
    clearTag(tagString); //Clear the char of all value
    resetReader(); //eset the RFID reader
    }

    void checkTag(char tag[]){
    ///////////////////////////////////
    //Check the read tag against known tags
    ///////////////////////////////////

    if(strlen(tag) == 0) return; //empty, no need to contunue

    if(compareTag(tag, tag1)){ // if matched tag1, do this

    Serial.println("apple");
    }else if(compareTag(tag, tag2)){ //if matched tag2, do this

    Serial.println("orange");
    }else if(compareTag(tag, tag3)){

    Serial.println("pear");
    }else if(compareTag(tag, tag4)){

    Serial.println("cherry");
    }else if(compareTag(tag, tag5)){

    Serial.println("banana");
    }

    else{
    Serial.println(tag); //read out any unknown tag
    Serial.write('0');
    }

    }

    void lightLED(int pin){
    ///////////////////////////////////
    //Turn on LED on pin "pin" for 250ms
    ///////////////////////////////////
    Serial.println(pin);

    digitalWrite(pin, HIGH);
    delay(250);
    digitalWrite(pin, LOW);
    }

    void resetReader(){
    ///////////////////////////////////
    //Reset the RFID reader to read again.
    ///////////////////////////////////
    digitalWrite(RFIDResetPin, LOW);
    digitalWrite(RFIDResetPin, HIGH);
    delay(1000);
    }

    void clearTag(char one[]){
    ///////////////////////////////////
    //clear the char array by filling with null – ASCII 0
    //Will think same tag has been read otherwise
    ///////////////////////////////////
    for(int i = 0; i < strlen(one); i++){
    one[i] = 0;
    }
    }

    boolean compareTag(char one[], char two[]){
    ///////////////////////////////////
    //compare two value to see if same,
    //strcmp not working 100% so we do this
    ///////////////////////////////////

    if(strlen(one) == 0) return false; //empty

    for(int i = 0; i < 12; i++){
    if(one[i] != two[i]) return false;
    }

    return true; //no mismatches
    }

  4. For python 2.7
    Install py.serial and MySAQLdb
    import serial
    import MySQLdb
    import time

    arduino = serial.Serial("COM5", 9600)
    db = MySQLdb.connect("108.167.189.14","root","your_password","evotsis_rfidproducts" )
    while True:
    data = arduino.readline()[:-2] #the last bit gets rid of the new-line chars
    data=str(data)
    print data

    cursor = db.cursor()
    sql = "UPDATE evotsis_rfidproducts.products SET quantity=quantity+'1' WHERE name='%s'" %data
    if data:
    cursor.execute(sql)

Leave a Reply

Your email address will not be published. Required fields are marked *