Difference between revisions of "Matrix Starter Kit with NanoPi"
(→连接使用) |
(→编译程序及使用) |
||
Line 245: | Line 245: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | + | 然后启动NanoPi,在Debian的shell终端中执行如下命令运行模块Matrix-temperature_and_humidity_monitoring的测试程序 <br> | |
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> | ||
− | $ matrix- | + | $ matrix-temperature_and_humidity_monitoring |
</syntaxhighlight> | </syntaxhighlight> | ||
代码展示: | 代码展示: | ||
Line 255: | Line 255: | ||
#include <stdlib.h> | #include <stdlib.h> | ||
#include <signal.h> | #include <signal.h> | ||
+ | #include "common.h" | ||
#include "libfahw.h" | #include "libfahw.h" | ||
− | #define | + | #define LCD_LENGTH 16 |
− | #define | + | #define SET_TEMP 37000 |
− | + | ||
− | + | int DHT11pin = GPIO_PIN1; | |
− | + | int LEDpin = GPIO_PIN2; | |
− | + | int BUZZERpin = PWM_PIN1; | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | int | + | int devFD_LCD; |
+ | int devFD_DHT11; | ||
int pwm_HZ = 1000; | int pwm_HZ = 1000; | ||
int pwm_duty = 500; | int pwm_duty = 500; | ||
− | |||
− | |||
− | |||
int stop = 0; | int stop = 0; | ||
Line 283: | Line 276: | ||
} | } | ||
− | int | + | int THmonitoring_init() { |
− | + | //LED | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | //LED | + | |
if ((exportGPIOPin(LEDpin)) == -1) { | if ((exportGPIOPin(LEDpin)) == -1) { | ||
printf("exportGPIOPin(%d) failed\n", LEDpin); | printf("exportGPIOPin(%d) failed\n", LEDpin); | ||
return -1; | return -1; | ||
} | } | ||
− | + | if ((setGPIODirection(LEDpin, GPIO_OUT)) == -1) { | |
printf("setGPIODirection(%d) failed\n", LEDpin); | printf("setGPIODirection(%d) failed\n", LEDpin); | ||
+ | return -1; | ||
+ | } | ||
+ | //LCD | ||
+ | if ((devFD_LCD = LCD1602Init()) == -1) { | ||
+ | printf("Fail to init LCD1602\n"); | ||
+ | return -1; | ||
+ | } | ||
+ | if (LCD1602Clear(devFD_LCD) == -1) { | ||
+ | printf("Fail to Clear\n"); | ||
return -1; | return -1; | ||
} | } | ||
+ | //dht11 | ||
+ | if ((devFD_DHT11 = dht11Init(DHT11pin)) == -1) { | ||
+ | printf("Fail to init dht11\n"); | ||
+ | return -1; | ||
+ | } | ||
return 0; | return 0; | ||
} | } | ||
− | void | + | void THmonitoring_deinit() { |
− | + | ||
PWMStop(BUZZERpin); | PWMStop(BUZZERpin); | ||
− | unexportGPIOPin(LEDpin); | + | unexportGPIOPin(LEDpin); |
+ | LCD1602DeInit(devFD_LCD); | ||
+ | dht11DeInit(devFD_DHT11); | ||
} | } | ||
− | int | + | int THmonitoring_alart() { |
if(setGPIOValue(LEDpin, GPIO_HIGH) == -1) { | if(setGPIOValue(LEDpin, GPIO_HIGH) == -1) { | ||
printf("setGPIOValue(%d) failed\n", LEDpin); | printf("setGPIOValue(%d) failed\n", LEDpin); | ||
return -1; | return -1; | ||
} | } | ||
− | + | if(PWMPlay(BUZZERpin, pwm_HZ, pwm_duty) == -1) { | |
− | if (PWMPlay(BUZZERpin, pwm_HZ, pwm_duty) == -1) { | + | printf("Fail to output PWM\n"); |
− | + | return -1; | |
− | + | ||
} | } | ||
return 0; | return 0; | ||
} | } | ||
− | int | + | int THmonitoring_noalart() { |
− | + | if(setGPIOValue(LEDpin, GPIO_LOW) == -1) { | |
− | + | printf("setGPIOValue(%d) failed\n", LEDpin); | |
− | + | return -1; | |
− | + | } | |
− | + | if(PWMStop(BUZZERpin) == -1) { | |
− | + | printf("Fail to stop PWM\n"); | |
− | + | return -1; | |
− | + | } | |
− | + | return 0; | |
} | } | ||
int main(int argc, char ** argv) { | int main(int argc, char ** argv) { | ||
− | int | + | int dhtTemp = 0; |
− | char | + | int dhtHdty = 0; |
− | + | int INT_BUF = 0; | |
+ | char* buf = (char *) malloc(LCD_LENGTH); | ||
+ | memset(buf, 0, LCD_LENGTH); | ||
+ | char* line1 = " hum: "; | ||
+ | char* line2 = " temp: "; | ||
+ | THmonitoring_init(); | ||
+ | if (LCD1602DispLines(devFD_LCD, line1, line2) == -1) { | ||
+ | printf("Fail to Display String\n"); | ||
+ | } | ||
signal(SIGINT, Stop); | signal(SIGINT, Stop); | ||
printf("you can press ctrl+c to stop\n"); | printf("you can press ctrl+c to stop\n"); | ||
while(!stop) { | while(!stop) { | ||
− | + | if ((dht11Read(DHT_HUMIDITY, &dhtHdty)) == -1) { | |
− | if( | + | dhtHdty = 0; |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
} | } | ||
− | if( | + | if ((dht11Read(DHT_TEMP, &dhtTemp)) == -1) { |
− | + | dhtTemp = 0; | |
− | + | ||
} | } | ||
+ | |||
+ | if (dhtHdty == 0) { | ||
+ | sprintf(buf, "%s", "Er"); | ||
+ | } else { | ||
+ | INT_BUF = dhtHdty / 1000; | ||
+ | sprintf(buf, "%d", INT_BUF); | ||
+ | } | ||
+ | LCD1602DispStr(devFD_LCD, 6, 0, buf); | ||
+ | |||
+ | if (dhtTemp == 0) { | ||
+ | sprintf(buf, "%s", "Er"); | ||
+ | } else { | ||
+ | INT_BUF = dhtTemp / 1000; | ||
+ | sprintf(buf, "%d", INT_BUF); | ||
+ | } | ||
+ | LCD1602DispStr(devFD_LCD, 7, 1, buf); | ||
+ | |||
+ | if (dhtTemp >= SET_TEMP) { | ||
+ | if (THmonitoring_alart() != 0) { | ||
+ | printf("fail to alart!\n"); | ||
+ | break; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | sleep(1); | ||
+ | THmonitoring_noalart(); | ||
+ | sleep(1); | ||
} | } | ||
− | + | free(buf); | |
+ | THmonitoring_deinit(); | ||
return 0; | return 0; | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> |
Revision as of 09:34, 19 September 2015
Contents
1 NanoPi门铃系统
1.1 NanoPi门铃系统介绍
- 使用Matrix Starter Kit套件的蜂鸣器、LED、声音传感器做一个门铃系统,当按键按下时,蜂鸣器会响起来,以及LED会闪烁;当声音传感器识别到声音时,蜂鸣器也会响起来。
- 如果需要更好的效果,可以发挥自己的想象力,按键在按下来的时候,蜂鸣器随机播放一首音乐。
1.2 Matrix配件
- 这里需要使用到Matrix的配件为LED、声音传感器、无源蜂鸣器,均可参考以下链接了解Matrix配件的使用:
NanoPi门铃系统 | NanoPi |
Matrix-LED | [1] |
Matrix-Sound Sensor | [2] |
Matrix-Buzzer | [3] |
Matrix-Button | [4] |
1.3 下载Matrix源码
Matrix配件相关的代码是完全开源的,统一由一个仓库进行管理:git://github.com/friendlyarm/matrix.git
该仓库里不同的分支代表着Matrix配件所支持的不同开发板。
- nanopi分支包含了Matrix对NanoPi的支持;
- tiny4412分支包含了Matrix对Tiny4412的支持;
- raspberrypi分支包含了Matrix对RaspberryPi的支持;
1.4 连接使用
NanoPi门铃系统 | NanoPi |
Matrix-LED | PIN12 |
Matrix - Sound Sensor | PIN7 |
Matrix - Sound Sensor | PIN22 |
Matrix - Sound Sensor | PIN11 |
1.5 编译程序及使用
进入Matrix代码仓库,切换到nanopi分支
$ cd matrix $ git checkout nanopi
编译Matrix配件代码
$ make CROSS_COMPILE=arm-linux- clean $ make CROSS_COMPILE=arm-linux- $ make CROSS_COMPILE=arm-linux- install
注意:请确保你的主机PC当前使用的交叉编译器为NanoPi-Debian配套的arm-linux-gcc-4.4.3。
编译出来的库文件位于install/lib目录下,而测试程序则位于install/usr/bin目录下,模块Matrix-doorbell对应的测试程序为matrix-doorbell。
拷贝库文件和测试程序到NanoPi的文件系统上
$ cp install/usr/bin/* nanopi_rootfs/usr/bin/ $ cp install/lib/* nanopi_rootfs/lib/ -d
然后启动NanoPi,在Debian的shell终端中执行如下命令运行模块Matrixdoorbell的测试程序
$ matrix-doorbell
代码展示:
#include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <signal.h> #include "libfahw.h" #define SOUND 0 #define BUTTON 1 static struct sensor sensor_dev[2] = { { GPIO_PIN1, IRQ_TYPE_EDGE_BOTH, }, { GPIO_PIN2, IRQ_TYPE_EDGE_FALLING, } }; int devFD = -1; int pwm_HZ = 1000; int pwm_duty = 500; int BUZZERpin = PWM_PIN1; int LEDpin = GPIO_PIN3; int stop = 0; void Stop(int signo) { stop = 1; } int doorbell_init() { //sensor init if ((devFD = sensorInit(sensor_dev, ARRAY_SIZE(sensor_dev))) == -1) { printf("Fail to init sensor sound\n"); return -1; } //LED init if ((exportGPIOPin(LEDpin)) == -1) { printf("exportGPIOPin(%d) failed\n", LEDpin); return -1; } if ((setGPIODirection(LEDpin, GPIO_OUT)) == -1) { printf("setGPIODirection(%d) failed\n", LEDpin); return -1; } return 0; } void doorbell_deinit() { sensorDeinit(devFD); PWMStop(BUZZERpin); unexportGPIOPin(LEDpin); } int doorbell_open() { if(setGPIOValue(LEDpin, GPIO_HIGH) == -1) { printf("setGPIOValue(%d) failed\n", LEDpin); return -1; } if (PWMPlay(BUZZERpin, pwm_HZ, pwm_duty) == -1) { printf("Fail to output PWM\n"); return -1; } return 0; } int doorbell_close() { if(setGPIOValue(LEDpin, GPIO_LOW) == -1) { printf("setGPIOValue(%d) failed\n", LEDpin); return -1; } if(PWMStop(BUZZERpin) == -1) { printf("Fail to stop PWM\n"); return -1; } return 0; } int main(int argc, char ** argv) { int sensor_state; char value[ARRAY_SIZE(sensor_dev)]; doorbell_init(); signal(SIGINT, Stop); printf("you can press ctrl+c to stop\n"); while(!stop) { sensor_state = sensorRead(devFD, value, ARRAY_SIZE(sensor_dev)); if(value[SOUND] || value[BUTTON]) { if(doorbell_open() == -1) { printf("fail to open doorbell\n"); return -1; } value[SOUND] = 0; value[BUTTON] = 0; usleep(1000*500); } if(doorbell_close() == -1) { printf("fail to close doorbell\n"); return -1; } } doorbell_deinit(); return 0; }
2 温室报警系统
2.1 温室报警系统介绍
- 使用Matrix Starter Kit套件的无源蜂鸣器、LED、温湿度传感器、lcd1602做一个温室报警系统。
- 温室报警系统,使用NanoPi作为中控,接上温湿度传感器检测温室的温湿度变化,这里介绍的温室报警系统每隔两秒读取一次温湿度传感器的温湿度并显示在LCD1602上,如果温度超过预定设置的值,蜂鸣器会响和LED会亮一秒。
2.2 Matrix配件
- 这里需要使用到Matrix的配件为LED、温湿度传感器、无源蜂鸣器,I2C LCD1602,均可参考以下链接了解Matrix配件的使用:
NanoPi温室报警系统 | NanoPi |
LED | [5] |
Matrix - Temperature and Humidity Sensor | [6] |
Matrix - Buzzer | [7] |
Matrix - I2C LCD1602 | [8] |
2.3 下载Matrix源码
Matrix配件相关的代码是完全开源的,统一由一个仓库进行管理:git://github.com/friendlyarm/matrix.git
该仓库里不同的分支代表着Matrix配件所支持的不同开发板。
- nanopi分支包含了Matrix对NanoPi的支持;
- tiny4412分支包含了Matrix对Tiny4412的支持;
- raspberrypi分支包含了Matrix对RaspberryPi的支持;
2.4 连接使用
NanoPi门铃系统 | NanoPi |
LED | PIN12 |
声音传感器 | PIN7 |
无源蜂鸣器 | PIN22 |
按键 | PIN11 |
2.5 编译程序及使用
进入Matrix代码仓库,切换到nanopi分支
$ cd matrix $ git checkout nanopi
编译Matrix配件代码
$ make CROSS_COMPILE=arm-linux- clean $ make CROSS_COMPILE=arm-linux- $ make CROSS_COMPILE=arm-linux- install
注意:请确保你的主机PC当前使用的交叉编译器为NanoPi-Debian配套的arm-linux-gcc-4.4.3。
编译出来的库文件位于install/lib目录下,而测试程序则位于install/usr/bin目录下,模块Matrix-doorbell对应的测试程序为matrix-doorbell。
拷贝库文件和测试程序到NanoPi的文件系统上
$ cp install/usr/bin/* nanopi_rootfs/usr/bin/ $ cp install/lib/* nanopi_rootfs/lib/ -d
然后启动NanoPi,在Debian的shell终端中执行如下命令运行模块Matrix-temperature_and_humidity_monitoring的测试程序
$ matrix-temperature_and_humidity_monitoring
代码展示:
#include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <signal.h> #include "common.h" #include "libfahw.h" #define LCD_LENGTH 16 #define SET_TEMP 37000 int DHT11pin = GPIO_PIN1; int LEDpin = GPIO_PIN2; int BUZZERpin = PWM_PIN1; int devFD_LCD; int devFD_DHT11; int pwm_HZ = 1000; int pwm_duty = 500; int stop = 0; void Stop(int signo) { stop = 1; } int THmonitoring_init() { //LED if ((exportGPIOPin(LEDpin)) == -1) { printf("exportGPIOPin(%d) failed\n", LEDpin); return -1; } if ((setGPIODirection(LEDpin, GPIO_OUT)) == -1) { printf("setGPIODirection(%d) failed\n", LEDpin); return -1; } //LCD if ((devFD_LCD = LCD1602Init()) == -1) { printf("Fail to init LCD1602\n"); return -1; } if (LCD1602Clear(devFD_LCD) == -1) { printf("Fail to Clear\n"); return -1; } //dht11 if ((devFD_DHT11 = dht11Init(DHT11pin)) == -1) { printf("Fail to init dht11\n"); return -1; } return 0; } void THmonitoring_deinit() { PWMStop(BUZZERpin); unexportGPIOPin(LEDpin); LCD1602DeInit(devFD_LCD); dht11DeInit(devFD_DHT11); } int THmonitoring_alart() { if(setGPIOValue(LEDpin, GPIO_HIGH) == -1) { printf("setGPIOValue(%d) failed\n", LEDpin); return -1; } if(PWMPlay(BUZZERpin, pwm_HZ, pwm_duty) == -1) { printf("Fail to output PWM\n"); return -1; } return 0; } int THmonitoring_noalart() { if(setGPIOValue(LEDpin, GPIO_LOW) == -1) { printf("setGPIOValue(%d) failed\n", LEDpin); return -1; } if(PWMStop(BUZZERpin) == -1) { printf("Fail to stop PWM\n"); return -1; } return 0; } int main(int argc, char ** argv) { int dhtTemp = 0; int dhtHdty = 0; int INT_BUF = 0; char* buf = (char *) malloc(LCD_LENGTH); memset(buf, 0, LCD_LENGTH); char* line1 = " hum: "; char* line2 = " temp: "; THmonitoring_init(); if (LCD1602DispLines(devFD_LCD, line1, line2) == -1) { printf("Fail to Display String\n"); } signal(SIGINT, Stop); printf("you can press ctrl+c to stop\n"); while(!stop) { if ((dht11Read(DHT_HUMIDITY, &dhtHdty)) == -1) { dhtHdty = 0; } if ((dht11Read(DHT_TEMP, &dhtTemp)) == -1) { dhtTemp = 0; } if (dhtHdty == 0) { sprintf(buf, "%s", "Er"); } else { INT_BUF = dhtHdty / 1000; sprintf(buf, "%d", INT_BUF); } LCD1602DispStr(devFD_LCD, 6, 0, buf); if (dhtTemp == 0) { sprintf(buf, "%s", "Er"); } else { INT_BUF = dhtTemp / 1000; sprintf(buf, "%d", INT_BUF); } LCD1602DispStr(devFD_LCD, 7, 1, buf); if (dhtTemp >= SET_TEMP) { if (THmonitoring_alart() != 0) { printf("fail to alart!\n"); break; } } sleep(1); THmonitoring_noalart(); sleep(1); } free(buf); THmonitoring_deinit(); return 0; }