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
- import RPi.GPIO as GPIO
- from datetime import datetime
- import smtplib
- from email.mime.text import MIMEText
- from twilio.rest import Client
- # empty variables
- waarde0 = 0
- waarde1 = 0
- # MySql-library importeren
- import MySQLdb
- # Verbinden met de lokale database
- database = MySQLdb.connect(host="localhost", user="root", passwd="******", db="******")
- # Database selecteren via de cursor methode
- # Via deze cursor kunnen we de database later aanpassen
- cursor = database.cursor()
- # ADC activeren
- spi = spidev.SpiDev() # create spi object
- spi.open(0,0) # open spi port 0, device CS0 pin 24
- # To use Raspberry PI board GPIO numbers
- GPIO.setmode(GPIO.BCM) #GPIO
- GPIO.setup(17, GPIO.OUT) # GPIO 17 output
- # Blinking function
- def blink(pin):
- # Setup GPIO output channel
- GPIO.setup(pin, GPIO.OUT)
- GPIO.output(pin, 1)
- #time.sleep(0.1)
- #GPIO.output(pin, 0)
- #time.sleep(0.1)
- def blink2(pin):
- # Setup GPIO output channel
- GPIO.setup(pin, GPIO.OUT)
- GPIO.output(pin, 0)
- # 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:
- # waardes ophalen
- waarde0 = ((readadc(0)*100) / 1023) # read channel 0
- waarde1 = ((readadc(1)*100) / 1023) # read channel 1
- print "waarde 1: ", waarde0
- print "waarde 2: ", waarde1
- if ((waarde0 < 50) or (waarde1 < 50)):
- blink(17)
- if (((waarde0 > 50) and (waarde0 < 80)) or ((waarde1 > 50) and (waarde1 < 80))):
- # Create the email content
- message = "The value passed 50%, the first value is " + str(waarde0) + "% and the second value is " + str(waarde1) + "%."
- msg = MIMEText(message)
- msg['Subject'] = 'RPi Python values'
- msg['From'] = 'wielandtjordan@gmail.com'
- msg['To'] = 'wielandtjordan@gmail.com'
- # Send the email via Gmail server
- username = 'wielandtjordan'
- password = '*****'
- server = smtplib.SMTP('smtp.gmail.com:587') # Gmail rewriting port 25 to port 587
- server.starttls() # Support SMTP AUTH
- server.login(username,password)
- server.sendmail(msg['From'], msg['To'], msg.as_string())
- server.quit()
- if ((waarde0 > 80) or (waarde1 > 80)):
- # Your account SID from twilio.com/console - dashboard
- account_sid = "AC*******"
- # Your auth token from twilio.com/console - dashboard
- auth_token = "dc********"
- client = Client(account_sid, auth_token)
- message = client.messages.create(
- to="+32*******",
- from_="+32********",
- body="The value passed 80%, the first value is " + str(waarde0) + "% and the second value is " + str(waarde1) + "%.")
- print(message.sid)
- # De funtctie cursor.execute voert SQL commando's uit
- # waarde0 en waarde1 wegschrijven naar de tabel in de database)
- cursor.execute("INSERT INTO Test(waarde0, waarde1) VALUES(%s, %s)", (waarde0, waarde1))
- # Voert elke openstaande opdracht uit op de database zelf
- database.commit()
- # Wait 1s
- time.sleep(1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement