Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- import cgitb ; cgitb.enable()
- import spidev
- import time
- from datetime import datetime
- import RPi.GPIO as GPIO
- import smtplib
- from email.mime.text import MIMEText
- from twilio.rest import Client
- tijdstip = 0
- #MySql-library importeren
- import MySQLdb
- #Verbinden met de lokale database
- database = MySQLdb.connect(host="localhost", user="root",
- passwd="_Kultuur16", db="data")
- #Database selecteren
- cursor = database.cursor()
- spi = spidev.SpiDev() #create spi object
- spi.open(0,0) #open spi port 0, device CS0 pin 24
- #read SPI data 8 possible adc's (0 thru 7)
- def readadc(adcnum):
- if ((adcnum > 7) or (adcnum < 0)):
- return -1
- r = spi.xfer2([1,(8+adcnum)<<4,0])
- adcout = ((r[1]&3) << 8) + r[2]
- return adcout
- while True:
- tmp0 = readadc(0) # read channel 0
- tmp1 = readadc(1) # read channel 1
- ad_waarde = ((tmp0 * 100) / 1024)
- tijdstip = datetime.now()
- print tijdstip,"--> input0:",ad_waarde,"%"
- #print "input1:",tmp1
- #Tijdstip en teller wegschrijven naar de tabel in de database
- cursor.execute("INSERT INTO examen(tijdstip,ad_waarde) VALUES(%s,%s)",
- (tijdstip,ad_waarde))
- database.commit()
- if ((ad_waarde >= 50) and (ad_waarde < 80)):
- #opmaken van email
- message = ('De waarde is ' + format(ad_waarde) + '%')
- msg = MIMEText(message)
- msg['Subject'] = 'RPi Python test'
- msg['From'] = 'raspberrypitm@gmail.com'
- msg['To'] = 'mattidc.mdc@gmail.com'
- # send the email via Gmail server
- username = 'raspberrypitm@gmail.com'
- password = 'thomasmore'
- server = smtplib.SMTP('smtp.gmail.com:587') # Gmail rewriting port 25 to port 587
- server.starttls() # Support SMPT AUTH
- server.login(username,password)
- server.sendmail(msg['From'], msg['To'], msg.as_string())
- server.quit()
- if (ad_waarde >= 80)=
- # Your Account SID from twilio.com/console - dashboard
- account_sid = "AC728f597da5b0f85246ce559539bd2673"
- # Your Auth Token from twilio.com/console - dashboard
- auth_token = "83082db0d4d58955e61446109e92689e"
- client = Client(account_sid, auth_token)
- message = client.messages.create(
- to="+32479677232",
- from_="+32460205439",
- body='De waarde is ' + format(ad_waarde) + '%'))
- time.sleep(0.2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement