Commit 61e00a34 authored by aohui.cn's avatar aohui.cn

噪音传感器采集并上发平台

parents
# 使用说明
## python main.py 脚本
### 环境
```shell
pip install modbus-tk
pip install serial
pip install paho-mqtt
```
### 配置COM口
`SerialCom = 'COM5'`
### 上发主题
`WT/WTG93RF/99991213/opData`
### 控制对应噪音传感器上发数据
```python
# Enable sensor
enableJianDa = 1 #开启采集
enableShangLuo = 0 #关闭采集
enableZhaotai = 1
```
### 数据查询页
[Witium 测试平台](http://gm.cloud.witium.com.cn/#/app/dataMonitor/gearMotorMonitor/monitorDetail?reducerNo=RN704134230753816576&reducerName=%E5%99%AA%E9%9F%B3%E6%B5%8B%E8%AF%95%E8%AE%BE%E5%A4%87&status=0&boundState=1)
![](./Pic/传感器采集平台显示截图.png)
## python noiseLab_dataDeal.py脚本
- 会将data.csv的数据转换为图形;
![](./Pic/noiseLab数据转换图形.png)
## App
![noiseLab App](./Pic/noiseLab.jpg)
Second Time dB
0 00:00:00.0 33.14538955688477
1.486076213604974 00:00:01.4 33.62919998168945
2.97213970372286 00:00:02.9 39.49447250366211
4.458232207249239 00:00:04.4 45.17623138427734
5.944315659739004 00:00:05.9 111.6162719726562
7.43039092388608 00:00:07.4 111.6188583374023
8.916452582592683 00:00:08.9 97.84574890136719
10.4025342904315 00:00:10.4 111.304328918457
11.88862129972586 00:00:11.8 111.865234375
13.37468295843064 00:00:13.3 111.8299331665039
14.86076649768256 00:00:14.8 102.7115097045898
16.34685534227901 00:00:16.3 110.9648056030273
17.83293243978369 00:00:17.8 111.7288131713867
19.31899766871175 00:00:19.3 111.6903305053711
20.80506858086483 00:00:20.8 104.5469207763672
22.29116108439121 00:00:22.2 110.144416809082
23.7772280445497 00:00:23.7 111.0834808349609
25.26330078811407 00:00:25.2 110.9999084472656
26.74939329164044 00:00:26.7 105.1817092895508
28.2354721223237 00:00:28.2 108.1932678222656
29.72154275483081 00:00:29.7 110.9568023681641
31.20760441353741 00:00:31.2 110.9409408569336
32.69369537482271 00:00:32.6 106.848876953125
34.17977313067058 00:00:34.1 106.7852935791016
35.66583507854557 00:00:35.6 110.9578552246094
37.15192758207195 00:00:37.1 110.9469833374023
38.63801180486189 00:00:38.6 106.402229309082
40.12408784095169 00:00:40.1 104.3719787597656
41.61014949965647 00:00:41.6 110.9848480224609
43.09622966525603 00:00:43.0 110.9713516235352
44.58231821678964 00:00:44.5 96.230712890625
46.06837987549443 00:00:46.0 45.30447387695312
47.55446187250527 00:00:47.5 111.0034103393555
49.0405514874019 00:00:49.0 110.9919738769531
50.52662858490839 00:00:50.5 109.7706604003906
52.01269458577553 00:00:52.0 99.5394287109375
53.49876395568754 00:00:53.4 110.9729843139648
54.98485645921392 00:00:54.9 110.9592971801758
56.47092496161531 00:00:56.4 110.6303634643555
57.95699616293678 00:00:57.9 92.91825866699219
59.44308866646315 00:00:59.4 110.9457015991211
60.9291682674484 00:01:00.9 110.9426879882812
62.41523967189642 00:01:02.4 110.8155517578125
63.9013013306012 00:01:03.9 86.09075927734375
65.38739074964724 00:01:05.3 110.9206466674805
66.87347004773437 00:01:06.8 110.9187545776367
68.35953170644098 00:01:08.3 110.8093795776367
69.84562295689648 00:01:09.8 78.72113037109375
71.33170794998659 00:01:11.3 110.901252746582
72.81778475801548 00:01:12.8 110.893196105957
74.30384641672026 00:01:14.3 110.8143005371094
75.78992504007874 00:01:15.7 75.91162872314453
77.27601513385343 00:01:17.2 110.9192276000977
78.76207679256004 00:01:18.7 110.9198226928711
80.24815724732798 00:01:20.2 110.8604583740234
81.7342476325266 00:01:21.7 72.69077301025391
83.2203247300331 00:01:23.2 110.9173049926758
84.70639150284114 00:01:24.7 110.9169311523438
86.19245933051025 00:01:26.1 110.8597106933594
87.67855183403844 00:01:27.6 75.89144134521484
89.1646218786791 00:01:29.1 110.9002380371094
90.6506915377613 00:01:30.6 110.9017105102539
92.13678404128768 00:01:32.1 110.8542098999023
93.62286441257311 00:01:33.6 77.19607543945312
95.10893658896202 00:01:35.1 110.8978729248047
96.59499824766681 00:01:36.5 110.9077377319336
98.08108612446995 00:01:38.0 110.8354797363281
99.56716696479998 00:01:39.5 77.94960021972656
101.0532286235066 00:01:41.0 110.8964233398438
102.5393183317192 00:01:42.5 110.902214050293
104.0254040951131 00:01:44.0 110.8611373901367
105.5114811926178 00:01:45.5 80.33138275146484
106.9975433337877 00:01:46.9 110.9099502563477
108.4836204149033 00:01:48.4 110.9081039428711
109.969712050919 00:01:49.9 110.8635101318359
111.4557737096256 00:01:51.4 83.86908721923828
112.9418526221525 00:01:52.9 110.9203567504883
114.4279437776531 00:01:54.4 110.9189834594727
115.9140208751578 00:01:55.9 110.8662567138672
117.4000884199068 00:01:57.4 85.36241912841797
118.8861547053348 00:01:58.8 110.9496307373047
120.3722472088612 00:02:00.3 110.9492950439453
121.8583187957447 00:02:01.8 109.2220230102539
123.3443869125858 00:02:03.3 110.8902816772461
124.8304794161104 00:02:04.8 110.8674468994141
126.3165605576978 00:02:06.3 87.99713134765625
127.8026335060258 00:02:07.8 110.9844360351562
129.2886951647324 00:02:09.2 110.984130859375
130.7747814992945 00:02:10.7 110.9348602294922
132.2608638818638 00:02:12.2 90.01749420166016
133.7469255405704 00:02:13.7 110.9729309082031
135.2330137065437 00:02:15.2 110.9768905639648
136.7191002402378 00:02:16.7 110.9523086547852
138.2051773377425 00:02:18.2 93.30815887451172
139.6912402508515 00:02:19.6 111.0423431396484
141.177315789726 00:02:21.1 111.0532608032227
142.6634082932524 00:02:22.6 111.0266952514648
144.1494706266894 00:02:24.1 94.40406036376953
145.6355479969752 00:02:25.6 110.9552307128906
147.121639922776 00:02:27.1 111.0309448242188
148.6077170202825 00:02:28.6 111.0048980712891
150.0937853369705 00:02:30.0 96.29152679443359
151.5798500801593 00:02:31.5 110.840576171875
153.0659425836857 00:02:33.0 110.9899291992188
154.5520157128085 00:02:34.5 110.9748916625977
156.0380822874085 00:02:36.0 97.78910827636719
157.5241747909349 00:02:37.5 110.7740173339844
159.0102567028225 00:02:39.0 111.0262832641602
160.4963304230896 00:02:40.4 110.9986267089844
161.9823920817962 00:02:41.9 99.27511596679688
163.468476874119 00:02:43.4 47.93854522705078
164.9545607989276 00:02:44.9 38.08506011962891
166.4406224576342 00:02:46.4 40.25478744506836
167.9267090813682 00:02:47.9 37.94446563720703
169.4127963853607 00:02:49.4 38.04161834716797
170.898873482869 00:02:50.8 37.90328598022461
172.3849371679171 00:02:52.3 38.24496078491211
173.8710111645505 00:02:53.8 38.51912307739258
175.3571036680751 00:02:55.3 38.17788696289062
176.8431675437532 00:02:56.8 70.08360290527344
178.3292433717997 00:02:58.3 71.34625244140625
179.8153358753261 00:02:59.8 71.16543579101562
181.3014131654072 00:03:01.3 71.28524017333984
182.7874822540343 00:03:02.7 71.38112640380859
184.273545454982 00:03:04.2 71.44187164306641
185.7596379585102 00:03:05.7 71.47019195556641
187.2457126298741 00:03:07.2 54.67401123046875
188.7317776622313 00:03:08.7 87.21419525146484
190.2178701657595 00:03:10.2 72.24671173095703
191.7039528479472 00:03:11.7 71.36215972900391
193.1900273401552 00:03:13.1 71.43703460693359
194.6760889988618 00:03:14.6 71.48910522460938
196.1621722489417 00:03:16.1 71.53965759277344
197.6482577159932 00:03:17.6 71.56464385986328
199.1343193746998 00:03:19.1 70.58549499511719
200.620404456191 00:03:20.6 90.51291656494141
202.1064925304872 00:03:22.1 80.68486785888672
203.5925696279919 00:03:23.5 71.48480224609375
205.0786340849809 00:03:25.0 71.55615234375
206.564706539375 00:03:26.5 71.58049774169922
208.0507990429014 00:03:28.0 71.62610626220703
209.5368644608188 00:03:29.5 71.65668487548828
211.0229387466243 00:03:31.0 71.65696716308594
212.5090312501507 00:03:32.5 70.26891326904297
213.9951093105319 00:03:33.9 90.25801849365234
215.4811791710999 00:03:35.4 71.36141967773438
216.9672408298065 00:03:36.9 71.57598114013672
218.4533333333329 00:03:38.4 71.57225036621094
219.9394095469379 00:03:39.9 71.61154174804688
221.4254730370558 00:03:41.4 71.64971923828125
222.9115655405822 00:03:42.9 77.02584838867188
224.3976489930719 00:03:44.3 82.43305206298828
225.883724257219 00:03:45.8 37.56554412841797
227.3697859159256 00:03:47.3 47.22731018066406
228.8558676237644 00:03:48.8 44.42107391357422
230.3419546330588 00:03:50.3 33.03444290161133
231.8280162917636 00:03:51.8 35.368896484375
233.3140998310155 00:03:53.3 35.04887390136719
234.8001886756119 00:03:54.8 33.47512054443359
236.2862657731166 00:03:56.2 40.12441635131836
237.7723310020447 00:03:57.7 32.76504516601562
239.2584019141977 00:03:59.2 32.20753860473633
240.7444944177241 00:04:00.7 40.67828369140625
242.2305613778826 00:04:02.2 38.59640502929688
243.716634121447 00:04:03.7 42.79354476928711
245.2027266249734 00:04:05.2 35.36727905273438
246.6888054556566 00:04:06.6 57.6150016784668
import serial
import modbus_tk.defines as cst
from modbus_tk import modbus_rtu
import threading
from module.myMqtt import myMqtt
import time
#############################[Sensor Config]###################################
# 485 Addr Index
# 1 2 3
# 建大jd 上洛sl 兆泰盛zt
# 0x00 0x06 0x00
SerialCom = 'COM18'
# (sensor_id, register)
noise_Message = [ (1, 0x00),
(2, 0x06),
(3, 0x00)]
# Enable sensor
enableJianDa = 1
enableShangLuo = 0
enableZhaotai = 1
# Enable Print OnLineTime
enableOnLineTime = 0
#############################[MQTT Config]###################################
RequestTopic = 'WT/WTG93RF/99991213/opDataRequest'
EchoNoiseTopic = 'WT/WTG93RF/99991213/opData'
begin_time = time.time()
class Sensor:
def __init__(self, port):
self._port = port
try:
self._ser = serial.Serial(self._port, baudrate=4800)
self._ser.open() if not self._ser.is_open else None
self.__master = modbus_rtu.RtuMaster(self._ser)
self.__master.set_timeout(2, use_sw_timeout=True)
except Exception as exp:
print(exp)
def get_noise(self, noise_sensor_idx):
noise_sensor_idx -= 1
data = self.__master.execute(noise_Message[noise_sensor_idx][0], cst.READ_HOLDING_REGISTERS, noise_Message[noise_sensor_idx][1], 1)
return data
def getNoise(sendData):
ser.write(bytes(sendData))
time.sleep(0.5) # 需要等待
address = ser.read(1) # 读取地址字节
function_code = ser.read(1) # 读取功能码字节
data_length = ser.read(1) # 读取数据字节数字节
data = ser.read(2)
crc = ser.read(2) # 读取CRC校验(2字节)
# 将字节串转换为整数
dB_data = int.from_bytes(data, byteorder='big')
dB_data = dB_data / 10.0 # trans dB
return dB_data
S = Sensor(SerialCom)
# ser = serial.Serial(SerialComTest, 4800, parity=serial.PARITY_NONE, bytesize=serial.EIGHTBITS, stopbits=serial.STOPBITS_ONE)
# data_to_send = [0x02,0x03,0x00,0x06,0x00, 0x01 ,0x64 ,0x38]
def mqtt_task():
try:
while True:
if mqtt._start_flag:
if enableJianDa:
time.sleep(0.3)
n1 = S.get_noise(1)[0] / 10.0
else:
n1 = 0.0
if enableShangLuo:
time.sleep(0.3)
n2 = S.get_noise(2)[0] / 10.0
else:
n2 = 0.0
if enableZhaotai:
time.sleep(0.3)
n3 = S.get_noise(3)[0] / 10.0
else:
n3 = 0.0
now = int(time.time())
print(time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(now)))
data = [{"addr":"1","ts":int(now),"jd":float(n1),"sl":float(n2),"zt":float(n3)}]
# data = json.dumps(data)
mqtt.publish(EchoNoiseTopic, data)
print(data)
time.sleep(1)
if enableOnLineTime:
end_time = time.time()
run_time = round(end_time-begin_time)
hour = run_time//3600
minute = (run_time-3600*hour)//60
second = run_time-3600*hour-60*minute
print(f"online-time:{hour}:{minute}:{second}")
except Exception as exp:
print("Error Exception:", exp)
print("Need to restart the script")
if __name__ == '__main__':
mqtt_info = {
'broker': 'www.witium.com.cn',
'port': 12883,
'user': 'witcd',
'password': 'Witium37774020'
}
mqtt = myMqtt(mqtt_info)
mqtt.set_add_topic(RequestTopic)
mqtt.open_mqtt()
mqtt_thread = threading.Thread(target=mqtt.loop_task)
mqtt_thread.start()
app_thread = threading.Thread(target=mqtt_task)
app_thread.start()
#!usr/bin/python3
import json
import time
import paho.mqtt.client as mqtt
from sympy import false
import os
import ssl
path = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))
resourcesPath = path + os.sep
tls = {
"ca_certs": resourcesPath+"file\\ca.crt",
"certfile": "./file/client.crt",
"keyfile": "./file/client.key",
"tls_version": ssl.PROTOCOL_TLSv1_2
}
class myMqtt:
def __init__(self, mqtt_info):
self._topic_enable = False
self._topic = []
self._mqtt_enable = False
self._mqtt_info = mqtt_info
self._client = None
self._context = None
self._start_flag = False
self._request_flag = False
def open_mqtt(self):
# open mqtt
try:
if 'user' in self._mqtt_info and 'password' in self._mqtt_info \
and 'broker' in self._mqtt_info and 'port' in self._mqtt_info:
self._client = mqtt.Client()
self._client.username_pw_set(self._mqtt_info['user'], self._mqtt_info['password'])
self._client.on_connect = self.on_connect
self._client.on_message = self.on_message
# self._client.tls_set(tls)
# self._client.tls_set(ca_certs=resourcesPath +"file\\cacert.pem", certfile=resourcesPath + "file\\cert.pem",
# keyfile=resourcesPath + "file\\key.pem",tls_version=ssl.PROTOCOL_TLSv1_2)
# self._client.tls_insecure_set(True)
self._client.connect(self._mqtt_info['broker'], self._mqtt_info['port'])
self._mqtt_enable = True
print("mqtt config success")
else:
self._mqtt_enable = False
print("mqtt config error")
except Exception as exp:
self._mqtt_enable = False
print(exp)
def set_add_topic(self, topic):
self._topic_enable = True
self._topic.append(topic)
def get_add_topic(self):
if self._topic_enable:
return self._topic
else:
return None
def clear_topic(self):
self._topic = []
self._topic_enable = False
def on_connect(self, client, userdata, flags, rc):
if self._topic_enable and self._mqtt_enable:
for index in self._topic:
self._client.subscribe(index)
print(index)
self._start_flag = True
def on_message(self, client, userdata, msg):
try:
if self._topic_enable and self._mqtt_enable:
if 'Request' in msg.topic:
self._request_flag = True
except Exception as exp:
print(exp)
def publish(self, config_topic, data):
try:
if self._topic_enable and self._mqtt_enable:
self._client.publish(config_topic, json.dumps(data, separators=(',', ':')), qos=1)
except Exception as exp:
print(exp)
def loop_task(self):
try:
if self._topic_enable and self._mqtt_enable:
self._client.loop_forever()
except Exception as exp:
print(exp)
import pandas as pd
import matplotlib.pyplot as plt
from io import StringIO
file_path = 'data.csv'
# Read data into a pandas DataFrame
df = pd.read_csv(file_path, sep=" ", parse_dates=["Time"])
# Convert Time to seconds for plotting
df['Time_in_seconds'] = df['Time'].dt.hour * 3600 + df['Time'].dt.minute * 60 + df['Time'].dt.second
# Plotting
plt.figure(figsize=(100, 30))
plt.plot(df['Time_in_seconds'], df['dB'], marker='o')
plt.title('dB Level Over Time')
plt.xlabel('Time (seconds)')
plt.ylabel('dB Level')
plt.grid(True)
plt.savefig('result_noise_data.png')
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment