【IoT Development Based on Raspberry Pi Education Kit】Project Submission
[Copy link]
This post was last edited by eew_7QI3Yq on 2022-10-17 15:02
Title of Work
IoT development based on Raspberry Pi educational kit
Author: eew_7QI3Yq
In this competition, we applied for Raspberry Pi as our IoT development kit, which mainly relied on the powerful ecology of Raspberry Pi and the benefits of a large user group. Raspberry Pi can realize our image development, directly add our QT, and then do interface processing. We can also use QT to call OPENCV to run, and can also directly do related development, which greatly simplifies our development process. At the same time, there are many tutorials on the Internet, so it is very suitable for our development work. As an applicant for IoT development, I used QT's MQTT module to connect to Alibaba Cloud, upload data, and send control data through the Alibaba Cloud platform to control the status of our underlying LED lights.
2. System Block Diagram
The overall block diagram is as above. The hardware is all connected through the expansion interface of Raspberry Pi 400. The software is installed by QT in the Raspberry Pi system to carry out desktop development. Later, I also want to learn some image recognition functions between OPENCV and QT.
3. Functional description of each part
The three main sensors are a PM2.5 laser sensor, a SHT20 temperature and humidity sensor, and a SGP30 TVOC and CO2 sensor.
An expansion interface board for the Raspberry Pi 400 and an LED light device.
The overall picture shows the Raspberry Pi 400, a keyboard-style development board that saves us from having to use an external keyboard. It is very useful, but the HDMI interface requires the purchase of a separate cable for output conversion.
On the display screen, I developed a QT solution, which can directly run the program and read the values of the three underlying sensors, and control a button as the function of turning the underlying light on and off. Another part is the camera image acquisition function, which can display the picture in real time.
QStringhost("iot-06z00ac7lu605si.mqtt.iothub.aliyuncs.com");//代理服务器IP
QByteArraypassword=xxxx;//设备密码
quint16port=1883;//代理服务器端口
QStringClientId=xxxx;//设备ID
QStringproductId="qt_test&h14lBOv58fr";//产品ID
client->setKeepAlive(120);//心跳
client->setHostName(host);//设置EMQ代理服务器
client->setPort(port);//设置EMQ代理服务器端口
client->setClientId(ClientId);//设备ID
client->setUsername(productId);//产品ID
client->setPassword(password);
client->cleanSession();
client->setVersion(QMQTT::MQTTVersion::V3_1_1);//设置mqtt版本
client->connectToHost();//连接EMQ代理服务器
The overall connection to Alibaba Cloud Server is the code above. We only need to set the server URL, which is the URL of Alibaba Cloud's unified East China server. The device password, proxy server port, and ID behind it all need to be done on Alibaba Cloud, and it will be generated.
#include"sht20.h"
#defineSHT2X_ADDR0x40
#defineSHT2X_SOFTRESET0xFE
#defineSHT2X_MEAS_TEMPER0xF3
#defineSHT2X_MEAS_HUMIDITY0xF5
intMainWindow::sht20_Init()
{
fd=open(DEV_FILE,O_RDWR);
if(fd<0)
{
printf("App:Opendevfailed.\n");
return-1;
//gotoEND;
}
ioctl(fd,I2C_TENBIT,0);
ioctl(fd,I2C_SLAVE,SHT2X_ADDR);
returnfd;
}
voidMainWindow::read_Sht20_data()
{
charbuf[50];
staticintnow=0;
unsignedchartmp[5];
unsignedshorttempori,humiori;//温湿度原始数据
staticinttemp_max=0,humi_max=0;
intret=-1;
ioctl(fd,I2C_SLAVE,SHT2X_ADDR);
ret=sht2x_softreset(fd);
if(ret<0)
{
printf("sht2x_softresetfailure.\n");
//return-2;
}
ret=sht2x_get_temper_rh(fd,&temp,&humi);
if(ret<0)
{
printf("sht2x_get_temper_rhfailure.\n");
//return-3;
}
printf("SHT2x-T:%fRH:%f%%\n",temp,humi);
ui->label_8->setText(QString::number(temp,'f',2));
ui->label_9->setText(QString::number(humi,'f',2));
if(show_temp_humi)
{
now_time++;
dataCustomPlot->graph(0)->addData(now_time,temp);//addData(doublekey,doublevalue);原型
dataCustomPlot->graph(1)->addData(now_time,humi);
dataCustomPlot->replot();
}
}
voidMainWindow::msleep(unsignedinttime)
{
structtimespecsleeper,temp;
sleeper.tv_sec=(time_t)(time/1000);
sleeper.tv_nsec=(long)(time%1000)*1000000;
nanosleep(&sleeper,&temp);
return;
}
intMainWindow::sht2x_softreset(intfd)
{
intret=-1;
uint8_tbuf[2]={0};
if(fd<0)
{
printf("inputtheinvalidargument.\n");
return-1;
}
buf[0]=SHT2X_SOFTRESET;
ret=write(fd,buf,1);
if(ret<0)
{
printf("writesoftrestcmdtosht2xfailure.\n");
return-2;
}
msleep(50);
return0;
}
intMainWindow::sht2x_send_cmd_wr(intfd,char*which)
{
intret=-1;
uint8_tbuf[2]={0};
if(fd<0)
{
printf("inputtheinvalidargument.\n");
return-1;
}
if(strcmp(which,"temper")==0)
{
buf[0]=SHT2X_MEAS_TEMPER;
ret=write(fd,buf,1);
if(ret<0)
{
printf("writetempercmdtosht2xfailure.\n");
return-2;
}
msleep(85);//datasheettyp=66,max=85
}
elseif(strcmp(which,"rh")==0)
{
buf[0]=SHT2X_MEAS_HUMIDITY;
ret=write(fd,buf,1);
if(ret<0)
{
printf("writehumiditycmdtosht2xfailure.\n");
return-3;
}
msleep(29);//datasheettyp=22,max=29
}
return0;
}
intMainWindow::sht2x_get_temper_rh(intfd,float*temper,float*rh)
{
uint8_tbuf[4]={0};
intret=-1;
if(fd<0||!temper||!rh)
{
printf("inputtheinvalidarguments.\n");
return-1;
}
ret=sht2x_send_cmd_wr(fd,"temper");
if(ret<0)
{
printf("sht2x_send_cmd_wrtemperfailure.\n");
return-2;
}
else
{
ret=read(fd,buf,3);
if(ret<0)
{
printf("getthetemperfailure.\n");
return-3;
}
*temper=175.72*(((((int)buf[0])<<8)+buf[1])/65536.0)-46.85;
}
ret=sht2x_send_cmd_wr(fd,"rh");
if(ret<0)
{
printf("sht2x_send_cmd_wrrhfailure.\n");
return-2;
}
else
{
read(fd,buf,3);
if(ret<0)
{
printf("getthetemperfailure.\n");
return-3;
}
*rh=125*(((((int)buf[0])<<8)+buf[1])/65536.0)-6;
}
return0;
}
The overall temperature and humidity code of sht20 is actually still based on Linux, operating the Linux bottom layer and operating through ioctl. QT is compatible with all of these. Similarly, our SGP30 can also be read directly. On the Raspberry Pi, more of a way is to write and test through python, and we can try it ourselves later.
QSerialPort*myplotcom=newQSerialPort;
voidMainWindow::usartDataInit()
{
//UsartData::myplotcom=newQSerialPort;
myplotcom->setBaudRate(QSerialPort::Baud9600,QSerialPort::AllDirections);//设置波特率和读写方向
myplotcom->setDataBits(QSerialPort::Data8);//数据位为8位
myplotcom->setFlowControl(QSerialPort::NoFlowControl);//无流控制
myplotcom->setParity(QSerialPort::NoParity);//无校验位
myplotcom->setStopBits(QSerialPort::OneStop);//一位停止位
#ifdefQ_OS_WIN//如果是windows系统
myplotcom->setPortName("COM1");//ttymxc2
#else//如果是unix或者其他系统
myplotcom->setPortName("/dev/ttyAMA1");//ttymxc2
#endif
myplotcom->open(QIODevice::ReadWrite);
if(myplotcom->isOpen())
{
qDebug()<<"myplotcomisopen"<<endl;
}
else
{
qDebug()<<"myplotcomiserror"<<endl;
}
//connect(myplotcom,SIGNAL(readyRead()),this,SLOT(receiveInfo()));
myReadTimer=newQTimer(this);
myReadTimer->setInterval(500);
connect(myReadTimer,&QTimer::timeout,this,&MainWindow::receiveInfo);//这个定时器是一进此界面就开始启动,定时读取,主要读取探测器参数。
myReadTimer->start();
}
//接收到单片机发送的数据进行解析
voidMainWindow::receiveInfo()
{
if(myplotcom->bytesAvailable()<=0){return;}
myHelper::Sleep(100);//延时100毫秒保证接收到的是一条完整的数据,而不是脱节的,
QByteArrayinfo=myplotcom->readAll();
QStringplotDataHex=myHelper::ByteArrayToHexStr(info);//分出16进制HEX文件打印输出,后期可以关闭
qDebug()<<plotDataHex<<endl;
if(info.length()<24)return;
if((uchar)info.at(0)==0x32&&(uchar)info.at(1==0x3d))
{
PM_2_5_data=(uchar)info.at(6)<<8|(uchar)info.at(7);
PM10_data=(uchar)info.at(8)<<8|(uchar)info.at(9);
ui->label_12->setText(QString::number(PM_2_5_data));
ui->label_13->setText(QString::number(PM10_data));
if(show_pm2_5)
{
now_time++;
dataCustomPlot->graph(0)->addData(now_time,PM_2_5_data);//addData(doublekey,doublevalue);原型
dataCustomPlot->graph(1)->addData(now_time,PM10_data);
dataCustomPlot->replot();
}
}
}
For PM2.5, we use a serial port. Currently, Raspberry Pi 4 can have multiple serial ports. I used an additional serial port, ttyAMA1, so I don’t have to worry about having too few serial ports.
voidMainWindow::led_init()
{
system("echo26>/sys/class/gpio/export");
system("echoout>/sys/class/gpio/gpio26/direction");
system("echo0>/sys/class/gpio/gpio26/value");
}
voidMainWindow::on_pushButton_2_clicked()
{
if(ui->pushButton_2->text()=="开灯")
{
on_off=true;
system("echo1>/sys/class/gpio/gpio26/value");
ui->pushButton_2->setText("关灯");
}
else
{
on_off=false;
system("echo0>/sys/class/gpio/gpio26/value");
ui->pushButton_2->setText("开灯");
}
}
The same principle applies to turning on and off the lights, which can be accomplished using Linux's underlying operations.
5. Demonstration video of the work’s functions
【IoT development based on Raspberry Pi educational kit】
6. Project Summary
First of all, I would like to thank Digi-Key Electronics for providing the material costs. At the same time, you can also buy the corresponding electronic materials on Digi-Key, and the delivery is still very fast. Secondly, the time given for this competition is long enough. I have learned a lot after completing this work. Finally, the Raspberry Pi is really rich in ecology. I will share the development cases later.
VII. Others
|