Pushing Latitude and Longitude to Xively based on BSSID?

Does anyone have sample code showing how to pass latitude and longitude to Xively? I would like to pass discrete Lat and Long values based on BSSID. My device connects to several known A/Ps and I’d like to use that as an indication of a change in location on Xively. The way I imagine it would work is with a series of IF/ELSE IF statements, such as

IF impgetbssid() = 0026fd6570
Latlong = XXXXXXX

ELSE IF impgetbssid() = ec43f611d20
Latlong = YYYYYYY

Then it would post the XXXXX or YYYY to the Xively feed for that device and it would show the change in location on the map. I know there are ways of getting more accurate location information using google APIs and scanning available A/Ps, but for my purposes, this simple set up would suffice. Please excuse my rudimentary grasp of programming. Any assistance would be appreciated.

I actually added that part to the Xively code on the Electric Imp github page. You can copy and paste the entire namespace into your agent code and then add something like this function:

`client <- Xively.Client(APIKEY);

function setLocation(ele, lat, lon) {
server.log(“sending to Xively”);
location <- Xively.Location(“Enter your Feed ID here”);
location.Set(“mobile”, “some_city”, “outdoor”, “physical”, ele, lat, lon)
client.PutLocation(location);
}`

How about using a GPS? I really need to finish the code for the Adafruit GPS breakout.

GPS increases cost and power consumption and much of my use will be indoors. It might be an add-on feature later but for now location determined by BSSID is good enough. I am trying to keep the overall device cost as low as possible. I assume lat long data is in decimal form? Thanks for the agent code. I am still having trouble with my IF/ESLE IF. I’ll take a look at the github page again. Thanks.

Ok, I’m sure this could be written better, but it works.

Device Code:

`
bssid1 <- “c0c1c05b1638”;
bssid2 <- “c0c1c05b1639”;
bssid1_loc <- {“lat”:40.1234, “lon”:-76.1234};
bssid2_loc <- {“lat”:40.5678, “lon”:-76.5678};

function checkLocation() {
local current_bssid = imp.getbssid();
server.log("BSSID: " + imp.getbssid());
if (current_bssid == bssid1) {
agent.send(“bssid”, bssid1_loc);
}
else if (current_bssid == bssid2){
agent.send(“bssid”, bssid2_loc)
}
else {
//Do something else
}
}

checkLocation();
`

Agent Code minus Xively namespace:

`
client <- Xively.Client(APIKEY);

function setLocation(ele, lat, lon) {
server.log(“sending to Xively”);
location <- Xively.Location(“YOUR XIVELY FEED ID”);
location.Set(“mobile”, “YOUR CITY”, “outdoor”, “physical”, ele, lat, lon)
client.PutLocation(location);
}

device.on (“bssid”, function(data) {
setLocation(0, data.lat, data.lon);
});
`

If I leave the line client <- Xively.Client(APIKEY); in the agent code, my imp goes into an endless loop and I see these errors:

[Device] ERROR: the index ‘Xively’ does not exist
[Device] ERROR: at main:98

where that line is at line 98 in the devce code. If I take that line out, I don’t get the endless loop and my device goes back to sleep for 15 minutes as it should. But the lat/lon update does not work and instead I get this error:

[Agent] no handler for agent.send()

Otherwise my device and agent code work fine and I continue to update the three separate feeds on Xively for this device (battery level, RSSI and temperature). I am not sure what I may have done wrong:(. My agent and device code for Xively is a slightly modified version of the code done for Growerbot by havoc.

Sorry - that first sentence should read “in the device code”

@joeolives Since pasting into the forum can get a little messy, I’ve created a GitHub repository here for this project. You can copy and paste or fork from there.

You’ll need to make sure that the Xively namespace you are using has all of the classes and functions. I only added the putLocation peice a few months ago. I’m going to post my code exactly as I have it running minus my API-key, ect.

I’ll give this a shot when I get home tonight. Thanks a million for the assistance.

That definitely works if I only use that to update location. Unfortunately, my attempt to merge my previously functioning 3 channel agent with the new location code either breaks the channel feed or location update. I’ve posted my failed merged code and my current working code here: https://github.com/joeolives/xively-bssid
I am new to using github so hopefully you can make sense of that. Thanks.

Looks like you are just missing the putLocation function near the top of the Xively class in the combined code.

You rock! That did it. I cleaned up a bit of my mess by substituting the constants for the api key (XIVELY_API_KEY) and feed ID (XIVELY_FEED_ID) for APIKEY and “Feed ID here” and that didn’t seem to break anything. Thanks a milion for the handholding. My last stint programming involved punch cards:), so it has been a while. Looks like XIvely wants to truncate my lat and lon data to four decimal places from my six, but that is good enough to show me my different AP locations.

Yep, I noticed that too. You can pass the keys and IDs around however you like, as long as they get into the function properly to be sent. In fact, you could name the channel after your BSSID and pass it up from the device. Xively will allow you to create channels that don’t exist just by POSTing to the Feed.