0

My Python Program is,

from xbee import ZigBee
import serial
import sys

sPort = serial.Serial("/dev/ttyAMA0", 9600)
xbee = ZigBee(sPort)

targStr = sys.argv[1]
data = str(targStr)
print (data)
destAdd = "\x00\x13\xa2\x00\x40\xa9\xcc\xad"
xbee.send("tx",dest_addr_long=destAdd,dest_addr="\xff\fe",data=data)
print ("Sent!")

When I execute this python program on linux shell, it works all great! How do execute this program on certain event from HTML Page (Either using Javascript, AJAX or PHP) ?

2
  • Well, you can't do it directly from JavaScript (except in an environment like Node.js); and to call it with arguments from PHP, read the applicable "exec" documentation. PHP has a terrible API here, but using exec and escapeshellarg should get the job done. (The "command" to PHP's exec includes the parameters.) Commented Dec 26, 2014 at 3:38
  • tried exec, but still the page remains blank. and no effect on hardware side Commented Dec 26, 2014 at 4:53

1 Answer 1

1

You need to embed this code into some Python web server - for example Flask.

See example bellow - xbee_test.py (it is very raw, don't use that in production environment):

from flask import Flask
app = Flask(__name__)

from xbee import ZigBee
import serial

sPort = serial.Serial("/dev/ttyAMA0", 9600)
xbee = ZigBee(sPort)

@app.route("/<data>")
def index(data):
    destAdd = "\x00\x13\xa2\x00\x40\xa9\xcc\xad"
    xbee.send("tx",dest_addr_long=destAdd,dest_addr="\xff\fe",data=data)
    return "Sent!"

if __name__ == "__main__":
    app.run()

Now, when you run this in Python and hit http //localhost:5000/test_data you get your code executed.

Embedding this into HTML page as e.g. AJAX call is from this point on uncomplicated.

Here is how to obtain Flask installation and run that:

$ pip install Flask
$ python xbee_test.py
 * Running on http://localhost:5000/
Sign up to request clarification or add additional context in comments.

6 Comments

How do I run this ? Can you explain in a little detail ?
I add flask installation procedure + how to exec this.
It doesn't work. Still, I'm getting no response on page, neither any change in hardware side.
Anything in the console?
No! it's just blank page.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.