0% found this document useful (0 votes)
40 views10 pages

Source Code Cho ESP8266

Source code cho ESP8266

Uploaded by

trungrichie
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views10 pages

Source Code Cho ESP8266

Source code cho ESP8266

Uploaded by

trungrichie
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

#include <ESP8266WiFi.

h>

#include <Wire.h>

#include <ESP8266WebServer.h>

#include <ESP8266mDNS.h>

#include <ESP8266HTTPClient.h> // http web access library

#include <ArduinoJson.h> // JSON decoding library

#include <WiFiUdp.h>

#include <NTPClient.h>

#include <TimeLib.h>

WiFiUDP ntpUDP;

NTPClient timeClient(ntpUDP, "[Link]", 25200, 60000);

byte last_second, second_, minute_, hour_, day_, month_;

int year_;

MDNSResponder mdns;

// Create Access Point Wifi ESP8266

const char* ssid = "FPTU_Library";

const char* password = "12345678";

String set_alarm_page = "<!DOCTYPE html>\n"

"<html lang=\"en\">\n"

" <head>\n"

" <meta charset=\"UTF-8\" />\n"

" <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\" />\n"

" <title>Set Alarm</title>\n"

" </head>\n"

" <body>\n"

" <form action=\"/alarm\" method=\"post\">\n"


" <div>\n"

" <label for=\"date\">Date:</label>\n"

" <input required type=\"date\" name=\"date\" id=\"date\" />\n"

" </div>\n"

" <div>\n"

" <label for=\"time\">Time:</label>\n"

" <input required type=\"time\" name=\"time\" id=\"time\" />\n"

" </div>\n"

" <button type=\"submit\">SET</button>\n"

" </form>\n"

" </body>\n"

"</html>";

String set_time_page = "<!DOCTYPE html>\n"

"<html lang=\"en\">\n"

" <head>\n"

" <meta charset=\"UTF-8\" />\n"

" <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\" />\n"

" <title>Set Time</title>\n"

" </head>\n"

" <body>\n"

" <form action=\"/time\" method=\"post\">\n"

" <div>\n"

" <label for=\"date\">Date:</label>\n"

" <input required type=\"date\" name=\"date\" id=\"date\" />\n"

" </div>\n"

" <div>\n"

" <label for=\"time\">Time:</label>\n"

" <input required type=\"time\" name=\"time\" id=\"time\" />\n"


" </div>\n"

" <button type=\"submit\">SET</button>\n"

" </form>\n"

" </body>\n"

"</html>";

String main_page = "<!DOCTYPE html>\n"

"<html lang=\"en\">\n"

" <head>\n"

" <meta charset=\"UTF-8\" />\n"

" <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\" />\n"

" <title>Alarm</title>\n"

" </head>\n"

" <body>\n"

" <a href=\"/alarm\">Set Alarm Time</a>\n"

" <br />\n"

" <a href=\"/time\">Set Time</a>\n"

" <br />\n"

" <a href=\"/set_current_time\">Set Current Time</a>\n"

" <br />\n"

" <a href=\"/set_weather\">Set Weather</a>\n"

" </body>\n"

"</html>";

ESP8266WebServer server(80);

// Weather

