1. 程式人生 > >樹莓派DHT11溫溼度感測器+BMP280大氣氣壓感測器

樹莓派DHT11溫溼度感測器+BMP280大氣氣壓感測器

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-&