物联网Arduino定时开关与手机控制智能灯

此项目主要是包含NTP获取时间和手机连接控制组成

1. 本项目效果:
晚上到点强制关灯睡觉,早上开灯提醒起床,如果晚上不想睡可以通过手机建立连接进行控制开灯或者关灯,还增加了一些KTV爆闪等有趣效果模式(有些费灯)

2. 项目使用的设备及软件
使用的硬件:wemos D1,继电器
软件环境编译 :Arduino

线路连接如下:
物联网Arduino定时开关与手机控制智能灯_第1张图片
3. 代码

#include 
// change next line to use with another board/shield
#include 
//#include  // for WiFi shield
//#include  // for WiFi 101 shield or MKR1000
#include 

#define GPIOR D5//调用GPIOR端口 D5
//#define WEEK1  "1"   
//#define WEEK2  "2"   
//#define WEEK3  "3"   
//#define WEEK4  "4"   
#define WEEK5  "5"     
#define WEEK6  "6"
#define WEEK7  "7"
const char *ssid     = "Xiaomi_0992";//WIFI账号
const char *password = "2020AP1335";//WIFI密码

int port =8888;//端口号,手机连接时输入
WiFiServer server(port);//设置服务器端口号

WiFiUDP ntpUDP;//用于获取当前时间

// You can specify the time server pool and the offset (in seconds, can be
// changed later with setTimeOffset() ). Additionaly you can specify the
// update interval (in milliseconds, can be changed using setUpdateInterval() ).
NTPClient timeClient(ntpUDP, "europe.pool.ntp.org", 8*3600, 60000);

void initWifiSta()//WIFI初始化
{
    WiFi.mode(WIFI_STA);         // 设置STA模式
    WiFi.begin(ssid, password);   //连接网络
    while (WiFi.status() != WL_CONNECTED)//等待连接上,在连接过程打印.
    {
        Serial.print(".");
        delay(500);
    }
    Serial.println(WiFi.localIP());//通过串口调试小助手 打印wemos的IP地址
}
void initGPIOR()//定义GPIOR函数
{
    pinMode(GPIOR,OUTPUT);//打开
    digitalWrite(GPIOR,LOW);//一开始灯是开的
}
  
void setup()
{
    initGPIOR();
    initWifiSta();
    server.begin();
    timeClient.begin();
    Serial.begin(115200);
    WiFi.begin(ssid, password);
    while ( WiFi.status() != WL_CONNECTED )
    {
        delay ( 500 );
        Serial.print ( "." );
    }
    timeClient.begin();
}

void loop() 
{
    int  i;
    char cmd;
    
    String time0;//当前时间变量
    String week;//当前星期几变量
    const char *time1 = "22:40:00";
    const char *time2 = "22:45:00";
    const char *time3 = "23:18:00";
    const char *time4 = "23:20:00";
    const char *time5 = "09:30:00";
    const char *time6 = "07:42:00";

    timeClient.update();
    //获取时间
    week = timeClient.getDay();//获取当下星期几
    time0 = timeClient.getFormattedTime();//获取当下时间
    week.c_str();
    time0.c_str();//将string类型转换为 char类型
    Serial.println(time0.c_str());//往串口打印当前时间
    Serial.println(timeClient.getDay());//往串口打印当前星期几
    WiFiClient client =server.available();//服务器初始化
    //如果是周五或者周六或者周天
    if((strcmp(week.c_str(),WEEK5)==0)||(strcmp(week.c_str(),WEEK6)==0)||(strcmp(week.c_str(),WEEK7)==0))
    { 
        if(strcmp(week.c_str(),WEEK5)==0)
        {
            if(strcmp(time0.c_str(),time3) == 0)//周五23:18:00闪烁一下提醒关灯
            {
                digitalWrite(GPIOR,HIGH);//关灯
                Serial.println("t 5 close");
                delay(1500);
                digitalWrite(GPIOR,LOW);//开灯
                Serial.println("t 5 open");
            }
            if(strcmp(time0.c_str(),time4) == 0)//周五23:20:00到点关灯
            {
                digitalWrite(GPIOR,HIGH);//关灯
                Serial.println("5 close");
            }
            else if(strcmp(time0.c_str(),time6) == 0)//周五07:42:00到点开灯
            {
                digitalWrite(GPIOR,LOW);//开灯
                Serial.println("5 open");
            }
        }
        if(strcmp(week.c_str(),WEEK6)==0)
        {
            if(strcmp(time0.c_str(),time3) == 0)//周六23:18:00闪烁一下提醒关灯
            {
                digitalWrite(GPIOR,HIGH);//关灯
                Serial.println("6 t close");
                delay(1500);
                digitalWrite(GPIOR,LOW);//开灯
                Serial.println("6 t open");
            }
            if(strcmp(time0.c_str(),time4) == 0)//周六23:20:00到点关灯
            {
                digitalWrite(GPIOR,HIGH);//关灯
                Serial.println("6 close");
            }
            else if(strcmp(time0.c_str(),time5) == 0)//周六09:30:00到点开灯
            {
                digitalWrite(GPIOR,LOW);//开灯
                Serial.println("6 open");
            }
        }
        if(strcmp(week.c_str(),WEEK7)==0)
        {
            if(strcmp(time0.c_str(),time1) == 0)//周六22:40:00闪烁一下提醒关灯
            {
                digitalWrite(GPIOR,HIGH);//关灯
                Serial.println("7 t close");
                delay(1500);
                digitalWrite(GPIOR,LOW);//开灯
                Serial.println("7 t open"); 
            }
            if(strcmp(time0.c_str(),time2) == 0)//周天22:45:00到点关灯
            {
                digitalWrite(GPIOR,HIGH);//关灯
                Serial.println("7 close");
            }
            else if(strcmp(time0.c_str(),time5) == 0)//周天09:30:00到点开灯
            {
                digitalWrite(GPIOR,LOW);//开灯
                Serial.println("7 open");
            }
        }
    }
    else  //((strcmp(week.c_str(),WEEK1)==0)||(strcmp(week.c_str(),WEEK2)==0)||(strcmp(week.c_str(),WEEK3)==0)||(strcmp(week.c_str(),WEEK4)==0))//如果是周一到周四
    {
        if(strcmp(time0.c_str(),time1) == 0)//22:46:00灯闪一下
        {
            digitalWrite(GPIOR,HIGH);//关灯
            Serial.println("1 t close");
            delay(1500);
            digitalWrite(GPIOR,LOW);//开灯
            Serial.println("1 t open");
        }
        if(strcmp(time0.c_str(),time2) == 0)//22:50:00彻底关灯
        {
            digitalWrite(GPIOR,HIGH);//关灯
            Serial.println("1 close");
        }
        if(strcmp(time0.c_str(),time6) == 0)//07:42:00开灯
        {
            digitalWrite(GPIOR,LOW);//开灯
            Serial.println("1 open");
        }
    }
    while(client.connected())//等待手机连接Wemos D1
    { 
       while(client.available()>0)//有数据到来,类似串口那个函数
       {  
            cmd = client.read();//读取数据
            Serial.println(client.read());//打印输入的几
            Serial.println(cmd);//打印输入的几
            if(cmd == '1')//如果手机输入字符串 1
            {
                Serial.println("phone 1");
                digitalWrite(GPIOR,LOW);//开灯
            }
            if(cmd == '2')//如果手机输入字符串 2
            {
                Serial.println("phone 2");
                digitalWrite(GPIOR,HIGH);//关灯
            }
            if(cmd == '3')//如果手机输入字符串 3//小众吓人模拟停电模式
            {
                Serial.println("phone 3");
                for(i=0;i<3;i++)
                {
                    digitalWrite(GPIOR,HIGH);//关灯
                    delay(6000);
                    digitalWrite(GPIOR,LOW);//开灯
                    delay(150);
                }
            }
           if(cmd == '4')//如果手机输入字符串 4//进入顿挫模式
           {
                Serial.println("phone 4");
                for(i=0;i<8;i++)
                {
                    digitalWrite(GPIOR,HIGH);//关灯
                    delay(1000);
                    digitalWrite(GPIOR,LOW);//开灯
                    delay(1000);
                }
            }
           if(cmd == '5')//如果手机输入字符串 4//进入快顿挫模式
           {
                Serial.println("phone 5");
                for(i=0;i<10;i++)
                {
                    digitalWrite(GPIOR,HIGH);//关灯
                    delay(500);
                    digitalWrite(GPIOR,LOW);//开灯
                    delay(500);
                }
            }
            if(cmd == '6')//如果手机输入字符串 4//进入激情爆闪模式
            {
                Serial.println("phone 6");
                for(i=0;i<25;i++)
                {
                    digitalWrite(GPIOR,HIGH);//关灯
                    delay(200);
                    digitalWrite(GPIOR,LOW);//开灯
                    delay(200);
                 }
             }
            if(cmd == '7')//如果手机输入字符串 4//进入摇滚模式
            {
                Serial.println("phone 7");
                for(i=0;i<15;i++)
                {
                    digitalWrite(GPIOR,HIGH);//关灯
                    delay(500);
                    digitalWrite(GPIOR,LOW);//开灯
                    delay(400);
                    digitalWrite(GPIOR,HIGH);//关灯
                    delay(300);
                    digitalWrite(GPIOR,LOW);//开灯
                    delay(200);
                }
            }
        }   
        delay(1000); 
    }
    delay(1000);
    //Serial.println(WiFi.localIP());  //通过串口打印wemos的IP地址
}   


4. 调试过程
物联网Arduino定时开关与手机控制智能灯_第2张图片

5. 视频展示

代录制

你可能感兴趣的:(实操项目,c语言,arduino,ntp,物联网)