sampazzer - 2017-09-30

Hi all,

I wrote the following in python to read from a SI7021 sensor. I had to the use smBUS2 library that I found on pypi as the original smbus library wouldnt let me read/write bytes in the format that the manual for the SI7021 showed.

from smbus2 import SMBus, i2c_msg, SMBusWrapper
import time
class temp_humid:
    """My class to handle the readings from the SI7021 temp humid sensor
    """
    def __init__(self):
        self.address = 0x40
        self.humidityCommand = 0xF5
        self.tempCommand = 0xE0
        self.temp_value = None
        self.humidity_value = None
    def humidity_temp_set(self):
        """Sends command to read humidity. Then reads humidity with a pause between the two bytes.
        Temperature then reads as a temp reading is taken by the sensor for compensation for humidity reading
        """
        #---HUMIDITY I2C WRITE READ---#
        with SMBusWrapper(1) as bus:
            self.humidity_write = i2c_msg.write(self.address,[self.humidityCommand])
            self.humidity_read = i2c_msg.read(self.address,2)
            bus.i2c_rdwr(self.humidity_write)
            time.sleep(0.25)
            bus.i2c_rdwr(self.humidity_read)
        #---TEMP I2C WRITE READ---#
        with SMBusWrapper(1) as bus:
            self.temp_write = i2c_msg.write(self.address,[self.tempCommand])
            self.temp_read = i2c_msg.read(self.address,2)
            bus.i2c_rdwr(self.temp_write)
            bus.i2c_rdwr(self.temp_read)
    def humidity_get(self):
        """Carries out the maths for the humidity reading and returns it in %
        """
        #---HUMIDITY VALUE EDITING---#
        humidity_byte_list = list(self.humidity_read)
        humidity_MSB = humidity_byte_list[0]
        humidity_LSB = humidity_byte_list[1]
        humidity_word = (humidity_MSB<<8) + humidity_LSB
        self.humidity_value = ((125.0*humidity_word)/65536.0) - 6
        return round(self.humidity_value,2)
    def temp_get(self):
        """Carries out the maths for the temperature reading and returns it in DegC
        """
        #---TEMPERATURE VALUE EDITIING---#
        temp_byte_list = list(self.temp_read)
        temp_MSB = temp_byte_list[0]
        temp_LSB = temp_byte_list[1]
        temp_word = (temp_MSB<<8) + temp_LSB
        self.temp_value = ((175.72*temp_word)/65536)-46.85
        return round(self.temp_value,2)

Could anyone point me in the right direction for writing something similar for this in codesys? Do I have to write a device description or can I just write a function that calls the correct I2C blocks? (whats the deal with the Super^ call also?) I've tried to follow the PDF that comes with the codesys pi controller documentation but I don't find it the easiest thing to follow.

Thanks in advance.

Sam.

 

Related

Talk.ru: 1