What is the best way for a django application to query a remote noise meter and update the live data in a users browser?

4 days ago 3
ARTICLE AD BOX

As the title implies, I am attempting to write a Django application to query noise meters remotely and display the real time information in a web page. Currently I am able to display live data in the web page through the use of django-channels plus websockets. I have also written a program capable of querying the remote meter using asyncio, the code for which is below:

import datetime import time import serial import asyncio async def tcp_serial_ip_client(): host = '192.168.10.1' # Server's IP address port = 6889 # Server port reader, writer = await asyncio.open_connection(host, port) #list of command stings for meter id_message= '*IDN?\n' reset_message = '*RST\n' autosave_message='SYST:KEY NEXT NEXT NEXT ENTER PREV ENTER ESC\n' measurement_start_message= 'INIT START\n' measurement_trigger_message= 'MEAS:INIT\n' measurement_query_message= 'MEAS:SLM:123? LAEQ\n' measurement_end_message= 'INIT STOP\n' # resets the meter to be in a known state for remote command writer.write(reset_message.encode()) await writer.drain() time.sleep(1) # queries and returns the id info for the meter writer.write(id_message.encode()) await writer.drain() time.sleep(3) data = await reader.read(1024) print(f'Received: {data.decode()!r}') # executes keypresses on the noise meter to enable autosave function writer.write(autosave_message.encode()) await writer.drain() time.sleep(3) data = await reader.read(1024) print(f'Received: {data.decode()!r}')# ok is returned if successful # begin measurement writer.write(measurement_start_message.encode()) await writer.drain() time.sleep(6) start_datetime = datetime.datetime.now() print(start_datetime) count = 0 while True: # trigger a new reading writer.write(measurement_trigger_message.encode()) await writer.drain() # query previously triggered reading writer.write(measurement_query_message.encode()) await writer.drain() data = await reader.read(1024) print("measurement time: ", datetime.datetime.now()) print(f"Server response: {data.decode()}") time.sleep(1) count += 1 #after 15 readings stop the measurement on the meter if count > 15: writer.write(measurement_end_message.encode()) await writer.drain() break #end connection once reading is complete print('Close the connection') writer.close() await writer.wait_closed() asyncio.run(tcp_serial_ip_client())

My issue is that I am unsure of the best way to integrate the code to query the meter into my Django application. I need Django to act as the client for the meter and initiate the connection and send queries over the connection at regular intervals(i.e. every second) then send the response via a channel so it can be used to update the users web page. I also need to ensure the solution allows multiple noise meters to be connected and queried concurrently.

Any advice on the best way to achieve this would be welcome as my attempts to solve this have been unsuccessful so far.

Read Entire Article