tomhartley / AirPi

a Raspberry Pi weather station
351 stars 139 forks source link

Custom Sensor #34

Open WirthmU opened 4 years ago

WirthmU commented 4 years ago

I am not sure if you still are using the AirPi?!

I try to add a new sensor based on your template (SHT31 temperature and humidity sensor) with no luck so far.

Could you please give some advice?

`#!/usr/bin/python

import sensor import time import smbus

class SHT31(sensor.Sensor): requiredData = [] optionalData = []

def __init__(self,data):
    """
    Initialisation.
    Any initialisation code goes here.
    """
    self.readingtype = "temperature"
    self.sensorname = "SHT31"
    self.valname = "Temperature"
    self.valunit = "Degrees Celsius"
    self.valsymbol = "C"
    self.description = "SHT31 Sensor for Temperature and Humidity."
    return

def getval(self):
    try:
        bus = smbus.SMBus(1)
        bus.write_i2c_block_data(0x44, 0x2C, [0x06])
        time.sleep(0.5)
        data = bus.read_i2c_block_data(0x44, 0x00, 6)
        ctemp = data[0] * 256 + data[1]
        temp = -45 + (175 * ctemp / 65535.0)
    except TypeError as terr:
        pass
    return temp`