0

Error trying to control multiple thermometers
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Error trying to control multiple thermometers

by Trillion on Tue May 14, 2019 7:34 pm

Devices involved:
- 2 Huzzah Breakout ESP8266s
- 2 MCP9808 Thermometers
- 1 CPU controlling both using Johnny-Five.

The following code works fine when there's one thermometer, but it fails trying to control two.
In other words this code works when line 4 is commented out and fails when it's included.

Code: Select all | TOGGLE FULL SIZE
var five = require("johnny-five");
var {EtherPortClient}=require("etherport-client");
var Thermometers=[
   //{Name:"Thermometer1", Ip:"192.168.1.101"},
   {Name:"Thermometer2", Ip:"192.168.1.102"} // !!Works fine by itself, fails if Thermometer1 is uncommented.
];
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);
         });
      })
   });
}


The only difference is whether or not two or more thermometers are included.
Error is: "node_modules/johnny-five/lib/board.pins.js:91 var type = board.pins.type; TypeError: Cannot read property 'type' of null"'
It seems to be the result of trying run new five.Thermometer({controller:"MCP9808"}) more than once.

Trillion
 
Posts: 29
Joined: Tue Apr 23, 2019 11:20 pm

Re: Error trying to control multiple thermometers

by adafruit_support_mike on Wed May 15, 2019 12:02 am

The error is probably related to the fact that each thermometer will need its own I2C address, and the code above doesn't provide one.

adafruit_support_mike
 
Posts: 57131
Joined: Thu Feb 11, 2010 2:51 pm

Re: Error trying to control multiple thermometers

by Trillion on Wed May 15, 2019 12:29 am

adafruit_support_mike wrote:The error is probably related to the fact that each thermometer will need its own I2C address, and the code above doesn't provide one.


Thanks Mike. That's great. A quick search to learn about that wasn't super revealing. I'll figure it out one way or another, but if you have any links that gets into how to do that, I'd much appreciate it. Thanks again.

Trillion
 
Posts: 29
Joined: Tue Apr 23, 2019 11:20 pm

Re: Error trying to control multiple thermometers

by Trillion on Wed May 15, 2019 12:50 am

Trillion wrote:
adafruit_support_mike wrote:The error is probably related to the fact that each thermometer will need its own I2C address, and the code above doesn't provide one.


Thanks Mike. That's great. A quick search to learn about that wasn't super revealing. I'll figure it out one way or another, but if you have any links that gets into how to do that, I'd much appreciate it. Thanks again.


Answering my own question here...
Basic description from Adafruit's tutorial (not immediately understanding it, but helpful)
Python integration (as opposed to Johnny-Five)
Explanation -- heavy on the tech, light on how to
Johnny-Five's overview


In our case, with each single thermometer controlled by a single dedicated ESP8266 it looks like we wouldn't be using I2C Data Pins, just I2C addressing. Correct?

Trillion
 
Posts: 29
Joined: Tue Apr 23, 2019 11:20 pm

Re: Error trying to control multiple thermometers

by Trillion on Wed May 15, 2019 5:04 pm

adafruit_support_mike wrote:The error is probably related to the fact that each thermometer will need its own I2C address, and the code above doesn't provide one.


Where in the code would you provide the I2C address?

Trillion
 
Posts: 29
Joined: Tue Apr 23, 2019 11:20 pm

Re: Error trying to control multiple thermometers

by adafruit_support_mike on Wed May 15, 2019 11:57 pm

Trillion wrote:In our case, with each single thermometer controlled by a single dedicated ESP8266 it looks like we wouldn't be using I2C Data Pins, just I2C addressing. Correct?

Those terms aren't comparable.

An I2C bus consists of a clock line (SCL) and a data line (SDA). All communication happens on SDA, with the timing controlled by SCL.

I2C is a shared-bus protocol, meaning many devices can connect to the same SCL and SDA lines. The microcontroller identifies the device it wants to talk to by starting each message with a 7-bit address. The device whose address matches the one transmitted by the microcontroller responds, and all of the devices with different addresses ignore the traffic on SDA until the microcontroller emits another message with another address.

In general, no two devices connected to the same I2C bus should share the same address. All the devices with the same address would try to respond simultaneously, and the most likely result is a failure for any of them to communicate.

The MCP9808 has three exposed address pins so you can change a specific chip's address. There are three address pins, meaning you can assign up to 8 separate addresses, and in turn can connect up to 8 different MCP9808s to the same I2C bus.

Trillion wrote:Where in the code would you provide the I2C address?

I'm afraid I don't know the Johnny-five API well enough to tell you.

It's a common task for setting up I2C devices though, so check the documentation looking for that subject.

adafruit_support_mike
 
Posts: 57131
Joined: Thu Feb 11, 2010 2:51 pm

Re: Error trying to control multiple thermometers

by Trillion on Thu May 16, 2019 9:08 am

adafruit_support_mike wrote:It's a common task for setting up I2C devices though, so check the documentation looking for that subject.


From what I've found so far, documentation on I2C & J5 hasn't been particularly clear. I'll see if I can translate from Python.

adafruit_support_mike wrote:The error is probably related to the fact that each thermometer will need its own I2C address, and the code above doesn't provide one.


Perhaps I2C addressing is not the issue here after all.
In this project the temperature sensors are each being handled by a different micro controller.
The problem instead may be that we haven't figured out how to associate the Johnny-Five's Thermometer API (i.e. new five.Thermometer({controller:"MCP9808"}) with a specific Board object under one JS script. Was hoping to find API documentation that might speak to this, but so far haven't turned up anything.

Trillion
 
Posts: 29
Joined: Tue Apr 23, 2019 11:20 pm

Re: Error trying to control multiple thermometers

by adafruit_support_mike on Thu May 16, 2019 11:59 pm

I'm afraid we don't have anything on that subject. Your best bet will be to look for a Johnny-Five forum or mailing list and talk to the folks who have more experience with that platform.

There's a chance that Johnny-Five uses the same kind of modular design as Scratch or MakeCode, where all the details like I2C addresses are hidden in the module that talks to the device.

adafruit_support_mike
 
Posts: 57131
Joined: Thu Feb 11, 2010 2:51 pm

Re: Error trying to control multiple thermometers

by Trillion on Sat May 18, 2019 1:11 pm

adafruit_support_mike wrote:I'm afraid we don't have anything on that subject. Your best bet will be to look for a Johnny-Five forum or mailing list and talk to the folks who have more experience with that platform.

There's a chance that Johnny-Five uses the same kind of modular design as Scratch or MakeCode, where all the details like I2C addresses are hidden in the module that talks to the device.


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.

So for example the above code would be edited as follows...

Code: Select all | TOGGLE FULL SIZE
ThisThermometer.Controller=new five.Thermometer({
    board: ThisThermometer.Board, //<-- the missing magic
    controller:"MCP9808"
});


Thanks to Donovan Buck on GitHub for figuring this out. Expect documentation soon.

Trillion
 
Posts: 29
Joined: Tue Apr 23, 2019 11:20 pm

Re: Error trying to control multiple thermometers

by adafruit_support_mike on Sun May 19, 2019 4:31 am

Glad to hear you got it working, and thanks for posting the follow-up. Happy hacking!

adafruit_support_mike
 
Posts: 57131
Joined: Thu Feb 11, 2010 2:51 pm

Please be positive and constructive with your questions and comments.