Read data from MCP9808

Posted on
  • Hello ,
    I have bought the MCP9808 and connect it

    copy the code from here:

    I2C1.setup({scl:B6,sda:B7, bitrate: 60000});
    var mcp = require("MCP9808").connect(I2C1, 0b000); // 0b000 means A2 = A2 = A1 = GND 
    var temperature = mcp.getTemperature();
    console.log("Temperature: " + temperature + " C");
    

    but I get this error:

    in function called from system
    Uncaught Error: Cannot read property 'getTemperature' of undefined
     at line 10 col 22
    var temperature = mcp.getTemperature();
                         ^
    in function called from system
    Uncaught Error: Cannot read property 'getTemperature' of undefined
     at line 10 col 22
    var temperature = mcp.getTemperature()
    

    what is missing?
    do I need to download something?
    Thanks,

  • another problem

    I have try to upload this code

    I2C1.setup({scl:D25,sda:D26, bitrate: 60000});
    var mcp = require("MCP9808").connect(I2C1, 0b000); // 0b000 means A2 = A2 = A1 = GND 
    var temperature = mcp.getTemperature();
    console.log("Temperature: " + temperature + " C");
    
    function ReadTemp() { 
    setInterval (function()
                 {
      var temperature = mcp.getTemperature();
      console.log("Temperature: " + temperature + " C");
      digitalWrite(LED1,1);
      }
      ,1000);}
    
    digitalWrite(LED1,0);
    ReadTemp();
    

    I get no error while uploading ,
    just see the bar running , then "SENT"
    see the

    ____                 _
    |  __|___ ___ ___ _ _|_|___ ___
    |  __|_ -| . |  _| | | |   | . |
    |____|___|  _|_| |___|_|_|_|___|
             |_| espruino.com
     2v07 (c) 2019 G.Williams
    Espruino is Open Source. Our work is supported
    only by sales of official boards and donations:
    http://espruino.com/Donate
    

    then nothing - the device is not replay
    nothing on the console , can't write nothing on it

    I don't get any error - but it doesn't uplaod the code
    after reboot from power - It run the old code (the one on the first post)
    how could it be ?

    Thanks ,

  • The i2c address does not look correct, check the datasheet.

  • Sun 2020.10.11

    Hi @David1234321 I see that you are running 2V07 but on what device?

    Please post process.env so we are all on the same page.


    'then nothing - the device is not replay'

    Will need to understand which device is under test.

    What are the contents of the var when entering the vaiable name mcp into the L-Hand console window of the WebIDE?

    Maybe try wrapping in sections, several lines of code within a try/catch block to possibly learn more about the error itself.


    'after reboot from power - It run the old code (the one on the first post)'

    Was save() used?

  • Just did a double check, it is 0x18 plus base 0b000 so the address is ok.

    Please check wire and pin names.

  • the address is OK
    I have check in some i2c scanner

    I think something is wrong with the flash memory
    because I have upload the code using the esperino IDE in chrome this time , selected RAM
    and it's working , it print me the data

    this is the code:

    I2C1.setup({scl:D25,sda:D26, bitrate: 60000});
    var mcp = require("MCP9808").connect(I2C1, 0b000);
    console.log("I2C Setup Finsih");
    
    
    function ReadTemp() { 
      
    setInterval (function()
                 {
      digitalWrite(LED1,0);
      var temperature = mcp.getTemperature();
      var TimeStemp = new Date().toString();
      console.log(TimeStemp + "  :  " + "Temperature: " + temperature + " C");
      setTimeout(function(){
      digitalWrite(LED1,1);
      },5000);} , 10000);}
    
    ReadTemp();
    

    I have used upload to RAM
    and it's working

    Mon Oct 12 2020 10:53:35 GMT+0300 : Temperature: 21.625 C Mon Oct 12
    2020 10:53:45 GMT+0300 : Temperature: 21.5625 C Mon Oct 12 2020
    10:53:55 GMT+0300 : Temperature: 21.5 C Mon Oct 12 2020 10:54:05
    GMT+0300 : Temperature: 21.5625 C Mon Oct 12 2020 10:54:15 GMT+0300
    : Temperature: 21.5625 C Mon Oct 12 2020 10:54:25 GMT+0300 :
    Temperature: 21.5625 C

    now I have enter save() , in order to make it work after reboor\power off - everything
    got stuck:
    can't write nothing in the console
    get no replay
    the led is off

    this is what I have in the console after "save()"

    >save()
    =undefined
    Compacting Flash...
    Calculating Size...
    Writing..
    Compressed 40000 bytes to 6792
    

    what to do now ? something went wrong ?

    Thanks ,

  • I think most likely what's happened is device hasn't been recognised - it looks like during the var mcp = require("MCP9808").connect... line there might have been an error reported. If that failed then mcp wouldn't get set - but if you're uploading to RAM then the lines are executed one by one so the next lines would still get executed.

    As you'd found with the MAX30102 chip, there could be some reasons why the device isn't discovered using I2C.

    • Maybe there are no pullup resistors - you could use software I2C and see if it's any better:

    var i = new I2C();
    i.setup({scl:D25,sda:D26, bitrate: 60000});
    var mcp = require("MCP9808").connect(i, 0b000); 
    
    • But maybe sda/scl are around the wrong way, grounding it wrong, etc
    • Or maybe the address isn't 0b000 is A0/1/2 aren't GND
  • Ok, just got your response - so ignore what I just wrote.

    You've hit this: http://www.espruino.com/Saving

    If you type save() on the left-hand side of the IDE after your code is uploaded, the contents of Espruino's RAM at that point will be compressed and written in to flash memory.
    ...
    However, this means that any code that was executed at upload time will not be re-executed. For instance you may have some external hardware like an LCD display connected to Espruino - after applying power, the LCD will need to be initialised (since it can not remember its state)....

    All you need to do is choose to save to Flash, not RAM in the IDE (or you can use an onInit() function as that link describes).

  • so what to do now?
    I have change it to "Flash" but it say "Sent"
    but still it stuck (can't do nothing on the console )
    I have disconnect , and now I can't reconnect

    I have reboot the device ,recoonet it
    nothing is running now (no output on the console)
    but I can "talk" with the device:

    >new Date().toString()
    ="Thu Jan 1 1970 00:01:34 GMT+0000"
    

    have try to run upload to FLASH
    and this is what I get :

    ____                 _
    |  __|___ ___ ___ _ _|_|___ ___
    |  __|_ -| . |  _| | | |   | . |
    |____|___|  _|_| |___|_|_|_|___|
             |_| espruino.com
     2v07 (c) 2019 G.Williams
    Espruino is Open Source. Our work is supported
    only by sales of official boards and donations:
    http://espruino.com/Donate
    >Uncaught Error: File already written with different data
     at line 1 col 1077
    ....REG_TEMPERATURE);r",0,3939);
                                  ^
    Uncaught Error: Unable to find or create file
     at line 2 col 1082
    ...FIG,d&65522|a&13)};a.",1024);
                                  ^
    Uncaught Error: Unable to find or create file
     at line 3 col 1070
    ...otype.write8=function",2048);
                                  ^
    Uncaught Error: Unable to find or create file
     at line 4 col 943
    ...\n\nReadTemp();\n\n\n",3072);
                                  ^
    >
    

    so what to do now?

    ** I have uplaod it now to RAM - and it's working (if it's help you to understand and help me)

    Thanks ,

  • Try running require("Storage").eraseAll() just to ensure that nothing gets left in storage that shouldn't be there. That might help you a little.

    Since RAM obviously works for you and you seem happy with that, try:

    var mcp;
    
    function onInit() {
      clearInterval();
      I2C1.setup({scl:D25,sda:D26, bitrate: 60000});
      mcp = require("MCP9808").connect(I2C1, 0b000);
      console.log("I2C Setup Finsih");
      ReadTemp();
    };
    
    function ReadTemp() {   
      setInterval (function() {
        digitalWrite(LED1,0);
        var temperature = mcp.getTemperature();
        var TimeStemp = new Date().toString();
        console.log(TimeStemp + "  :  " + "Temperature: " + temperature + " C");
        setTimeout(function(){
          digitalWrite(LED1,1);
        },5000);
      } , 10000);
    }
    
    onInit();
    
  • 2 questions:

    1. to uplaod the new code on FLASH , right?
    2. run "require("Storage").eraseAll()"
      anyway before uplaod the code?

    *** update
    when I run the erase I get this

    require("Storage").eraseAll()
    =undefined
    

    Thanks ,

  • when I run the erase I get this

    That's expected.

    to uplaod the new code on FLASH , right?

    You don't need to, no. Just use the code I posted above.

    Read http://www.espruino.com/Saving

    run "require("Storage").eraseAll()" anyway before uplaod the code?

    No - that was just because you were getting some strange errors and it ensures that everything is cleared out 'just in case'.

  • I don't know what to say:

    I have try to upload the code you posted(I'm write it so you will see no errors)

    var mcp;
    function onInit() {
      clearInterval();
      I2C1.setup({scl:D25,sda:D26, bitrate: 60000});
      mcp = require("MCP9808").connect(I2C1, 0b000);
      console.log("I2C Setup Finsih");
      ReadTemp();
    }
    function ReadTemp() {   
      setInterval (function() {
        digitalWrite(LED1,0);
        var temperature = mcp.getTemperature();
        var TimeStemp = new Date().toString();
        console.log(TimeStemp + "  :  " + "Temperature: " + temperature + " C");
        setTimeout(function(){
          digitalWrite(LED1,1);
        },5000);
      } , 10000);
    }
    
    onInit();
    

    this is what I get :

    >
     ____                 _
    |  __|___ ___ ___ _ _|_|___ ___
    |  __|_ -| . |  _| | | |   | . |
    |____|___|  _|_| |___|_|_|_|___|
             |_| espruino.com
     2v07 (c) 2019 G.Williams
    Espruino is Open Source. Our work is supported
    only by sales of official boards and donations:
    http://espruino.com/Donate
    >
    Running onInit()...
    Uncaught Error: Module MCP9808 not found
     at line 3 col 26
      mcp = require("MCP9808").connect(I2C1, 0b000);
                             ^
    in function called from system
    

    both on RAM\Flash uplaod

    what is wrong now?

    update*
    I get the same error when I try to uplaod my own code that works before

  • Do you have an internet connection on the computer? Modules are loaded from the internet so if your computer no longer has access to the net, it won't be able to load the module you've requested.

  • the computer is connected all the time to the internet

    1. where it load the module , maybe something happand to the server?
    2. if there was a problem , how could I upload the code before? what have change?

    Thanks ,

  • wellll
    it's seem that I had some problem in the computer that block me some websites......

    I have try with another computer - and everything start to work

    what a waste of a day!

    so now its working and read the data every 10 seconds

    Thank you for the help!

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Read data from MCP9808

Posted by Avatar for David1234321 @David1234321

Actions