Johnny-Five,I2C,用ESP8266控制多个温度传感器 [英] Johnny-Five, I2C, Controlling multiple temperature sensors using ESP8266

查看:888
本文介绍了Johnny-Five,I2C,用ESP8266控制多个温度传感器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试弄清楚如何控制多个温度传感器.

I'm trying figure out how to control multiple temperature sensors.

设置:

  • 2 ESP8266 微控制器
  • 2 MCP9808 温度传感器
  • 1使用Johnny-Five控制两个ESP的机器.
  • 2 ESP8266 Micro Controllers
  • 2 MCP9808 Temperature Sensors
  • 1 Machine controlling both ESPs using Johnny-Five.

注意:每个ESP8266微控制器都处理一个MCP9808温度传感器.

NOTE: Each ESP8266 micro controller handles one MCP9808 Temperature Sensor.

目标: 中央计算机(运行Johnny-Five的MacOS)在一个Node JS脚本下处理两个微控制器.

THE GOAL: The central machine (MacOS running Johnny-Five) handles both microcontrollers under one Node JS script.

问题: 我可以控制一个微控制器/温度配对,但不能在同一脚本下同时控制两者. 显然,一次处理两者的关键在于了解如何处理IC2寻址. 到目前为止,我还找不到任何页面,论坛,说明或其组合,这些页面,论坛,说明或其组合可以用我能理解的术语清楚地解释逻辑.

THE PROBLEM: I can control one Micro Controller / Temperature pairing, but not both under the same script. Apparently the key to handling both at once lies in knowing how to handle the IC2 addressing. So far I haven't been able to find any pages, forums, instructions or combinations thereof that clearly explain the logic in terms that I can understand.

问题: 如何使用Johnny-Five处理I2C来控制多个设备

THE QUESTION: How to handle I2C using Johnny-Five to control multiple devices

代码: 仅在处理一个Sensor时有效,而不能同时使用 换句话说,第四行注释掉了.不加注释,没有.

THE CODE: It only works when handling one Sensor, not both In other words with the 4th line commented out it works. Uncommented, it doesn't.

    var five = require("johnny-five");
    var {EtherPortClient}=require("etherport-client");
    var Thermometers=[
        //{Name:"Thermometer1", Ip:"192.168.1.101"}, //Uncommenting causes fail.
        {Name:"Thermometer2", Ip:"192.168.1.102"} 
    ];
    TrackThermometers();

    function TrackThermometers(){
        Thermometers.forEach(function(ThisThermometer, ThermometerCount){
            ThisThermometer.Board=new five.Board({
                port: new EtherPortClient({
                    host: ThisThermometer.Ip,
                    port: 3030
                }),
                repl: false
            });
            ThisThermometer.Board.on("ready", function(){
                ThisThermometer.Controller=new five.Thermometer({ //This cmd triggers the error
                    controller:"MCP9808"
                });
                ThisThermometer.Controller.on("change", function(){
                    console.log(this.id, this.fahrenheit);
                });
            })
        });
    }

推荐答案

解决方案

在J5的Thermometer API下有一个board属性(本文中未记录).将有问题的Board实例分配给该属性,会将Thermometer实例与该板关联.

There is a board property (undocumented as of this post) under the J5's Thermometer API. Assigning the Board instance in question to that property associates the Thermometer instance with that board.

通过示例,上面的代码将按如下方式进行编辑...

By way of example the above code would be edited as follows...

    ThisThermometer.Controller=new five.Thermometer({
       board: ThisThermometer.Board, //<-- the missing magic
       controller:"MCP9808"
    });

感谢 Donovan Buck 弄清楚了这一点.可能很快记录.

Thanks to Donovan Buck for figuring this out. May be documented soon.

这篇关于Johnny-Five,I2C,用ESP8266控制多个温度传感器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