0

Im making a web interface for a RFID reader and trying to make it so there is a button to push then it waits for the user to scan a card and retrieves the UID of the card. I was thinking of using ajax to call a php script which calls the python script, but it doesnt wait for the card to be scanned. Heres the ajax:

<script type = "text/javascript">
function scancard () {
    $.ajax( { type : 'GET',
                        data : { },
                        url    : 'scancard.php',
                        success: function (data) {
                            var card_id = document.getElementById('card_id');
                            card_id.value = data;
                        },
                        error: function (xhr) {
                            alert( "error");
                        }
                    });    
}
</script>

And the button html:

<button onclick="scancard()">Scan Card</button>

Testing with a simple echo cmd it fills in the field but then refreshes the page and the field is blank again.

The next problem I have is when I put in this for the php script to call the python code:

<?php
  exec('sudo python scancard.py', $output, $return);
  echo $return;
?>

It doesnt wait for the python code to finish before it does the echo command. The python code has a while loop in it that is waiting for a RFID card to be read and then returns the uid code.

# Lots of configuration and setup stuff above this...it works when run via command line
# Main loop to detect cards and read a block.
while True:
    uid = pn532.read_passive_target()
    if uid is None:
        continue
    else:
        if not pn532.mifare_classic_authenticate_block(uid, 4, PN532.MIFARE_CMD_AUTH_B,
                                                   [0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF]):
            continue
        uid = '0x{0}'.format(binascii.hexlify(uid))
        sys.exit(uid)

I might be going about this in a roundabout way as well so if there is a better way to do this, please let me know

DRing
  • 6,825
  • 6
  • 29
  • 45
  • Did you try one of the other command-related functions? http://php.net/manual/en/function.shell-exec.php – A.L Jul 05 '17 at 15:59
  • Just tried it and same results – DRing Jul 05 '17 at 16:08
  • 1
    That might not work because you're calling `sudo` and it's probably returning an error or something. Give your web server permissions to execute that file and try it without the `sudo` part. – odannyc Jul 05 '17 at 16:18
  • if I remove the sudo it gets an error in the ajax function – DRing Jul 05 '17 at 16:35

0 Answers0