Advertisement
Guest User

Untitled

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