聚豐項目 > 智能夜燈
一款夜燈,有兩個光源,既可以當夜燈,也可以當照明燈。 帶人體感應(yīng),自己寫了個android上位機,可以通過手機控制(懶人福音~~),可以設(shè)置定時關(guān)閉等。
用戶協(xié)議

用戶協(xié)議
團隊成員
梁學友 開發(fā)
1、主控芯片 nucleo stmf401開發(fā)板
2、藍牙模塊 hc-06:用于與手機進行藍牙通信
3、光線感應(yīng)模塊:用于感應(yīng)人體
4、led燈和燈帶:提供光源
5、三極管,電阻等:搭建外圍電路和電源電路
6、android手機:運行上位機,控制夜燈
7、亞克力板,作品外殼
單片機部分軟件,使用的開發(fā)平臺為mbed os,代碼及介紹如下:
#include "mbed.h"
#include "string"
//#include "stdlib.h"
//------------------------------------
// Hyperterminal configuration
// 9600 bauds, 8-bit data, no parity
//------------------------------------
//Serial pc(SERIAL_TX, SERIAL_RX);
Serial pc(D10,D2,115200);//與藍牙模塊通信的串口
DigitalOut myled(LED1);
DigitalOut nightled(A0);
DigitalOut lightingled(A1);
InterruptIn button(USER_BUTTON);
InterruptIn reshidian(D3);
Timeout timeout1;//定時器1,延時關(guān)閉時間
Timeout timeout2;//定時器2,定時關(guān)閉時間
Timer timer1;
Timer timer2;
Ticker ticker1;
unsigned int auto_close_time=300;//延時關(guān)閉時間
unsigned int clock_time=600;//定時關(guān)閉時間
unsigned char mode_flag=0;//運行模式標志,1-AutoMode,2-ControledMode
void AutoMode(void);
void ControledMode(void);
char str[]="000000000000000000";
int str_index=0;
void timeout1_evevt_1()
{
lightingled=false;
}
void timeout2_evevt_2()
{
lightingled=false;
}
//串口中斷處理函數(shù)
void echouart()
{
while(pc.readable())
{
str[str_index]=pc.getc();
str_index++;
if(str[str_index-1]=='$')
{
str_index=0;
//pc.printf(str);
//string s(&str[1],&str[str_index-1]);
switch(str[str_index])
{
case '0': /*pc.printf("lighting light on ");*/lightingled=true;break;
case '1':lightingled=false;break;
case '2':nightled=true;break;
case '3':nightled=false;break;
case '4':AutoMode();pc.printf("success!\r\n");break;
case '5':ControledMode();pc.printf("success!\r\n");break;
case '6':
auto_close_time=atoi(&str[1]);
auto_close_time*60;
pc.printf("success!\r\n");
break;
case '7':clock_time=atoi(&str[1]);
timeout2.attach(timeout2_evevt_2,clock_time*60);
pc.printf("success!\r\n");
break;
default:break;
}
int i=0;
while(i<10)
{
str[i]='0';
i++;
}
}
}
/*
while(pc.readable())
{
pc.putc(pc.getc());
}
*/
}
//按鍵中斷處理函數(shù),單擊,雙擊,長按
void flip()
{
wait(0.03);
if(button==0)
{
timer1.start();
while(button==0)
{
if(timer1.read_ms()>2000)
{
timer1.stop();
timer1.reset();
if(mode_flag==0)
{
mode_flag=1;
ControledMode();
//pc.printf("controled\r\n");
}
else if(mode_flag==1)
{
mode_flag=0;
AutoMode();
//pc.printf("auto\r\n");
}
//pc.printf("statu_2-long_click\r\n");
while(button==0);
return;
}
}
if(timer1.read_ms()<=2000)
{
timer1.stop();
timer1.reset();
timer2.start();
button.disable_irq();
while(timer2.read_ms()<400)
{
if(button==0)
{
wait(0.03);
if(button==0)
{
timer2.stop();
timer2.reset();
//pc.printf("statu_1-double_click\r\n");
nightled=!nightled;
button.enable_irq();
break;
}
}
}
if(timer2.read_ms()>=400)
{
timer2.stop();
timer2.reset();
//pc.printf("statu_0-short_click\r\n");
lightingled=!lightingled;
button.enable_irq();
}
}
}
}
//D3,熱釋電傳感器信號中斷處理函數(shù),上升沿,檢測到有人
void flip2_1()
{
lightingled=true;
timeout1.attach(&timeout1_evevt_1,auto_close_time);
//myled=false;
}
//D3,熱釋電傳感器信號中斷處理函數(shù),下降沿,傳感器延時結(jié)束
void flip2_2()
{
lightingled=false;
timeout1.attach(&timeout1_evevt_1,auto_close_time);
myled=true;
}
//自動運行模式
void AutoMode()
{
myled=false;
reshidian.enable_irq();
}
//手動控制模式
void ControledMode()
{
myled=true;
reshidian.disable_irq();
}
int main()
{
lightingled=true;
timeout1.attach(&timeout1_evevt_1,auto_close_time);
pc.attach(&echouart,SerialBase::RxIrq);//聲明窗口中斷處理函數(shù)
button.fall(&flip);//聲明按鍵中斷處理函數(shù)
reshidian.fall(&flip2_1);
AutoMode();
while(1)
{
}
}
手機App部分,使用的開發(fā)平臺為android,開發(fā)工具為android studio,開發(fā)語言為JAVA,主要代碼及介紹如下:
(android代碼在這里顯示好像有問題,源程序在附件,需要請聯(lián)系本人索要密碼)
com.example.john.myapplication;
android.content.BroadcastReceiver;
android.content.Context;
android.content.DialogInterface;
android.content.Intent;
android.content.IntentFilter;
android.os.Handler;
android.support.v7.app.AppCompatActivity;
android.os.Bundle;
android.text.TextUtils;
android.text.method.ScrollingMovementMethod;
android.view.View;
android.widget.*;
android.bluetooth.*;
android.bluetooth.BluetoothDevice;
android.bluetooth.BluetoothSocket;
java.io.IOException;
java.lang.reflect.Method;
java.util.ArrayList;
java.util.HashMap;
java.util.List;
java.util.Set;
java.util.UUID;
android.annotation.;
android.app.Activity;
android.bluetooth.BluetoothAdapter;
android.bluetooth.BluetoothDevice;
android.content.BroadcastReceiver;
android.content.Context;
android.content.Intent;
android.content.IntentFilter;
android.os.Build;
android.os.Bundle;
android.os.Handler;
android.os.Message;
android.view.Menu;
android.view.MenuItem;
android.view.View;
android.view.ViewConfiguration;
android.view.Window;
android.widget.AdapterView;
android.widget.ListView;
android.widget.Toast;
MainActivity AppCompatActivity{
BluetoothAdapter ;
TextView ;
ScrollView ;
;
String =;
;
Switch ;Switch ;Switch ;Switch ;ListView ;EditText ;EditText ;Button ;Button ;DeviceAdapter ;
List =ArrayList<>();BlueToothController =BlueToothController();
BluetoothDevice ;
BluetoothSocket =;
ConnectThread ;
=;onCreate(Bundle savedInstanceState) {
.onCreate(savedInstanceState);
setContentView(R.layout.);
=(Switch) findViewById(R.id.);
=(Switch) findViewById(R.id.);
=(Switch) findViewById(R.id.);
=(EditText)findViewById(R.id.);
=(EditText)findViewById(R.id.);
=(Button)findViewById(R.id.);
=(Button)findViewById(R.id.);
=(Switch) findViewById(R.id.);
=(ListView)findViewById(R.id.);
= BluetoothAdapter.();
= (TextView) findViewById(R.id.);
(== ) {
showToast();
;
}
= .getState();() {
:
.setText();
.setChecked();;
:
.setText();
;
:
.setText();
.setChecked();
;
:
.setText();
;
:
;
}
.setOnCheckedChangeListener(CompoundButton.OnCheckedChangeListener() {
onCheckedChanged(CompoundButton buttonView, isChecked) {
(isChecked)
{
{.enable();
showToast();
.setText();}
(Exception e)
{
e.printStackTrace();
}
}
{
{
.disable();
showToast();
.setText();
}
(Exception e)
{e.printStackTrace();}
}
}
});
.setOnCheckedChangeListener(CompoundButton.OnCheckedChangeListener() {
onCheckedChanged(CompoundButton buttonView, isChecked) {
(isChecked)
{
(!=)
{
.sendData((Instructs.+).getBytes());
}
{
showToast();
.setChecked();
}
}
{
(!=)
{
.sendData((Instructs.+).getBytes());
}
{
showToast();
.setChecked();
}
}
}
});
.setOnCheckedChangeListener(CompoundButton.OnCheckedChangeListener() {
onCheckedChanged(CompoundButton buttonView, isChecked) {
(isChecked)
{
(!=)
{
.sendData((Instructs.+).getBytes());
}
{
showToast();
.setChecked();
}
}
{
(!=)
{
.sendData((Instructs.+).getBytes());
}
{
showToast();
.setChecked();
}
}
}
});
.setOnCheckedChangeListener(CompoundButton.OnCheckedChangeListener() {
onCheckedChanged(CompoundButton buttonView, isChecked) {
(isChecked)
{
(!=)
{
.sendData((Instructs.+).getBytes());
}
{
showToast();
.setChecked();
}
}
{
(!=)
{
.sendData((Instructs.+).getBytes());
}
{
showToast();
.setChecked();
}
}
}
});
.setOnClickListener(View.OnClickListener() {
onClick(View v) {
(!=)
{
String str=.getText().toString();
(TextUtils.(.getText()))
{
showToast();
;
}
(Integer.(str)>)
{
showToast();
;
}
.sendData((Instructs.+str+).getBytes());
}
{
showToast();
}
}
});
.setOnClickListener(View.OnClickListener() {
onClick(View v) {
(!=)
{
String str=.getText().toString();
(TextUtils.(.getText()))
{
showToast();
;
}
(Integer.(str)>)
{
showToast();
;
}
.sendData((Instructs.+str+).getBytes());
}
{
showToast();
}
}
});
getBonded();
registerBluetoothReceiver();
}
getBonded(){
Set devices = .getBondedDevices();ArrayAdapter BondedList=ArrayAdapter(,android.R.layout.);.clear();
BondedList.clear();(BluetoothDevice boneddevice : devices) {
(boneddevice.getName().equals())
{
.add(boneddevice);
BondedList.add(boneddevice.getName());}
}
.setAdapter(BondedList);
.setOnItemClickListener();}
registerBluetoothReceiver() {
IntentFilter filter = IntentFilter();
filter.addAction(BluetoothAdapter.);
filter.addAction(BluetoothAdapter.);
filter.addAction(BluetoothAdapter.);
filter.addAction(BluetoothDevice.);
filter.addAction(BluetoothAdapter.);
filter.addAction(BluetoothDevice.);
registerReceiver(, filter);
}
initUI() {
=.getBondedDeviceList();ArrayAdapter BondedList=ArrayAdapter(,android.R.layout.);
String[] arr=String[]{};
(i=;i<.size();i++)
{
arr[i]=.get(i).getName();
BondedList.add(arr[i]);
}
.setAdapter(BondedList);
}
BroadcastReceiver = BroadcastReceiver() {
onReceive(Context context, Intent intent) {
String Action = intent.getAction();
(..equals(Action)){
showToast();
}
(..equals(Action)){
BluetoothDevice device=intent.getParcelableExtra(BluetoothDevice.);
String name=device.getName();
showToast(name);
.setText(name);
}
(..equals(Action))
{
(.getState()==.)
getBonded();
(.getState()==.)
{
getBonded();
}
}
}
};
onDestroy() {
.onDestroy();
unregisterReceiver();
}
Handler = MyHandler();
AdapterView.OnItemClickListener = AdapterView.OnItemClickListener() {
onItemClick(AdapterView parent, View view, position, id) {
BluetoothDevice device=.get(position);
( != ) {
.cancel();}
showToast();
= ConnectThread(device, , );
.start();
}
};
connect(BluetoothDevice device)IOException{
String SPP_UUID=;
UUID uuid=UUID.(SPP_UUID);
BluetoothSocket socket=device.createRfcommSocketToServiceRecord(uuid);
socket.connect();
showToast();
}
showToast(String msg)
{
Toast.(,msg,Toast.).show();
}
MyHandler Handler {
String =String();
String =String();
handleMessage(Message msg) {
.handleMessage(msg);
(msg.) {
Constant.:
;
Constant.:
;
Constant.:
=String.(msg.);
[] ch=.toCharArray();
=+;
((ch[.length()-]==)&&(ch[.length()-]==))
{
showToast();
=;
}
;
Constant.:
showToast(+String.(msg.)+);
;
Constant.:
showToast();
;
Constant.:
;
}
}
}
};照明燈:上面的燈
夜燈:里面的燈

1、運行:插上電就自動運行了~~
2、兩種運行模式:
1)自動模式,在這個模式下,可以自動感應(yīng)人體,感應(yīng)到人體后,照明燈自動點亮,延時一段時間后關(guān)閉。
2)手動模式,就是不會自動感應(yīng)人體,完全依靠按鍵和手機控制。
開機默認進入自動模式,按鍵長按2s切換模式,進入手動模式(當進入手動模式的時候,板子上綠色的指示燈會亮)
按鍵:兩個按鍵,突出的那個是控制按鍵,凹下去的那個是復位按鍵,相當于重啟。
控制按鍵:單擊-照明燈開關(guān)。雙擊-夜燈開關(guān)(雙擊按兩下之間的時間間隔要小于0.4s)。長按-模式切換。
使用步驟:
1,在手機設(shè)置里打開藍牙,搜索設(shè)備,配對(藍牙設(shè)備名稱為DJ`s Light,配對密鑰1234或者0000)
2、配對完成后打開APP,點擊列表里的設(shè)備就可以自動連接,然后進行相應(yīng)的控制操作

1、最上面textview顯示藍牙狀態(tài)
下面依次是藍牙開關(guān),如果藍牙打開,listview里會顯示已配對并且可用的設(shè)備。點擊就可以連接,連接成功會有提示,然后就可以進行控制。
2、Lighting light:照明燈開關(guān)
3、Night light:夜燈開關(guān)
4、手動模式:手動模式/自動模式切換開關(guān),成功會返回 “success!”。
5、延時關(guān)閉時間:自動模式下照明燈感應(yīng)到人體開啟后延時關(guān)閉的時間。手動模式下失效。成功會返回 “success!”。
6、定時關(guān)閉時間:可以設(shè)置照明燈在某個時間后關(guān)閉(大于等于1分鐘,小于24小時-1440分鐘)成功會返回 “success!”。
android程序源代碼在附件,需要請聯(lián)系本人
演示效果:




(0.09 MB)下載