如何将JSON数据从URL发布到Pubnub

时间:2016-02-23 19:10:25

标签: javascript html json ajax pubnub

我有来自此网址的一些JSON数据

http://api.thingspeak.com/channels/18675/feed/last.json

如何将此数据发布到Pubnub? 这里我已经为实时图形编写了一些代码,但图形的输入是常量数据。我想从URL中获取数据

<html>
  <head>
  <script type="text/javascript" src="http://pubnub.github.io/eon/v/eon/0.0.10/eon.js"></script>
  <link type="text/css" rel="stylesheet" href="http://pubnub.github.io/eon/v/eon/0.0.10/eon.css" />
  </head>
  <body>
  <div id="chart"></div>
  <script>
  var pubnub = PUBNUB.init({
  publish_key: '',
  subscribe_key: ''
  });
  var channel = "c3-spline-history-test";
  eon.chart({
  history: true,
  channel: 'process-memory-demo',
  flow: true,
  limit: 100,
  debug: false,
  pubnub: pubnub,
  rate: 500,
  debug: true,
  generate: {
  bindto: '#chart',
  transition: {
  duration: 1
  },
  data: {
  type: 'scatter',
  labels: true
  }      
  }
  });
  </script>
  <script type="text/javascript">
  setInterval(function(){
  pubnub.publish({
  channel: channel,
  message: [23,45,66,99,77] //replace this data with JSON data from URL
  });
  }, 1000);
  </script>
  </body>
  </html>

1 个答案:

答案 0 :(得分:2)

使用Jquery的getJSON函数获取thingpeak数据并发布到pubnub

   <!doctype html>
    <html lang="en">
    <head>
      <meta charset="utf-8">
      <title>THingspeak data to Pubnub</title>
      <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
      <script type="text/javascript" src="http://pubnub.github.io/eon/v/eon/0.0.10/eon.js"></script>
    <link type="text/css" rel="stylesheet" href="http://pubnub.github.io/eon/v/eon/0.0.10/eon.css">
    </head>
    <body>
    <div id="chart"></div>
    <script>
    var pubnub = PUBNUB.init({
          publish_key: '',
          subscribe_key: ''
          });
          var channel = "";
          eon.chart({
            channel: channel,
            history: true,
            flow: true,
            pubnub: pubnub,
            debug: true,

            generate: {
              bindto: '#chart',
              data: {
                labels: false
              }
            },

            transform: function(a) {
              return {
                eon: {
                  'val1': a[0],
                  'val2': a[1],
                  'val3': a[2],
                  'val4': a[3],
                  'val5': a[4]
                }
              };
            }
          });
    (function() {
      var sensors = "http://api.thingspeak.com/channels/(channel_id)/feed/last.json";//replace channel _id with yours
      $.getJSON( sensors, {
        format: "json"
      })
        .done(function( data ) {
            setInterval(function(){ 
            var f1=data.field4;
            var f2=data.field5;
            var f3=data.field6;
            var f4=data.field7;
            var f5=data.field8;
            console.log(f1,f2,f3,f4);  
            pubnub.publish({
              channel: channel,
              message: [f1,f2,f3,f4,f5]
         });
            }, 15000);
        });
    })();
    </script>

    </body>
    </html>