0% found this document useful (0 votes)
13 views

Source Code Cho ESP8266

Source code cho ESP8266

Uploaded by

trungrichie
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Source Code Cho ESP8266

Source code cho ESP8266

Uploaded by

trungrichie
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

#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, "asia.pool.ntp.org", 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() {
Serial.begin(9600);

Wire.begin(D1, D2);

pinMode(D4, OUTPUT);

Serial.println("Connecting to ");

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());

if (mdns.begin("esp8266", WiFi.localIP()))

Serial.println("MDNS responder started");

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() {

server.send(200, "text/html", main_page);

void handle_set_current_time() {

// Process your alarm setting logic here...

Wire.beginTransmission(8); /* begin with device address 8 */

String statement = "SET_C_TIME ";

get_current_time(statement);

Wire.write(statement.c_str(), statement.length() + 1); // +1 to include the null terminator

Wire.endTransmission(); /* stop transmitting */

// Extract the redirection URL from the hidden input

String redirectUrl = "/";

// Respond with a redirection to the specified URL

server.sendHeader("Location", redirectUrl, true);

server.send(302, "text/plain", ""); // 302 Redirect response

void handle_alarm() {

if (server.hasArg("date") && server.hasArg("time")) {

String date = server.arg("date");

String time = server.arg("time");

// Process your alarm setting logic here...


Wire.beginTransmission(8); /* begin with device address 8 */

String statement = "SET_ALARM ";

statement.concat(date);

statement.concat(" ");

statement.concat(time);

Wire.write(statement.c_str(), statement.length() + 1); // +1 to include the null terminator

Wire.endTransmission(); /* stop transmitting */

// Extract the redirection URL from the hidden input

String redirectUrl = "/";

// Respond with a redirection to the specified URL

server.sendHeader("Location", redirectUrl, true);

server.send(302, "text/plain", ""); // 302 Redirect response

} else {

server.send(200, "text/html", set_alarm_page);

void handle_time() {

if (server.hasArg("date") && server.hasArg("time")) {

String date = server.arg("date");

String time = server.arg("time");

// Process your alarm setting logic here...

Wire.beginTransmission(8); /* begin with device address 8 */

String statement = "SET_TIME ";

statement.concat(date);

statement.concat(" ");

statement.concat(time);
Wire.write(statement.c_str(), statement.length() + 1); // +1 to include the null terminator

Wire.endTransmission(); /* stop transmitting */

// Extract the redirection URL from the hidden input

String redirectUrl = "/";

// Respond with a redirection to the specified URL

server.sendHeader("Location", redirectUrl, true);

server.send(302, "text/plain", ""); // 302 Redirect response

} else {

server.send(200, "text/html", set_time_page);

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) {

String payload = http.getString(); //Get the request response payload;

DynamicJsonBuffer jsonBuffer(512);

JsonObject& root = jsonBuffer.parseObject(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

Serial.print(temp);

http.end(); //Close connection

Wire.beginTransmission(8); /* begin with device address 8 */

String statement = "GET_WEATHER ";

statement.concat(temp);

statement.concat(" ");

statement.concat(humidity);

statement.concat(" ");

statement.concat(pressure);

statement.concat(" ");

statement.concat(wind_speed);

Wire.write(statement.c_str(), statement.length() + 1); // +1 to include the null terminator

Wire.endTransmission(); /* stop transmitting */

// Extract the redirection URL from the hidden input

String redirectUrl = "/";

// Respond with a redirection to the specified URL

server.sendHeader("Location", redirectUrl, true);

server.send(302, "text/plain", ""); // 302 Redirect response

void get_current_time(String &stmt) {

timeClient.update();

unsigned long unix_epoch = timeClient.getEpochTime(); // Get Unix epoch time from the NTP server
second_ = second(unix_epoch);

char Time[ ] = "00:00:00";

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_;

stmt.concat(Date);

stmt.concat(" ");
stmt.concat(Time);

You might also like