As this post is more of an update, I won’t be adding any explanations, just giving the python code.
This will read 3 values from the adc and put them into the database “adc_database”. It will put them in the table “adc_input_data_4” in the columns “Channel_1″,”Channel_2” and “Channel_3” respectively.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
#!/usr/bin/env python # -*- coding: utf-8 -*- import spidev import time import MySQLdb import sys import RPi.GPIO as GPIO pin = 26 GPIO.setmode(GPIO.BOARD) GPIO.setup(pin, GPIO.OUT) con = MySQLdb.connect('localhost','adc_user','adc_user_pass','adc_database'); cursor = con.cursor() spi = spidev.SpiDev() spi.open(0, 0) count = 0 maxcyclenumber = 5 tmp = "derp" def readadc(adcnum): # read SPI data from MCP3008 chip, 8 possible adc's (0 thru 7) if adcnum > 7 or adcnum < 0: return -1 r = spi.xfer2([1, 8 + adcnum << 4, 0]) adcout = ((r[1] & 3) << 8) + r[2] return adcout for _ in range(maxcyclenumber): GPIO.output(pin,True) cursor.execute("INSERT INTO adc_input_data_4(Channel_1,Channel_2,Channel_3) VALUES('%s','%s','%s')",(readadc(0),readadc(1),readadc(2)) ) GPIO.output(pin,False) count = count+1 print count time.sleep (1) if count == maxcyclenumber: GPIO.cleanup() con.commit() con.close() |
There you go, bigger post coming later tonight.
Love the derp. Love the blog as well – planning on adapting your PiPlanter idea to my situation. Everything has been very informative.
I’m just wondering, based off your blog and other searching, how one would go about connecting two ADC’s to the RPI to essentially expand the number of analog sensors?
I have 2 “zones” for planting that I wish to gather all the sensor data from, as well as several hygrometers, working out to about 15 sensors overall. With the MCP3008, it would appear I’m limited to 8 inputs. I was unable to find a larger newbie friendly ADC with more inputs, but I did note the CS0 and CS1 pins that would appear you could use for two ADC’s sharing the SCLK, MISO and MOSI pins?
I apologize if this is a bit far out for a comment 🙂 But I do appreciate your input!
Hey, I’m not totally sure if that would work out, but It might be worth it to experiment. Please come back if you find out if this works.