使用XivelyJS将JS值发送到xively

时间:2014-02-25 16:43:54

标签: javascript arduino xively

如何将两个Javascript值发送到xively feed,一个作为ID,一个作为值。目前的代码是。我希望将这些值添加到xively中,以便我可以通过带有wifi的arduino访问它们。不幸的是,从JS直接从网站获取值似乎并不简单,所以这是我的解决方法,除非有人有更好的方法从arduino访问这些数据。使用此示例作为参考例如我想从网站发送数据而不是检索它。 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>

1 个答案:

答案 0 :(得分:0)

以下是向Feed中的两个频道(&#34; foo&#34;和&#34; bar&#34;)中的每一个发送单个值的简单示例...

<!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>