Post data to index.html

819 views
Skip to first unread message

Ruben Mendes

unread,
Sep 20, 2014, 5:08:33 AM9/20/14
to web...@googlegroups.com
Hello,

can anyone help me to transform this programm to send the data to the index.html, I'm stuck on this because I don't have experience with python.

Thanks in advance.



GPSController.py

from gps import *
import time
import threading
import math

class GpsController(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)
        self.gpsd = gps(mode=WATCH_ENABLE) #starting the stream of info
        self.running = False
    
    def run(self):
        self.running = True
        while self.running:
            # grab EACH set of gpsd info to clear the buffer
            self.gpsd.next()

    def stopController(self):
        self.running = False
  
    @property
    def fix(self):
        return self.gpsd.fix

if __name__ == '__main__':
    # create the controller
    gpsc = GpsController() 
    try:
        # start controller
        gpsc.start()
        while True:
            print "latitude ", gpsc.fix.latitude
            print "longitude ", gpsc.fix.longitude
            time.sleep(0.5)

    #Error
    except:
        print "Unexpected error:", sys.exc_info()[0]
        raise

    #Ctrl C
    except KeyboardInterrupt:
        print "User cancelled"

    finally:
        print "Stopping gps controller"
        gpsc.stopController()
        #wait for the tread to finish
        gpsc.join()
      
    print "Done"

Toshi Bass

unread,
Sep 20, 2014, 8:08:37 AM9/20/14
to web...@googlegroups.com
This is the webiopi help group your code has nothing to do with webiopi try some python help group.

Ruben Mendes

unread,
Sep 20, 2014, 8:51:11 AM9/20/14
to web...@googlegroups.com
I'm using the webiopi, I just would like to post the results I get with this code in the index of webiopi

JOHN D

unread,
Sep 22, 2014, 8:11:46 AM9/22/14
to web...@googlegroups.com
Unfortunately I don't know enough about Python to suggest anything but you need to get that code in to a function so you can call it and return it with JavaScript.  Perhaps you can create a function that calls that code and then return what you need.

JOHN D

unread,
Sep 22, 2014, 8:39:00 AM9/22/14
to web...@googlegroups.com
Actually, now that I'm looking at it some more you might just be able to do this.  But again, I don't know if it will work.  I put comments after the changes I made and then I added some code at the bottom.

from gps import *
import time
import threading
import math
import webiopi ######

class GpsController(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)
        self.gpsd = gps(mode=WATCH_ENABLE) #starting the stream of info
        self.running = False
   
    def run(self):
        self.running = True
        while self.running:
            # grab EACH set of gpsd info to clear the buffer
            self.gpsd.next()

    def stopController(self):
        self.running = False
 
    @property
    def fix(self):
        return self.gpsd.fix

def loop(): #####
    # create the controller
    gpsc = GpsController()
    try:
        # start controller
        gpsc.start()
        while True:
            print "latitude ", gpsc.fix.latitude
            print "longitude ", gpsc.fix.longitude
            time.sleep(0.5)

    #Error
    except:
        print "Unexpected error:", sys.exc_info()[0]
        raise

    #Ctrl C
    except KeyboardInterrupt:
        print "User cancelled"

    finally:
        print "Stopping gps controller"
        gpsc.stopController()
        #wait for the tread to finish
        gpsc.join()
     
    print "Done"

@webiopi.macro
def read_latitude():
   
gpsc = GpsController()
   
return
"latitude ", gpsc.fix.latitude

@webiopi.macro
def read_longitude():
   
gpsc = GpsController()
    return
"longitude ", gpsc.fix.longitude


Then you can use a button or something to call read_latitude and read_longitude or you can combine it to include both. 

Ruben Mendes

unread,
Sep 29, 2014, 4:50:59 AM9/29/14
to web...@googlegroups.com
Thanks,

now i'm having some problems posting the data on html

this is what i'm doing

[code]
button = webiopi().createButton("getLat", "Latitude", callLatMacro);
content.append(button);


function callLatMacro() {
   webiopi().callMacro("read_latitude", null, callLatMacroCallback);
}
function callLatMacroCallback(macro, args, data) {
alert(data);
}
[/code]

Toshi Bass

unread,
Sep 29, 2014, 6:21:38 AM9/29/14
to web...@googlegroups.com
Ruben

Posting small portion of your code obviously makes it hard to see all faults, first how are you running webiopi?, you should be starting webiopi in debug mode with     sudo webiopi -d -c /etc/webiopi/config     if you did and your code looks like this :

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content = "height = device-height, width = 420, user-scalable = no" /> 
<title>Toshi | Demo</title>
<script type="text/javascript" src="/webiopi.js"></script>
<script type="text/javascript">
webiopi().ready(function() {

                button = webiopi().createButton("getLat", "Latitude", callLatMacro);
content.append(button);

webiopi().refreshGPIO(true);
});

function callLatMacro() {
    webiopi().callMacro("read_latitude", null, callLatMacroCallback);
}
function callLatMacroCallback(macro, args, data) {
alert(data);
}
</script>

You would see on the debug screen that the error is your sending 2 args from python

    return "latitude", gpsc.fix.latitude

But the function callLatMacro is expecting only one

Suggest you try following in you python script

