1、【道生物联TKB-620开发板试用】定期休眠并发布数据
2、在前一篇的基础上,我在另一块开发板上创建数据接收。
在user.c中的状态机添加数据解析:
case FSM_RCVDATA:
if (WorkUartRcv.USART_RX_STA.b.rdy) {
if (strstr((char *)WorkUartRcv.DataBuffer,"+DI")) {
RF_DEBUG("%s", WorkUartRcv.DataBuffer);
pStrData = strstr((char *)WorkUartRcv.DataBuffer, "Data ");
if (pStrData) {
SensorData sensorData = extractSensorData(pStrData);
printf("Temperature:%.2f°C\n", sensorData.temperature);
printf("Humidity: %.2f%%\n", sensorData.humidity);
Queue_Enqueue(&temperatureQueue, sensorData.temperature);
Display_Temperature_Curve(&temperatureQueue);
sprintf(tmpStr,"T:%.1fC",sensorData.temperature);
OLED_ShowString(0,0,tmpStr,1);
DelayMs(10);
sprintf(tmpStr1,"H:%.1f%%",sensorData.humidity);
OLED_ShowString(64,0,tmpStr1,1);
}
}
WorkUartRcvClr();
}
在OLED上可为示实现的温度与数据曲线:

3、编写电脑端的数据显示,我使用pyqt编写了一个上位机软,其效果如下:

其源代码如下:
import sys
import serial
from serial import Serial
import pyqtgraph as pg
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QLabel
from PyQt5.QtCore import QTimer, QDateTime
class SerialDataDisplay(QWidget):
def __init__(self):
super().__init__()
self.x_points = []
self.temperatures = []
self.humidities = []
self.initUI()
self.initSerial()
self.initGraph()
self.timer = QTimer(self)
self.timer.timeout.connect(self.readSerialData)
self.timer.start(1000)
def initUI(self):
self.setWindowTitle('温湿度数据显示')
self.setGeometry(100, 100, 800, 600)
self.layout = QVBoxLayout()
self.temp_label = QLabel('温度: N/A')
self.humidity_label = QLabel('湿度: N/A')
self.layout.addWidget(self.temp_label)
self.layout.addWidget(self.humidity_label)
self.setLayout(self.layout)
def initSerial(self):
try:
self.ser = Serial('COM20', 115200, timeout=1)
except serial.SerialException as e:
print(f"串口打开失败: {e}")
def initGraph(self):
self.graphWidget = pg.GraphicsLayoutWidget()
self.graphWidget.setWindowTitle('温湿度趋势图')
self.layout.addWidget(self.graphWidget)
self.p1 = self.graphWidget.addPlot(row=0, col=0)
self.p1.setLabel('left', '温度 (°C)')
self.p1.setLabel('bottom', '时间 (mm:ss)')
self.p1.showGrid(x=True, y=True)
self.p1.setYRange(0, 30)
self.p1.addLegend()
self.p2 = pg.ViewBox()
self.graphWidget.scene().addItem(self.p2)
self.p2.setXLink(self.p1)
self.p2.setYRange(0, 100)
self.axis2 = pg.AxisItem(orientation='right')
self.axis2.setLabel('湿度 (%)')
self.graphWidget.addItem(self.axis2, row=0, col=1)
self.axis2.linkToView(self.p2)
def updateViews():
self.p2.setGeometry(self.p1.vb.sceneBoundingRect())
self.p2.linkedViewChanged(self.p1.vb, self.p2.XAxis)
updateViews()
self.p1.vb.sigResized.connect(updateViews)
self.temp_curve = self.p1.plot(pen='r', name='温度 (°C)')
self.humidity_curve = pg.PlotDataItem(pen='b', name='湿度 (%)')
self.p2.addItem(self.humidity_curve)
def readSerialData(self):
if self.ser.is_open:
try:
line = self.ser.readline().decode('utf-8').strip()
if line.startswith('+DI:'):
data_str = line.split('Data ')[-1]
temp_str = data_str[6:10]
humidity_str = data_str[10:]
temperature = float(temp_str) / 100
humidity = float(humidity_str) / 100
self.temp_label.setText(f'温度: {temperature:.2f} °C')
self.humidity_label.setText(f'湿度: {humidity:.2f} %')
current_time = QDateTime.currentDateTime().toString("mm:ss")
self.x_points.append(current_time)
self.temperatures.append(temperature)
self.humidities.append(humidity)
self.temp_curve.setData(x=range(len(self.x_points)), y=self.temperatures)
self.humidity_curve.setData(x=range(len(self.x_points)), y=self.humidities)
ticks = [(i, t if i % 5 == 0 else '') for i, t in enumerate(self.x_points)]
self.p1.getAxis('bottom').setTicks([])
self.p1.getAxis('bottom').setTicks([ticks])
if len(self.x_points) > 50:
self.x_points.pop(0)
self.temperatures.pop(0)
self.humidities.pop(0)
except (UnicodeDecodeError, ValueError, IndexError) as e:
print(f"数据解析错误: {e}")
if __name__ == '__main__':
app = QApplication(sys.argv)
window = SerialDataDisplay()
window.show()
sys.exit(app.exec_())
【总结】
通过这次的试用,我成功的实现的一个冷链监控系统。经过试用,现将感受总结如下:
1、道生物联TKB-620有比较完整的开发板资料,提供的SDK切实可以用,用户可以通过修改少许代码就可以实现自己的工程。
2、技术支持非常好,在试用过程中,热心的创建了交流群,如果有问题,在群里提出,可以快速的得到解答。
3、此款通信模块的穿透力非常强,放置在冰箱的模块,可以成功的穿透密封的带有铁壳的封装。这给特殊环境下的无线通信提供的较好的支持,如果是空旷的地带,也可以提供长距离的数据传输。
4、具有良好的低功耗特性,唤醒时间也是特别的短。