Search code examples
python-3.xsqliteraspberry-pigpio

how do i strip down a tuple from sqlite so i can use it as a float in python?


i am trying to use a single data reading from a sqlite3 file it is returned to me in the following way (25.5,) format. my next part of the script i want to be able to trigger a GPIO pin output to turn on a relay when the temperature drops below a certain point and off again once it gets above another point. my code is as follows

import RPi.GPIO as GPIO
import time
import sqlite3
GPIO.setmode(GPIO.BCM) 
GPIO.setwarnings(False)
#GPIO.setup(18, GPIO.OUT)
# Set up all variables #
temp_min = 25.375
temp_max = 25.5


#############
# Main Loop #
#############
while True:
    
    def display_5sec_data():
        conn = sqlite3.connect('ds18b20.db')
        c = conn.cursor()
        c.execute("SELECT (display1) FROM aquarium2 ORDER BY rowid DESC LIMIT 1")
        data1 = c.fetchone()
        conn.close()
        #return data1    
        print(data1)


    Display = display_temp()

    data1= (display_5sec_data())     
    if data1 <= 25.25 : # if temp is less or equal to 25 deg
        print("heater on")
        #GPIO.output(18, GPIO.HIGH)
    if data1 >= 25.80 :
        print("heater off")
        #GPIO.output(18, GPIO.LOW)
    

    time.sleep(10)
    
    display_5sec_data()

any help would be great.


Solution

  • Sorted was an easy fix just wished i had played a little bit more with it. i just need to add float to the if statement here is the code.

    if float(data1[0]) >= 25.60 :

    print("heater off")
    bus.write_pin(11, 0)
    bus.write_pin(10, 0)
    time.sleep(120)