如何使用传感器作为模块导出初始化板

时间:2016-03-15 00:40:30

标签: node.js johnny-five

作为一个基本的例子,我有:

<div class="well">
<%= form_for @electro, html: {class: 'form-horizontal'} do |f| %>

<div class="control-group"> 
    <%= f.label :Dagsetning, class: 'control-label' %><br>
        <div class="controls">
            <%= f.date_select :date %>
        </div>
</div>  

  <div class="control-group">   
    <%= f.label :Bygging, class: 'control-label' %><br>
        <div class="controls">
            <%= f.text_field :building_name %>
      </div>
</div>  

<div class="control-group"> 
    <%= f.label :Rafmagn_kwst, class: 'control-label' %><br>
        <div class="controls">
            <%= f.number_field :electricity_kwst %>
        </div>
  </div>    

  <div class="control-group">   
    <%= f.label :Rafmagn_kostnaður, class: 'control-label' %><br>
        <div class="controls">
            <%= f.number_field :electricity_cost %>
        </div>
  </div>    




  <div class="control-group">
    <div class="controls">
      <%= f.submit class: 'btn btn-xs btn-warning' %>
    </div>  
  </div>

  <% end %>
   <%= link_to 'Skoða allar færslur', electros_path %>  
  </div>

通过以下方式调用:     //tempModule.js var five = require("johnny-five"); var board = new five.Board(); var temp; board.on("ready", function() { temp = new five.Temperature({ pin: "A2", controller: "AD8495" }); }); module.export = board;

//moduleTest.js

此代码目前返回“未定义”。 如何构建tempModule.js,以便连接到电路板的传感器的数据可以在另一个程序中使用?

1 个答案:

答案 0 :(得分:1)

1. temp变量不是board的属性所以board.temp没有意义。

2.您没有导出temp,因此您无法访问它。

所以你需要输出像

这样的临时工具
module.exports = temp;

或使用

exports.board = board; 
exports.temp = temp;

然后

var module = require('tempModule.js');

并使用

访问它
var board = module.board;
var temp = module.temp;

如果以上仍然不起作用那么还有另一种方式

<强> tempModule.js

var five = require("johnny-five"); 
var board = new five.Board();

function init(cb){
    board.on("ready", function() {
        var temp = new five.Temperature({ pin: "A2", controller: "AD8495" }); 
        cb(temp);
    });
}
exports.init = init;

并像这样使用

var tempModule = require('tempModule.js');

tempModule.init(function (temp){
    temp.on("data", function() { 
        console.log(this.celsius + "°C", this.fahrenheit + "°F"); 
    });
});

更新:添加了另一个示例

// boardModule.js
var five = require("johnny-five"); 
var b = new five.Board();
var board = {};

function init(cb){
    b.on("ready", function() {
        board.temp1 = new five.Temperature({ pin: "A2", controller: "AD8495" }); 
        board.temp2 = new five.Temperature({ pin: "A3", controller: "AD8495" });
        board.temp3 = new five.Temperature({ pin: "A4", controller: "AD8495" }); 
        board.motor1 = new five.Motor({ pin: 5 });

        cb(board);
    });
}
exports.init = init;

// testModule.js
var boardModule = require('boardModule.js');

boardModule.init(function (board){

    board.temp1.on("data", function() { 
        console.log('temp1:', this.celsius + "°C", this.fahrenheit + "°F"); 
    });

    board.temp2.on("data", function() { 
        console.log('temp2:', this.celsius + "°C", this.fahrenheit + "°F"); 
    });

    board.temp3.on("data", function() { 
        console.log('temp3:', this.celsius + "°C", this.fahrenheit + "°F"); 
    });


    board.motor1.on("start", function() {
        console.log("start", Date.now());

        // Demonstrate motor stop in 2 seconds
        board.wait(2000, function() {
            board.motor1.stop();
        });
    });

    board.motor1.on("stop", function() {
        console.log("stop", Date.now());
    });

    board.motor1.start();
});