用ESP32和RTC做一个“唤醒闹钟”吧! |
您所在的位置:网站首页 › 为聋子设计闹钟的人 › 用ESP32和RTC做一个“唤醒闹钟”吧! |
#include
#include
#include
#include
#include
#include
#include
#include
#include "time.h"
#include
#include
#include
#include
#include
// Initial setup const char *ssid = “Wi-Fi SSID”; const char *pass = “Wi-Fi password”; IPAddress ip(IP address assigned to main unit); IPAddress gateway(IP address of default gateway); const char* notify_url = “http://IP address of extension unit/alarm”; const char* adjust_time = “04:00:00”; #define DF_VOLUME 30 // Constants, etc. #define ALARM_SIG 25 #define TFT_DC 2 #define TFT_CS 5 #define TFT_RST 4 #define TFT_WIDTH 320 Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST); RTC_DS3231 rtc; HardwareSerial hs(1); DFRobotDFPlayerMini myDFPlayer; WebServer server(80); char old_date[15]; char old_time[9]; char old_alarm[15]; char alarm_time[9]; char wdays[7][4] = { “Sun”, “Mon”, “Tue”, “Wed”, “Thu”, “Fri”, “Sat” }; bool alarm_checked = false; bool alarm_on = false; bool ntp_adjusted = false; int alarm_ctr; // Set date and time using NTP void setTimeByNTP() { struct tm t; configTime(9 * 3600L, 0, “ntp.nict.jp”, “time.google.com”, “ntp.jst.mfeed.ad.jp”); if (!getLocalTime(&t)) { Serial.println(“getLocalTime Error”); return; } rtc.adjust(DateTime(t.tm_year + 1900, t.tm_mon + 1, t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec)); } // Display string on LCD void showMessage(char* s_new, char* s_old, int y0, int height) { int16_t x1, y1; uint16_t w, w2, h; int x, y; if (strcmp(s_new, s_old) != 0) { tft.getTextBounds(s_old, 0, 0, &x1, &y1, &w, &h); w2 = w * 11 / 10; tft.fillRect((TFT_WIDTH – w2) / 2 , y0 – (height / 2) + 1, w2, height, ILI9341_BLACK), tft.getTextBounds(s_new, 0, 0, &x1, &y1, &w, &h); tft.setCursor((TFT_WIDTH – w) / 2, y0 + (h / 2) – 1); tft.print(s_new); strcpy(s_old, s_new); } } // Main settings page void handleRoot() { int i; String html = “\n” “\n” “\n” “\n” “Alarm clock settings\n” “\n” “\n” “\n” “ \n” “\n”; for (i = 0; i < 24; i++) { html += “”; html += String(i); html += “\n”; } html += “(h)\n”; html += “\n”; for (i = 0; i < 60; i++) { html += “”; html += String(i); html += “\n”; } html += “(min) \n”; html += “\n”; html += “ \n”; html += “\n”; html += “\n”; html += “\n”; html += “\n”; html += “\n”; html += “\n”; html += “\n”; server.send(200, “text/html”, html); }// Set alarm void handleSetAlarm() { int i, hour, min, sec; bool is_off = false; String s_hour = “”, s_min = “”, s_sec = “”; // Get “off/hour/min/sec” parameters from URL for (i = 0; i < server.args(); i++) { if (server.argName(i).compareTo(“off”) == 0) { is_off = true; break; } else if (server.argName(i).compareTo(“hour”) == 0) { s_hour = server.arg(i); } else if (server.argName(i).compareTo(“min”) == 0) { s_min = server.arg(i); } else if (server.argName(i).compareTo(“sec”) == 0) { s_sec = server.arg(i); } } // Turn off alarm if “off” parameter is set if (is_off) { strcpy(alarm_time, “Off”); server.send(200, “text/plain; ”, “Alarm turned off.”); } // Set alarm time else if (s_hour.length() > 0 && s_min.length() > 0) { hour = s_hour.toInt(); min = s_min.toInt(); if (s_sec.length() > 0) { sec = s_sec.toInt(); } else { sec = 0; } if (hour >= 0 && hour = 0 && min = 0 && sec |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |