-1

The source code I have below works for a two analog voltages(water level detection and temperature sensor). The code is set right now for just these two to run and it does work. If there is a problem the finally at the end will throw an error and I think this has something to do with the SPI bus. When I try to incorporate a dht11 temperature and humidity sensor that has a digital input, it will run for a couple seconds and then through the error. I tried it again and now it says that it is unable to set line (mcp = Adafruit....) to input. I think this problem is because both sensor are fighting over SPI bus for adafruit. I am not an expert programmer, I just want something that will work at the same time, so three total sensors(two channel voltages and 1 sensor output). Many lines of code I have commented out because I have combined different code. Some lines may be valuable for the temp and humidity sensor.

from time import sleep
import board
import adafruit_dht
import psutil
#
#import RPi.GPIO as GPIO

#import spidev import Adafruit_MCP3008 #spi = spidev.SpiDev() #spi.open(0, 0) #spi.max_speed_hz = 250000

#GPIO.setmode(GPIO.BCM) #GPIO.setup(14, GPIO.OUT) #GPIO.setup(15, GPIO.OUT) mcp = Adafruit_MCP3008.MCP3008(clk=11, cs=8, miso=9, mosi=10) #sensor = adafruit_dht.DHT11(board.D23)

def poll_sensor(channel): """Poll MCP3002 ADC Args: channel (int): ADC channel 0 or 1 Returns: int: 10 bit value relating voltage 0 to 1023 """ assert 0 <= channel <= 1, 'ADC channel must be 0 or 1.'

    # First bit of cbyte is single=1 or diff=0.
    # Second bit is channel 0 or 1
    if channel:
        cbyte = 0b11000000
    else:
        cbyte = 0b10000000

    # Send (Start bit=1, cbyte=sgl/diff &amp; odd/sign &amp; MSBF = 0)
    r = spi.xfer2([1, cbyte, 0])

    # 10 bit value from returned bytes (bits 13-22):
    # XXXXXXXX, XXXX####, ######XX
    return ((r[1] &amp; 31) &lt;&lt; 6) + (r[2] &gt;&gt; 2)

try: while True:

    #temp = sensor.temperature
   # humidity = sensor.humidity
   # print(&quot;Temperature: {}*C   Humidity: {}% &quot;.format(temp, humidity))

   #channel = 0
   # channeldata = poll_sensor(channel)

   # voltage = round(((channeldata * 3300) / 1024), 0)
    #print('Voltage (mV): {}'.format(voltage))
    #print('Data        : {}\n'.format(channeldata))

   # if voltage &lt; 50:
        # Green
    #    GPIO.output(14, GPIO.HIGH)
    #    GPIO.output(15, GPIO.LOW)
    #elif voltage &lt; 1800:
        # Yellow
     #   GPIO.output(14, GPIO.HIGH)
      #  GPIO.output(15, GPIO.HIGH)
   # else:
        # Red
      #  GPIO.output(14, GPIO.LOW)
      #  GPIO.output(15, GPIO.HIGH)

    print('Reading MCP3004 values, press Ctrl-C to quit...')
    # Print nice channel column headers.
   # print('| {0:&gt;4} | {1:&gt;4} | {2:&gt;4} | {3:&gt;4} | {4:&gt;4} | {5:&gt;4} | {6:&gt;4} | {7:&gt;4} |'.format(*range(8)))
   # print('-' * 57)
    # Main program loop.

    # Read all the ADC channel values in a list.
    values = [0]*4
    for i in range(4):
    # The read_adc function will get the value of the specified channel (0-7).
        values[i] = mcp.read_adc(i)
    # Print the ADC values.
    print('|Channel 0 ={0:&gt;4} | Channel 1 ={1:&gt;4} | '.format(*values))
     # Pause for half a second.
    sleep(0.5)

finally: # run on exit spi.close() # clean up #GPIO.cleanup() #print ("\n All cleaned up.")

Dirk
  • 3,541
  • 3
  • 18
  • 25
Nate
  • 7
  • 2

1 Answers1

0

DHT11 has nothing to do with SPI - it is a proprietary 1-Wire like protocol which can use ANY pin. The timing on DHT11 is critical.

In my experience the Adafruit code is unreliable (not to mention complex to setup and inflexible).

I use a version of Joan's DHT code.
See Reliable temperature/humidity logging with Python and a DHT11

Milliways
  • 59,890
  • 31
  • 101
  • 209