Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import cgitb ; cgitb.enable()
- import spidev
- import time
- import sys
- from twilio.rest import Client
- import RPi.GPIO as GPIO
- import smtplib
- from email.mime.text import MIMEText
- from pubnub.pubnub import PubNub
- from pubnub.pnconfiguration import PNConfiguration
- from pubnub.callbacks import SubscribeCallback
- SMSTeller = 0
- GPIO.setmode (GPIO.BCM)
- PIN1 = 17
- PIN2 = 18
- GPIO.setup(PIN1,GPIO.OUT)
- GPIO.setup(PIN2,GPIO.OUT)
- pnconfig = PNConfiguration()
- pnconfig.subscribe_key = 'sub-c-d9cbfc6e-d828-11e7-96e5-36e8e28b70d2'
- pnconfig.publish_key = 'pub-c-c7544bad-f255-4e58-8f47-e2bffe7147a1'
- pubnub = PubNub(pnconfig)
- channel = 'knop'
- pnconfig2 = PNConfiguration()
- pnconfig2.subscribe_key = 'sub-c-dd037966-d828-11e7-a9ef-2291f454b5f8'
- pnconfig2.publish_key = 'pub-c-83062419-8e00-451f-b36e-12f902c7b258'
- pubnub2 = PubNub(pnconfig2)
- channel2 = 'pot'
- spi = spidev.SpiDev()
- spi.open(0,0)
- # account_sid = ""
- # auth_token = ""
- # client = Client(account_sid, auth_token)
- 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
- class MySubscribeCallback_ITF(SubscribeCallback):
- def message (self, pubnub, message):
- if message.message == '0':
- print('Alarmknop ingedrukt!!')
- def site(procent0,procent1):
- site1 = str(procent0) + "%"
- site2 = str(procent1) + "%"
- try:
- pubnub2.publish().channel('pot').message(site1 + " " + site2).sync()
- except PubNubException as e:
- print(e)
- # MAILGEGEVENS INVULLEN
- def mail(procent0):
- message = (procent0 + "%")
- msg = MIMEText(message)
- msg['Subject'] = 'Oefenen examen'
- msg['From'] = 'raspberrypitm@gmail.com'
- msg['To'] = 'kennyjoris97@gmail.com'
- username = 'raspberrypitm@gmail.com'
- password = 'thomasmore'
- server = smtplib.SMTP('smtp.gmail.com:587')
- server.starttls()
- server.login(username,password)
- server.sendmail(msg['From'], msg['To'], msg.as_string())
- server.quit()
- print ('Mail verstuurd')
- # SMS GEGEVENS INVULLEN
- def sms(procent0):
- # message = client.messages.create(
- # to="+32496849506",
- # from_= "+32460201369",
- # body="procent1" + "%")
- print ('Berichtje verstuurd')
- SMSTeller = 1
- print('Listening...')
- pubnub.add_listener(SubscribeCallback())
- pubnub.subscribe().channels('knop').execute()
- while True:
- procent0 = int(round(readadc(0)/10.24))
- procent1 = int(round(readadc(1)/10.24))
- site(procent0,procent1)
- if(procent0 > 50):
- GPIO.output(PIN1,1)
- mail(str(procent0))
- else:
- GPIO.output(PIN1,0)
- if(procent0 > 80):
- GPIO.output(PIN2,1)
- if (SMSTeller == 0 ):
- sms(str(procent0))
- SMSTeller = 1
- else:
- GPIO.output(PIN2,0)
- print("Waarde 1 = " + str(procent0) + "%")
- print("Waarde 2 = " + str(procent1) + "%")
- time.sleep(0.5)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement