Added concentrator script
This commit is contained in:
		
							
								
								
									
										7
									
								
								tests/concentrator/README.md
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										7
									
								
								tests/concentrator/README.md
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,7 @@ | ||||
| # Concentrator | ||||
|  | ||||
| Future software for Central Unit (RPi HAT / standalone embedded Linux computer). Temporary stored here, to be moved to Central Unit repository in the future. | ||||
|  | ||||
| This script should read all sensors, log output (optionally send it to MQTT broker) and visualize current IAQ status (possibly show history graph) in webserver. | ||||
|  | ||||
| For now only Flask is used to serve http content. This is not good solution for production environment; use nginx + uWSGI in the future. | ||||
							
								
								
									
										56
									
								
								tests/concentrator/concentrator.py
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										56
									
								
								tests/concentrator/concentrator.py
									
									
									
									
									
										Executable file
									
								
							| @@ -0,0 +1,56 @@ | ||||
| #!/usr/bin/env python3 | ||||
| from sensor import Sensor | ||||
| from sys import argv,exit | ||||
| from flask import Flask | ||||
| from time import sleep | ||||
| import threading | ||||
|  | ||||
| app = Flask('Sensor central unit') | ||||
| # list of sensor addresses | ||||
| address_list = [ 247 ] | ||||
| baudrate = 19200 | ||||
| sensors = [] | ||||
| modbus_mutex = threading.Lock() | ||||
|  | ||||
| # Flask functions | ||||
| @app.route('/') | ||||
| def index(): | ||||
|     html = '' | ||||
|     for s in sensors: | ||||
|         html += f'<h2>Address {s.address}</h2><br><h3>Input registers:</h3><br>' | ||||
|         for reg_name in s.input_registers: | ||||
|             reg_number = s.input_registers[reg_name] | ||||
|             with modbus_mutex: | ||||
|                 reg_value = s.read_register(reg_number) | ||||
|             html += f'{reg_number : <10}  {int(reg_value) : >10}  {reg_name}<br>' | ||||
|         html += '<h3>Holding registers</h3><br>' | ||||
|         for reg_name in s.holding_registers: | ||||
|             reg_number = s.holding_registers[reg_name] | ||||
|             with modbus_mutex: | ||||
|                 reg_value = s.read_register(reg_number) | ||||
|             html += f'{reg_number : <10}  {int(reg_value) : >10}  {reg_name}<br>' | ||||
|     return html | ||||
|  | ||||
| for addr in address_list: | ||||
|     sensors.append(Sensor(addr, baudrate)) | ||||
|  | ||||
| # run webserver thread | ||||
| flask_thread = threading.Thread(target=app.run, kwargs={'host': '0.0.0.0', 'port': 8080}) | ||||
| flask_thread.start() | ||||
| #app.run(host='0.0.0.0', port=8080) | ||||
|  | ||||
| # measuring | ||||
| while True: | ||||
|     # logging: for now just writing to csv file (can be anything: write to db, mqtt...) | ||||
|     for s in sensors: | ||||
|         log_string = '' | ||||
|         for reg_name, reg_number in s.input_registers.items(): | ||||
|             with modbus_mutex: | ||||
|                 log_string += str(int(s.read_register(reg_number))) + ' ' | ||||
|         log_string += str(s.readout_total) + ' ' | ||||
|         log_string += str(s.readout_error_no_response) + ' ' | ||||
|         log_string += str(s.readout_error_invalid_response) + ' ' | ||||
|         with open(f'sensor_{s.address}.csv', 'a+') as f: | ||||
|             f.write(log_string + '\n') | ||||
|     sleep(10) | ||||
|  | ||||
							
								
								
									
										1
									
								
								tests/concentrator/sensor.py
									
									
									
									
									
										Symbolic link
									
								
							
							
						
						
									
										1
									
								
								tests/concentrator/sensor.py
									
									
									
									
									
										Symbolic link
									
								
							| @@ -0,0 +1 @@ | ||||
| ../sensor.py | ||||
		Reference in New Issue
	
	Block a user
	 mj
					mj