Advertisement
Guest User

Untitled

a guest
Jul 20th, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.79 KB | None | 0 0
  1. #!/usr/bin/python
  2. import datetime
  3. import random
  4. from time import sleep
  5. import queue
  6. import serial
  7. import _thread
  8. import zmq
  9.  
  10. context = zmq.Context()
  11. socket = context.socket(zmq.PUSH)
  12. socket.connect("tcp://0.0.0.0:5556") # Provide Server IP here.
  13. remote_sensor = queue.Queue()
  14. debug = False
  15. direction = {
  16. "0": "E",
  17. "1": "ENE",
  18. "2": "NE",
  19. "3": "NNE",
  20. "4": "N",
  21. "5": "NNW",
  22. "6": "NW",
  23. "7": "WNW",
  24. "8": "W",
  25. "9": "WSW",
  26. "10": "SW",
  27. "11": "SSW",
  28. "12": "S",
  29. "13": "SSE",
  30. "14": "SE",
  31. "15": "ESE"
  32. }
  33.  
  34.  
  35. def split_and_return(string, split_token, index):
  36. try:
  37. return string.split(split_token, 1)[index]
  38. except IndexError:
  39. return None
  40.  
  41.  
  42. def parseline(line):
  43. split_line = line.split("#")
  44. temp_dict = \
  45. {split_and_return(tmpkey, ":", 0): split_and_return(tmpkey, ":", 1) for tmpkey in split_line}
  46. return temp_dict
  47.  
  48.  
  49. # process raw sensor values here, output friendly values as dict
  50. def process_values(temporary):
  51. global remote_sensor
  52. try:
  53. output_dict = {
  54. "time_now": datetime.datetime.utcnow().isoformat(),
  55. "air_humidity": float(temporary.get("HUMB")),
  56. "soil_humidity": float(temporary.get("HUMA")),
  57. "air_temp": float(temporary.get("TCA")),
  58. "wind_direction": direction.get(temporary.get("WV")),
  59. "wind_speed": float(temporary.get("ANE")),
  60. "solar_radiation": float(temporary.get("UV")),
  61. "soil_temperature": float(temporary.get("SOILT")),
  62. "rainfall_now": float(temporary.get("PLV1")),
  63. "rainfall_hour": float(temporary.get("PLV2")),
  64. "rainfall_day": float(temporary.get("PLV3"))
  65. }
  66. if None in output_dict.values():
  67. raise Exception("Incomplete values!") # if data is missing, don't continue
  68. except:
  69. if debug:
  70. print("creating mock data.")
  71. sleep(30)
  72. return {
  73. "time_now": datetime.datetime.utcnow().isoformat(),
  74. "air_humidity": float(random.randint(0, 100)),
  75. "soil_humidity": float(random.randint(0, 100)),
  76. "air_temp": float(random.randint(-20, 20)),
  77. "wind_direction": direction.get(random.choice(["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15"])),
  78. "wind_speed": float(random.randint(0, 20)),
  79. "solar_radiation": float(random.randint(0, 1000)),
  80. "soil_temperature": float(random.randint(-20, 20)),
  81. "rainfall_now": float(random.randint(0, 5)),
  82. "rainfall_hour": float(random.randint(0, 30)),
  83. "rainfall_day": float(random.randint(0, 30*24))
  84. }
  85. else:
  86. raise
  87. return output_dict
  88.  
  89.  
  90. bufstring = ""
  91. # set serial port where xbee/waspmote gateway is plugged in below.
  92. try:
  93. xbee = serial.Serial('COM4')
  94. except:
  95. debug = True
  96. print("No receiver, debug mode.")
  97.  
  98. oldvalues = {}
  99. error = 0
  100.  
  101. while True:
  102. error = 0
  103. try:
  104. bufstring += xbee.read(3).decode('cp437')
  105. except:
  106. if not debug:
  107. if remote_sensor.empty():
  108. continue
  109. lines = bufstring.split("<=>")
  110. current_line_number = len(lines)
  111. current_line = lines[current_line_number - 2]
  112. temp_values = parseline(current_line)
  113. try:
  114. error = 1
  115. values = process_values(temp_values)
  116. if oldvalues == values or oldvalues.get("time_now") == values.get("time_now"):
  117. continue
  118. print(values)
  119. bufstring = current_line
  120. oldvalues = values
  121. error = 2
  122. socket.send_pyobj(values)
  123. print("data sent to remote service.")
  124. except Exception as e:
  125. print(type(e))
  126. if error == 2:
  127. raise
  128. continue
  129. # do nothing
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement