uilfut
Posts: 72
Joined: Sat Mar 03, 2018 1:20 am
Location: Toronto

GPIO: mixing i2c with GPIO simple button

Mon Jan 28, 2019 4:15 pm

Hey guys

I have a pi that I'd like to connect three things to:
  • collecting temp / humidity readings from DHT22 sensor (Adafruit_DHT library handles pins) - working!
  • displaying temp on 128x32 oled (Adafruit_SSD1306 handles pins - i2c I believe) - working!
  • push-button to launch script to display latest readings to the screen - not working :(
The button I'm trying to set up with GPIO physical pin locations - which clashed with whatever Adafruit is doing.

The physical GPIO pin I'm using is "10", aka "GPIO 15".

How should I reference this in my code in i2c parlance, or am I on completely the wrong track?!

Thank you

Will

uilfut
Posts: 72
Joined: Sat Mar 03, 2018 1:20 am
Location: Toronto

Re: GPIO: mixing i2c with GPIO simple button

Mon Jan 28, 2019 4:25 pm

Picture attached
Attachments
20190128_111841-02.jpeg
20190128_111841-02.jpeg (144.54 KiB) Viewed 512 times

uilfut
Posts: 72
Joined: Sat Mar 03, 2018 1:20 am
Location: Toronto

Re: GPIO: mixing i2c with GPIO simple button

Mon Jan 28, 2019 4:33 pm

Code: Select all


import Adafruit_GPIO.SPI as SPI
import Adafruit_SSD1306
import Adafruit_GFX
from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont
import subprocess
import mysql.connector as mariadb
import RPi.GPIO as GPIO
import time

# button setup - 
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD) # uses board physical pin numbering
GPIO.setup(10, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) # initialises with down/off

# Raspberry Pi pin configuration:
RST = None     # on the PiOLED this pin isnt used
# Note the following are only used with SPI:
DC = 23
SPI_PORT = 0
SPI_DEVICE = 0

# 128x32 display with hardware I2C:
disp = Adafruit_SSD1306.SSD1306_128_32(rst=RST)
# Initialize library.
disp.begin()

# Clear display.
disp.clear()
disp.display()

# Create blank image for drawing.
# Make sure to create image with mode '1' for 1-bit color.
width = disp.width
height = disp.height
image = Image.new('1', (width, height))

# Get drawing object to draw on image.
draw = ImageDraw.Draw(image)

# Draw a black filled box to clear the image.
draw.rectangle((0,0,width,height), outline=0, fill=0)

# Draw some shapes.
# First define some constants to allow easy resizing of shapes.
padding = -2
top = padding
bottom = height-padding
# Move left to right keeping track of the current x position for drawing shapes.
x = 0

# Load  font
font = ImageFont.truetype('/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf', 18)

def display_on_oled():
    
    # Draw a black filled box to clear the image.
    draw.rectangle((0,0,width,height), outline=0, fill=0)

    conn = mariadb.connect(user='xx', password='xxxx', database='weather')
    cur = conn.cursor()
    cur.execute('SELECT TempSensor3, TIME_FORMAT(TS, "%H:%i") AS HourMin FROM readings WHERE TempSensor3 IS NOT NULL ORDER BY readingID DESC LIMIT 1;')
    balcony = cur.fetchone()
    cur.execute('SELECT HumidSensor1, TIME_FORMAT(TS, "%H:%i") AS HourMin FROM readings WHERE HumidSensor1 IS NOT NULL ORDER BY readingID DESC LIMIT 1;')
    humid1 = cur.fetchone()
    cur.close()

    draw.text((x, top),       str(balcony[0]) + " " + str(balcony[1]),  font=font, fill=255)
    #    draw.text((x, top+8),     str(balcony[1]), font=font, fill=255)
    draw.text((x, top+17),    str(humid1[0]) + "% " + str(humid1[1]),  font=font, fill=255)
    #    draw.text((x, top+25),    str(Disk),  font=font, fill=255)

    # Display image.
    disp.image(image)
    disp.display()
    time.sleep(10)
    disp.clear()
    disp.display()
    return

while True:
    if GPIO.input(1) == GPIO.HIGH:
        display_on_oled()
        time.sleep(10)



uilfut
Posts: 72
Joined: Sat Mar 03, 2018 1:20 am
Location: Toronto

Re: GPIO: mixing i2c with GPIO simple button

Mon Jan 28, 2019 5:09 pm

Error Message:
pi@raspberrypi:~ $ python3 display_temp.py
Traceback (most recent call last):
File "display_temp.py", line 34, in <module>
disp = Adafruit_SSD1306.SSD1306_128_32(rst=RST)
File "/usr/local/lib/python3.5/dist-packages/Adafruit_SSD1306-1.6.2-py3.5.egg/Adafruit_SSD1306/SSD1306.py", line 288, in __init__
File "/usr/local/lib/python3.5/dist-packages/Adafruit_SSD1306-1.6.2-py3.5.egg/Adafruit_SSD1306/SSD1306.py", line 85, in __init__
File "/usr/local/lib/python3.5/dist-packages/Adafruit_GPIO-1.0.3-py3.5.egg/Adafruit_GPIO/GPIO.py", line 418, in get_platform_gpio
File "/usr/local/lib/python3.5/dist-packages/Adafruit_GPIO-1.0.3-py3.5.egg/Adafruit_GPIO/GPIO.py", line 172, in __init__
ValueError: A different mode has already been set!

uilfut
Posts: 72
Joined: Sat Mar 03, 2018 1:20 am
Location: Toronto

Re: GPIO: mixing i2c with GPIO simple button

Tue Jan 29, 2019 4:40 am

I figured it out don't worry.

Return to “Beginners”