title: Raspberry Pi - Interfacing with an i2c enabled IR temp sensor (MLX90614)
body: <p>So I was recruited to my school's Baja racing team where we design build and compete with offroad dune-buggy type cars. I am a CS major and have quite a bit experience with python, and therefore was asked to help out the electrical team to interface with all of the sensors which we wanted on the car. So far so good, but now I am using an IR temperature sensor which reads ambient and object temperatures. (We are going to put this somewhere on the engine to read it's temperature and output to our GUI) </p>
  
  <p>The problem is that the only libraries that seem to have used this sensor are all written in C, and normally used with arduinos... Nonetheless I compiled and edited some C code that I found online and it works great! In C. :( Since our project is completely based in python; I'd really like some help reading this sensor via i2c and Python, although I really don't have a ton of experience in writing libraries, especially not for electronics. Any tips would be great to steer me in the correct direction. Here is the C code that we are currently using that I basically want the same thing in Python:</p>
  
  <pre><code>    //fordit:  gcc MLXi2c.c -o i2c -l bcm2835
  #include &lt;stdio.h&gt;
  #include &lt;bcm2835.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;fcntl.h&gt;
  #include &lt;string.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;sys/stat.h&gt;
  #include &lt;unistd.h&gt;
  #include&lt;time.h&gt;
  #define AVG 1   //averaging samples
  #define LOGTIME 10  //loging period
  int main(int argc, char **argv)
  {
      unsigned char buf[6];
      unsigned char i,reg;
      double temp=0,calc=0, skytemp,atemp;
      FILE *flog;
      flog=fopen("mlxlog.csv", "a");..
      bcm2835_init();
      bcm2835_i2c_begin();
      bcm2835_i2c_set_baudrate(25000);.
      // set address...........................................................................................
      bcm2835_i2c_setSlaveAddress(0x5a);
  ....
      printf("\nOk, your device is working!!\n");
  ....
  ....
      while(1) {
          time_t t = time(NULL);
  &lt;------&gt;struct tm tm = *localtime(&amp;t);
  &lt;------&gt;calc=0;
  &lt;------&gt;reg=7;
  &lt;------&gt;for(i=0;i&lt;AVG;i++){
  &lt;------&gt;    bcm2835_i2c_begin();
  &lt;------&gt;    bcm2835_i2c_write (&amp;reg, 1);
  &lt;------&gt;    bcm2835_i2c_read_register_rs(&amp;reg,&amp;buf[0],3);
  &lt;------&gt;    temp = (double) (((buf[1]) &lt;&lt; 8) + buf[0]);
  &lt;------&gt;    temp = (temp * 0.02)-0.01;
      &lt;--&gt;    temp = temp - 273.15;
  &lt;------&gt;    calc+=temp;
  &lt;------&gt;    sleep(1);
  &lt;------&gt;    }
  &lt;------&gt;skytemp=calc/AVG;
  &lt;------&gt;calc=0;
  &lt;------&gt;reg=6;
  &lt;------&gt;for(i=0;i&lt;AVG;i++){
  &lt;------&gt;    bcm2835_i2c_begin();
  &lt;------&gt;    bcm2835_i2c_write (&amp;reg, 1);
  &lt;------&gt;    bcm2835_i2c_read_register_rs(&amp;reg,&amp;buf[0],3);
  &lt;------&gt;    temp = (double) (((buf[1]) &lt;&lt; 8) + buf[0]);
  &lt;------&gt;    temp = (temp * 0.02)-0.01;
      &lt;--&gt;    temp = temp - 273.15;
  &lt;------&gt;    calc+=temp;
  &lt;------&gt;    sleep(1);
  &lt;------&gt;    }
  &lt;------&gt;atemp=calc/AVG;
  &lt;------&gt;printf("%02d-%02d %02d:%02d:%02d\n    Tambi=%04.2f C, Tobj=%04.2f C\n", tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec,atemp,skytemp);
  &lt;------&gt;fprintf(flog,"%04d-%02d-%02d %02d:%02d:%02d,%04.2f,%04.02f\n",tm.tm_year+1900, tm.tm_mon +1, tm.tm_mday,tm.tm_hour, tm.tm_min, tm.tm_sec,atemp,skytemp);
  &lt;------&gt;fflush(flog);
  &lt;------&gt;sleep(LOGTIME-(2*AVG));
      }
  ...
      printf("[done]\n");
  }
  </code></pre>
  
  <p>thanks in advance!</p>
  
  <ul>
  <li>Eddie</li>
  </ul>