Advertisement
Guest User

Untitled

a guest
Dec 3rd, 2017
386
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.78 KB | None | 0 0
  1. import cgitb ; cgitb.enable()
  2. import spidev
  3. import time
  4. import sys
  5. from twilio.rest import Client
  6. import RPi.GPIO as GPIO
  7. import smtplib
  8. from email.mime.text import MIMEText
  9. from pubnub.pubnub import PubNub
  10. from pubnub.pnconfiguration import PNConfiguration
  11. from pubnub.callbacks import SubscribeCallback
  12.  
  13.  
  14.  
  15.  
  16. SMSTeller = 0
  17. GPIO.setmode (GPIO.BCM)
  18. PIN1 = 17
  19. PIN2 = 18
  20. GPIO.setup(PIN1,GPIO.OUT)
  21. GPIO.setup(PIN2,GPIO.OUT)
  22. pnconfig = PNConfiguration()
  23. pnconfig.subscribe_key = 'sub-c-d9cbfc6e-d828-11e7-96e5-36e8e28b70d2'
  24. pnconfig.publish_key = 'pub-c-c7544bad-f255-4e58-8f47-e2bffe7147a1'
  25. pubnub = PubNub(pnconfig)
  26. channel = 'knop'
  27. pnconfig2 = PNConfiguration()
  28. pnconfig2.subscribe_key = 'sub-c-dd037966-d828-11e7-a9ef-2291f454b5f8'
  29. pnconfig2.publish_key = 'pub-c-83062419-8e00-451f-b36e-12f902c7b258'
  30. pubnub2 = PubNub(pnconfig2)
  31. channel2 = 'pot'
  32. spi = spidev.SpiDev()
  33. spi.open(0,0)
  34. # account_sid = ""
  35. # auth_token = ""
  36. # client = Client(account_sid, auth_token)
  37.  
  38. def readadc(adcnum):
  39. if ((adcnum > 7) or (adcnum < 0)):
  40. return -1
  41. r = spi.xfer2([1,(8+adcnum)<<4,0])
  42. adcout = ((r[1]&3) << 8) + r[2]
  43. return adcout
  44.  
  45. class MySubscribeCallback_ITF(SubscribeCallback):
  46. def message (self, pubnub, message):
  47. if message.message == '0':
  48. print('Alarmknop ingedrukt!!')
  49.  
  50.  
  51. def site(procent0,procent1):
  52. site1 = str(procent0) + "%"
  53. site2 = str(procent1) + "%"
  54. try:
  55. pubnub2.publish().channel('pot').message(site1 + " " + site2).sync()
  56. except PubNubException as e:
  57. print(e)
  58.  
  59. # MAILGEGEVENS INVULLEN
  60. def mail(procent0):
  61. message = (procent0 + "%")
  62. msg = MIMEText(message)
  63. msg['Subject'] = 'Oefenen examen'
  64. msg['From'] = 'raspberrypitm@gmail.com'
  65. msg['To'] = 'kennyjoris97@gmail.com'
  66. username = 'raspberrypitm@gmail.com'
  67. password = 'thomasmore'
  68. server = smtplib.SMTP('smtp.gmail.com:587')
  69. server.starttls()
  70. server.login(username,password)
  71. server.sendmail(msg['From'], msg['To'], msg.as_string())
  72. server.quit()
  73. print ('Mail verstuurd')
  74.  
  75.  
  76. # SMS GEGEVENS INVULLEN
  77. def sms(procent0):
  78. # message = client.messages.create(
  79. # to="+32496849506",
  80. # from_= "+32460201369",
  81. # body="procent1" + "%")
  82. print ('Berichtje verstuurd')
  83. SMSTeller = 1
  84.  
  85. print('Listening...')
  86. pubnub.add_listener(SubscribeCallback())
  87. pubnub.subscribe().channels('knop').execute()
  88.  
  89. while True:
  90. procent0 = int(round(readadc(0)/10.24))
  91. procent1 = int(round(readadc(1)/10.24))
  92. site(procent0,procent1)
  93.  
  94. if(procent0 > 50):
  95. GPIO.output(PIN1,1)
  96. mail(str(procent0))
  97.  
  98. else:
  99. GPIO.output(PIN1,0)
  100.  
  101. if(procent0 > 80):
  102. GPIO.output(PIN2,1)
  103. if (SMSTeller == 0 ):
  104. sms(str(procent0))
  105. SMSTeller = 1
  106. else:
  107. GPIO.output(PIN2,0)
  108. print("Waarde 1 = " + str(procent0) + "%")
  109. print("Waarde 2 = " + str(procent1) + "%")
  110. time.sleep(0.5)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement