Guest User

Untitled

a guest
Oct 29th, 2017
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.69 KB | None | 0 0
  1. # combine the MQTT and RF receive codes
  2. import paho.mqtt.client as mqtt
  3. import paho.mqtt.publish as publish
  4. import picamera
  5. import argparse
  6. import signal
  7. import sys
  8. import time
  9. import logging
  10. from rpi_rf import RFDevice
  11. rfdevice = None
  12. ### camera
  13. camera = picamera.PiCamera()
  14. camera.vflip=True
  15. #
  16. def post_image():
  17. print('Taking photo')
  18. camera.capture('image.jpg')
  19. file_name = 'image_' + str(datetime.now()) + '.jpg'
  20. camera.capture(file_name) # time-stamped image
  21. with open('image.jpg', "rb") as imageFile:
  22. myFile = imageFile.read()
  23. data = bytearray(myFile)
  24. client.publish('dev/camera', data, mqttQos, mqttRetained) #
  25. client.publish('dev/test', 'Capture!') # to trigger an automation later
  26. print('image published')
  27. #
  28. ### MQTT
  29. broker = '192.168.0.100'
  30. topic ='dev/test'
  31. mqttQos = 0
  32. mqttRetained = False
  33. #
  34. def on_connect(client, userdata, flags, rc):
  35. print("Connected with result code "+str(rc))
  36. client.subscribe(topic)
  37. # The callback for when a PUBLISH message is received from the server.
  38. #
  39. def on_message(client, userdata, msg):
  40. payload = str(msg.payload.decode('ascii')) # decode the binary string
  41. print(msg.topic + " " + payload)
  42. process_trigger(payload)
  43. #
  44. def process_trigger(payload):
  45. if payload == 'ON':
  46. print('ON triggered')
  47. post_image()
  48. #
  49. client = mqtt.Client()
  50. client.on_connect = on_connect # call these on connect and on message
  51. client.on_message = on_message
  52. client.username_pw_set(username='user',password='pass') # need this
  53. client.connect(broker)
  54. client.loop_start() # run in background and free up main thread
  55. ### RF
  56. #
  57. def exithandler(signal, frame):
  58. rfdevice.cleanup()
  59. sys.exit(0)
  60. logging.basicConfig(level=logging.INFO, datefmt='%Y-%m-%d %H:%M:%S',
  61. format='%(asctime)-15s - [%(levelname)s] %(module)s: %(message)s', )
  62. parser = argparse.ArgumentParser(description='Receives a decimal code via a 433/315MHz GPIO device')
  63. parser.add_argument('-g', dest='gpio', type=int, default=27,
  64. help="GPIO pin (Default: 27)")
  65. args = parser.parse_args()
  66. signal.signal(signal.SIGINT, exithandler)
  67. rfdevice = RFDevice(args.gpio)
  68. rfdevice.enable_rx()
  69. timestamp = None
  70. logging.info("Listening for codes on GPIO " + str(args.gpio))
  71. code_of_interest = '9181186'
  72. #
  73. while True:
  74. if rfdevice.rx_code_timestamp != timestamp:
  75. timestamp = rfdevice.rx_code_timestamp
  76. print(str(rfdevice.rx_code))
  77. if str(rfdevice.rx_code) == code_of_interest:
  78. post_image()
  79. time.sleep(1) # prevent registering multiple times
  80. time.sleep(0.01)
  81. rfdevice.cleanup()
Add Comment
Please, Sign In to add comment