I have used this sensor before when evaluating Mir MP157. Today I will continue to use this sensor to test our IMX8, and also record some problems in the process.
According to the board description, we can use three I2C interfaces, of which I2C2 is for touch, which is currently connected to the FPC of the screen so it cannot be used. Here I used I2C4, which is connected to the pin header of J25.
The schematic diagram is as above, and the actual connection method is as follows:
Next is the driver. As before, modify the Makefile.
I used the official independent kernel here, which can be downloaded from git. Please pay attention to modify gcc.
Note that when compiling this development board, the kernel driver needs to modify a return parameter, which is long here. I am not a kernel driver developer, and I guess it is because the kernel is arm64.
Similarly, we copy .ko to the board.
To compile the testapp, just replace the gcc in the script with our tool chain.
#include "mainwindow.h"
#include "ui_mainwindow.h"
#define Plot2_DotColor QColor(236,110,0)
#define Plot2_LineColor QColor(246,98,0)
#define LineWidth 2
#define DotWidth 10
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
dataCustomPlotInit();
sht20_Init();
mainTimer = new QTimer(this);
/* 信号与槽函数连接 */
connect(mainTimer, SIGNAL(timeout()), this, SLOT(read_Sht20_data()));
/* 开启定时器 */
mainTimer->start(1000); // 1ms秒更新一次
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::dataCustomPlotInit()
{
QFont font;
// font.setPointSize(12);
/* 实例化,设置位置、背景颜色 */
QBrush brush(QColor(50, 50, 50));
dataCustomPlot = new QCustomPlot(ui->widget);
dataCustomPlot->setGeometry(0, 0, 550, 300);
dataCustomPlot->setBackground(brush);
dataCustomPlot->installEventFilter(this);
/* x轴、Y轴相关配置 */
QPen pen(Qt::white);
font.setPointSize(8);
dataCustomPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom); //可拖拽+可滚轮缩放
dataCustomPlot->xAxis->setLabelColor(QColor(Qt::white)); // X轴上标识label字体颜色
dataCustomPlot->yAxis->setLabelColor(QColor(Qt::white));
dataCustomPlot->xAxis->setTickPen(pen); // 设置x轴上坐标点上对应的刻度线的颜色
dataCustomPlot->xAxis->setTickLabelRotation(60);//设置标签角度旋转
dataCustomPlot->yAxis->setTickPen(pen);
dataCustomPlot->xAxis->setBasePen(pen); // 设置x轴 轴线本身的颜色
dataCustomPlot->yAxis->setBasePen(pen);
dataCustomPlot->xAxis->setTickLabelColor(QColor(Qt::white)); // 设置x轴刻度值文本的颜色
dataCustomPlot->yAxis->setTickLabelColor(QColor(Qt::white));
dataCustomPlot->xAxis->setSubTicks(false); // 隐藏x轴刻度线
dataCustomPlot->yAxis->setSubTicks(false); // 隐藏y轴刻度线
dataCustomPlot->xAxis->setLabelFont(font); // 设置x轴标识label文本字体大小
dataCustomPlot->yAxis->setLabelFont(font); // 设置y轴标识label文本字体大小
font.setPointSize(10);
dataCustomPlot->xAxis->setTickLabelFont(font);
dataCustomPlot->yAxis->setTickLabelFont(font);
dataCustomPlot->xAxis->setLabel("时间");
dataCustomPlot->yAxis->setLabel("温湿度");
dataCustomPlot->legend->setVisible(true);
/* 增加一个数据曲线 */
pen.setColor(Qt::yellow); // 设置画笔的颜色
dataCustomPlot->addGraph(); // 增加曲线图
dataCustomPlot->graph(0)->setName("温度"); // 设置曲线的名字
dataCustomPlot->graph(0)->setPen(pen); // 设置曲线画笔的颜色
dataCustomPlot->graph(0)->setLineStyle(QCPGraph::lsLine); // 设置连接线的类型 两点直线连接
dataCustomPlot->graph(0)->setScatterStyle( //绘制打出来的点
QCPScatterStyle(QCPScatterStyle::ssCircle,
QPen(Plot2_DotColor, LineWidth),
QBrush(Plot2_DotColor), DotWidth));
dataCustomPlot->graph(0)->rescaleAxes(false);
pen.setColor(Qt::red); // 设置画笔的颜色
dataCustomPlot->addGraph(); // 增加曲线图
dataCustomPlot->graph(1)->setName("湿度"); // 设置曲线的名字
dataCustomPlot->graph(1)->setPen(pen); // 设置曲线画笔的颜色
dataCustomPlot->graph(1)->setLineStyle(QCPGraph::lsLine); // 设置连接线的类型 两点直线连接
// dataCustomPlot->graph(1)->setScatterStyle( //绘制打出来的点
// QCPScatterStyle(QCPScatterStyle::ssCircle,
// QPen(Plot2_DotColor, LineWidth),
// QBrush(Plot2_DotColor), DotWidth));
dataCustomPlot->graph(1)->rescaleAxes(false);
}
void MainWindow::sht20_Init()
{
fd=open("/dev/sht2x",O_RDWR);
if(fd<0)
{
printf("App:Open dev failed.\n");
// goto END;
}
}
void MainWindow::read_Sht20_data()
{
char buf[50];
static int now = 0;
unsigned char tmp[5];
unsigned short tempori,humiori;//温湿度原始数据
float temp,humi;
static float temp_max=0,humi_max=0;
if(read(fd,tmp,5)>0)
{
// for(int i=0;i<4;i++)
// {
// printf("%.2x ",tmp[i]);//打印收到的数据
// }
// printf("\n");
tempori=(tmp[0]<<8|tmp[1]);
humiori=(tmp[2]<<8|tmp[3]);
temp=-46.85+175.72/65536*(float)tempori;
humi=-6.0 +125.0 /65536*(float)humiori;
printf("SHT2x-T:%f RH:%f%%\n",temp,humi);
ui->label_4->setText(QString::number(temp,'f',2)+"℃");
ui->label_5->setText(QString::number(humi,'f',2)+"%");
if(temp_max<temp)
{
temp_max = temp;
}
if(humi_max<humi)
{
humi_max = humi;
}
ui->label_7->setText(QString::number(temp_max,'f',2)+"℃");
ui->label_9->setText(QString::number(humi_max,'f',2)+"%");
if(22.0<temp && temp<26.0)
{
if(40.0<humi && humi<70.0)
{
ui->label_10->setStyleSheet("color:black");
ui->label_10->setText("环境舒适");
}
else if(humi<40.0)
{
ui->label_10->setStyleSheet("color:red");
ui->label_10->setText("环境干燥");
}
else if(humi>70.0)
{
ui->label_10->setStyleSheet("color:red");
ui->label_10->setText("环境潮湿");
}
}
else if(temp<22.0)
{
ui->label_10->setStyleSheet("color:red");
ui->label_10->setText("环境温度低");
}
else if(temp>26.0)
{
ui->label_10->setStyleSheet("color:red");
ui->label_10->setText("环境温度高");
}
// qDebug() << temp;
}
now++;
dataCustomPlot->graph(0)->addData(now, temp); //addData(double key, double value);原型
dataCustomPlot->graph(1)->addData(now, humi); //addData(double key, double value);原型
dataCustomPlot->replot();
}
We mainly want to display it, so the above is the QT code, which can display the content graphically. As follows:
Labels are added here to display the temperature and humidity values, which is more intuitive. The curve is used to show the historical trend. At the same time, the highest temperature and humidity are recorded. If QT is compiled, the release mode must be used, otherwise it will not run.
The kernel still loads the driver first. After the driver is loaded, our device should appear under the device. This is correct, and the corresponding device tree also needs to be modified.
The specific location is as above. The modification is to add the sht20 node and then update the device tree. All files are in the attachment below.
sht20.zip
(550.12 KB, downloads: 4)