Source Code Cho ESP8266
Source Code Cho ESP8266
h>
#include <Wire.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <NTPClient.h>
#include <TimeLib.h>
WiFiUDP ntpUDP;
int year_;
MDNSResponder mdns;
"<html lang=\"en\">\n"
" <head>\n"
" </head>\n"
" <body>\n"
" </div>\n"
" <div>\n"
" </div>\n"
" </form>\n"
" </body>\n"
"</html>";
"<html lang=\"en\">\n"
" <head>\n"
" </head>\n"
" <body>\n"
" <div>\n"
" </div>\n"
" <div>\n"
" </form>\n"
" </body>\n"
"</html>";
"<html lang=\"en\">\n"
" <head>\n"
" <title>Alarm</title>\n"
" </head>\n"
" <body>\n"
" </body>\n"
"</html>";
ESP8266WebServer server(80);
// Weather
void setup() {
Serial.begin(9600);
Wire.begin(D1, D2);
pinMode(D4, OUTPUT);
Serial.println("Connecting to ");
WiFi.begin(ssid, password);
delay(500);
Serial.print(".");
Serial.println("");
Serial.println("WiFi connected");
Serial.println(WiFi.localIP());
if (mdns.begin("esp8266", WiFi.localIP()))
server.on("/", handle_get_main);
server.on("/alarm", handle_alarm);
server.on("/time", handle_time);
server.on("/set_current_time", handle_set_current_time);
server.on("/set_weather", handle_get_weather);
server.begin();
timeClient.begin();
void loop() {
server.handleClient();
void handle_get_main() {
void handle_set_current_time() {
get_current_time(statement);
void handle_alarm() {
statement.concat(date);
statement.concat(" ");
statement.concat(time);
} else {
void handle_time() {
statement.concat(date);
statement.concat(" ");
statement.concat(time);
Wire.write(statement.c_str(), statement.length() + 1); // +1 to include the null terminator
} else {
void handle_get_weather() {
HTTPClient http;
WiFiClient client;
float temp;
int humidity;
float pressure;
float wind_speed;
http.begin(client, "http://api.openweathermap.org/data/2.5/weather?q=Ho%20Chi%20Minh
%20City,vn&APPID=6c0da3213e150b86801a8865b3a603ac");
if (http.GET() > 0) {
DynamicJsonBuffer jsonBuffer(512);
Serial.print(temp);
statement.concat(temp);
statement.concat(" ");
statement.concat(humidity);
statement.concat(" ");
statement.concat(pressure);
statement.concat(" ");
statement.concat(wind_speed);
timeClient.update();
unsigned long unix_epoch = timeClient.getEpochTime(); // Get Unix epoch time from the NTP server
second_ = second(unix_epoch);
if (last_second != second_) {
minute_ = minute(unix_epoch);
hour_ = hour(unix_epoch);
day_ = day(unix_epoch);
month_ = month(unix_epoch);
year_ = year(unix_epoch);
last_second = second_;
stmt.concat(Date);
stmt.concat(" ");
stmt.concat(Time);