Guest User

Untitled

a guest
Mar 23rd, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.51 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. import os
  4. import datetime
  5. import ftplib
  6. import traceback
  7. import math
  8. import random, string
  9. import base64
  10. import json
  11. import paho.mqtt.client as mqtt
  12. import picamera
  13. from time import sleep
  14. from time import gmtime, strftime
  15.  
  16. packet_size=3000
  17.  
  18. def randomword(length):
  19. return ''.join(random.choice(string.lowercase) for i in range(length))
  20.  
  21. # Create unique image name
  22. img_name = 'pi_image_{0}_{1}.jpg'.format(randomword(3),strftime("%Y%m%d%H%M%S",gmtime()))
  23.  
  24. # Capture Image from Pi Camera
  25. try:
  26. camera = picamera.PiCamera()
  27. camera.annotate_text = " Stored with Apache NiFi "
  28. camera.capture(img_name, resize=(500,281))
  29.  
  30.  
  31. pass
  32. finally:
  33. camera.close()
  34.  
  35. # MQTT
  36. client = mqtt.Client()
  37. client.username_pw_set("CloudMqttUserName","!MakeSureYouHaveAV@5&L0N6Pa55W0$4!")
  38. client.connect("cloudmqttiothoster", 14162, 60)
  39.  
  40. f=open(img_name)
  41. fileContent = f.read()
  42. byteArr = bytearray(fileContent)
  43. f.close()
  44. message = '"image": {"bytearray":"' + byteArr + '"} } '
  45. print client.publish("image",payload=message,qos=1,retain=False)
  46. client.disconnect()
  47.  
  48. # FTP
  49. ftp = ftplib.FTP()
  50. ftp.connect("ftpserver", "21")
  51. try:
  52. ftp.login("reallyLongUserName", "FTP PASSWORDS SHOULD BE HARD")
  53. ftp.storbinary('STOR ' + img_name, open(img_name, 'rb'))
  54. finally:
  55. ftp.quit()
  56.  
  57. # clean up sent file
  58. os.remove(img_name)
Add Comment
Please, Sign In to add comment