聚豐項目 > 基于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.
回復