CODESYS - the IEC 61131-3 automation software

Welcome to the official CODESYS Forum
Deutsche Version English version russian version 
It is currently Sat Nov 18, 2017 3:49 am

All times are UTC+01:00




Post new topic  Reply to topic  [ 1 post ] 
Author Message
 Post subject: I2C Raspberry PI Query
PostPosted: Thu Sep 07, 2017 8:35 am 
Offline

Joined: Sat Apr 29, 2017 6:20 pm
Posts: 3
Hi Folks.

I've written the following code in python for my raspberry pi to read a SI7021 temp + humidity sensor:

Code:
#---SI7021 CLASS---#

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)


I'm looking to create something similar using the i2c function blocks provided for the pi SL Codesys. I've read the read-me which seems to be a little vague. Can I do it without having to write a device description or can I just build it with the FB's? I'd appreciate anyone that can steer me in the right direction!

Thanks in advance.

Sam.


Top
   
Display posts from previous:  Sort by  
Post new topic  Reply to topic  [ 1 post ] 

All times are UTC+01:00


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Limited