Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- import datetime
- import random
- from time import sleep
- import queue
- import serial
- import _thread
- import zmq
- context = zmq.Context()
- socket = context.socket(zmq.PUSH)
- socket.connect("tcp://0.0.0.0:5556") # Provide Server IP here.
- remote_sensor = queue.Queue()
- debug = False
- direction = {
- "0": "E",
- "1": "ENE",
- "2": "NE",
- "3": "NNE",
- "4": "N",
- "5": "NNW",
- "6": "NW",
- "7": "WNW",
- "8": "W",
- "9": "WSW",
- "10": "SW",
- "11": "SSW",
- "12": "S",
- "13": "SSE",
- "14": "SE",
- "15": "ESE"
- }
- def split_and_return(string, split_token, index):
- try:
- return string.split(split_token, 1)[index]
- except IndexError:
- return None
- def parseline(line):
- split_line = line.split("#")
- temp_dict = \
- {split_and_return(tmpkey, ":", 0): split_and_return(tmpkey, ":", 1) for tmpkey in split_line}
- return temp_dict
- # process raw sensor values here, output friendly values as dict
- def process_values(temporary):
- global remote_sensor
- try:
- output_dict = {
- "time_now": datetime.datetime.utcnow().isoformat(),
- "air_humidity": float(temporary.get("HUMB")),
- "soil_humidity": float(temporary.get("HUMA")),
- "air_temp": float(temporary.get("TCA")),
- "wind_direction": direction.get(temporary.get("WV")),
- "wind_speed": float(temporary.get("ANE")),
- "solar_radiation": float(temporary.get("UV")),
- "soil_temperature": float(temporary.get("SOILT")),
- "rainfall_now": float(temporary.get("PLV1")),
- "rainfall_hour": float(temporary.get("PLV2")),
- "rainfall_day": float(temporary.get("PLV3"))
- }
- if None in output_dict.values():
- raise Exception("Incomplete values!") # if data is missing, don't continue
- except:
- if debug:
- print("creating mock data.")
- sleep(30)
- return {
- "time_now": datetime.datetime.utcnow().isoformat(),
- "air_humidity": float(random.randint(0, 100)),
- "soil_humidity": float(random.randint(0, 100)),
- "air_temp": float(random.randint(-20, 20)),
- "wind_direction": direction.get(random.choice(["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15"])),
- "wind_speed": float(random.randint(0, 20)),
- "solar_radiation": float(random.randint(0, 1000)),
- "soil_temperature": float(random.randint(-20, 20)),
- "rainfall_now": float(random.randint(0, 5)),
- "rainfall_hour": float(random.randint(0, 30)),
- "rainfall_day": float(random.randint(0, 30*24))
- }
- else:
- raise
- return output_dict
- bufstring = ""
- # set serial port where xbee/waspmote gateway is plugged in below.
- try:
- xbee = serial.Serial('COM4')
- except:
- debug = True
- print("No receiver, debug mode.")
- oldvalues = {}
- error = 0
- while True:
- error = 0
- try:
- bufstring += xbee.read(3).decode('cp437')
- except:
- if not debug:
- if remote_sensor.empty():
- continue
- lines = bufstring.split("<=>")
- current_line_number = len(lines)
- current_line = lines[current_line_number - 2]
- temp_values = parseline(current_line)
- try:
- error = 1
- values = process_values(temp_values)
- if oldvalues == values or oldvalues.get("time_now") == values.get("time_now"):
- continue
- print(values)
- bufstring = current_line
- oldvalues = values
- error = 2
- socket.send_pyobj(values)
- print("data sent to remote service.")
- except Exception as e:
- print(type(e))
- if error == 2:
- raise
- continue
- # do nothing
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement