树莓派DHT11温湿度传感器+BMP280大气气压传感器

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weifuliu/article/details/80994202

bmp:

#ifndef _RASPI_BMP280_H_
#define _RASPI_BMP280_H_
#include <stdio.h>
#include <wiringPi.h>
#include <wiringPiI2C.h>

unsigned short dig_t1,dig_p1;
signed short dig_t2,dig_t3,dig_p2,dig_p3,dig_p4;
signed short dig_p5,dig_p6,dig_p7,dig_p8,dig_p9;
float temp,pres,var1,var2;
int pres_int=0,bmp280_flag=-1;
uint8_t msb,lsb,xlsb,pres_h=0,pres_l=0,pres_flo=0;

int i2c_fd;

int Raspi_BMP280_Init()
{
    wiringPiSetup();
    i2c_fd=wiringPiI2CSetup(0x76);
    if(wiringPiI2CReadReg8(i2c_fd,0xd0)==0x58)
    {
        system("i2cset -y 1 0x76 0xff 0xf4");
        dig_t1=(wiringPiI2CReadReg8(i2c_fd,0x89)<<8)
        |wiringPiI2CReadReg8(i2c_fd,0x88);

        dig_t2=(wiringPiI2CReadReg8(i2c_fd,0x8b)<<8)
        |wiringPiI2CReadReg8(i2c_fd,0x8a);

        dig_t3=(wiringPiI2CReadReg8(i2c_fd,0x8d)<<8)
        |wiringPiI2CReadReg8(i2c_fd,0x8c);

        dig_p1=(wiringPiI2CReadReg8(i2c_fd,0x8f)<<8)
        |wiringPiI2CReadReg8(i2c_fd,0x8e);

        dig_p2=(wiringPiI2CReadReg8(i2c_fd,0x91)<<8)
        |wiringPiI2CReadReg8(i2c_fd,0x90);

        dig_p3=(wiringPiI2CReadReg8(i2c_fd,0x93)<<8)
        |wiringPiI2CReadReg8(i2c_fd,0x92);
        dig_p4=(wiringPiI2CReadReg8(i2c_fd,0x95)<<8)
        |wiringPiI2CReadReg8(i2c_fd,0x94);
        dig_p5=(wiringPiI2CReadReg8(i2c_fd,0x97)<<8)
        |wiringPiI2CReadReg8(i2c_fd,0x96);
        dig_p6=(wiringPiI2CReadReg8(i2c_fd,0x99)<<8)
        |wiringPiI2CReadReg8(i2c_fd,0x98);
        dig_p7=(wiringPiI2CReadReg8(i2c_fd,0x9b)<<8)
        |wiringPiI2CReadReg8(i2c_fd,0x9a);
        dig_p8=(wiringPiI2CReadReg8(i2c_fd,0x9d)<<8)
        |wiringPiI2CReadReg8(i2c_fd,0x9c);
        dig_p9=(wiringPiI2CReadReg8(i2c_fd,0x9f)<<8)
        |wiringPiI2CReadReg8(i2c_fd,0x9e);
        return 0;
    }
    return -1;
}

float Raspi_BMP280_Read_Pressure()
{
    msb=wiringPiI2CReadReg8(i2c_fd,0xf7);
    lsb=wiringPiI2CReadReg8(i2c_fd,0xf8);
    xlsb=wiringPiI2CReadReg8(i2c_fd,0xf9);
    pres=(msb<<16|lsb<<8|xlsb)>>4;

    msb=wiringPiI2CReadReg8(i2c_fd,0xfa);
    lsb=wiringPiI2CReadReg8(i2c_fd,0xfb);
    xlsb=wiringPiI2CReadReg8(i2c_fd,0xfc);
    temp=(msb<<16|lsb<<8|xlsb)>>4;

    var1=(temp/16384.0-dig_t1/1024.0)*(dig_t2);
    var2=((temp/131072.0-dig_t1/8192.0)*(temp/131072.0-dig_t1/8192.0))*dig_t3;
    temp=var1+var2;
    temp/=5120.0;

    var1=(temp/2.0)-64000.0;
    var2=var1*var1*(dig_p6)/32768.0;
    var2=var2+var1*(dig_p5)*2.0;
    var2=(var2/4.0)+((dig_p4)*65536.0);
    var1=(dig_p3)*var1*var1/524288.0+(dig_p2)*var1/524288.0;
    var1=(1.0+var1/32768.0)*(dig_p1);

    pres=1048576.0-pres;
    pres=(pres-(var2/4096.0))*6250.0/var1;
    var1=(dig_p9)*pres*pres/2147483648.0;
    var2=pres*(dig_p8)/32768.0;
    pres=pres+(var1 + var2+(dig_p7))/16.0;
    return pres;
}

#endif

dht

#ifndef _RASPI_DHT11_H_
#define _RASPI_DHT11_H_
#include <wiringPi.h>

int dht11_val[5]={0,0,0,0,0},dht11_flag=-1;
#define DHT11PIN 7

int Raspi_DHT11_Read()
{
    uint8_t lststate=HIGH;
    uint8_t counter=0;
    uint8_t j=0,i;
    for(i=0;i<5;i++)
        dht11_val[i]=0;

    pinMode(DHT11PIN,OUTPUT);
    digitalWrite(DHT11PIN,LOW);
    delay(18);
    digitalWrite(DHT11PIN,HIGH);
    delayMicroseconds(40);

    pinMode(DHT11PIN,INPUT);
    for(i=0;i<85;i++)
    {
        counter=0;
        while(digitalRead(DHT11PIN)==lststate)
        {
            counter++;
            delayMicroseconds(1);
            if(counter==255)
                break;
        }
        lststate=digitalRead(DHT11PIN);

        if(counter==255)break;
        if((i>=4)&&(i%2==0))
        {
            dht11_val[j/8]<<=1;
            if(counter>30)
                dht11_val[j/8]|=1;
            j++;
        }
    }
    if((j>=40)&&(dht11_val[4]==((dht11_val[0]+dht11_val[1]+dht11_val[2]+dht11_val[3])&0xFF)))
        return 0;

    return 1;
}

#endif

main

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <dirent.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <linux/videodev2.h>
#include <wiringPi.h>
#include "raspi_dht11.h"
#include "raspi_bmp280.h"
//#include "raspi_led_pwm.h"
//#include "raspi_rtc.h"

int newsock,count=0,local=0;
//pthread_t id1;
struct dirent *ptr;
DIR *dir;
//char mp3_buf[100][100]={"/home/pi/Music"};
//char mp3_play[100];

void TCP_Listen()
{
    char recvbuf[10],sendbuf[1000],time_buf[10];
    //Raspi_LED_Init();
    //Raspi_PWM_Init(100);
    while(1)
    {
        recv(newsock,recvbuf,10,0);
       /* if(recvbuf[0]==0x01)
        {
            if(recvbuf[1]==1)
                digitalWrite(LED5V,LOW);
            else if(recvbuf[1]==2)
                digitalWrite(LED5V,HIGH);
        }*/

        /*else if(recvbuf[0]==0x02)
        {
            if(recvbuf[1]==1)
            {
                digitalWrite(MOTOR1,LOW);
                digitalWrite(MOTOR2,HIGH);
            }
            else if(recvbuf[1]==2)
            {
                digitalWrite(MOTOR1,HIGH);
                digitalWrite(MOTOR2,LOW);
            }
            else if(recvbuf[1]==3)
            {
                digitalWrite(MOTOR1,LOW);
                digitalWrite(MOTOR2,LOW);
            }
        }*/
/*
        else if(recvbuf[0]==0x03)
        {
            Raspi_PWM_RGB(recvbuf[1],recvbuf[2],recvbuf[3]);
        }

        else if(recvbuf[0]==0x04)
        {
            count=0;
            dir=opendir("/home/pi/Music");
            while((ptr=readdir(dir))!=NULL)
            {
                if(strcmp(ptr->d_name,".")!=0&&strcmp(ptr->d_name,"..")!=0)
                {
                    strcpy(mp3_buf[count],"/home/pi/Music/");
                    strcat(mp3_buf[count],ptr->d_name);
                    count++;
                }
            }

            memset(sendbuf,0,sizeof(sendbuf));
            sendbuf[0]=0x02;
            sendbuf[1]=count;
            fflush(stdout);
            local=0;
            while(local<count)
            {
                strcat(sendbuf,mp3_buf[local]);
                strcat(sendbuf,"|");
                local++;
            }
            fflush(stdout);
            send(newsock,sendbuf,sizeof(sendbuf),10);
            recvbuf[0]=0x00;
            closedir(dir);
        }

        else if(recvbuf[0]==0x05)
        {
            strcpy(mp3_play,"");
            strcat(mp3_play,"madplay ");
            strcat(mp3_play,mp3_buf[recvbuf[1]]);
            strcat(mp3_play," &");
            recvbuf[0]=0x00;
            system(mp3_play);
            continue;
        }

        else if(recvbuf[0]==0x06)
        {
            recvbuf[0]=' ';
            sprintf(time_buf,"date -s "%s"",recvbuf);
            system(time_buf);
        }

        else if(recvbuf[0]==0x07)
            system("killall -9  madplay &");
*/
    }
}

int main()
{
        int ret,sockfd,video_fd,i=0,j=0;
        char sendbuf[10];
        if(Raspi_BMP280_Init()==0)
            bmp280_flag=0;
        int addrsize=sizeof(struct sockaddr);
        struct sockaddr_in bindaddr;
        struct sockaddr_in boyaddr;
        bzero(&bindaddr,sizeof(bindaddr));
        bindaddr.sin_family=AF_INET;
        bindaddr.sin_port=htons(10086);
        bindaddr.sin_addr.s_addr=inet_addr("192.168.1.109");
        sockfd=socket(AF_INET,SOCK_STREAM,0);

        if(sockfd==-1)
        {
                printf("create socket failed!\n");
                return -1;
        }

        ret=bind(sockfd,(struct sockaddr *)&bindaddr,addrsize);
        if(ret==-1)
        {
                printf("bind failed!\n");
                return -1;
        }
        ret=listen(sockfd,5);
        if(ret==-1)
        {
                printf("listen failed!\n");
                return -1;
        }

        newsock=accept(sockfd,(struct sockaddr *)&boyaddr,&addrsize);
        if(newsock==-1)
        {
                printf("accept failed!\n");
                return -1;
        }
        //pthread_create(&id1,NULL,(void *)TCP_Listen,NULL);

        while(1)
        {

            if(Raspi_DHT11_Read()==0)
            {
                sendbuf[1]=dht11_val[0]; //tem
                sendbuf[2]=dht11_val[2]; //rah
                dht11_flag=0;
            }
            if(bmp280_flag==0)
            {
                Raspi_BMP280_Read_Pressure();
                pres_int=(int)pres;
                pres_h=pres_int>>8;
                pres_l=pres_int&0xff;
                sendbuf[3]=pres_h;    
                sendbuf[4]=pres_l;
                pres_flo=(int)(pres*100);
                sendbuf[5]=pres_flo;
            }
/*
            Get_Time();
            sendbuf[6]=hour;
            sendbuf[7]=minute;
            sendbuf[8]=second;*/

            if(dht11_flag==0&&bmp280_flag==0)
            {
                sendbuf[0]=0x01;
                send(newsock,sendbuf,10,0);
            }

        }
        close(sockfd);
        close(newsock);

        return 0;
}

mainwindow.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>336</width>
    <height>246</height>
   </rect>
  </property>
  <property name="mouseTracking">
   <bool>false</bool>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralWidget">
   <widget class="QTabWidget" name="tabWidget">
    <property name="geometry">
     <rect>
      <x>0</x>
      <y>0</y>
      <width>331</width>
      <height>201</height>
     </rect>
    </property>
    <property name="currentIndex">
     <number>0</number>
    </property>
    <widget class="QWidget" name="T1">
     <property name="layoutDirection">
      <enum>Qt::LeftToRight</enum>
     </property>
     <attribute name="title">
      <string>环境检测</string>
     </attribute>
     <widget class="QLabel" name="LABEL3">
      <property name="geometry">
       <rect>
        <x>20</x>
        <y>10</y>
        <width>41</width>
        <height>31</height>
       </rect>
      </property>
      <property name="text">
       <string>温度</string>
      </property>
     </widget>
     <widget class="QLCDNumber" name="LCDN1">
      <property name="geometry">
       <rect>
        <x>80</x>
        <y>10</y>
        <width>64</width>
        <height>31</height>
       </rect>
      </property>
     </widget>
     <widget class="QLabel" name="LABEL1">
      <property name="geometry">
       <rect>
        <x>200</x>
        <y>10</y>
        <width>41</width>
        <height>31</height>
       </rect>
      </property>
      <property name="text">
       <string>湿度</string>
      </property>
     </widget>
     <widget class="QLCDNumber" name="LCDN2">
      <property name="geometry">
       <rect>
        <x>250</x>
        <y>10</y>
        <width>64</width>
        <height>31</height>
       </rect>
      </property>
     </widget>
     <widget class="QLabel" name="LABEL2">
      <property name="geometry">
       <rect>
        <x>20</x>
        <y>60</y>
        <width>31</width>
        <height>17</height>
       </rect>
      </property>
      <property name="text">
       <string>气压</string>
      </property>
     </widget>
     <widget class="QLabel" name="LABEL4">
      <property name="geometry">
       <rect>
        <x>160</x>
        <y>50</y>
        <width>41</width>
        <height>31</height>
       </rect>
      </property>
      <property name="text">
       <string>百帕</string>
      </property>
     </widget>
     <widget class="QLabel" name="LABEL5">
      <property name="geometry">
       <rect>
        <x>20</x>
        <y>100</y>
        <width>67</width>
        <height>21</height>
       </rect>
      </property>
      <property name="text">
       <string>服务器ip</string>
      </property>
     </widget>
     <widget class="QTextEdit" name="TE1">
      <property name="geometry">
       <rect>
        <x>80</x>
        <y>100</y>
        <width>111</width>
        <height>31</height>
       </rect>
      </property>
     </widget>
     <widget class="QTextEdit" name="TE2">
      <property name="geometry">
       <rect>
        <x>280</x>
        <y>100</y>
        <width>41</width>
        <height>31</height>
       </rect>
      </property>
      <property name="autoFillBackground">
       <bool>false</bool>
      </property>
      <property name="inputMethodHints">
       <set>Qt::ImhMultiLine</set>
      </property>
     </widget>
     <widget class="QLabel" name="LABEL6">
      <property name="geometry">
       <rect>
        <x>230</x>
        <y>100</y>
        <width>51</width>
        <height>21</height>
       </rect>
      </property>
      <property name="text">
       <string>端口号</string>
      </property>
     </widget>
     <widget class="QPushButton" name="PB1">
      <property name="geometry">
       <rect>
        <x>150</x>
        <y>140</y>
        <width>89</width>
        <height>25</height>
       </rect>
      </property>
      <property name="text">
       <string>连接</string>
      </property>
     </widget>
     <widget class="QLabel" name="label7">
      <property name="geometry">
       <rect>
        <x>80</x>
        <y>60</y>
        <width>71</width>
        <height>17</height>
       </rect>
      </property>
      <property name="text">
       <string/>
      </property>
     </widget>
    </widget>
    <widget class="QWidget" name="T2">
     <attribute name="title">
      <string>音乐播放</string>
     </attribute>
     <widget class="QTextBrowser" name="TB">
      <property name="geometry">
       <rect>
        <x>10</x>
        <y>10</y>
        <width>311</width>
        <height>91</height>
       </rect>
      </property>
     </widget>
     <widget class="QSlider" name="HS">
      <property name="geometry">
       <rect>
        <x>10</x>
        <y>110</y>
        <width>341</width>
        <height>21</height>
       </rect>
      </property>
      <property name="orientation">
       <enum>Qt::Horizontal</enum>
      </property>
     </widget>
     <widget class="QPushButton" name="PB2">
      <property name="geometry">
       <rect>
        <x>20</x>
        <y>140</y>
        <width>89</width>
        <height>25</height>
       </rect>
      </property>
      <property name="text">
       <string>检索音乐</string>
      </property>
     </widget>
     <widget class="QPushButton" name="PB3">
      <property name="geometry">
       <rect>
        <x>130</x>
        <y>140</y>
        <width>89</width>
        <height>25</height>
       </rect>
      </property>
      <property name="text">
       <string>播放音乐</string>
      </property>
     </widget>
     <widget class="QPushButton" name="PB4">
      <property name="geometry">
       <rect>
        <x>240</x>
        <y>140</y>
        <width>89</width>
        <height>25</height>
       </rect>
      </property>
      <property name="text">
       <string>暂停</string>
      </property>
     </widget>
    </widget>
   </widget>
  </widget>
  <widget class="QToolBar" name="mainToolBar">
   <attribute name="toolBarArea">
    <enum>TopToolBarArea</enum>
   </attribute>
   <attribute name="toolBarBreak">
    <bool>false</bool>
   </attribute>
  </widget>
  <widget class="QMenuBar" name="menuBar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>336</width>
     <height>28</height>
    </rect>
   </property>
   <widget class="QMenu" name="menu">
    <property name="title">
     <string>智能家居系统</string>
    </property>
   </widget>
   <addaction name="menu"/>
  </widget>
  <widget class="QToolBar" name="toolBar">
   <property name="windowTitle">
    <string>toolBar</string>
   </property>
   <attribute name="toolBarArea">
    <enum>TopToolBarArea</enum>
   </attribute>
   <attribute name="toolBarBreak">
    <bool>false</bool>
   </attribute>
  </widget>
 </widget>
 <layoutdefault spacing="6" margin="11"/>
 <resources/>
 <connections/>
</ui>

main.cpp

#include "mainwindow.h"
#include <QApplication>
#include <QLabel>
#include <QPushButton>
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();

}

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    connect(socket,SIGNAL(disconnected()),this,SLOT(Disconnect_to_Server_Succeed()));
    connect(socket,SIGNAL(connected()),this,SLOT(Connect_to_Server_Succeed()));
    connect(socket,SIGNAL(readyRead()),this,SLOT(Read_from_Server_Succeed()));
    ui->PB1->setEnabled(true);
/*    ui->HS1->setEnabled(false);
    ui->HS2->setEnabled(false);
    ui->HS3->setEnabled(false);*/
    //ui->tab2->setEnabled(false);
    ui->LCDN1->setDigitCount(8);
    ui->LCDN2->setDigitCount(8);
    //ui->LCDN3->setDigitCount(8);
    //server->listen(QHostAddress::Any,10086);
    //QObject::connect(server,&QTcpServer::newConnection,this,MainWindow::server_New_Connect);
}

void MainWindow::server_New_Connect()
{
    socket=server->nextPendingConnection();
    qDebug()<<"有一个新客户端接入本机!";
    QObject::connect(socket,&QTcpSocket::readyRead,this,&MainWindow::socket_Read_Data);
}

void MainWindow::socket_Read_Data()
{
    buffer=socket->readAll();
}

MainWindow::~MainWindow()
{
    delete ui,server,socket;
}

void MainWindow::on_PB1_clicked()
{
    qs_server_ip=ui->TE1->toPlainText();
    port=ui->TE2->toPlainText().toInt();
    socket->connectToHost(qs_server_ip,port,QTcpSocket::ReadWrite);
    connect(ui->PB1,SIGNAL(clicked()),this,SLOT(on_PB1_clicked_2()));
    disconnect(ui->PB1,SIGNAL(clicked()),this,SLOT(on_PB1_clicked()));
}

void MainWindow::on_PB1_clicked_2()
{
    //ui->PGB1->setValue(0);
    //ui->PGB2->setValue(0);
    socket->disconnectFromHost();
    connect(ui->PB1,SIGNAL(clicked()),this,SLOT(on_PB1_clicked()));
    disconnect(ui->PB1,SIGNAL(clicked()),this,SLOT(on_PB1_clicked_2()));
}

void MainWindow::Connect_to_Server_Succeed()
{
/*    ui->HS1->setEnabled(true);
    ui->HS2->setEnabled(true);
    ui->HS3->setEnabled(true);*/ //三色都亮
   // ui->tab2->setEnabled(true);
    ui->PB1->setEnabled(true);
    ui->PB2->setText("断开连接");
/*    ui->LE1->setEnabled(false);
    ui->LE2->setEnabled(false);*/
}

void MainWindow::Disconnect_to_Server_Succeed()
{
    /*ui->HS1->setEnabled(false);
    ui->HS2->setEnabled(false);
    ui->HS3->setEnabled(false);*/
    //ui->tab2->setEnabled(false);
    ui->PB1->setEnabled(false);
    ui->PB1->setText("连接服务器");
/*    ui->LE1->setEnabled(true);
    ui->LE2->setEnabled(true);*/
}
/*
void MainWindow::on_PB1_clicked()
{
    ui->PB1->setText("关闭灯光");
    qba_send_data[0]=0x01;
    qba_send_data[1]=0x02;
    socket->flush();
    socket->write(qba_send_data.data());
    connect(ui->PB1,SIGNAL(clicked()),this,SLOT(on_PB1_clicked_2()));
    disconnect(ui->PB1,SIGNAL(clicked()),this,SLOT(on_PB1_clicked()));
}

void MainWindow::on_PB1_clicked_2()
{
    ui->PB1->setText("开启灯光");
    qba_send_data[0]=0x01;
    qba_send_data[1]=0x01;
    socket->flush();
    socket->write(qba_send_data.data());
    connect(ui->PB1,SIGNAL(clicked()),this,SLOT(on_PB1_clicked()));
    disconnect(ui->PB1,SIGNAL(clicked()),this,SLOT(on_PB1_clicked_2()));
}
*/
void MainWindow::Read_from_Server_Succeed()
{
    qba_read=socket->readAll();
    if(qba_read.data()[0]==0x01)
    {
        //ui->PGB1->setValue(qba_read.data()[1]);
        //ui->PGB2->setValue(qba_read.data()[2]);
        qs_rh.sprintf("%d",qba_read.data()[1]);
        ui->LCDN2->display(qs_rh);
        qs_tem.sprintf("%d",qba_read.data()[2]);
        ui->LCDN1->display(qs_tem);
        pres_int=qba_read.data()[3]<<8|qba_read.data()[4];
        pres_flo=qba_read.data()[5];
        qs_pres.sprintf("%d.%d",pres_int,pres_flo);
        ui->label7->setText(qs_pres);
        //qs_time.sprintf("%02d:%02d:%02d",qba_read.data()[6],qba_read.data()[7],qba_read.data()[8]);
        //ui->LCDN1->display(qs_time);
    }
/*
    if(qba_read.data()[0]==0x02)
    {
        mp3_count=qba_read.data()[1];
        qDebug("mp3_count=%d",mp3_count);
        qs_mp3.clear();
        qs_mp3.prepend(qba_read);
        qs_mp3=qs_mp3.mid(2);
        qDebug()<<qs_mp3;

        qsl_mp3=qs_mp3.split("|");
        ui->LW1->clear();
        ui->LW1->addItems(qsl_mp3);
    }
*/
}
/*
void MainWindow::on_PB3_clicked()
{
    ui->PB3->setText("反转电机");
    qba_send_data[0]=0x02;
    qba_send_data[1]=0x01;
    socket->flush();
    socket->write(qba_send_data.data());
    connect(ui->PB3,SIGNAL(clicked()),this,SLOT(on_PB3_clicked_2()));
    disconnect(ui->PB3,SIGNAL(clicked()),this,SLOT(on_PB3_clicked()));

}

void MainWindow::on_PB3_clicked_2()
{
    ui->PB3->setText("关闭电机");
    qba_send_data[0]=0x02;
    qba_send_data[1]=0x02;
    socket->flush();
    socket->write(qba_send_data.data());
    connect(ui->PB3,SIGNAL(clicked()),this,SLOT(on_PB3_clicked_3()));
    disconnect(ui->PB3,SIGNAL(clicked()),this,SLOT(on_PB3_clicked_2()));
}

void MainWindow::on_PB3_clicked_3()
{
    ui->PB3->setText("开启电机");
    qba_send_data[0]=0x02;
    qba_send_data[1]=0x03;
    socket->flush();
    socket->write(qba_send_data.data());
    connect(ui->PB3,SIGNAL(clicked()),this,SLOT(on_PB3_clicked()));
    disconnect(ui->PB3,SIGNAL(clicked()),this,SLOT(on_PB3_clicked_3()));
}
*/
/*
void MainWindow::on_HS1_actionTriggered(int action)
{
    qba_send_data[0]=0x03;
    qba_send_data[1]=ui->HS1->value();
    qba_send_data[2]=ui->HS2->value();
    qba_send_data[3]=ui->HS3->value();
    socket->flush();
    socket->write(qba_send_data.data());
}

void MainWindow::on_HS2_actionTriggered(int action)
{
    qba_send_data[0]=0x03;
    qba_send_data[1]=ui->HS1->value();
    qba_send_data[2]=ui->HS2->value();
    qba_send_data[3]=ui->HS3->value();
    socket->flush();
    socket->write(qba_send_data.data());
}

void MainWindow::on_HS3_actionTriggered(int action)
{
    qba_send_data[0]=0x03;
    qba_send_data[1]=ui->HS1->value();
    qba_send_data[2]=ui->HS2->value();
    qba_send_data[3]=ui->HS3->value();
    socket->flush();
    socket->write(qba_send_data.data());
}
*/

void MainWindow::on_tabWidget_tabBarClicked(int index)
//打开歌曲播放界面
{
    if(index==1)
    {
        qba_send_data[0]=0x04;
        socket->flush();
        socket->write(qba_send_data.data());
    }

}

void MainWindow::on_PB4_clicked()
//打开歌曲播放界面
{
    qba_send_data[0]=0x04;
    socket->flush();
    socket->write(qba_send_data.data());
}
/*
void MainWindow::on_LW1_doubleClicked(const QModelIndex &index)
{
    tw1_index=ui->LW1->currentRow();
    qba_send_data[0]=0x05;
    qba_send_data[1]=tw1_index;
    socket->flush();
    socket->write(qba_send_data.data());
}

void MainWindow::on_PB5_clicked()
{
    tw1_index=ui->LW1->currentRow();
    qba_send_data[0]=0x05;
    qba_send_data[1]=tw1_index;
    socket->flush();
    socket->write(qba_send_data.data());
}

void MainWindow::on_PB6_clicked()
{
    qdt=QDateTime::currentDateTime();
    qs_sys_time=qdt.time().toString();
    qs_sys_date=qdt.date().toString();
    qs_sys_time.insert(0,' ');
    qba_send_data=qs_sys_time.toLatin1();
    qba_send_data[0]=0x06;
    socket->flush();
    socket->write(qba_send_data.data());
}

void MainWindow::on_pushButton_clicked()
{
    qtimer->start(1000);
    connect(qtimer,SIGNAL(timeout()),this,SLOT(Timer_Update()));
}

void MainWindow::Timer_Update()
{
    int val;
    val=ui->HS4->value();
    val++;
    ui->HS4->setValue(val);
    //ui->HS4->set
}

void MainWindow::on_PB7_clicked()
{
    qba_send_data[0]=0x07;
    socket->flush();
    socket->write(qba_send_data.data());
}
*/

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QSerialPort>
#include <QSerialPortInfo>
#include <QtCore>
#include <QApplication>
#include <QDebug>
#include <QtSql/QSqlDatabase>
#include <QSqlQuery>
#include <QTcpSocket>
#include <QTcpServer>
#include <QTime>
#include <QTimer>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    void server_New_Connect();
    void socket_Read_Data();
    ~MainWindow();

private slots:
    void on_PB1_clicked();
    void on_PB1_clicked_2();
    //void on_PB2_clicked();
    //void on_PB2_clicked_2();
    void Connect_to_Server_Succeed();
    void Disconnect_to_Server_Succeed();
    void Read_from_Server_Succeed();
    void on_tabWidget_tabBarClicked(int index);
    //void on_PB3_clicked();
    //void on_PB3_clicked_2();
    //void on_PB3_clicked_3();
    //void on_HS1_actionTriggered(int action);
    //void on_HS2_actionTriggered(int action);
    //void on_HS3_actionTriggered(int action);
    //void on_LW1_doubleClicked(const QModelIndex &index);
    void on_PB4_clicked();
    //void on_PB5_clicked();
    //void on_PB6_clicked();
    //void on_pushButton_clicked();
    //void Timer_Update();
    //void on_PB7_clicked();

private:

    QTcpServer *server = new QTcpServer(this);
    QTcpSocket *socket = new QTcpSocket(this);
    QString qs_server_ip,buffer,qs_pres,qs_read,qs_mp3,qs_time,qs_sys_time,qs_sys_date,qs_rh,qs_tem;
    QStringList qsl_mp3;
    QByteArray qba_send_data,qba_read,qba_sys_time;
    int port=0,pres_int=0,pres_flo=0,mp3_count=0,tw1_index;
    QDateTime qdt=QDateTime::currentDateTime();
    QTimer *qtimer=new QTimer(this);

    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H

猜你喜欢

转载自blog.csdn.net/weifuliu/article/details/80994202