OpenSim or Second Life data to Pachube

As part of the mellifera โ€“ echoes of travels in new territories it was necessary to connect data from Second Life (and later OpenSim) to Unity. Rather than spend too much of the valuable development time fiddling around with php backends, I decided to run with pachube as a mediator for our data. What follows is the LSL code, commented so it should be fairly self explanatory. the following script is for outbound traffic only and works in both Second Life and OpenSim. (the pachube site itself also has a tutorial on second life and pachube, which also covers getting data back into SL.)

 

 //lsl

//the following is a fake api key replace it with your own pachube key
string apiKey = "8c966#################################26ac87";  

//this is your feed number. you set it up at the pachube.com website
integer feedNumber = 0000;

//how often to send data (remember that pachube limits the frequency with which you can update and retrieve data)
float updateFrequency = 20.0;

//vector variable to hold position of a detected avatar
vector pos;

//and some dummy data to send to pachube
float otherData = 0.1111;

 default
{
    state_entry()
    {
        //set timer running at update frquency
        llSetTimerEvent(updateFrequency);
    }

    timer()
    {
        //run a sensorSweep to detect closest an avatar in 50m range
        llSensor("","",AGENT,50,PI);

        //put the url for the feed together
        string url = "http://www.pachube.com/api/" + (string)feedNumber + ".csv?key=" + apiKey; 

        //this is where you construct the comma separated list to send to pachube
        //every float sent must be converted to a string and followed by a comma - ","
        //we send each of the cartesian coordinates of the avatar gathered by the sensor sweep and the dummy data
        string dataToSend =
        (string)pos.x+","+
        (string)pos.y+","+
        (string)pos.z+","+
        (string)otherData;

        //do the actual sending of the data to pachube
        llHTTPRequest(url, [ HTTP_METHOD, "PUT"], dataToSend);
    }

    sensor(integer num)
    {
        //set pos to position of closest avatar
        pos = llDetectedPos(0);
    }

    no_sensor()
    {
        //if there was no avatar detected set pos to 0,0,0
        pos = ;
    }

    http_response(key request_id, integer status, list metadata, string body)
    {
       //for debugging sets the floating text on the prim to the status recieved back from pachube
       llSetText("updated: status: " + (string) status, , 1);
    }

}

you can then access the feed at http://www.pachube.com/feeds/0000 (change to your feed number) and more importantly you can get the csv version of the feed here: http://www.pachube.com/api/feeds/0000.csv this than can be retrieved in unity (or from anywhere else really) and used as data there.. next post will be on grabbing and manipulating the data using c# in unity.

Categories: tutorials & resources

One Response to “OpenSim or Second Life data to Pachube”

×

Comments are closed.