#include
#include #include #include <Fonts/FreeSans12pt7b.h>#include <Fonts/FreeSans18pt7b.h>#include <Fonts/FreeSans40pt7b.h>#include #include #include "time.h"#include #include #include #include #include // Initial setupconst 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 320Adafruit_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 NTPvoid 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 LCDvoid 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 pagevoid handleRoot() { int i; String html = "<!DOCTYPE html>\n" "<html>\n" "<head>\n" "<meta charset="utf-8">\n" "<title>Alarm clock settings</title>\n" "</head>\n" "<body>\n" "<form method="get" action="/set">\n" "<p>\n" "<select name="hour">\n"; for (i = 0; i < 24; i++) { html += "<option value=""; html += String(i); html += "">"; html += String(i); html += "</option>\n"; } html += "</select>(h)\n"; html += "<select name="min">\n"; for (i = 0; i < 60; i++) { html += "<option value=""; html += String(i); html += "">"; html += String(i); html += "</option>\n"; } html += "</select>(min) \n"; html += "<input type="submit" name="submit" value="Alarm setting" />\n"; html += "</p>\n"; html += "</form>\n"; html += "<form method="get" action="/set">\n"; html += "<input type="hidden" name="off" value="1">\n"; html += "<p><input type="submit" name="submit" value="Alarm off" /></p>\n"; html += "</form>\n"; html += "</body>\n"; html += "</html>\n"; server.send(200, "text/html", html);}// Set alarmvoid 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; charset=utf-8", "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 <= 23 && min >= 0 && min <= 59 && sec >= 0 && sec <= 59) { sprintf(alarm_time, "%02d:%02d:%02d", hour, min, sec); String msg = "Alarm set to "; msg.concat(alarm_time); msg.concat(" ."); server.send(200, "text/plain; charset=utf-8", msg); } else { server.send(200, "text/plain; charset=utf-8", "Incorrect date/time."); } } else { server.send(200, "text/plain; charset=utf-8", "Incorrect parameters."); }}/ Stop alarmvoid handleStopAlarm() { myDFPlayer.pause(); alarm_on = false; tft.drawRect(30, 180, 260, 40, ILI9341_BLACK); server.send(200, "text/plain", "Alarm stop"); }// If an invalid URL is specifiedvoid handleNotFound() { String message = "Not Found : "; message += server.uri(); server.send(404, "text/plain", message);}// Setupvoid setup() { int16_t x1, y1; uint16_t w, h; Serial.begin(115200); strcpy(old_date, "00000000000000"); strcpy(old_time, "00000000"); strcpy(old_alarm, "00000000000000"); strcpy(alarm_time, "Off"); // Initialize display tft.begin(); tft.setRotation(3); tft.fillScreen(ILI9341_BLACK); tft.setTextColor(ILI9341_WHITE); tft.setFont(&FreeSans12pt7b); String s = "Initializing..."; tft.getTextBounds(s, 0, 0, &x1, &y1, &w, &h); tft.setCursor(0, h); tft.println(s); // Connect to WiFi WiFi.begin(ssid, pass); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } WiFi.config(ip, gateway, WiFi.subnetMask(), IPAddress(8, 8, 8, 8), IPAddress(8, 8, 4, 4)); Serial.println(""); Serial.println("WiFi Connected."); tft.println("WiFi Connected."); // Initialize DFPlayer hs.begin(9600, SERIAL_8N1, 16, 17); int count = 0; while (count < 10) { if (!myDFPlayer.begin(hs)) { count++; Serial.print("DFPlayer initialize attempt "); Serial.println(count); } else { break; } } if (count < 10) { Serial.println("DFPlayer Initialized."); tft.println("DFPlayer Initialized."); myDFPlayer.pause(); myDFPlayer.volume(DF_VOLUME); } else { Serial.println("DFPlayer Error."); tft.println("DFPlayer Error."); while(1); } // Initialize RTC if (!rtc.begin()) { Serial.println("Couldn't find RTC"); while (1); } Serial.println("RTC Initialized"); tft.println("RTC Initialized."); // Get current date/time via NTP and set to RTC setTimeByNTP(); // Initialize web server server.on("/", handleRoot); server.on("/set", handleSetAlarm); server.on("/stop", handleStopAlarm); server.onNotFound(handleNotFound); server.begin(); // Fill display with black tft.fillScreen(ILI9341_BLACK);}void loop() { char new_time[9], new_date[15], new_alarm[15]; // Launch web server server.handleClient(); // Display current date/time on LCD DateTime now = rtc.now(); sprintf(new_date, "%04d/%02d/%02d ", now.year(), now.month(), now.day()); strcat(new_date, wdays[now.dayOfTheWeek()]); sprintf(new_time, "%02d:%02d:%02d", now.hour(), now.minute(), now.second()); strcpy(new_alarm, "Alarm "); strcat(new_alarm, alarm_time); tft.setFont(&FreeSans18pt7b); tft.setTextColor(ILI9341_WHITE); showMessage(new_date, old_date, 40, 28); showMessage(new_alarm, old_alarm, 200, 28); tft.setFont(&FreeSans40pt7b); showMessage(new_time, old_time, 120, 64); // Check if current time is time set for alarm if (strstr(new_time, alarm_time) != NULL) { if (!alarm_checked) { // If it's alarm time, ring out then send message to extension unit myDFPlayer.loop(1); alarm_checked = true; alarm_on = true; alarm_ctr = 0; HTTPClient http; http.begin(notify_url); int httpCode = http.GET(); } } else { alarm_checked = false; } // While alarm is sounding, make red frame flash around alarm time on display if (alarm_on) { if (alarm_ctr == 0) { tft.drawRect(30, 180, 260, 40, ILI9341_RED); } else if (alarm_ctr == ALARM_SIG) { tft.drawRect(30, 180, 260, 40, ILI9341_BLACK); } alarm_ctr++; alarm_ctr %= ALARM_SIG * 2; } // Update date/time using NTP at a specific time everyday if (strstr(new_time, adjust_time) != NULL) { if (!ntp_adjusted) { setTimeByNTP(); ntp_adjusted = true; } } else { ntp_adjusted = false; } delay(20);}复制代码
文章评论(0条评论)
登录后参与讨论