Guest User

Untitled

a guest
Nov 5th, 2017
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.72 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!')
  26. print(file_name + '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_forever() # don't get past this
  55. client.loop_start() # run in background and free up main thread
  56. ### RF
  57. #
  58. def exithandler(signal, frame):
  59. rfdevice.cleanup()
  60. sys.exit(0)
  61. logging.basicConfig(level=logging.INFO, datefmt='%Y-%m-%d %H:%M:%S',
  62. format='%(asctime)-15s - [%(levelname)s] %(module)s: %(message)s', )
  63. parser = argparse.ArgumentParser(description='Receives a decimal code via a 433/315MHz GPIO device')
  64. parser.add_argument('-g', dest='gpio', type=int, default=27,
  65. help="GPIO pin (Default: 27)")
  66. args = parser.parse_args()
  67. signal.signal(signal.SIGINT, exithandler)
  68. rfdevice = RFDevice(args.gpio)
  69. rfdevice.enable_rx()
  70. timestamp = None
  71. logging.info("Listening for codes on GPIO " + str(args.gpio))
  72. code_of_interest = '9181186'
  73. #
  74. while True:
  75. if rfdevice.rx_code_timestamp != timestamp:
  76. timestamp = rfdevice.rx_code_timestamp
  77. print(str(rfdevice.rx_code))
  78. if str(rfdevice.rx_code) == code_of_interest:
  79. post_image()
  80. time.sleep(1) # prevent registering multiple times
  81. time.sleep(0.01)
  82. rfdevice.cleanup()
Add Comment
Please, Sign In to add comment