聚丰项目 > 智能夜灯
一款夜灯,有两个光源,既可以当夜灯,也可以当照明灯。 带人体感应,自己写了个android上位机,可以通过手机控制(懒人福音~~),可以设置定时关闭等。
用户协议

用户协议
团队成员
梁学友 开发
1、主控芯片 nucleo stmf401开发板
2、蓝牙模块 hc-06:用于与手机进行蓝牙通信
3、光线感应模块:用于感应人体
4、led灯和灯带:提供光源
5、三极管,电阻等:搭建外围电路和电源电路
6、android手机:运行上位机,控制夜灯
7、亚克力板,作品外壳
单片机部分软件,使用的开发平台为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,延时关闭时间
Timeout timeout2;//定时器2,定时关闭时间
Timer timer1;
Timer timer2;
Ticker ticker1;
unsigned int auto_close_time=300;//延时关闭时间
unsigned int clock_time=600;//定时关闭时间
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;
}
//串口中断处理函数
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());
}
*/
}
//按键中断处理函数,单击,双击,长按
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,热释电传感器信号中断处理函数,上升沿,检测到有人
void flip2_1()
{
lightingled=true;
timeout1.attach(&timeout1_evevt_1,auto_close_time);
//myled=false;
}
//D3,热释电传感器信号中断处理函数,下降沿,传感器延时结束
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);//声明窗口中断处理函数
button.fall(&flip);//声明按键中断处理函数
reshidian.fall(&flip2_1);
AutoMode();
while(1)
{
}
}
手机App部分,使用的开发平台为android,开发工具为android studio,开发语言为JAVA,主要代码及介绍如下:
(android代码在这里显示好像有问题,源程序在附件,需要请联系本人索要密码)
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)自动模式,在这个模式下,可以自动感应人体,感应到人体后,照明灯自动点亮,延时一段时间后关闭。
2)手动模式,就是不会自动感应人体,完全依靠按键和手机控制。
开机默认进入自动模式,按键长按2s切换模式,进入手动模式(当进入手动模式的时候,板子上绿色的指示灯会亮)
按键:两个按键,突出的那个是控制按键,凹下去的那个是复位按键,相当于重启。
控制按键:单击-照明灯开关。双击-夜灯开关(双击按两下之间的时间间隔要小于0.4s)。长按-模式切换。
使用步骤:
1,在手机设置里打开蓝牙,搜索设备,配对(蓝牙设备名称为DJ`s Light,配对密钥1234或者0000)
2、配对完成后打开APP,点击列表里的设备就可以自动连接,然后进行相应的控制操作

1、最上面textview显示蓝牙状态
下面依次是蓝牙开关,如果蓝牙打开,listview里会显示已配对并且可用的设备。点击就可以连接,连接成功会有提示,然后就可以进行控制。
2、Lighting light:照明灯开关
3、Night light:夜灯开关
4、手动模式:手动模式/自动模式切换开关,成功会返回 “success!”。
5、延时关闭时间:自动模式下照明灯感应到人体开启后延时关闭的时间。手动模式下失效。成功会返回 “success!”。
6、定时关闭时间:可以设置照明灯在某个时间后关闭(大于等于1分钟,小于24小时-1440分钟)成功会返回 “success!”。
android程序源代码在附件,需要请联系本人
演示效果:




(0.09 MB)下载