Skip to content

Instantly share code, notes, and snippets.

@guglielmino
Last active August 29, 2015 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save guglielmino/b4fa6e5d1c81f93acea8 to your computer and use it in GitHub Desktop.
Save guglielmino/b4fa6e5d1c81f93acea8 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
# -*- coding: utf-8 -*-
from smbus import SMBus
from pushetta import Pushetta
import math
import time
# Interfaccia generica per i sensori
class GenericSensor:
def readValue(self):
raise NotImplementedError('Metodo readValue da implementare')
def measureUnit(self):
raise NotImplementedError('Metodo measureUnit da implementare')
# Impementazione concreta per il sensore di temperatura
class TemperatureSensor(GenericSensor):
adc_address = 0x48 # Address I2C
adc_channel = 0x40 # Channel of device
V_REF = 3.3 # Tensione riferimento misura ADC
R1 = 15000.0 # Resistenza R1
V_IN = 3.3 # Tensione alimentazione partitori tensione
R_th0 = 10000.0 # Resistenza riferimento termistore a 25 c
A = 0.00335402 # Steinhart-Hart Costante A
B = 0.000256985 # Steinhart-Hart Costante B
C = 2.62013e-6 # Steinhart-Hart Costante C
D = 6.38309e-8 # Steinhart-Hart Costante D
pB = 4100.0 # Costante parametro B
K = 6.0 # Fattore dissipazione K 6mV C
def readValue(self):
bus = SMBus(1) # 0 per le rev.1
bus.write_byte(self.adc_address, self.adc_channel)
raw_val = bus.read_byte(self.adc_address)
raw_val = bus.read_byte(self.adc_address)
raw_val = bus.read_byte(self.adc_address)
V = raw_val * self.V_REF / 256.0
R_th = self.R1 * V / (self.V_IN - V)
# Calcolo dei gradi Kelvin con la formula di Steinhart-Hart
logR = math.log(R_th / self.R_th0)
logR2 = logR ** 2
logR3 = logR ** 3
Stein = 1.0 / (self.A + self.B * logR + self.C * logR2 + self.D
* logR3)
# Conversione in Celsius
Celsius = round(Stein - 273.15 - V ** 2 / (self.K * R_th), 2)
return Celsius
def measureUnit(self):
return ' C'
pushetta = Pushetta('0011223344556677aabbccddeeff001122334455')
sensor = TemperatureSensor()
while True:
temp = sensor.readValue()
if temp < 18.0:
pushetta.pushMessage('Camera', 'Brrrr...qui fa freddo')
time.sleep(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment