Reachy 发表于 2016-6-1 15:27:07

Cubieboard2防火防盗家居设计

最近本人用cubiebaord的开发板做了毕业设计,搞了很多东西,

整个设计的硬件:
Cubiebaord2及DVK扩展板MQ-2 烟雾传感器 HC-SR501人体热释红外传感器
18B20温度传感器 数码管模块 蜂鸣器 led灯

设计系统的整体

各个模块设计
HC-SR501红外模块

功能:感应到人在检测范围内就会报警,usb摄像头也会对现场进行拍照

代码#!/bin/bash
videodev="/dev/video0"
redled=4
sensor=5
#second(s) between each check
waitperiod=1
photorepeattimes=3
resolution="800x600"
directory="/tmp"
init_led ()
{
      for i in $redled
      do
                if [ ! -d /sys/class/gpio/gpio4_pd0 ]
                then
                        echo $i > /sys/class/gpio/export
                fi
                        echo "out" > /sys/class/gpio/gpio4_pd0/direction
                        echo 0 > /sys/class/gpio/gpio4_pd0/value
      done
}

init_sensor ()
{
      for i in $sensor
      do
                if [ ! -d /sys/class/gpio/gpio5_pd1 ]
                then
                        echo $i > /sys/class/gpio/export
                fi
                        echo "in" > /sys/class/gpio/gpio5_pd1/direction
      done
}

init_led_sensor ()
{
      init_led
      init_sensor
}

cleanup ()
{
      init_led_sensor
      for i in $redled $sensor
      do
                if [ -d /sys/class/gpio/gpio$i ]
                then
                        echo $i > /sys/class/gpio/unexport
               fi
      done
      exit 0
}

capture_photo()
{
      for (( c=0; c<$photorepeattimes; c++ ))
      do
                filename=$directory/$(date -u +"%d%m%Y_%H%M-%S").jpg
                fswebcam -d $videodev --timestamp "%Y-%m-%d %H:%M:%S (%Z)" -r $r
esolution $filename
                sleep 1
      done
}
check_activity ()
{
      sensor_status=`cat /sys/class/gpio/gpio5_pd1/value`
      echo $sensor_status > /sys/class/gpio/gpio4_pd0/value
      if [ "$sensor_status" -eq 1 ]
                then
                        capture_photo
               fi
      }
init_led_sensor
trap cleanup INT TERM EXIT
while :
do
      check_activity
      sleep $waitperiod
done



18b20温度传感器
功能:实时检测当前温度,超过预设的温度就蜂鸣器响来报警
代码:#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <signal.h>
#include <pthread.h>
#include "gpio_lib.h"

#define LSBFIRST 0
#define MSBFIRST 1
#defineDISPBUF_LEN 8

typedef unsigned char byte;

change it in your case.
const char* DS18B20_DEVICE="/sys/bus/w1/devices/28-00000626fc86/w1_slave";

/*
* 74HC595 relative stuff
*/

unsigned int latchPin = SUNXI_GPB(11);

unsigned int clockPin = SUNXI_GPB(13);

unsigned int dataPin = SUNXI_GPH(14);

unsigned int zzzzPin = SUNXI_GPG(3);

/*
* Display relative stuff
*/

unsigned int digit_tab[] = { 0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92,
                        0x82, 0xf8, 0x80, 0x90};

unsigned int digitdot_tab[] = { 0xc0&0x7f, 0xf9&0x7f, 0xa4&0x7f,
                        0xb0&0x7f, 0x99&0x7f, 0x92&0x7f, 0x82&0x7f,
                        0xf8&0x7f, 0x80&0x7f, 0x90&0x7f};
unsigned int symbol_tab[]={ 0xff, 0x7f, 0xbf};
static char dispbuf;


/**
* Set Cubieboard's GPIO port-D pin I/O mode: INPUT/OUTPUT
*/
void pinMode(unsigned int pin, unsigned int io_mode)
{
    if (SETUP_OK != sunxi_gpio_set_cfgpin(pin, io_mode))
    {
      printf("Failed to config GPIO pin\n");
    }
}

/**
* Set Cubieboard's GPIO port-D pin value(LOW/HIGH)
*/
void digitalWrite(int pin, int hl)
{
    if (sunxi_gpio_output(pin, hl))
    {
      printf("Failed to set GPIO pin value\n");
    }
}

void shiftOut(unsigned int dataPin, unsigned int clockPin, int bitOrder, byte val)
{
    byte i;
    for (i = 0; i < 8; i++)
    {
      if (bitOrder == LSBFIRST)
            digitalWrite(dataPin, ! !(val & (1 << i)));
      else
            digitalWrite(dataPin, ! !(val & (1 << (7 - i))));

      digitalWrite(clockPin, HIGH);
      digitalWrite(clockPin, LOW);
    }
}

/**
* Initialize the GPIO & relative pins
*/
void init_gpio()
{
    if (SETUP_OK != sunxi_gpio_init())
    {
      printf("Failed to initialize GPIO\n");
    }
    pinMode(latchPin, OUTPUT);
    pinMode(clockPin, OUTPUT);
    pinMode(dataPin, OUTPUT);
    pinMode(zzzzPin, OUTPUT);
}

/**
* Get current temperature from the w1-thermal device
*/
void get_temperature(char* tempbuf, int len)
{
    FILE* fp=fopen(DS18B20_DEVICE,"r");
    char* line=NULL;
    char* temperature_tok=NULL;
    int temperature=0;
   
    int n;
    if(!fp){
      fprintf(stderr,"Failed to open device(%s) file!\n", DS18B20_DEVICE);
      return;
    }

    // skip the first line
    getline(&line, &n, fp);
    free(line);
    line=NULL;

   
    getline(&line, &n, fp);
    strtok(line,"=");
    temperature_tok=strtok(NULL,"\n");
   
    strncpy(tempbuf, temperature_tok, len);

    free(line);
    fclose(fp);
}

/**
* Thread of filling the time infomation into display buffer
*/
void* time_to_dispbuf()
{
    time_t timep;
    struct tm *p;
    char timebuf;
    int interval=1; // in seconds

    while(1){
      // get localtime
      time(&timep);
      p = localtime(&timep);
      sprintf(timebuf, "%02d%02d", p->tm_hour, p->tm_min);

      dispbuf=digit_tab - '0'];
      dispbuf=digitdot_tab - '0'];
      dispbuf=digit_tab - '0'];
      dispbuf=digit_tab - '0'];
      dispbuf=symbol_tab; // '-'

      sleep(interval);
    }
}

/**
* Thread of filling the temperature into display buffer
*/
void* temp_to_dispbuf()
{
    char tempbuf;
    int interval=5;
    float tmp = 0;
   
    while(1){
      get_temperature(tempbuf, sizeof tempbuf);

      dispbuf=digit_tab-'0'];
      dispbuf=digitdot_tab-'0'];
      dispbuf=digit_tab-'0'];
      tmp = (tempbuf-'0')*10 + (tempbuf-'0');

            printf("temperature = %f -------------\n", tmp);
        if (tmp > 28)
            {
                printf("-----FIRE----FIRE----FIRE----\n");
              digitalWrite(zzzzPin, LOW);
           }
        else
        {
                digitalWrite(zzzzPin, HIGH);
        }

      sleep(interval);
    }
   
}

int main(int argc, char **argv)
{
    pthread_t get_time_thread, get_temp_thread;
    void * thread_ret;

    init_gpio();

    pthread_create( &get_time_thread, NULL, time_to_dispbuf, NULL);
    pthread_create( &get_temp_thread, NULL, temp_to_dispbuf, NULL);

    while (1)
    {

      int i;
      for(i=0;i<DISPBUF_LEN;i++){
            digitalWrite(latchPin, 0);
            shiftOut(dataPin, clockPin, MSBFIRST, 1<<i);
            shiftOut(dataPin, clockPin, MSBFIRST, dispbuf);
            digitalWrite(latchPin, 1);
            usleep(2000);
      }
      
    }

    pthread_join(get_time_thread,&thread_ret);
    pthread_join(get_temp_thread,&thread_ret);
    return 0;
}

Reachy 发表于 2016-6-1 15:31:38

继续下面内容
MQ-2模块

功能:
检测到煤气,天然气,甲烷等气体超过预设的警报值就进行声报警
代码#include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <termios.h>
    #include <errno.h>
    #include <sys/time.h>
    #include <string.h>
    #define TRUE 1
    #define FALSE -1
    #define FREZZ_ON "echo 0 > /sys/class/gpio/gpio10_pd17/value"
    #define FREZZ_OFF "echo 1 > /sys/class/gpio/gpio10_pd17/value"
    int speed_arr[] = {B115200, B38400, B19200, B9600, B4800, B2400, B1200, B300,
    B38400, B19200, B9600, B4800, B2400, B1200, B300, };
    int name_arr[] = {115200, 38400, 19200, 9600, 4800, 2400, 1200, 300,
    38400, 19200, 9600, 4800, 2400, 1200, 300, };
    void set_speed(int fd, int speed)
    {
    int i;
    int status;
    struct termios Opt;
    tcgetattr(fd,&Opt);
    for (i= 0;i<sizeof(speed_arr)/sizeof(int);i++)
    { if(speed == name_arr)
    {
    tcflush(fd, TCIOFLUSH);
    cfsetispeed(&Opt, speed_arr);
    cfsetospeed(&Opt, speed_arr);
    status = tcsetattr(fd, TCSANOW, &Opt);
    if(status != 0)
    perror("tcsetattr fd1");
    return;
    } tcflush(fd,TCIOFLUSH);
    }
    }
    int set_Parity(int fd,int databits,int stopbits,int parity)
    {
    struct termios options;
    if( tcgetattr( fd,&options)!= 0)
    {
    perror("SetupSerial 1");
    return(FALSE);
    }
    options.c_cflag &= ~CSIZE;
    switch(databits)
    {
    case 7:
    options.c_cflag |= CS7;
    break;
    case 8:
    options.c_cflag |= CS8;
    break;
    default:
    fprintf(stderr,"Unsupported data size\n");
    return (FALSE);
    }
    switch(parity)
    {
    case 'n':
    case 'N':
    options.c_cflag &= ~PARENB; /* Clear parity enable */
    options.c_iflag &= ~INPCK; /* Enable parity checking */
    options.c_iflag &= ~(ICRNL|IGNCR);
    options.c_lflag &= ~(ICANON );
    break;
    case 'o':
    case 'O':
    options.c_cflag |= (PARODD | PARENB);
    options.c_iflag |= INPCK; /* Disnable parity checking */
    break;
    case 'e':
    case 'E':
    options.c_cflag |= PARENB; /* Enable parity */
    options.c_cflag &= ~PARODD;
    options.c_iflag |= INPCK; /* Disnable parity checking */
    break;
    case 'S':
    case 's': /*as no parity*/
    options.c_cflag &= ~PARENB;
    options.c_cflag &= ~CSTOPB;
    break;
    default:
    fprintf(stderr,"Unsupported parity\n");
    return (FALSE);
    }
    switch(stopbits)
    {
    case 1:
    options.c_cflag &= ~CSTOPB;
    break;
    case 2:
    options.c_cflag |= CSTOPB;
    break;
    default:
    fprintf(stderr,"Unsupported stop bits\n");
    return (FALSE);
    }
    /* Set input parity option */
    if(parity != 'n')
    options.c_iflag |= INPCK;
    options.c_cc = 150; // 15 seconds
    options.c_cc = 0;
    tcflush(fd,TCIFLUSH); /* Update the options and do it NOW */
    if(tcsetattr(fd,TCSANOW,&options) != 0)
    {
    perror("SetupSerial 3");
    return (FALSE);
    }
    return (TRUE);
    }
    int main(int argc, char **argv)
    {
    int fd;
    int nread; int nwrite;
    int n=0; int i=0;
    int buffer;

    char devname_head = "/dev/";
    char dev_name;
    #if 1
    if(argc < 2)
    { printf("Please input './test_uart ttySx'\n");
    exit(1);
    }
    else
    {
    strcpy(dev_name, devname_head); strcat(dev_name, argv
    ); }
    fd = open(dev_name, O_RDWR);
    if(fd < 0)
    {
    perror("error to open /dev/ttySx\n");
    exit(1);
    }
    #endif
    if (fd > 0)
    {
    set_speed(fd,9600);
    }
    else
    {
    printf("Can't Open Serial Port!\n");
    exit(0);
    }
    if (set_Parity(fd,8,1,'N') == FALSE)
    {
    printf("Set Parity Error\n");
    exit(1);
    }
    printf("\nWelcome to gps_test\n\n");
    system(FREZZ_OFF);
    memset(buffer,0,sizeof(buffer));
    while(1)
    {
    nread = read(fd,&buffer,200);

    if(nread < 0)
    {
    printf("read error\n");
    }
    int a=atoi(buffer);
    printf("%d\n",a);
    if(a > 47)
    {
    system(FREZZ_ON);

    }
    else{
    system(FREZZ_OFF);
    }

    memset(buffer,0,sizeof(buffer));

    }
    }

Reachy 发表于 2016-6-1 15:48:24

本帖最后由 Reachy 于 2016-6-1 15:49 编辑

18b20温度传感器是w1总线的传感器,需要在底层驱动开启编译成模块然后加载到开发板就可以用了makeARCH=armmenuconfig


选好之后退出,进入1-wire slaves ---> 将Thermal family implementation选上

最后保存退出

然后在Ubuntu终端执行下面指令$ make ARCH=arm CROSS_COMPILE=arm-linux-gnueabi- -j4 uImage若编译内核没有报错则进入需要编译的驱动目录cd /driver/spi编译没有出错则就在/lib/modules/3.13.0-32-genric/kernel/drivers下有模块生成 W1

在w1下就有驱动模块了


Reachy 发表于 2016-6-1 15:54:46

摄像头除了拍照,如果搭建了局部网络还可以通过手机进行网络访问usb摄像头,了解现场的情况

bill 发表于 2016-6-2 14:39:07

不错不错

wty123 发表于 2016-7-26 10:40:42

页: [1]
查看完整版本: Cubieboard2防火防盗家居设计