arduino(13):使用ESP8266加蜂鸣器,做一个定时闹钟,督促闺女上课,上网校时间提前两分钟,唱歌提醒。超级实用。

前言


相关arduino 全部分类:
https://blog.csdn.net/freewebsys/category_8799254.html

本文的原文连接是:
https://blog.csdn.net/freewebsys/article/details/104528199

未经博主允许不得转载。
博主地址是:http://blog.csdn.net/freewebsys

1,关于arduino


特别简单的应用场景。闺女上网课程,但是大人在家工作。
有的时候顾不上,结果闺女偷摸看动画片,把时间忘记了。
开始准时上课,后来都错过几分钟。有的时候错过半个小时。

上网课时间上午, 8 - 11 下午 14-20 提前 2 分钟,也就是上一个小时的 58 分钟。
最近研究 Arduino ,而且正好也有蜂鸣器。从网上搜索了一个欢乐颂歌曲:
需求很简单。提前2 分钟提醒下闺女上课。

https://blog.csdn.net/qq_42216781/article/details/91994595

2,代码


因为ESP8266 没有电池,需要使用阿里的时间服务器。 安装NTPClinet。
在这里插入图片描述
主要使用 NTPClient timeClient(ntpUDP,“ntp1.aliyun.com”, 60608 , 30601000);
设置时区中国,设置半小时同步一次时钟时间。
然后先判断小时 在 8 - 11 下午 14-20 ,往前一小时, 然后再判断分钟 == 58 即可。
如果符合就播放音乐提醒。

/*
    This sketch establishes a TCP connection to a "quote of the day" service.
    It sends a "hello" message, and then prints received data.
*/

// ################### song begin  #####################
#define NTD0 -1
#define NTD1 294
#define NTD2 330
#define NTD3 350
#define NTD4 393
#define NTD5 441
#define NTD6 495
#define NTD7 556

#define NTDL1 147
#define NTDL2 165
#define NTDL3 175
#define NTDL4 196
#define NTDL5 221
#define NTDL6 248
#define NTDL7 278

#define NTDH1 589
#define NTDH2 661
#define NTDH3 700
#define NTDH4 786
#define NTDH5 882
#define NTDH6 990
#define NTDH7 112

#define WHOLE 1
#define HALF 0.5
#define QUARTER 0.25
#define EIGHTH 0.25
#define SIXTEENTH 0.625
// huanlesong 
int tune[]=
{
  NTD3,NTD3,NTD4,NTD5,
  NTD5,NTD4,NTD3,NTD2,
  NTD1,NTD1,NTD2,NTD3,
  NTD3,NTD2,NTD2,
  NTD3,NTD3,NTD4,NTD5,
  NTD5,NTD4,NTD3,NTD2,
  NTD1,NTD1,NTD2,NTD3,
  NTD2,NTD1,NTD1,
  NTD2,NTD2,NTD3,NTD1,
  NTD2,NTD3,NTD4,NTD3,NTD1,
  NTD2,NTD3,NTD4,NTD3,NTD2,
  NTD1,NTD2,NTDL5,NTD0,
  NTD3,NTD3,NTD4,NTD5,
  NTD5,NTD4,NTD3,NTD4,NTD2,
  NTD1,NTD1,NTD2,NTD3,
  NTD2,NTD1,NTD1
};
float durt[]=
{
  1,1,1,1,
  1,1,1,1,
  1,1,1,1,
  1+0.5,0.5,1+1,
  1,1,1,1,
  1,1,1,1,
  1,1,1,1,
  1+0.5,0.5,1+1,
  1,1,1,1,
  1,0.5,0.5,1,1,
  1,0.5,0.5,1,1,
  1,1,1,1,
  1,1,1,1,
  1,1,1,0.5,0.5,
  1,1,1,1,
  1+0.5,0.5,1+1,
};
int length;
int tonepin=3;
int ledp=15;

//song function 
void sing_song()
{
  for(int x=0;x<length;x++)
  {
    tone(tonepin,tune[x]);
    digitalWrite(LED_BUILTIN, HIGH); 
    delay(400*durt[x]);
    digitalWrite(LED_BUILTIN, LOW);
    delay(100*durt[x]);
    noTone(tonepin);
  }
}


// ################### song end    #####################


#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <NTPClient.h>
#include <WiFiUdp.h>


#ifndef STASSID
#define STASSID "wifi-ssd"
#define STAPSK  "wifi-password"
#endif

const char* ssid     = STASSID;
const char* password = STAPSK;


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,"ntp1.aliyun.com", 60*60*8 , 30*60*1000);
// 8 timezone 30 min update


void setup() {
  Serial.begin(9600); //115200
  // We start by connecting to a WiFi network
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  /* Explicitly set the ESP8266 to be a WiFi-client, otherwise, it by default,
     would try to act as both a client and an access-point and could cause
     network-issues with your other WiFi-devices on your WiFi-network. */
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());

  // init song  初始化 歌曲,
  pinMode(tonepin,OUTPUT);
  pinMode(LED_BUILTIN,OUTPUT);
  length=sizeof(tune)/sizeof(tune[0]);

  digitalWrite(LED_BUILTIN, LOW);   // wif连接成功才走到这步,让led 闪一下。 turn the LED on (HIGH is the voltage level)
  delay(500);                       // wait for a second
  digitalWrite(LED_BUILTIN, HIGH);    // turn the LED off by making the voltage LOW
  
}

void loop() {

  timeClient.update();
  int time_h = timeClient.getHours();
  int time_m = timeClient.getMinutes();
  
  Serial.println(timeClient.getFormattedTime());
  Serial.println(String(time_h) + ":" + String(time_m));

  //sing song every hour . 上网课时间上午,  8 - 11  下午 14-20   提前 2 分钟,也就是上一个小时的 58 分钟。
  if( time_h == (8 -1) || time_h == (9 -1) || time_h == (10 -1) || time_h == (11 -1) ||
    time_h == (14 -1) || time_h == (15 -1) || time_h == (16 -1) || time_h == (17 -1) ||
    time_h == (18 -1) || time_h == (19 -1) || time_h == (20 -1) ){

      // every hour 58 min.
      if( timeClient.getMinutes() == 58){
        sing_song();
      }
  }

  // execute once every 30 second, don't flood remote service
  delay(30*1000); 

}

3,总结


arduino 现在已经非常的成熟了,是一个非常成熟的解决方案了。
从想法到实现做出来 一个 多小时,主要是代码不熟悉,找代码时间多些。
这个使用的是ESP8266 ,用 ESP32 也可以。库稍微改下。
开发也挺快的。以后就智能家具了。后续看看其他设备咋接入联网。

本文的原文连接是:
https://blog.csdn.net/freewebsys/article/details/104528199

博主地址是:https://blog.csdn.net/freewebsys

发布了639 篇原创文章 · 获赞 260 · 访问量 211万+

猜你喜欢

转载自blog.csdn.net/freewebsys/article/details/104528199