void setup() {
[Link](9600);

[Link](D1, D2);

pinMode(D4, OUTPUT);

[Link]("Connecting to ");

[Link](ssid, password);

while ([Link]() != WL_CONNECTED)

delay(500);

[Link](".");

[Link]("");

[Link]("WiFi connected");

[Link]("IP address: ");

[Link]([Link]());

if ([Link]("esp8266", [Link]()))

[Link]("MDNS responder started");

[Link]("/", handle_get_main);

[Link]("/alarm", handle_alarm);

[Link]("/time", handle_time);

[Link]("/set_current_time", handle_set_current_time);

[Link]("/set_weather", handle_get_weather);

[Link]();

[Link]();

void loop() {
[Link]();

void handle_get_main() {

[Link](200, "text/html", main_page);

void handle_set_current_time() {

// Process your alarm setting logic here...

[Link](8); /* begin with device address 8 */

String statement = "SET_C_TIME ";

get_current_time(statement);

[Link](statement.c_str(), [Link]() + 1); // +1 to include the null terminator

[Link](); /* stop transmitting */

// Extract the redirection URL from the hidden input

String redirectUrl = "/";

// Respond with a redirection to the specified URL

[Link]("Location", redirectUrl, true);

[Link](302, "text/plain", ""); // 302 Redirect response

void handle_alarm() {

if ([Link]("date") && [Link]("time")) {

String date = [Link]("date");

String time = [Link]("time");

// Process your alarm setting logic here...


[Link](8); /* begin with device address 8 */

String statement = "SET_ALARM ";

[Link](date);

[Link](" ");

[Link](time);

[Link](statement.c_str(), [Link]() + 1); // +1 to include the null terminator

[Link](); /* stop transmitting */

// Extract the redirection URL from the hidden input

String redirectUrl = "/";

// Respond with a redirection to the specified URL

[Link]("Location", redirectUrl, true);

[Link](302, "text/plain", ""); // 302 Redirect response

} else {

[Link](200, "text/html", set_alarm_page);

void handle_time() {

if ([Link]("date") && [Link]("time")) {

String date = [Link]("date");

String time = [Link]("time");

// Process your alarm setting logic here...

[Link](8); /* begin with device address 8 */

String statement = "SET_TIME ";

[Link](date);

[Link](" ");

[Link](time);
[Link](statement.c_str(), [Link]() + 1); // +1 to include the null terminator

[Link](); /* stop transmitting */

// Extract the redirection URL from the hidden input

String redirectUrl = "/";

// Respond with a redirection to the specified URL

[Link]("Location", redirectUrl, true);

[Link](302, "text/plain", ""); // 302 Redirect response

} else {

[Link](200, "text/html", set_time_page);

void handle_get_weather() {

HTTPClient http;

WiFiClient client;

float temp;

int humidity;

float pressure;

float wind_speed;

[Link](client, "[Link]
%20City,vn&APPID=6c0da3213e150b86801a8865b3a603ac");

if ([Link]() > 0) {

String payload = [Link](); //Get the request response payload;

DynamicJsonBuffer jsonBuffer(512);

JsonObject& root = [Link](payload);

temp = (float)(root["main"]["temp"]) - 273.15; // get temperature in °C

humidity = root["main"]["humidity"]; // get humidity in %


pressure = (float)(root["main"]["pressure"]) / 1000; // get pressure in bar

wind_speed = root["wind"]["speed"]; // get wind speed in m/s

[Link](temp);

[Link](); //Close connection

[Link](8); /* begin with device address 8 */

String statement = "GET_WEATHER ";

[Link](temp);

[Link](" ");

[Link](humidity);

[Link](" ");

[Link](pressure);

[Link](" ");

[Link](wind_speed);

[Link](statement.c_str(), [Link]() + 1); // +1 to include the null terminator

[Link](); /* stop transmitting */

// Extract the redirection URL from the hidden input

String redirectUrl = "/";

// Respond with a redirection to the specified URL

[Link]("Location", redirectUrl, true);

[Link](302, "text/plain", ""); // 302 Redirect response

void get_current_time(String &stmt) {

[Link]();

unsigned long unix_epoch = [Link](); // Get Unix epoch time from the NTP server
second_ = second(unix_epoch);

char Time[ ] = "[Link]";

char Date[ ] = "2000-00-00";

if (last_second != second_) {

minute_ = minute(unix_epoch);

hour_ = hour(unix_epoch);

day_ = day(unix_epoch);

month_ = month(unix_epoch);

year_ = year(unix_epoch);

Time[7] = second_ % 10 + 48;

Time[6] = second_ / 10 + 48;

Time[4] = minute_ % 10 + 48;

Time[3] = minute_ / 10 + 48;

Time[1] = hour_ % 10 + 48;

Time[0] = hour_ / 10 + 48;

Date[8] = day_ / 10 + 48;

Date[9] = day_ % 10 + 48;

Date[5] = month_ / 10 + 48;

Date[6] = month_ % 10 + 48;

Date[2] = (year_ / 10) % 10 + 48;

Date[3] = year_ % 10 % 10 + 48;

last_second = second_;

[Link](Date);

[Link](" ");
[Link](Time);

You might also like