Tuesday, April 30, 2019

Getting Started with Database SQLIte3 python and Raspberry pi

Lab 1e

Adding Database to our Project

Objective

We want to add time when we turned Lights on to our Database Let us learn how to do that for this, we would be using SQlite3 and make sure to download DB browser on Raspberry pi to open your database file.

In [4]:
%%html
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAOAAAADgCAMAAAAt85rTAAAAzFBMVEX////m5+h7msuitdrL1emxs7YrKiwuLC/r7O0pJyr4+PgxMDK1t7p+ntHc3d7u7/Camp03Njg8NzPR2/C7u7s0MTGoqKk3MzBZWVugs9hTXXBgZnVBQEJvcnunu+JEREdMS05wcHKAgILFxcdSV2JYZXyOjpDR0tNJSUxhYWRzjrlzdHeGhoiWnat4hJygoKJ+g49veY5gcpBqgaZ+i6VdYGilrb1thayKmbd2ksC4wdOwucrCzN9+g46OlKGOnr1lbX9kd5hPVmZfZXXCjBVeAAAPs0lEQVR4nO2di1eiTBjGowIcR4FBQKopbsolLbb7ddPq//+fvnlBy0o3kYv6HZ5zdrez6sCvF2HmeYZhZ6dWrVq1atWqVatWrVq1atWqVYyanBRoGm/EqQxe8wKJa657t/KrGWixG1GEBPGnBCRY0TDWpO0EbXqxQ9AUBYm26QzcoQ8augPHDD+pkTUwgu2iDHwbiRiLSIQSBQsORlkKWrFrCkLyVjMOKt7LVeW5DE5ExDUCeakPcKzYWGCQ2N98xmZMGB12tUbGD3K8gzAWLL6U3SpMCttJceU6sNpjjI1C96hQBQSLtpanhaZBMLa5onaoYBmIEC9/KwJBm/lV1BAeFNGObBIkFdFQwWoiMiyoqYiEBbVUpFqiVVRTTUHYwBIqol9YW5GY61RVjhQcFdYWwZsISIT8p9BU8YYCUlQMYSzQDQWkaJh/UNCIMNlMQOw4hIhxPsSGjwh2ow0F1HyLEOSu3g/xHNYZsmPN3MyzqNNqtYYMUSS+l72OsuaKmOH5rJWNBeT5VkuJCCEYmYq3fJ+Z0/wQMTrixC1oZIMBAZH3HcoYsSBGPu/901tqSp4xNJEAcJajtFppEx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ouv1J6fsmYqHcVErb7asUrQ8sWUfu7FSG2T0MVDSvlYxogiH0qQGw/HKplPZL1n/IIVull2VVsn/YZnlP5swMSxSJW1f6PDKFAut2rN5UIduVrXn/IIOxsE/7MSYqAa7fv+iqrXrQ+PJDnIF1Vrf7cuCQH3cPrIaPD2K/w4rdAMm8iDPMEkkwoN2U7yaksVdWx4K7hsRZzJbcGoqgzyLD/mlCuhJnkNleXhxTgkOWv99D8IclwRAHraVL0evewfO6QvvH06rIPiVQX4IZaVSOjbOI0P0KMspsmYof916u704eZnOW7dh9O7yCASrK2LqwuSwZxjmcCVyLOg7WF4Olz+jQDhKDs7fCw/6HDw7e3cBqqdWHdXAFRx28V9ATJKtSUPD4eOjZJFoKdrgP7KaynD8RF1BzAAxLWf7JcWU1YylfTeCOO4fGfcDMNLBUFz/iV5BJXWa9Vq1atWrVq1apVq1atWrW2Wf8BbgSSz+7LUmUAAAAASUVORK5CYII=" , width=400, height=200>

Download Sqlite Browser

In [2]:
%%html
<img src="https://windows-cdn.softpedia.com/screenshots/SQLite-Database-Browser_1.png" , width=400, height=200>

CIRCUIT DIAGRAM:

In [3]:
%%html

<img src="http://razzpisampler.oreilly.com/images/rpck_0901.png" , width=400, height=200>

we are using BOARD numbering for this experiment Pin number 12

In [ ]:
# import the important library
import tkinter as tk
import RPi.GPIO as GPIO
import time
import datetime
import sqlite3


GPIO.setmode(GPIO.BOARD) # to use Raspberry Pi board pin numbers
GPIO.setup(11, GPIO.OUT) # set up GPIO output channel


mainwindow=tk.Tk()
mainwindow.title('Test ')
mainwindow.geometry('640x340')


my_label=tk.Label(mainwindow,text="My First UI",
                  font=("Arial",22), bg= "Green",fg="white")
my_label.grid(row=0,column=0,sticky='NSEW',padx=10,pady=10)

button_on=tk.Button(mainwindow,text="On",bg="black",fg="white",
                    command=lambda :my_on())
button_on.grid(row=1,column=0,sticky='NSEW',padx=10,pady=10)


button_off=tk.Button(mainwindow,text="OFF",bg="black",fg="white",
                     command=lambda:my_off())
button_off.grid(row=1,column=1,columnspa=1,sticky='NSEW',padx=10,pady=10)


def my_on():
    t=datetime.datetime.now()
    my_t = "{}-{}-{}".format(t.hour,t.minute,t.second)
    database_on(my_t)

    print('Led Turn On !!!!! ')
    GPIO.output(11, GPIO.LOW) # set RPi board pin 11 low. Turn off LED.
    time.sleep(1)
    print('Yes you did it !')


def my_off():

    t=datetime.datetime.now()
    my_off = "{}-{}-{}".format(t.hour,t.minute,t.second)
    database_off(my_off)

    print('Led Turned Off !!!!!!  ')
    GPIO.output(11, GPIO.HIGH) # set RPi board pin 11 high. Turn on LED.
    time.sleep(2)
    print('Great Work ! ')

def database_on(on_time):

    conn = sqlite3.connect('led.db')
    cursor = conn.cursor()

    cursor.execute(""" CREATE TABLE IF NOT EXISTS
     ledon
     (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
     on_time TEXT)""")

    cursor.execute("""
    INSERT INTO ledon (on_time) 
     VALUES (?)""", (on_time))

    conn.commit()
    cursor.close()
    conn.close()

def database_off(of_time):

    conn = sqlite3.connect('led.db')
    cursor = conn.cursor()

    cursor.execute(""" CREATE TABLE IF NOT EXISTS
     ledoff
     (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
     off_time TEXT)""")

    cursor.execute("""
    INSERT INTO ledoff (off_time) 
     VALUES (?)""", (of_time))

    conn.commit()
    cursor.close()
    conn.close()

mainwindow.mainloop()

No comments:

Post a Comment

How to Use Publish-Audit-Merge Workflow in Apache Iceberg: A Beginner’s Guide

publish How to Use Publish-Audit-Merge Workflow in Apache Iceberg: A Beginner’s Guide ¶ In [24]: from ...