B4R Question MLX90614 lib porting to B4R

peacemaker

Expert
Licensed User
Hi, All

Any help, please, with porting lib code:

Lib source:
B4X:
/*********************************************************************/

uint16_t Adafruit_MLX90614::read16(uint8_t a) {
  uint16_t ret;

  Wire.beginTransmission(_addr); // start transmission to device
  Wire.write(a);                 // sends register address to read from
  Wire.endTransmission(false);   // end transmission

  Wire.requestFrom(_addr, (size_t)3); // send data n-bytes read
  ret = Wire.read();                  // receive DATA
  ret |= Wire.read() << 8;            // receive DATA

  uint8_t pec = Wire.read();

  return ret;
}

This my code always returns 65535:
B4X:
'Public SlaveAddress As Byte = 0x5A    'I2C address of the chipset.
'Public const MLX90614_TOBJ1 = 0x07
Sub readObjectTempC As Double
    wire.WriteTo(SlaveAddress, Array As Byte(MLX90614_TOBJ1))
    Dim b() As Byte = wire.RequestFrom(SlaveAddress, 3)    '3 bytes
    raf.Initialize(b, True)
    Dim b3 As UInt = raf.ReadUInt16(0)
    Log("b3=", b3)
    't = b3 * 0.02 - 273.15
    Return t
End Sub

Arduino's sketch works OK, returned 15000+ that is calculated later to 30 Celcius degrees...

What is incorrect in B4R code ?
 

rbghongade

Active Member
Licensed User
Here you go:
B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'Public variables can be accessed from all modules.
    Public Serial1 As Serial
    Public timer1 As Timer
    
    Public OBJ_TEMP,AMB_TEMP As Double
    
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")
    
    RunNative("setup",Null)
    timer1.Initialize("timer1_Tick",1000)
 
 
 
 
    timer1.Enabled = True
 
End Sub

Private Sub Timer1_Tick
    RunNative("read",Null)
    Log("Object Temperature: ",OBJ_TEMP," C")
    Log("Ambient Temperature: ",AMB_TEMP," C")
End Sub


#if C
#include <Wire.h>
#include <Adafruit_MLX90614.h>
Adafruit_MLX90614 mlx = Adafruit_MLX90614();
void setup(B4R::Object* o){
mlx.begin(); 
 }
    
void read (B4R::Object* o) {
   b4r_main::_amb_temp = mlx.readAmbientTempC();
   b4r_main::_obj_temp = mlx.readObjectTempC();
}

#End if

Note : You need to install Adafruit MLX90614 library for Arduino
 
Upvote 0

peacemaker

Expert
Licensed User
Here you go:
.....
Note : You need to install Adafruit MLX90614 library for Arduino

Thank you very much, in whole the task is solved...
But for me it's also important to understand what is wrong in my B4R code - i'm trying to use various sensors\chipsets, an it needs to understand how to use them inside B4R.
But i guess, now it's not interesting task for community, if already overcame...
 
Upvote 0
Top