Hallo Zusammen,
ich hab am Raspberry PI1 Model B+ einen MCP 3008 um Spannungen zu messen,
das ganze Funktioniert auch ohne Probleme mit diesem Python Code.
import RPi.GPIO as GPIO
import time
AO_pin = 0 #flame sensor AO connected to ADC chanannel 0
# change these as desired - they're the pins connected from the
# SPI port on the ADC to the Cobbler
SPICLK = 11
SPIMISO = 9
SPIMOSI = 10
SPICS = 8
#port init
def init():
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
# 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)
pass
#read SPI data from MCP3008(or MCP3204) 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 >>= 1 # first bit is 'null' so drop it
return adcout
def main():
init()
time.sleep(2)
print"will detect voltage"
while True:
ad_value = readadc(AO_pin, SPICLK, SPIMOSI, SPIMISO, SPICS)
voltage= ad_value*(3.3/1024)*5
print"***********"
print " Voltage is: " + str("%.2f"%voltage)+"V"
print"***********"
print' '
time.sleep(0.5)
if __name__ =='__main__':
try:
main()
except KeyboardInterrupt:
pass
GPIO.cleanup()
Alles anzeigen
Nun möchte ich zwei Sachen machen:
-> Den Wert in eine RRD Datenbank schreiben
-> Den Wert als Overlay auf einem Bild (Spannung des Solarakkus einer Autarken Webcam) ausgeben. Temperatur geb ich bereits mit Imagemagick aus (über ein Shellscript).
#Temperatur Auslesen
temp=`cat /sys/bus/w1/devices/28-0317714610ff/w1_slave | sed -n "s/.*t=\(.*\)/\1/p"`
temp2=`echo "scale=1; $temp / 1000" | bc`
#EXIF Daten
TIME=$(exiftool -s -s -s -ExposureTime "/tmp/webcam.jpg")
MM=$(exiftool -s -s -s -FocalLength "/tmp/webcam.jpg")
ISO=$(exiftool -s -s -s -ISO "/tmp/webcam.jpg")
FF=$(exiftool -s -s -s -FNumber "/tmp/webcam.jpg")
#Schriftzug erstellen für Webcambild
convert -background lightgray \
-fill black \
-pointsize 135 \
-gravity center label:" Dornbirn Wallenmahd | Blickrichtung Bregenz\n $date2 | $temp2°C \n ISO $ISO \| f\/$FF | $TIME s " \
-gravity southeast -splice 15x15 -gravity northwest -splice 15x15 \
/tmp/watermark.png
Alles anzeigen
Nun brauche ich Hilfe dabei, da ich mit Python noch nie gearbeitet habe.
DANKE Schonmal für eure Tips.
LG Thomas