@webiopi.macro
def read_latitude():
#    gpsc = GpsController() 
#    return "latitude ", gpsc.fix.latitude
    gpsc = 99
    print (gpsc)
    return (gpsc)

When that works then replace gsp = 99 with your result from your GspControler

Toshi



Ruben Mendes

unread,
Sep 30, 2014, 4:30:19 AM9/30/14
to web...@googlegroups.com
Hello Toshi,

I have this problem with webiopi

from gps import *
ImportError: No module named gps

does it have to do with the version of python used by webiopi?

Ruben Mendes

unread,
Sep 30, 2014, 4:36:38 AM9/30/14
to web...@googlegroups.com
I have the libraries:

/usr/lib/python2.6/dist-packages/gps-3.6.egg-info
/usr/lib/python2.6/dist-packages/gps/misc.py
/usr/lib/python2.6/dist-packages/gps/client.py
/usr/lib/python2.6/dist-packages/gps/__init__.py
/usr/lib/python2.6/dist-packages/gps/gps.py
/usr/lib/python2.6/dist-packages/gps/fake.py
/usr/lib/python2.7/dist-packages/gps-3.6.egg-info
/usr/lib/python2.7/dist-packages/gps/misc.py
/usr/lib/python2.7/dist-packages/gps/client.py
/usr/lib/python2.7/dist-packages/gps/__init__.py
/usr/lib/python2.7/dist-packages/gps/gps.py
/usr/lib/python2.7/dist-packages/gps/fake.py

Ruben Mendes

unread,
Sep 30, 2014, 5:19:15 AM9/30/14
to web...@googlegroups.com
Looks like webiopi is installed on version 3.2 of python and that there is no gps library for that version

Toshi Bass

unread,
Sep 30, 2014, 12:07:30 PM9/30/14
to web...@googlegroups.com
Ruben

Webiopi doesn't support gps currently, so I guess your trying to use the external gps libraries purely in python, then use webiopi to transfer the info to index.html 

I assume you tried getting results from the external libraries as per your first post (which had nothing to do with webiopi) and you were successful ?

If so were you then using python 2.7 ?

If so then its easy to load WebIOPi-0.7.0 for python 2.7  instead of WebIOPi-0.7.0 for python 3.2 as follows:

Edit /home/pi/webiopi/setup.sh   3rd line from top says      SEARCH="python python3"  delete   python3   so it says   SEARCH="python"  save the file.

From your ssh window ./home/pi/webiopi/setup.sh   that will re-install webiopi for python2.7  then you should be able to try it.

Hopefully the error your getting isn't from webiopi stating that there is no webiopi module driver for gps 

Toshi 

Ruben Mendes

unread,
Oct 1, 2014, 5:53:28 AM10/1/14
to web...@googlegroups.com
Well now webiopi is running with the python version 2.7

But when in the HTML page I push the button, it gives me the 99, but when I use this:

@webiopi.macro
def read_latitude():
    gpsc = GpsController() 
    return "latitude ", gpsc.fix.latitude

it gives me an empty box for some reason.

In the ssh I have it printing the result of latitude and its fine, but it's not exporting to html




Toshi Bass

unread,
Oct 1, 2014, 10:14:37 AM10/1/14
to web...@googlegroups.com


if result is a string try:

    return ("%s" % (gpsc.fix.latitude))

or alternatively

    return ("%d" % (gpsc.fix.latitude))

 Toshi

Ruben Mendes

unread,
Oct 2, 2014, 4:14:25 AM10/2/14
to web...@googlegroups.com
Thank you Toshi, I had to use a global variable to save the result and then print it.

Now in the index.html, it theres any way to use that global variable?

Toshi Bass

unread,
Oct 2, 2014, 5:01:14 AM10/2/14
to web...@googlegroups.com

Ruben


Now in the index.html, it theres any way to use that global variable?  

Use ?  what do you want to do with the variable ? you need to be a bit more specific !

__________________________________________________

For instance:

I assume you were successful with

   function callLatMacroCallback(macro, args, data) {
alert(data);
}

and alert(data) is poping up with your variable so if say you want to display the variable on you web page you could add the following:

   function callLatMacroCallback(macro, args, data) {
myDivi0.innerHTML = "Latitude = "+ data.split(" ")[0];
}

</script>


Then display it in a table or something

<body>

<div align="center">

<table width="160" border="2" bgcolor="Silver">
<tr>
<td><div id="myDivi0"</div></td>
       </tr>
</table

</div>

</body>

</html>

Toshi

Ruben Mendes

unread,
Oct 2, 2014, 5:07:02 AM10/2/14
to web...@googlegroups.com
Hi Toshi,

i'm using the google maps, and I want to replace this values with the ones I get from my GPS

var position = new google.maps.LatLng(-34.397, 150.644);

that's why I want the value of longitude and latitude to replace in the map.

Toshi Bass

unread,
Oct 2, 2014, 5:55:52 AM10/2/14
to web...@googlegroups.com
Perhaps something like this:

        var x=0;
        var y=0;

        //your code

   function callLatMacroCallback(macro, args, data) {
                
x = "data.split(" ")[0];
                alert(x)                                                               //<remove after testing
}

     var position = new google.maps.LatLng(x, y);
}
     
Toshi
Reply all
Reply to author
Forward
0 new messages