Johnny-Five, I2C, Controlling multiple temperature

2019-08-28 18:31发布

问题:

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

THE SETUP:

  • 2 ESP8266 Micro Controllers
  • 2 MCP9808 Temperature Sensors
  • 1 Machine controlling both ESPs using Johnny-Five.

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

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

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.

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

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);
                });
            })
        });
    }

回答1:

SOLUTION

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"
    });

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