聚丰项目 > 基于Intel Edision的Eclipse实现门禁确认系统
当有人按门铃时,发出声音提示有人来访;LCD上显示一条来访信息;声音响起后,将有30s的时间输入密码确认,这里使用浏览器输入密码。
anger0925
分享anger0925
团队成员
常旭磊 创客
硬件搭建
1、Intel edison开发板和Arduino breakout套件;
2、Grove按键
3、Grove蜂鸣器
4、Grove RGB LCD显示器
组装:
1、Grove按键连接到D6上;
2、Grove蜂鸣器连接在D5上;
3、Grove RGB LCD显示器接在任意一个I2C接口上。
准备工作
1)要使用web服务器,需要使用 Crow* Web 微框架,以提供易于使用,但功能强大的 Web 服务器。 Crow 库要求 libboost 软件包安装于英特尔® Edison 开发板,并将所需的包含和库文件添加至 Eclipse* Cross G++ Compiler 和 Cross G++ Linker。
开发板上已经安装了bootst,通过opkg info boost查看版本号。
opkg install boost升级,
已经是最新版本。
接下来需要将库和包含文件从开发板复制到运行 Eclipse 的计算机,以便 Cross G++ Compiler 和 Cross G++ Linker 能够找到它们。
/usr/include下的boost目录拷贝到iss-iot-win\devkit-x86\sysroots\i586-poky-linux\usr\include下
/usr/lib下的libboost前缀的库全部拷贝到iss-iot-win\devkit-x86\sysroots\i586-poky-linux\usr\lib下。
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <unistd.h>
#include <sstream>
#include <thread>
#include <ctime>
#include <chrono>
#include <string>
#include <jhd1313m1.h>
#include "biss0001.hpp"
#include "../lib/restclient-cpp/include/restclient-cpp/restclient.h"
#include "datastore.h"
#include "mqtt.h"
#include "../lib/crow/crow_all.h"
#include "../src/html.h"
#include "../src/css.h"
#include <buzzer.hpp>
#include <grove.hpp>
//#include <jhd1313m1.hpp>
using namespace std;
bool countdownStarted = false;
bool disarmed = false;
bool alarmTriggered = false;
chrono::time_point<chrono::system_clock> detectTime;
chrono::time_point<chrono::system_clock> disarmTime;
//读取密码
string access_code() {
if (!getenv("CODE")) {
return "4321";
} else {
return getenv("CODE");
}
}
//通知远程数据存储
void notify(std::string message) {
//time此函数会返回从公元 1970 年1 月1 日的UTC 时间从0 时0 分0 秒算起到现在所经过的秒数。
//如果t 并非空指针的话,此函数也会将返回值存到t 指针所指的内存.
//然后调用localtime将time_t所表示的CUT时间转换为本地时间,(我们是+8区,比CUT多8个小时)
time_t now = std::time(NULL);
char mbstr[sizeof "2011-10-08T07:07:09Z"];
strftime(mbstr, sizeof(mbstr), "%FT%TZ", localtime(&now));
stringstream text;
text << "{\"state\":";
text << "\"" << message << " " << mbstr << "\"}";
log_mqtt(text.str());
log_datastore(text.str());
}
struct Devices
{
upm::Jhd1313m1* screen;
upm::BISS0001* motion;
upm::GroveButton* button;
upm::Buzzer* buzzer;
Devices(){
};
void init() {
screen = new upm::Jhd1313m1(0);
motion = new upm::BISS0001(4);
buzzer = new upm::Buzzer(5);
stop_buzzing();
button = new upm::GroveButton(6);
};
void cleanup() {
delete screen;
delete motion;
delete button;
delete buzzer;
}
//LCD显示
void message(const string& input, const size_t color = 0x0000ff) {
// cout << input << std::endl;//输出到终端打印。我这里不需要
size_t red = (color & 0xff0000) >> 16;
size_t green = (color & 0x00ff00) >> 8;
size_t blue = (color & 0x0000ff);
string text(input);
text.resize(16, ' ');
screen->setCursor(0,0);
screen->write(text);
screen->setColor(red, green, blue);
}
void start_alarm_countdown() {
//检测到按键
countdownStarted = true;
detectTime = chrono::system_clock::now();//当前时间
string msg = "Person detected";//显示有人
message(msg, 0xff00ff);
notify(msg);
start_buzzing();
}
//见到到有,但没有输入确认
void trigger_alarm() {
alarmTriggered = true;
string msg = "Alarm triggered!";
message(msg, 0xff00ff);
notify(msg);
stop_buzzing();
}
void disarm() {
disarmTime = chrono::system_clock::now();
disarmed = true;
countdownStarted = false;
alarmTriggered = false;
}
void reset() {
disarmed = false;
countdownStarted = false;
alarmTriggered = false;
stop_buzzing();
}
int elapsed_since(chrono::time_point<chrono::system_clock> tp) {
chrono::duration<double> elapsed;
chrono::time_point<chrono::system_clock> now;
now = chrono::system_clock::now();
elapsed = now - tp;
return elapsed.count();
}
void detect() {
if (alarmTriggered) {
if (elapsed_since(detectTime) > 120) reset();//2分钟后恢复
} else if (disarmed) {
if (elapsed_since(disarmTime) > 120) reset();//确认后,预留2分钟,客人通过
} else if (countdownStarted) {
if (elapsed_since(detectTime) > 30) trigger_alarm();//按键后30s,没有输入确认密码,报警
} else if (button->value()) {
start_alarm_countdown();//有按键,报警准备开始
} else {
message("Monitoring...");
}
}
void start_buzzing() {
buzzer->setVolume(0.5);
buzzer->playSound(2600, 0);
}
void stop_buzzing() {
buzzer->setVolume(0);
buzzer->stopSound();
buzzer->stopSound();
}
};
void runner(Devices& devices) {
for (;;) {
devices.detect();
usleep(500);
}
}
Devices devices;
void exit_handler(int param)
{
devices.cleanup();
exit(1);
}
int main() {
signal(SIGINT, exit_handler);
mraa_platform_t platform = mraa_get_platform_type();
if ((platform != MRAA_INTEL_GALILEO_GEN1) &&
(platform != MRAA_INTEL_GALILEO_GEN2) &&
(platform != MRAA_INTEL_EDISON_FAB_C)) {
std::cerr << "ERROR: Unsupported platform" << std::endl;
return MRAA_ERROR_INVALID_PLATFORM;
}
devices.init();
std::thread t1(runner, std::ref(devices));
crow::SimpleApp app;
CROW_ROUTE(app, "/")
([]() {
std::stringstream text;
text << index_html;
return text.str();
});
CROW_ROUTE(app, "/alarm")
([](const crow::request& req) {
if(req.url_params.get("code") != nullptr) {
if (access_code() == req.url_params.get("code")) {
devices.disarm();
} else {
notify("invalid code");
}
}
return crow::response("OK");
});
CROW_ROUTE(app, "/styles.css")
([]() {
std::stringstream text;
text << styles_css;
return text.str();
});
// start web server
app.port(3000).multithreaded().run();
// wait forever for the thread to exit
t1.join();
return MRAA_SUCCESS;
}
1、上电
2、按键
这个时候门铃响起,如果从web上能确认密码。那么门打开,有2分钟时间让人通过。然后恢复到起始状态,等待按键。
3、如果按键后,30秒内,还没有web密码确认。那么产生报警。
等待一段时间恢复到起始状态等待按键。
动心忍性1234: 您好我是无线电杂志的编辑,我们对您的项目十分感兴趣,请问您有兴趣投稿吗?成为我们的作者除稿费外还有其他优厚条件。敬请参与。投稿请联系QQ260534978.
回复