table of Contents
Foreword
Related arduino All Categories:
https://blog.csdn.net/freewebsys/category_8799254.html
Original connection of this paper is:
https://blog.csdn.net/freewebsys/article/details/104528199
Bloggers may not be reproduced without the permit.
Bloggers address is: http://blog.csdn.net/freewebsys
1, on the arduino
A particularly simple scenarios. Girl online courses, but adults work from home.
Sometimes they attend, the result of petty theft girl watching cartoons, the time forgotten.
Start school on time, then missed a few minutes. Sometimes you miss half an hour.
Internet class time in the morning, 8--11 pm 14-20 ahead two minutes, which is one hour 58 minutes.
Recent studies Arduino, but also just a buzzer. Searching for a song Ode to Joy from the Internet:
demand is very simple. 2 minutes ahead reminded girl in class.
https://blog.csdn.net/qq_42216781/article/details/91994595
2, the code
Because ESP8266 no battery is required to use Ali's time server. Installation NTPClinet.
Timeclient mainly used NtpClient (ntpUDP, "ntp1.aliyun.com", 60 60 . 8, 30 60 1000);
zone China provided a half-hour clock synchronization setting time.
Analyzing then the first hour 8--1114-20 pm forward one hour, and then determines to 58 minutes ==.
If you play music in line with reminders.
/*
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, summary
arduino is now very mature, and is a very mature solution of.
From idea to realization made out more than one hour, mostly unfamiliar with the code, find the code time more.
This use is ESP8266, with ESP32 can. Library under slightly changed.
Development is also very fast. After just a smart furniture. Follow-up to see how other devices access network.
Original connection of this paper is:
https://blog.csdn.net/freewebsys/article/details/104528199
Bloggers address is: https://blog.csdn.net/freewebsys