1

How can I send two Javascript values to a xively feed, One as the ID and one as the value. Current code is. I want to get those values in to a xively feed so I can access them via an arduino with wifi. Unfortunately getting the values directly from the website from JS does not seem straight forward so this is my workaround unless anyone has a better way of accessing this data from an arduino. Using this example for reference execept i want to send the data from website rather than retrieve it. http://xively.github.io/xively-js/tutorial/

    <!DOCTYPE html >
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>

<script src="jquery-1.10.2.min.js"></script>
<script type="text/javascript" src="parse-1.2.16.min.js"></script>
</head>

<body>


<script>

jQuery(function($){

    xively.setKey( "APIKEY" ); 


    Parse.initialize("APIKEY", "APIKEY");



var mValue = Parse.Object.extend("mValue");
var queryLngLat = new Parse.Query(mValue);
var classObject = new mValue();
var existingMarkers= [];
var databaseMarkers= [];
var counter = 0;
var div ;
var resultsLength;



setInterval(function update() {

queryLngLat.notEqualTo("longAndLat", null);
queryLngLat.find({
  success: function(results) {

      console.log("Successfully retrieved " + results.length + " scores.");

     for (var j = 0; j < results.length; j++ ) {
    databaseMarkers = (results[j].attributes.longAndLat);
resultsLength = results.length;
counter++;
     var  markerValueRead = results[j].attributes.Val;

     CoordsPush = databaseMarkers.substring(1, databaseMarkers.length - 1);

     div =  document.createElement("div");
      div.style.width = "400px;";
    div.style.background = "white";
    div.style.color = "black";
    div.innerHTML = /*"Database LatLng: " + CoordsPush + " Marker Value: " + */markerValueRead;
    div.setAttribute("id", CoordsPush);   
    document.body.appendChild(div);
    //alert(div.id);


    JSON.stringify(markerValueRead);


xively.setKey("UYby76Zocsur664I6sRd13BXKUKrpM3xDSntN5qB5fvPxMhG");


var feedID = 129375335,
datastreamID = "LatLng";
selector = "50.3754565, -4.14265649999993"

xively.datastream.get(feedID, datastreamID, function(datastream) {

    $selector.html( datastream["current_value"] );
    xively.datastream.subscribe(feedID, datastreamID, function( even, datastream_updated) {

        $(selector).html(datastream_updated["current_value"]);
    });
});







    //console.log("(" + markers[d].getPosition().d + ", " + markers[d].getPosition().e +")");

    console.log("Database LatLng: " + databaseMarkers + " Marker Value: " + markerValueRead);

        }

    counter = 0;

    }
    });

    document.body.innerHTML = '';





}, 15000);





});



    </script>
    <script src="http://d23cj0cdvyoxg0.cloudfront.net/xivelyjs-1.0.4.min.js"></script>  
</body>
</html>
Dan W
  • 365
  • 1
  • 4
  • 20

1 Answers1

0

Here's a simple example of sending a single value to each of two channels ("foo" and "bar") in a feed...

<!DOCTYPE HTML>
<html>
<head>
   <title>Xively Test</title>
   <script language="JavaScript" type="text/javascript" src="lib/jquery/jquery-1.10.2.min.js"></script>
   <script language="JavaScript" type="text/javascript" src="lib/xively/xivelyjs-1.0.4.min.js"></script>

   <script language="JavaScript" type="text/javascript">
      var API_KEY = "YOUR_API_KEY";
      var FEED_ID = "YOUR_FEED_ID";

      $(document).ready(function() {
         // Set your API key first
         xively.setKey(API_KEY);

         // build the data packet
         var timestamp = new Date().toISOString();
         var data = { "version" : "1.0.0",
            "datastreams" : [
               { "id" : "foo", "datapoints" : [ {"at" : timestamp, "value" : 10} ] },
               { "id" : "bar", "datapoints" : [ {"at" : timestamp, "value" : 20} ] }
            ]
         };

         // upload the data
         xively.feed.update(FEED_ID, data, function(response) {
            if (response.status == "200") {
               console.log("Yay, it worked!: " + JSON.stringify(response, null, 3));
            }
            else {
               console.log("Boo, something went wrong!: " + JSON.stringify(response, null, 3));
            }
         });

      });
   </script>
</head>
<body>
Look in the console for output.
</body>
</html>
Chris Bartley
  • 417
  • 1
  • 6
  • 13