【问题标题】:IBM Bluemix IotFoundation : Iotfclient is offline. Retrying connectionIBM Bluemix IotFoundation:Iotfclient 处于脱机状态。重试连接
【发布时间】:2016-05-16 13:54:04
【问题描述】:

我正在尝试学习 Coursera 上的 IBM Bluemix 课程。

  1. 我的步骤:作为设备(客户端)的树莓派,作为注册客户端连接到 Watson IoT Platform。它每秒发出连续的随机数流。
  2. 我已在 IBM Bluemix 上部署了我的自定义 Nodejs 应用程序(Coursera 上提供的代码)。

    var express = require('express');
    
    var app = express();
    var Client = require('ibmiotf');
    var appConfig;
    
    var serverPort = process.env.VCAP_APP_PORT || 3000;
    var serverHost = process.env.VCAP_APP_HOST || 'localhost';
    
    if (process.env.VCAP_SERVICES) {
        var env = JSON.parse(process.env.VCAP_SERVICES);
    appConfig = {
                   'org' : env["iotf-service"][0].credentials.org,
                   'id' : 'dna-nodeserver',
                   'auth-key' : env["iotf-service"][0].credentials.apiKey,
                   'auth-token' : env["iotf-service"][0].credentials.apiToken
                  }
    } else {
        appConfig = require('./application.json');
    }
    
    var responseString = 'Hello Coursera';
    
    var appClient = new Client.IotfApplication(appConfig);
    
    app.get('/', function(req, res) {
        res.send(responseString);
    });
    var server = app.listen(serverPort, serverHost, function() {
    var host = server.address().address;
    var port = server.address().port;
    console.log('Listening at http://%s:%s', host, port);
    
    appClient.connect();
    
    appClient.on('connect', function() {
        appClient.subscribeToDeviceEvents('raspberrypi');
    });
    
    appClient.on("error", function (err) {
        console.log("Error : "+err);
    });
    
    appClient.on('deviceEvent', function(deviceType, deviceId, eventType, format, payload) {
    
        responseString = "Device Event from :: "+deviceType+" : "+deviceId+" of event "+eventType+" with payload : "+payload;
    
        console.log("Device Event from :: "+deviceType+" : "+deviceId+" of event "+eventType+" with payload : "+payload);
    });
    
    });
    

我面临的问题如下图所示:

Error

另外,由于我收到来自树莓派的连续事件...网页(由 res.send(responseString) 提供服务)应自动显示更改...无需我手动刷新页面。但这似乎不会发生。

任何帮助将不胜感激。谢谢!

【问题讨论】:

    标签: node.js ibm-cloud iot


    【解决方案1】:

    您可能有多个应用程序在监听事件。停止 Bluemix 上以前的 Node Red 应用程序。而是只运行上面显示的 Nodejs 应用程序。

    【讨论】:

      【解决方案2】:

      旧线程,但感谢 Alexei 为我指明了正确的方向。

      在我的情况下,当生产版本仍在 IBM Cloud 中运行时,测试本地版本的事件代理的行为相同。

      如果您生成额外的 API 密钥、保存两组凭据并检查合适的环境变量以查看要应用哪个组,那么您可以拥有多个侦听器。

      在我的应用程序中,我将它们包装在节点中的一个函数中:

      function getEventBrokerCredentials(siteCode) {
      
          var codeToCredentials = {
              'REMOTE_BROKER': {
                  'orgId': 'abcde',
                  'id': 'ThisIsABroker',
                  'apikey': '111111111111111',
                  'authtoken': '2222222222222'
              },
              'LOCAL_BROKER': {
                  'orgId': 'abcde',
                  'id': 'ThisIsALocalBroker',
                  'apikey': '3333333333333333',
                  'authtoken': '4444444444444'
              }
          };
      
          return codeToCredentials[siteCode];
      }
      
      
      var brokerCredentials = getEventBrokerCredentials(process.env.BROKER_HOST || 'REMOTE_BROKER');
      
      
      var appClient = new IOTClient.IotfApplication({
          'org': brokerCredentials.orgId,
          'id': brokerCredentials.id,
          'auth-key': brokerCredentials.apikey,
          'auth-token': brokerCredentials.authtoken
      });
      

      然后使用

      BROKER_HOST=LOCAL_BROKER nodemon
      

      用于本地测试和开发。

      当然,这个主题有很多变化。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-05-16
        • 2016-02-01
        • 1970-01-01
        • 1970-01-01
        • 2022-08-04
        • 1970-01-01
        • 2014-07-16
        • 2021-11-02
        相关资源
        最近更新 更多