Oh i’m sorry.
#include <FS.h>
#include <Ticker.h>
#include <Arduino.h>
#include <TimeLib.h>
#include <DNSServer.h>
#include <CountDown.h>
#include <WiFiClient.h>
#include <WiFiManager.h>
#include <ESP8266WiFi.h>
#include <ArduinoJson.h>
#include <ESPNtpClient.h>
#include <SoftwareSerial.h>
#include <ESP8266WebServer.h>
#include <ESP8266httpUpdate.h>
#include <ESP8266HTTPClient.h>
#include <BlynkSimpleEsp8266.h>
#define TRIGGER_PIN 0
#define NFC_PIN 12
#define CurrentVersion 1.01
#define BLYNK_Green "#23C48E"
#define BLYNK_Red "#D3435C"
ESP8266WebServer serverh(800);
const char* www_username = "admin";
const char* www_password = "esp8266";
CountDown CD;
Ticker ticker;
WiFiManager wifiManager;
char server[15] = "";
int port2 = 8080;
char blynk_token[34] = "";
char comma = ':';
char comma2 = '-';
char comma3 = '/';
String StringValue;
String StringValue1;
String StringValue2;
String StringValue3;
String StringValue4;
String StringValue5;
String StringValue6;
String StringValue7;
String StringValue8;
String StringValue9;
String StringValue10;
String StringValue11;
String StringValue12;
String StringValue13;
String StringValue14;
String Checkin;
String Checkout;
unsigned long int lastr;
int daycount;
int Hourcount;
unsigned long int saat;
unsigned long int daqiqe;
unsigned long int sanie;
unsigned long int rooz;
int nimcharge = 43200;
unsigned long int nimcharged;
bool wm_nonblocking = false;
bool shouldSaveConfig = false;
char ntpServer[16] = "";
char roomnum[4] = "";
char floornum[3] = "";
char mystr[6] = "nodeo";
char mystrz[2] = "0";
char mystrgu[7] = "gueacc";
char mystrgu2[8] = "gueaccn";
char mystrgu3[7] = "eprclr";
char mystrgu4[6] = "motoo";
char mystrgu5[6] = "motoc";
SoftwareSerial nfcdata(5, 2);
SoftwareSerial eepdata(15, 13);
WidgetLED ROOMLED(V10);
BLYNK_WRITE(V0) {
int pinValue = param.asInt();
if (pinValue == 1) {
Blynk.virtualWrite(V1, "system active");
digitalWrite(NFC_PIN, LOW);
}
if (pinValue == 0) {
Blynk.virtualWrite(V1, "system deactive");
digitalWrite(NFC_PIN, HIGH);
}
}
BLYNK_WRITE(V2) {
int pinValue1 = param.asInt();
if (pinValue1 == 1) {
Blynk.virtualWrite(V1, "door opened");
nfcdata.write(mystr, 6);
StringValue10 = (String(hour()) + comma + String(minute()) + comma + String(second()) + comma2 + String(year()) + comma3 + String(month()) + comma3 + String(day()) + comma2 + roomnum + " Master entered");
File file = SPIFFS.open("/log.txt", "a");
file.println(StringValue10);
file.close();
}
if (pinValue1 == 0) {
nfcdata.write(mystrz, 2);
Serial.flush();
delay(2000);
Blynk.virtualWrite(V1, "------");
Serial.flush();
}
}
BLYNK_WRITE(V4) {
int pinValue2 = param.asInt();
if (pinValue2 == 1) {
Blynk.virtualWrite(V1, "Guest lock");
nfcdata.write(mystrgu, 7);
Serial.flush();
}
if (pinValue2 == 0) {
nfcdata.write(mystrgu2, 8);
delay(2000);
Blynk.virtualWrite(V1, "------");
Serial.flush();
}
}
BLYNK_WRITE(V6) {
String pinValue3 = param.asStr();
for (int i = 0; i < pinValue3.length(); i++)
eepdata.write(pinValue3[i]);
Serial.flush();
delay(5000);
Blynk.virtualWrite(V6, "------");
}
BLYNK_WRITE(V7) {
int pinValue4 = param.asInt();
if (pinValue4 == 1) {
Blynk.virtualWrite(V1, "Emergency lock");
nfcdata.write(mystrgu4, 6);
Serial.flush();
}
if (pinValue4 == 0) {
nfcdata.write(mystrgu5, 6);
delay(2000);
Blynk.virtualWrite(V1, "------");
Serial.flush();
}
}
BLYNK_WRITE(V8) {
int pinValue5 = param.asInt();
daycount = pinValue5;
CD.stop();
CD.start(0, 0, 0, 0);
CD.stop();
CD.start(daycount, 0, 0, 0);
StringValue12 = String(daycount) + " day";
Blynk.virtualWrite(V11, StringValue12);
Blynk.virtualWrite(V1, "------");
Checkin = (String(hour()) + comma + String(minute()) + comma + String(second()) + comma2 + String(year()) + comma3 + String(month()) + comma3 + String(day()) + comma2 + roomnum + " Enter");
delay(1000);
Blynk.virtualWrite(V27, "------");
Blynk.virtualWrite(V26, Checkin);
Blynk.virtualWrite(V4, 0);
Blynk.virtualWrite(V8, "------");
}
BLYNK_WRITE(V11) {
int pinValue6 = param.asInt();
}
BLYNK_WRITE(V15) {
int pinValue8 = param.asInt();
if (pinValue8 == 1) {
Checkout = (String(hour()) + comma + String(minute()) + comma + String(second()) + comma2 + String(year()) + comma3 + String(month()) + comma3 + String(day()) + comma2 + roomnum + " Exit");
Blynk.virtualWrite(V27, Checkout);
CD.stop();
CD.start(0, 0, 0, 0);
CD.stop();
}
}
BLYNK_WRITE(V17) {
int pinValue9 = param.asInt();
lastr = pinValue9;
}
BLYNK_WRITE(V18) {
int pinValue10 = param.asInt();
if (pinValue10 == 1) {
CD.stop();
CD.start(0, 0, 0, nimcharge + nimcharged);
Blynk.virtualWrite(V4, 0);
}
}
BLYNK_WRITE(V19) {
int pinValue11 = param.asInt();
int fw = 1236547890;
if (pinValue11 == fw) {
wifiManager.setConfigPortalTimeout(120);
ticker.attach(0.1, tick);
Blynk.virtualWrite(V19, "------");
if (!wifiManager.startConfigPortal("ReConfig NFC", "12345678")) {
}
}
if (pinValue11 == 100000) {
Blynk.virtualWrite(V19, "------");
delay(2000);
ESP.reset();
}
if (pinValue11 == 700000) {
Blynk.virtualWrite(V19, "------");
delay(2000);
wifiManager.resetSettings();
SPIFFS.format();
ESP.restart();
}
}
BLYNK_WRITE(V20) {
int pinValue12 = param.asInt();
if (pinValue12 == 1) {
check_for_updates();
}
}
BLYNK_WRITE(V25) {
int pinValue13 = param.asInt();
Hourcount = pinValue13;
CD.stop();
CD.start(0, Hourcount, 0, 0);
StringValue11 = String(Hourcount) + " clock";
Blynk.virtualWrite(V11, StringValue11);
Blynk.virtualWrite(V1, "------");
Checkin = (String(hour()) + comma + String(minute()) + comma + String(second()) + comma2 + String(year()) + comma3 + String(month()) + comma3 + String(day()) + comma2 + roomnum + " Enter");
delay(1000);
Blynk.virtualWrite(V27, "------");
Blynk.virtualWrite(V26, Checkin);
Blynk.virtualWrite(V4, 0);
Blynk.virtualWrite(V25, "------");
}
BLYNK_WRITE(V26) {
String pinValue14 = param.asStr();
StringValue13 = pinValue14;
}
BLYNK_WRITE(V27) {
String pinValue15 = param.asStr();
StringValue14 = pinValue15;
}
void handleRootPath() {
if (serverh.method() == HTTP_GET && serverh.uri() == "/") {
String path = "/log.txt";
if (SPIFFS.exists(path)) {
if (!serverh.authenticate(www_username, www_password)) {
return serverh.requestAuthentication();
}
File file = SPIFFS.open(path, "r");
size_t sent = serverh.streamFile(file, "application/octet-stream");
file.close();
} else {
serverh.send(404, "text/plain", "404: Not Found");
}
}
}
void setup() {
Serial.begin(19200);
nfcdata.begin(19200);
eepdata.begin(19200);
pinMode(BUILTIN_LED, OUTPUT);
pinMode(NFC_PIN, OUTPUT);
pinMode(TRIGGER_PIN, INPUT);
digitalWrite(NFC_PIN, LOW);
ticker.attach(0.6, tick);
wifiManager.setClass("invert");
Serial.println("Mounting FS...");
if (SPIFFS.begin()) {
Serial.println("Mounted file system");
if (SPIFFS.exists("/config.json")) {
Serial.println("Reading config file");
File configFile = SPIFFS.open("/config.json", "r");
if (configFile) {
Serial.println("Opened config file");
size_t size = configFile.size();
std::unique_ptr<char[]> buf(new char[size]);
configFile.readBytes(buf.get(), size);
DynamicJsonDocument json(1024);
auto deserializeError = deserializeJson(json, buf.get());
serializeJson(json, Serial);
if (!deserializeError) {
Serial.println("\nparsed json");
strcpy(server, json["server"]);
strcpy(blynk_token, json["blynk_token"]);
strcpy(ntpServer, json["ntpServer"]);
strcpy(roomnum, json["roomnum"]);
strcpy(floornum, json["floornum"]);
} else {
Serial.println("Failed to load json config");
}
}
}
} else {
Serial.println("Failed to mount FS");
}
wifiManager.setSaveConfigCallback(saveConfigCallback);
WiFiManagerParameter custom_server("server", "Server", server, 15);
WiFiManagerParameter custom_blynk_token("blynk", "token", blynk_token, 33);
WiFiManagerParameter custom_nserver("ntpServer", "NTP IP", ntpServer, 16);
WiFiManagerParameter custom_roomnum("roomnum", "ROMM NUM", roomnum, 4);
WiFiManagerParameter custom_floornum("floornum", "Floor", floornum, 3);
Serial.println(blynk_token);
wifiManager.addParameter(&custom_server);
wifiManager.addParameter(&custom_blynk_token);
wifiManager.addParameter(&custom_nserver);
wifiManager.addParameter(&custom_roomnum);
wifiManager.addParameter(&custom_floornum);
wifiManager.setTimeout(180);
if (!wifiManager.autoConnect("NFC Access Config", "12345678")) {
Serial.println("Failed to connect and hit timeout");
delay(3000);
ESP.reset();
delay(5000);
}
Serial.println("Motasel shod :)");
ticker.detach();
strcpy(server, custom_server.getValue());
strcpy(blynk_token, custom_blynk_token.getValue());
strcpy(ntpServer, custom_nserver.getValue());
strcpy(roomnum, custom_roomnum.getValue());
strcpy(floornum, custom_floornum.getValue());
if (shouldSaveConfig) {
Serial.println("saving config");
DynamicJsonDocument json(1024);
//auto deserializeError = deserializeJson(json, buf.get());
json["server"] = server;
json["blynk_token"] = blynk_token;
json["ntpServer"] = ntpServer;
json["roomnum"] = roomnum;
json["floornum"] = floornum;
WiFi.hostname(roomnum);
ticker.detach();
flr();
File configFile = SPIFFS.open("/config.json", "w");
if (!configFile) {
Serial.println("Failed to open config file for writing");
}
serializeJson(json, Serial);
serializeJson(json, configFile);
configFile.close();
}
Serial.println("local ip");
Serial.println(WiFi.localIP());
WiFi.hostname(roomnum);
Blynk.config(blynk_token, server, port2);
Blynk.connect();
Blynk.syncVirtual(V4);
Blynk.syncVirtual(V0);
Blynk.syncVirtual(V17);
Blynk.syncVirtual(V11);
Blynk.syncVirtual(V26);
Blynk.syncVirtual(V27);
Blynk.virtualWrite(V6, "------");
Blynk.virtualWrite(V8, "------");
NTP.setTimeZone(TZ_Asia_Tehran);
NTP.begin(ntpServer);
NTP.onNTPSyncEvent(ntpEventHandler);
serverh.on("/", handleRootPath);
serverh.begin();
check_for_updates();
CD.stop();
CD.start(0, 0, 0, lastr);
Serial.flush();
}
void ntphhh() {
Blynk.virtualWrite(V13, WiFi.localIP().toString());
Blynk.virtualWrite(V16, map(WiFi.RSSI(), -105, -40, 0, 100));
}
void flr() {
eepdata.write("w,20,");
eepdata.write(floornum);
}
void tick() {
int state = digitalRead(BUILTIN_LED);
digitalWrite(BUILTIN_LED, !state);
}
void saveConfigCallback() {
Serial.println("Should save config");
shouldSaveConfig = true;
ticker.attach(0.2, tick);
}
void checkButton() {
if (digitalRead(TRIGGER_PIN) == LOW) {
delay(50);
if (digitalRead(TRIGGER_PIN) == LOW) {
Serial.println("Button Pressed");
delay(5000);
if (digitalRead(TRIGGER_PIN) == LOW) {
Serial.println("Button Held");
Serial.println("Erasing Config, restarting");
wifiManager.resetSettings();
SPIFFS.format();
ESP.restart();
}
Serial.println("Starting config portal");
wifiManager.setConfigPortalTimeout(120);
ticker.attach(0.1, tick);
if (!wifiManager.startConfigPortal("ReConfig NFC", "12345678")) {
Serial.println("failed to connect or hit timeout");
Serial.flush();
ESP.restart();
delay(5000);
} else {
Serial.println("connected...yeey :)");
Serial.flush();
}
}
}
}
void ntpEventHandler(NTPEvent_t e) {
if (e.event == timeSyncd || e.event == partlySync) { // && NTP.millis() % 1000
time_t utcTime = time(NULL);
tm* currentTime = localtime(&utcTime);
//Serial.printf("Hour: %d\n", currentTime->tm_hour);
//Serial.printf("Minute: %d\n", currentTime->tm_min);
//Serial.printf("Second: %d\n", currentTime->tm_sec);
setTime(currentTime->tm_hour, currentTime->tm_min, currentTime->tm_sec + 1, currentTime->tm_mday, currentTime->tm_mon + 1, currentTime->tm_year + 1900);
}
if (timeStatus() == timeSet) {
} else {
}
}
void nfcreaddata() {
if (nfcdata.available() > 0) {
Serial.flush();
StringValue = (String(hour()) + comma + String(minute()) + comma + String(second()) + comma2 + String(year()) + comma3 + String(month()) + comma3 + String(day()) + comma2 + roomnum + " G1");
StringValue1 = (String(hour()) + comma + String(minute()) + comma + String(second()) + comma2 + String(year()) + comma3 + String(month()) + comma3 + String(day()) + comma2 + roomnum + " G2");
StringValue2 = (String(hour()) + comma + String(minute()) + comma + String(second()) + comma2 + String(year()) + comma3 + String(month()) + comma3 + String(day()) + comma2 + roomnum + " G3");
StringValue3 = (String(hour()) + comma + String(minute()) + comma + String(second()) + comma2 + String(year()) + comma3 + String(month()) + comma3 + String(day()) + comma2 + roomnum + " G4");
StringValue4 = (String(hour()) + comma + String(minute()) + comma + String(second()) + comma2 + String(year()) + comma3 + String(month()) + comma3 + String(day()) + comma2 + roomnum + " G5");
StringValue5 = (String(hour()) + comma + String(minute()) + comma + String(second()) + comma2 + String(year()) + comma3 + String(month()) + comma3 + String(day()) + comma2 + roomnum + " G6");
StringValue6 = (String(hour()) + comma + String(minute()) + comma + String(second()) + comma2 + String(year()) + comma3 + String(month()) + comma3 + String(day()) + comma2 + roomnum + " G7");
StringValue7 = (String(hour()) + comma + String(minute()) + comma + String(second()) + comma2 + String(year()) + comma3 + String(month()) + comma3 + String(day()) + comma2 + roomnum + " G8");
StringValue8 = (String(hour()) + comma + String(minute()) + comma + String(second()) + comma2 + String(year()) + comma3 + String(month()) + comma3 + String(day()) + comma2 + roomnum + " G9");
String teststr = nfcdata.readString();
teststr.trim();
Serial.println(teststr);
if (teststr == "open") {
Blynk.virtualWrite(V5, "door opened");
Blynk.virtualWrite(V3, StringValue);
Serial.println("DOOR OPENED");
Serial.flush();
delay(2000);
Blynk.virtualWrite(V5, "------");
File file = SPIFFS.open("/log.txt", "a");
file.println(StringValue);
file.close();
}
if (teststr == "error") {
Blynk.virtualWrite(V5, "admin entered");
Blynk.virtualWrite(V3, StringValue4);
Serial.println("DOOR NOT OPENED");
Serial.flush();
delay(2000);
Blynk.virtualWrite(V5, "------");
File file = SPIFFS.open("/log.txt", "a");
file.println(StringValue4);
file.close();
}
if (teststr == "admin") {
Blynk.virtualWrite(V5, "admin entered");
Serial.flush();
delay(2000);
Blynk.virtualWrite(V5, "------");
}
if (teststr == "noadmin") {
Blynk.virtualWrite(V5, "admin exit");
Serial.flush();
delay(2000);
Blynk.virtualWrite(V5, "------");
}
if (teststr == "add") {
Blynk.virtualWrite(V5, "NFC added");
Serial.flush();
delay(2000);
Blynk.virtualWrite(V5, "------");
}
if (teststr == "remove") {
Blynk.virtualWrite(V5, "NFC removed");
Serial.flush();
delay(2000);
Blynk.virtualWrite(V5, "------");
}
if (teststr == "wifi") {
Serial.flush();
delay(2000);
wifiManager.resetSettings();
SPIFFS.format();
ESP.restart();
}
if (teststr == "khane") {
Blynk.virtualWrite(V5, "door opened");
Blynk.virtualWrite(V3, StringValue1);
Serial.println("DOOR OPENED");
Serial.flush();
delay(2000);
Blynk.virtualWrite(V5, "------");
File file = SPIFFS.open("/log.txt", "a");
file.println(StringValue1);
file.close();
}
if (teststr == "tasisat") {
Blynk.virtualWrite(V5, "door opened");
Blynk.virtualWrite(V3, StringValue2);
Serial.println("DOOR OPENED");
Serial.flush();
delay(2000);
Blynk.virtualWrite(V5, "------");
File file = SPIFFS.open("/log.txt", "a");
file.println(StringValue2);
file.close();
}
if (teststr == "master") {
Blynk.virtualWrite(V5, "door opened");
Blynk.virtualWrite(V3, StringValue3);
Serial.println("DOOR OPENED");
Serial.flush();
delay(2000);
Blynk.virtualWrite(V5, "------");
File file = SPIFFS.open("/log.txt", "a");
file.println(StringValue3);
file.close();
}
if (teststr == "bellboy") {
Blynk.virtualWrite(V5, "door opened");
Blynk.virtualWrite(V3, StringValue5);
Serial.println("DOOR OPENED");
Serial.flush();
delay(2000);
Blynk.virtualWrite(V5, "------");
File file = SPIFFS.open("/log.txt", "a");
file.println(StringValue5);
file.close();
}
if (teststr == "bellboyno") {
Blynk.virtualWrite(V3, StringValue6);
Serial.flush();
delay(2000);
File file = SPIFFS.open("/log.txt", "a");
file.println(StringValue6);
file.close();
}
if (teststr == "tasisatno") {
Blynk.virtualWrite(V3, StringValue7);
Serial.flush();
delay(2000);
File file = SPIFFS.open("/log.txt", "a");
file.println(StringValue7);
file.close();
}
if (teststr == "khaneno") {
Blynk.virtualWrite(V3, StringValue8);
Serial.flush();
delay(2000);
File file = SPIFFS.open("/log.txt", "a");
file.println(StringValue8);
file.close();
}
}
}
void connectionstatus() {
if ((WiFi.status() != WL_CONNECTED)) {
ESP.restart();
} else {
}
if (!Blynk.connected()) {
Blynk.connect();
} else {
}
}
void countday() {
static uint32_t last_remaining = 0;
if (last_remaining != CD.remaining() || CD.remaining() == 0) {
last_remaining = CD.remaining();
nimcharged = CD.remaining();
}
rooz = saat / 24;
saat = last_remaining / 3600L; // in integer arithmetics
daqiqe = (last_remaining - 3600UL * saat) / 60UL; // in integer arithmetics
sanie = last_remaining - 3600UL * saat - 60UL * daqiqe;
StringValue9 = (String(rooz) + " " + "Day" + " " + comma2 + " " + "و" + " " + String(saat) + comma + String(daqiqe));
Blynk.virtualWrite(V9, StringValue9);
Blynk.virtualWrite(V17, last_remaining);
if (CD.remaining() > 0) {
ROOMLED.on();
ROOMLED.setColor(BLYNK_Red);
}
if (CD.remaining() == 0) {
Blynk.virtualWrite(V4, 1);
CD.start(0, 0, 0, 0);
CD.stop();
Blynk.virtualWrite(V9, "End");
ROOMLED.on();
ROOMLED.setColor(BLYNK_Green);
}
}
void update_started() {
Blynk.virtualWrite(V21, "Updating");
}
void otaUpdate(String url) {
WiFiClient client;
HTTPClient http;
ESPhttpUpdate.setLedPin(LED_BUILTIN, LOW);
ESPhttpUpdate.onStart(update_started);
t_httpUpdate_return ret = ESPhttpUpdate.update(client, url);
switch (ret) {
case HTTP_UPDATE_FAILED:
Serial.printf("HTTP_UPDATE_FAILD Error (%d): %s\n", ESPhttpUpdate.getLastError(), ESPhttpUpdate.getLastErrorString().c_str());
break;
case HTTP_UPDATE_NO_UPDATES:
Serial.println("HTTP_UPDATE_NO_UPDATES");
break;
case HTTP_UPDATE_OK:
Serial.println("HTTP_UPDATE_OK");
break;
}
}
void check_for_updates() {
WiFiClient client;
HTTPClient http;
http.begin(client, "http://10.5.51.5/updates/firmware.json");
int httpCode = http.GET();
String payload = "";
if (httpCode > 0) {
payload = http.getString();
}
http.end(); //Close connection
String json = "";
DynamicJsonDocument doc(1024);
json = payload.c_str();
DeserializationError error = deserializeJson(doc, json);
if (error) {
}
if (doc["ver"] > CurrentVersion && doc["ver"] != CurrentVersion) {
Blynk.virtualWrite(V21, "Downloading");
delay(3000);
otaUpdate(doc["link"]);
} else {
Blynk.virtualWrite(V21, "Latest is installed");
}
}
void loop() {
if (wm_nonblocking) wifiManager.process();
checkButton();
serverh.handleClient();
Blynk.run();
nfcreaddata();
static unsigned long ntphhh_timer = millis();
if (millis() - ntphhh_timer >= 10000) {
ntphhh();
ntphhh_timer = millis();
}
// Run 'connectionstatus' every 1 minute
static unsigned long connectionstatus_timer = millis();
if (millis() - connectionstatus_timer >= 60000) {
connectionstatus();
connectionstatus_timer = millis();
}
// Run 'check_for_updates' every 30 minutes
static unsigned long check_for_updates_timer = millis();
if (millis() - check_for_updates_timer >= 1800000) {
check_for_updates();
check_for_updates_timer = millis();
}
// Run 'countday' every 2 seconds
static unsigned long countday_timer = millis();
if (millis() - countday_timer >= 2000) {
countday();
countday_timer = millis();
}
}