Commit 0c942578 authored by Charm Thiekshana Perera's avatar Charm Thiekshana Perera 💬

Upload raspberry pi python file

parent 5ea00cdd
Pipeline #4256 failed with stages
#Libraries
import RPi.GPIO as GPIO
import time
import pyrebase
import distance
import datetime, time
import gspread
from oauth2client.service_account import ServiceAccountCredentials
config = {
"apiKey": "AIzaSyAhxxCf-KaxaFY3LvSipgZUOJYM_m1bmNA",
"authDomain": "flood-data-905f4.firebaseapp.com",
"databaseURL": "https://flood-data-905f4-default-rtdb.firebaseio.com",
"projectId": "flood-data-905f4",
"storageBucket": "flood-data-905f4.appspot.com",
"messagingSenderId": "252644106216",
"appId": "1:252644106216:web:72a89cd23231e12de80e5d",
"measurementId": "G-GE2LTX5TTE"
};
scope = ['https://spreadsheets.google.com/feeds','https://www.googleapis.com/auth/drive']
creds = ServiceAccountCredentials.from_json_keyfile_name('mydata.json', scope)
#replace mydata.json with the name of your data file
client = gspread.authorize(creds)
sheet = client.open("Raspi_data").sheet1
#GPIO Mode (BOARD / BCM)
GPIO.setmode(GPIO.BCM)
#set GPIO Pins
GPIO_TRIGGER = 4
GPIO_ECHO = 17
#set GPIO direction (IN / OUT)
GPIO.setup(GPIO_TRIGGER, GPIO.OUT)
GPIO.setup(GPIO_ECHO, GPIO.IN)
def distance():
# set Trigger to HIGH
GPIO.output(GPIO_TRIGGER, True)
# set Trigger after 0.01ms to LOW
time.sleep(1)
GPIO.output(GPIO_TRIGGER, False)
StartTime = time.time()
StopTime = time.time()
# save StartTime
while GPIO.input(GPIO_ECHO) == 0:
StartTime = time.time()
# save time of arrival
while GPIO.input(GPIO_ECHO) == 1:
StopTime = time.time()
# time difference between start and arrival
TimeElapsed = StopTime - StartTime
# multiply with the sonic speed (34300 cm/s)
# and divide by 2, because there and back
distance = (TimeElapsed * 34300) / 2
return distance
if __name__ == '__main__':
try:
while True:
dateandtime = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f')
print(dateandtime)
firebase = pyrebase.initialize_app(config)
storage = firebase.storage()
database = firebase.database()
dist = (round(distance(),1))
print("Water Level:",dist)
values =[dateandtime,dist]
sheet.append_row(values)
print("Firebase water level :",dist)
database.child("River Current Water Level")
data = {"key1": dist}
database.set(data)
time.sleep(1)
# Reset by pressing CTRL + C
except KeyboardInterrupt:
print("Measurement stopped by User")
GPIO.cleanup()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment