From 542d326a833d98ec53ba73bead5c1c3dc1f408e6 Mon Sep 17 00:00:00 2001 From: Ole Artz Date: Tue, 5 Jul 2022 11:54:33 +0200 Subject: [PATCH] LANTelnetToI2C_Board: setable plot time range of live plot and also reduce cpu --- .../live_telnet_data_plot.py | 116 +++++++++++++++--- 1 file changed, 97 insertions(+), 19 deletions(-) diff --git a/esp32/EthernetUART/LANTelnetToI2C_Board/live_telnet_data_plot.py b/esp32/EthernetUART/LANTelnetToI2C_Board/live_telnet_data_plot.py index 9f596a8..af56229 100644 --- a/esp32/EthernetUART/LANTelnetToI2C_Board/live_telnet_data_plot.py +++ b/esp32/EthernetUART/LANTelnetToI2C_Board/live_telnet_data_plot.py @@ -6,18 +6,28 @@ import socket import sys import matplotlib.pyplot as plt import matplotlib.animation as animation +import matplotlib.dates as mdates +import matplotlib.ticker as ticker +import numpy as np +import time +import datetime s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) +Timestamp_list = list() AIN0_list = list() AIN1_list = list() AIN2_list = list() AIN3_list = list() +ch0 = str() +ch1 = str() +ch2 = str() +ch3 = str() unit = str() plot_title = str() -filename = str(sys.argv[3]) #ADC_ETHERNET_val.txt +filename = str(sys.argv[4]) # f.e. ADC_ETHERNET_val.txt fig = plt.figure('ADC_Ethernet 4 CH Measurment') @@ -26,64 +36,132 @@ ax1 = fig.add_subplot(1,1,1) def connection(): HOST = str(sys.argv[1]) #192.168.8.63 PORT = int(sys.argv[2]) #2323 + + if filename == '': + print("Fatal Error! - No filename was given.") + SystemExit + if HOST == '': + print("Fatal Error! - No IP-address as second argument was given.") + SystemExit + if PORT == '': + print("Fatal Error! - Port wasn't given.") + SystemExit s.connect( (HOST,PORT) ) -def get_data(): - reader = s.recv(1024).decode() +def get_data(): + 'Receive data and save them in external file. Seperate lines by the channel.' + + while True: + message = s.recv(1024).decode() #work but not well, ends in error + if ("AINO" and "AIN1" and "AIN2" and "AIN3") in message: + ch0_start = int(message.find('AIN0')) + ch1_start = int(message.find('AIN1')) + ch2_start = int(message.find('AIN2')) + ch3_start = int(message.find('AIN3')) + + try: + ch0_end = int(message.find('\n', ch0_start)) + try: + ch1_end = int(message.find('\n', ch1_start)) + try: + ch2_end = int(message.find('\n', ch2_start)) + try: + ch3_end = int(message.find('\n', ch3_start)) + + ch0 = message[ch0_start: ch0_end] + ch1 = message[ch1_start: ch1_end] + ch2 = message[ch2_start: ch2_end] + ch3 = message[ch3_start: ch3_end] + + reader = ch0 + '\n' + ch1 + '\n' + ch2 + '\n' + ch3 + '\n' + break + + except: + reader = ("Line 3 is incomplete!") + except: + reader = ("Line 2 is incomplete!") + except: + reader = ("Line 1 is incomplete!") + except: + reader = ("Line 0 is incomplete!") + + else: + reader = ("String is incomplete!") + + act_time = time.time() +# timestamp = datetime.datetime.fromtimestamp(act_time).strftime('%Y-%m-%d %H:%M:%S.%f') + timestamp = datetime.datetime.fromtimestamp(act_time) + Timestamp_list.append(timestamp) + print(timestamp.strftime('%Y-%m-%d %H:%M:%S.%f')) + print(reader) + try: - with open(filename, 'r') as file: - file_history = str(file.read()) - with open(filename, "w") as file: - file.write(file_history + '\n' + str(reader)) + with open(filename, 'w') as file: + file.write('\n' + timestamp.strftime('%Y-%m-%d %H:%M:%S.%f') + '\n' + str(reader)) +# file.write(file_history + '\n' + timestamp + '\n' + ch0 + '\n' + ch1 + '\n' + ch2 + '\n' + ch3 + '\n') except: with open(filename, "w") as file: - file.write(str(reader)) + file.write(timestamp.strftime('%Y-%m-%d %H:%M:%S.%f') + '\n' + str(reader)) +# file.write(timestamp + '\n' + ch0 + '\n' + ch1 + '\n' + ch2 + '\n' + ch3 + '\n') file.close() return reader def split_data(reader): + 'Fill list with the raw value and save them in the channellist' - unit = reader[reader.rfind(' ')+1:reader.rfind('\n')] + unit = reader[reader.rfind(' ')+1:reader.rfind('\n')] # find and save the correct unit by slicing method cause format: "channel: value unit" AIN0_list.append(float(reader[reader.find('AIN0: ') + 6:reader.find(unit)-1])) AIN1_list.append(float(reader[reader.find('AIN1: ') + 6:reader.find(unit, reader.find('AIN1: '))-1])) AIN2_list.append(float(reader[reader.find('AIN2: ') + 6:reader.find(unit, reader.find('AIN2: '))-1])) AIN3_list.append(float(reader[reader.find('AIN3: ') + 6:reader.find(unit, reader.find('AIN3: '))-1])) - - return unit # check for split data works fine -# print(AIN0_list) +# print(AIN0_list)0.712123 # print(AIN1_list) # print(AIN2_list) # print(AIN3_list) + return unit + def animate(i): + 'Live plotting of the received messages' + + reader = get_data() unit = split_data(reader) + + plot_time_range = 2*60*int(sys.argv[3]) # factor 2 cause 2 val/s. Input in mins. ax1.clear() - ax1.plot(AIN0_list, 'r', label="AIN0") - ax1.plot(AIN1_list, 'b', label="AIN1") - ax1.plot(AIN2_list, 'g', label="AIN2") - ax1.plot(AIN3_list, 'y', label="AIN3") - + ax1.plot(Timestamp_list[-plot_time_range:], AIN0_list[-plot_time_range:], 'r', label="AIN0") + ax1.plot(Timestamp_list[-plot_time_range:], AIN1_list[-plot_time_range:], 'b', label="AIN1") + ax1.plot(Timestamp_list[-plot_time_range:], AIN2_list[-plot_time_range:], 'g', label="AIN2") + ax1.plot(Timestamp_list[-plot_time_range:], AIN3_list[-plot_time_range:], 'y', label="AIN3") + +# ax1.plot(AIN0_list, 'r', label="AIN0") +# ax1.plot(AIN1_list, 'b', label="AIN1") +# ax1.plot(AIN2_list, 'g', label="AIN2") +# ax1.plot(AIN3_list, 'y', label="AIN3") + + plt.xticks(rotation=0) + if unit == 'mV': plot_title = 'Voltage' elif unit == 'mA': plot_title = 'Current' - elif unit == 'mbar': + elif unit == 'mbar' or unit == 'mPa': plot_title = 'Pressure' elif unit == 'degC': plot_title = 'Temperature' plt.title(plot_title) - plt.xlabel('~0.5s') + plt.xlabel('Time') plt.ylabel(unit) plt.legend(loc=2) -- 2.43.0