Advertisement
Guest User

Untitled

a guest
Dec 1st, 2017
363
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.33 KB | None | 0 0
  1. #!/usr/bin/env python
  2. import cgitb ; cgitb.enable()
  3. import spidev
  4. import time
  5. from datetime import datetime
  6. import RPi.GPIO as GPIO
  7. import smtplib
  8. from email.mime.text import MIMEText
  9. from twilio.rest import Client
  10.  
  11. tijdstip = 0
  12.  
  13. #MySql-library importeren
  14. import MySQLdb
  15. #Verbinden met de lokale database
  16. database = MySQLdb.connect(host="localhost", user="root",
  17. passwd="_Kultuur16", db="data")
  18. #Database selecteren
  19. cursor = database.cursor()
  20.  
  21. spi = spidev.SpiDev() #create spi object
  22. spi.open(0,0) #open spi port 0, device CS0 pin 24
  23.  
  24. #read SPI data 8 possible adc's (0 thru 7)
  25. def readadc(adcnum):
  26. if ((adcnum > 7) or (adcnum < 0)):
  27. return -1
  28. r = spi.xfer2([1,(8+adcnum)<<4,0])
  29. adcout = ((r[1]&3) << 8) + r[2]
  30. return adcout
  31.  
  32. while True:
  33. tmp0 = readadc(0) # read channel 0
  34. tmp1 = readadc(1) # read channel 1
  35. ad_waarde = ((tmp0 * 100) / 1024)
  36. tijdstip = datetime.now()
  37. print tijdstip,"--> input0:",ad_waarde,"%"
  38. #print "input1:",tmp1
  39.  
  40. #Tijdstip en teller wegschrijven naar de tabel in de database
  41. cursor.execute("INSERT INTO examen(tijdstip,ad_waarde) VALUES(%s,%s)",
  42. (tijdstip,ad_waarde))
  43. database.commit()
  44. if ((ad_waarde >= 50) and (ad_waarde < 80)):
  45. #opmaken van email
  46. message = ('De waarde is ' + format(ad_waarde) + '%')
  47. msg = MIMEText(message)
  48. msg['Subject'] = 'RPi Python test'
  49. msg['From'] = 'raspberrypitm@gmail.com'
  50. msg['To'] = 'mattidc.mdc@gmail.com'
  51.  
  52. # send the email via Gmail server
  53. username = 'raspberrypitm@gmail.com'
  54. password = 'thomasmore'
  55. server = smtplib.SMTP('smtp.gmail.com:587') # Gmail rewriting port 25 to port 587
  56. server.starttls() # Support SMPT AUTH
  57. server.login(username,password)
  58. server.sendmail(msg['From'], msg['To'], msg.as_string())
  59. server.quit()
  60.  
  61. if (ad_waarde >= 80)=
  62.  
  63. # Your Account SID from twilio.com/console - dashboard
  64. account_sid = "AC728f597da5b0f85246ce559539bd2673"
  65. # Your Auth Token from twilio.com/console - dashboard
  66. auth_token = "83082db0d4d58955e61446109e92689e"
  67. client = Client(account_sid, auth_token)
  68. message = client.messages.create(
  69. to="+32479677232",
  70. from_="+32460205439",
  71. body='De waarde is ' + format(ad_waarde) + '%'))
  72.  
  73. time.sleep(0.2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement