2

I have spent the day trying to port the Adafruit UV Sensor Library (Link) from Arduino to Python and SMBUS and am definitely making progress. The data sheet is a bit vague on detail compared to the HDC1008 sensor about how to read but am hoping that someone with a better understanding of C++ and SMBUS/i2c might be able to double check my assumptions and explain why I'm getting slightly different values than those seen on the Arduino.

Current outputs are Pi Vs Arduino:

  • Vis:350 Vs 266
  • IR: 941 Vs 304
  • UV: 0.46 Vs 0.05

The whole class is a bit long so I am linking to it here - bad I know. The two functions I'm most unsure of are when the UV sensor is enabled (C++ lines are commented):

    """uint8_t Adafruit_SI1145::writeParam(uint8_t p, uint8_t v) {"""
    def writeParam(self,p,v):
            #Serial.print("Param 0x"); Serial.print(p, HEX);
            #print("Param 0x",p,"HEX")

            #Serial.print(" = 0x"); Serial.println(v, HEX);
            #print("= 0x",v,"HEX")

            #write8(SI1145_REG_PARAMWR, v);
            self.bus.write_byte_data(self.SI1145_ADDR, self.SI1145_REG_PARAMWR, v)

            #write8(SI1145_REG_COMMAND, p | SI1145_PARAM_SET);
            self.bus.write_byte_data(self.SI1145_ADDR, self.SI1145_REG_COMMAND, p | self.SI1145_PARAM_SET)

            #return read8(SI1145_REG_PARAMRD);
            return self.bus.read_byte_data(self.SI1145_ADDR, self.SI1145_REG_PARAMRD)

and reading 16 bits for each of the different light types (I thought it was only 7!) e.g

    """returns the UV index * 100 (divide by 100 to get the index)"""
    """uint16_t Adafruit_SI1145::readUV(void) {"""
    def readUV(self):
            #return read16(0x2C);
            raw = float(self.read16(0x2C)) / 100.00
            return raw

Here is the method:

    """Read method used to read 16 bits"""
    """uint16_t Adafruit_SI1145::read16(uint8_t a) {"""
    def read16(self,reg):
            #Wire.beginTransmission(_addr); // start transmission to device
            #Wire.write(a); // sends register address to read from
            #Wire.endTransmission(); // end transmission
            self.bus.write_byte(self.SI1145_ADDR, reg)
            time.sleep(0.015)

            #Wire.requestFrom(_addr, (uint8_t)2);// send data n-bytes read
            #ret = Wire.read(); // receive DATA
            ret = self.bus.read_byte(self.SI1145_ADDR)
            #ret |= (uint16_t)Wire.read() << 8; // receive DATA
            ret |= self.bus.read_byte(self.SI1145_ADDR) << 8 
            return ret
Byte Insight
  • 246
  • 2
  • 11
  • 2
    This seems like you ae asking someone to check your code and see if you properly transitioned from c++ to python which doesn't really seem like it is on-topic here. It seems like it might be a better fight on codereveiw – sir_ian Jun 30 '16 at 13:43
  • 1
    On one hand I agree with you and I might ultimately have to ask there. However, it's quite a specific question and I'm reliant on someone with the appropriate knowledge and experience to see my question and answer it. I hope that will happen and more people will benefit from the net result. – Byte Insight Jun 30 '16 at 14:12
  • 1
    I agree it's "pi enough", although I doubt you will get an answer (I also doubt you will get an answer anywhere else, so there is not much point in moving it). I've done exactly the kind of thing your are doing here before (used adafruit python as a reference to create I2C userspace drivers in C++) but I do not use the "Wiring.Pi" library, so I have no clue about your translation. IMO the kernel's smbus interface is preferrable: http://raspberrypi.stackexchange.com/a/37690/5538 – goldilocks Jul 05 '16 at 12:05
  • Hi Goldilocks. I was hoping you might pass by at some point and offer a few words of encouragement. I'm not able to sit down and give this another stab for a few days so will have a look after the 20th. I also admit to having bought some Sparkfun sensors that I hope will be simpler to implement. – Byte Insight Jul 06 '16 at 14:22

1 Answers1

1

This is the python code for Si132 python library and with little modification you should be able to make it work for SI1145. if you still face any issue you can post this on CE I2C community they have all kinds of I2C libraries.

bruce
  • 436
  • 2
  • 4