Encore un autre morceau de PYTHON pour ne pas le perdre.
Cette fois ci, il s’agit d’une sonde température LM35.
En terme de sonde température, nous avons le choix entre de l’analogique (le cas du LM36, LM36…) et du digital (DS18B20, DS1621…).
Or, notre framboise ne fait que du digital… Pour exploiter l’analogique, il faut un MCP3008 qui permet d’ajouter 8 sondes analogiques en utilisant que 4 pin GPIO.
A terme, je conseille d’opter pour le digital, plus précis et plus simple, mais en attendant, rien ne nous empêche de faire mumuse, et surtout de se faire la main sur le MCP3008… Coté câblage, nous avons donc ceci :
Coté code, c’est la dessous :
#!/usr/bin/python ################################################################################ # PROJECT: Babyroom - FILE: sonde_temperature.py - CREATION: 2013/03/02 # Ce fichier est sous licence GPL. # $Id$ ################################################################################ """ Sonde temperature sur le port 0 du MCP3008 avec enregistrement dans un fichier de log Ce fichier de log est a destination de rrdtool. U{https://www.hamida.info/}. @version: 1.0 @author: U{Mehdi HAMIDA (idem) <idem@highlanders.org>} """ # Import required Python libraries import time import os import RPi.GPIO as GPIO import glob import datetime # Use BCM GPIO references # instead of physical pin numbers GPIO.setmode(GPIO.BCM) #DEBUG = 1 DEBUG = 0 # read SPI data from MCP3008 chip, 8 possible adc's (0 thru 7) def readadc(adcnum, clockpin, mosipin, misopin, cspin): if ((adcnum > 7) or (adcnum < 0)): return -1 GPIO.output(cspin, True) GPIO.output(clockpin, False) # start clock low GPIO.output(cspin, False) # bring CS low commandout = adcnum commandout |= 0x18 # start bit + single-ended bit commandout <<= 3 # we only need to send 5 bits here for i in range(5): if (commandout & 0x80): GPIO.output(mosipin, True) else: GPIO.output(mosipin, False) commandout <<= 1 GPIO.output(clockpin, True) GPIO.output(clockpin, False) adcout = 0 # read in one empty bit, one null bit and 10 ADC bits for i in range(12): GPIO.output(clockpin, True) GPIO.output(clockpin, False) adcout <<= 1 if (GPIO.input(misopin)): adcout |= 0x1 GPIO.output(cspin, True) adcout /= 2 # first bit is 'null' so drop it return adcout # change these as desired - they're the pins connected from the # SPI port on the ADC to the Cobbler SPICLK = 18 SPIMISO = 23 SPIMOSI = 24 SPICS = 25 # set up the SPI interface pins GPIO.setup(SPIMOSI, GPIO.OUT) GPIO.setup(SPIMISO, GPIO.IN) GPIO.setup(SPICLK, GPIO.OUT) GPIO.setup(SPICS, GPIO.OUT) # temperature sensor connected channel 0 of mcp3008 lm35_adcnum = 0 try: print "TEMPERATURE SENSOR (CTRL-C to exit)" while True: type_sonde = "sonde_temperature" logfile = "/data/rrd/log/"+ type_sonde +".log" # read the analog pin (temperature sensor LM35) read_lm35 = readadc(lm35_adcnum, SPICLK, SPIMOSI, SPIMISO, SPICS) # convert analog reading to millivolts = ADC * ( 3300 / 1024 ) lm35mv = read_lm35 * ( 3300.00 / 1024.00) # 10 mv per degree lm35_C = (((lm35mv) / 10.00)-1.50) #LM35 # remove decimal point from millivolts lm35mv = "%d" % lm35mv # show two decimal place for temprature and voltage readings lm35_C = "%.2f" % lm35_C print "temp_C:tt", lm35_C output_file = open(logfile, "w") output_file.write(lm35_C) output_file.close() if DEBUG: print "LM-35 :t ADC:",read_lm35,"t MV:",lm35mv,"t C:",lm35_C time.sleep(5) except KeyboardInterrupt: print " Quit" # Reset GPIO settings GPIO.cleanup()
bla
Un commentaire
Pingback: Ma framboise en digital | Geek and more