I am using arduino mega2560 and shield ethernet module.
I can’t edit the post because it’s been posted for too long.
The command lines is called upon WidgetButon action so i think its cycle is enough time to display. Can you tell me the time between these 2 command line calls?
yes I have placed the code in triple backticks.
My code has 1300 lines more than 32000 characters so it is limited
Below is the code that I have removed irrelevant lines so I can post it.
#define BLYNK_TEMPLATE_ID "TMPL65Ve1w7sj"
#define BLYNK_TEMPLATE_NAME "giam sat trung tam"
#define BLYNK_AUTH_TOKEN "3hg80WRz_VxgstG15_Vs7CC4O53oQKwa"
#define BLYNK_PRINT Serial
#include <Ethernet.h>
#include <avr/wdt.h>
#include <SPI.h>
#include <BlynkSimpleEthernet.h>
#include <WidgetRTC.h>
#include <EEPROM.h>
#include <DFRobotDFPlayerMini.h>
DFRobotDFPlayerMini myDFPlayer;
#include <TimerOne.h>
#include <TimerThree.h>
#include "DHT.h"
const int DHTPIN = 49; //Đọc dữ liệu từ DHT11 ở chân 2 trên mạch Arduino
const int DHTTYPE = DHT11; //Khai báo loại cảm biến, có 2 loại là DHT11 và DHT22
DHT dht(DHTPIN, DHTTYPE);
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C manhinh(0x27,20,4);
#include "RTClib.h"
RTC_DS1307 dongho;
void Gsm_Init();
void Gsm_MakeCall(String phone);
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
char auth[] = BLYNK_AUTH_TOKEN;
void Gsm_MakeSMS(String phone,String content);
const String myphone = "0396447217"; // Thay so cua ban vao day
String myphone2,myphone1;
//định nghĩa chân đầu vào ra
#define W5100_CS 10
#define SDCARD_CS 4
#define dauvao1 23
#define dauvao2 25
#define dauvao3 27
#define dauvao4 29
#define dauvao5 31
#define dauvao6 33
#define dauvao7 35
#define dauvao8 37
#define dauvao9 39
#define dauvao10 41
#define dauvao11 22
#define dauvao12 24
#define dauvao13 26
#define dauvao14 28
#define dauvao15 30
#define dauvao16 32
#define dauvao17 34
#define dauvao18 36
#define dauvao19 38
#define dauvao20 40
#define khoidonglai 42
#define phatxung 45
#define nhantinhieutreo 43
#define denmang 47
// khai báo vung nhớ
#define giokhoidong 3000
#define phutkhoidong 3001
#define thoigiantamdung 3002
#define amluong 3003
#define onhietdoCB 3004
//ten dau vao từ 1 đến 300
//cau cảnh báo từ 301 đến 700
//lịch sử từ 701 đến 1050
//thoi gian canh bao từ 2000 den 2019
//độ nhạy từ 2020 den 2039
//file canh bao loa từ 2040 den 2059
//khai báo biến
int al,tgtd,dn;
int giay,nhietdoCB,tamdungblynk,hienthilsblynk;
int id,cogoitam;
int filethongbaotam,thoigianCBtam,donhaytam;
unsigned long thoigian,moctggoidien,timedauvaoND,timedvphatxung;
String tendauvaotam,caucanhbaotam;
bool internet,trangthai;
float h,t;
bool cogoi,bitgoi;
bool CDid,CTDtendauvao,CTDcaucanhbao,CTDfilethongbao,CTDdonhay,CTDthoigianCB;
bool CDtendauvao,CDcaucanhbao,CDfilethongbao,CDdonhay,CDthoigianCB,CDnhanh;
const int sochan[20]={23,25,27,29,31,33,35,37,39,41,22,24,26,28,30,32,34,36,38,40};
unsigned long timedauvaoDN[15];
unsigned long timedauvaoCB[15];
bool cocanhbao[15];
byte sodienthoai[12];
String tendauvao[20];
String caucanhbao[20];
String tenled[20]={"ldv1","ldv2","ldv3","ldv4","ldv5","ldv6","ldv7","ldv8","ldv9","ldv10","ldv11","ldv12","ldv13","ldv14","ldv15","ldv16","ldv17","ldv18","ldv19","ldv20"};
byte thoigianCB[20];
byte donhay[20];
byte filethongbao[20];
//
//WidgetLCD hienthisdt(V25);
WidgetTerminal manhinhterminal(V33);
WidgetRTC rtc;
WidgetLED ldv1(V3);
WidgetLED ldv2(V4);
WidgetLED ldv3(V5);
WidgetLED ldv4(V6);
WidgetLED ldv5(V7);
WidgetLED ldv6(V8);
WidgetLED ldv7(V9);
WidgetLED ldv8(V10);
WidgetLED ldv9(V11);
WidgetLED ldv10(V12);
WidgetLED ldv11(V13);
WidgetLED ldv12(V14);
WidgetLED ldv13(V15);
WidgetLED ldv14(V16);
WidgetLED ldv15(V17);
WidgetLED ldv16(V18);
WidgetLED ldv17(V19);
WidgetLED ldv18(V20);
WidgetLED ldv19(V21);
WidgetLED ldv20(V22);
BLYNK_WRITE(V29)
{
// nut restart
digitalWrite(khoidonglai,HIGH);
}
BLYNK_WRITE(V28)
{
//nut tam dung
tamdungblynk=1;
}
BLYNK_WRITE(V47)
{
//nut hien thi lich su
hienthilsblynk=1;
}
BLYNK_WRITE(V50)
{
//nut xoa terminal
manhinhterminal.clear();
}
BLYNK_WRITE(V42)
{
// nhap gio khoi dong
EEPROM.write(giokhoidong,param.asInt()); // assigning incoming value from pin V1 to a variable
}
BLYNK_WRITE(V43)
{
// nhập phút khởi động
EEPROM.write(phutkhoidong,param.asInt()); // assigning incoming value from pin V1 to a variable
}
BLYNK_WRITE(V44)
{
//nhập thời gian tạm dừng
tgtd = param.asInt(); // assigning incoming value from pin V1 to a variable
EEPROM.write(thoigiantamdung,tgtd);
}
BLYNK_WRITE(V45)
{
// nhập am lương cảnh báo
al = param.asInt(); // assigning incoming value from pin V1 to a variable
EEPROM.write(amluong,al);
myDFPlayer.volume(al); //Set volume value. From 0 to 30
}
BLYNK_WRITE(V46)
{
// nhap nhiet do canh bao
nhietdoCB = param.asInt(); // assigning incoming value from pin V1 to a variable
EEPROM.write(onhietdoCB,nhietdoCB);
}
// luu sdt..................//
//luu so dien thoai
BLYNK_WRITE(V23)
{
WidgetLCD hienthisdt(V25);
String sdt = param.asStr();
sdt.getBytes(sodienthoai,11);
int tam=2100;
for(int i=0;i<11;i++){
Serial.println(sodienthoai[i]-48);
EEPROM.update(tam,sodienthoai[i]-48);
tam=tam+1;
}
myphone1="";
for(int i= 2100;i<2110;i++){
myphone1 += String(EEPROM.read(i));
delay(30);
}
Serial.println(myphone1);
manhinhterminal.println("so dien thoai 1 da cai dat: "+myphone1);
manhinhterminal.flush();
}
BLYNK_WRITE(V24)
{
WidgetLCD hienthisdt(V25);
String sdt = param.asStr();
sdt.getBytes(sodienthoai,11);
int tam=2110;
for(int i=0;i<11;i++){
Serial.println(sodienthoai[i]-48);
EEPROM.update(tam,sodienthoai[i]-48);
tam=tam+1;
}
myphone2="";
for(int i = 2110;i<2120;i++){
myphone2 += String(EEPROM.read(i));
delay(30);
}
Serial.println(myphone2);
manhinhterminal.println("so dien thoai 2 da cai dat: "+myphone2);
manhinhterminal.flush();
}
// ket thuc luu sdt
BLYNK_WRITE(V26)// test gọi
{
Gsm_MakeCall(myphone2);
}
BLYNK_WRITE(V27)// test nhắn tin
{
Gsm_MakeSMS(myphone2,"he thong giam sat trung tam nha moi gui tin nhan test");
delay(1000);
}
// ket thuc luu sdt
//...............
//luu ten dau vao
BLYNK_WRITE(V30) {
switch (param.asInt())
{
case 0: // Item 1
id = 1;
CDid=1;
Serial.println("da chon dau vao : "+String(id));
break;
case 1: // Item 1
id = 2;
CDid=1;
Serial.println("da chon dau vao : "+String(id));
break;
case 2: // Item 1
id = 3;
CDid=1;
Serial.println("da chon dau vao : "+String(id));
break;
case 3: // Item 1
id = 4;
CDid=1;
Serial.println("da chon dau vao : "+String(id));
break;
case 4: // Item 1
id = 5;
CDid=1;
Serial.println("da chon dau vao : "+String(id));
break;
case 5: // Item 1
id = 6;
CDid=1;
Serial.println("da chon dau vao : "+String(id));
break;
case 6: // Item 1
id = 7;
CDid=1;
Serial.println("da chon dau vao : "+String(id));
break;
case 7: // Item 1
id = 8;
CDid=1;
Serial.println("da chon dau vao : "+String(id));
break;
case 8: // Item 1
id = 9;
CDid=1;
Serial.println("da chon dau vao : "+String(id));
break;
case 9: // Item 1
id = 10;
CDid=1;
Serial.println("da chon dau vao : "+String(id));
break;
case 10: // Item 1
id = 11;
CDid=1;
Serial.println("da chon dau vao : "+String(id));
break;
case 11: // Item 1
id = 12;
CDid=1;
Serial.println("da chon dau vao : "+String(id));
break;
case 12: // Item 1
id = 13;
CDid=1;
Serial.println("da chon dau vao : "+String(id));
break;
case 13: // Item 1
id = 14;
CDid=1;
Serial.println("da chon dau vao : "+String(id));
break;
case 14: // Item 1
id = 15;
CDid=1;
Serial.println("da chon dau vao : "+String(id));
break;
case 15: // Item 1
id = 16;
CDid=1;
Serial.println("da chon dau vao : "+String(id));
break;
case 16: // Item 1
id = 17;
CDid=1;
Serial.println("da chon dau vao : "+String(id));
break;
case 17: // Item 1
id = 18;
CDid=1;
Serial.println("da chon dau vao : "+String(id));
break;
case 18: // Item 1
id = 19;
CDid=1;
Serial.println("da chon dau vao : "+String(id));
break;
case 19: // Item 1
id = 20;
CDid=1;
Serial.println("da chon dau vao : "+String(id));
break;
default:
Serial.println("Unknown item selected");
}
}
BLYNK_WRITE(V31)
{
// nhap tên đầu vao
tendauvaotam = param.asStr();
CTDtendauvao=1;
}
BLYNK_WRITE(V32)
{
// nhập cau canh báo
caucanhbaotam = param.asStr();
CTDcaucanhbao=1;
}
BLYNK_WRITE(V36)
{
// chinh file thong bao
filethongbaotam=param.asInt();
CTDfilethongbao=1;
myDFPlayer.play(filethongbaotam);
}
BLYNK_WRITE(V35)
{
//chỉnh thời gian canh báo
thoigianCBtam = param.asInt();
CTDthoigianCB=1;
}
BLYNK_WRITE(V34)
{
//chinh do nhay
donhaytam = param.asInt();
CTDdonhay=1;
}
BLYNK_WRITE(V51)
{
// nút cai dat nhanh
if(CDid==1){
if(CTDtendauvao==1){
if(CTDcaucanhbao==1){
if(CTDthoigianCB==1){
if(CTDdonhay==1){
if(CTDfilethongbao==1){
CDnhanh=1;
}else{
manhinhterminal.clear();
manhinhterminal.println("vui long nhap so file thong bao can cai dat");
manhinhterminal.flush();
}
}else{
manhinhterminal.clear();
manhinhterminal.println("vui long nhap do nhay ngo vao can cai dat");
manhinhterminal.flush();
}
}else{
manhinhterminal.clear();
manhinhterminal.println("vui long nhap thoi gian canh bao ngo vao can cai dat");
manhinhterminal.flush();
}
}else{
manhinhterminal.clear();
manhinhterminal.println("vui long nhap cau canh bao ngo vao can cai dat");
manhinhterminal.flush();
}
}else{
manhinhterminal.clear();
manhinhterminal.println("vui long nhap ten ngo vao can cai dat");
manhinhterminal.flush();
}
}else{
manhinhterminal.clear();
manhinhterminal.println("vui long chon ngo vao can cai dat");
manhinhterminal.flush();
}
}
BLYNK_WRITE(V37)
{
// nút cai dat ten dau vao
if(CDid==1){
if(CTDtendauvao==1){
CDtendauvao=1;
}else{
manhinhterminal.clear();
manhinhterminal.println("vui long nhap ten ngo vao can cai dat");
manhinhterminal.flush();
}
}else{
manhinhterminal.clear();
manhinhterminal.println("vui long chon ngo vao can cai dat");
manhinhterminal.flush();
}
}
BLYNK_WRITE(V38)
{
// nút cài dat cau canh bao
if(CDid==1){
if(CTDcaucanhbao==1){
CDcaucanhbao=1;
}else{
manhinhterminal.clear();
manhinhterminal.println("vui long nhap cau canh bao can cai dat");
manhinhterminal.flush();
}
}else{
manhinhterminal.clear();
manhinhterminal.println("vui long chon ngo vao can cai dat");
manhinhterminal.flush();
}
}
BLYNK_WRITE(V40)
{
// nút cài dat thoi gian canh bao
if(CDid==1){
if(CTDthoigianCB==1){
CDthoigianCB=1;
}else{
manhinhterminal.clear();
manhinhterminal.println("vui long nhap thoi gian canh bao can cai dat");
manhinhterminal.flush();
}
}else{
manhinhterminal.clear();
manhinhterminal.println("vui long chon ngo vao can cai dat");
manhinhterminal.flush();
}
}
BLYNK_WRITE(V41)
{
// nút cài dat độ nhạy
if(CDid==1){
if(CTDdonhay==1){
CDdonhay=1;
}else{
manhinhterminal.clear();
manhinhterminal.println("vui long nhap do nhay can cai dat");
manhinhterminal.flush();
}
}else{
manhinhterminal.clear();
manhinhterminal.println("vui long chon ngo vao can cai dat");
manhinhterminal.flush();
}
}
BLYNK_WRITE(V39)
{
// nút cài dat file thong bao
if(CDid==1){
if(CTDfilethongbao==1){
CDfilethongbao=1;
}else{
manhinhterminal.clear();
manhinhterminal.println("vui long nhap file thong bao can cai dat");
manhinhterminal.flush();
}
}else{
manhinhterminal.clear();
manhinhterminal.println("vui long chon ngo vao can cai dat");
manhinhterminal.flush();
}
}
BLYNK_CONNECTED() {
rtc.begin();
}
void setup()
{
pinMode(khoidonglai,OUTPUT);
pinMode(nhantinhieutreo,INPUT_PULLUP);
pinMode(phatxung,OUTPUT);
Serial.begin(9600);
Serial1.begin(9600);
Serial2.begin(9600);
pinMode(dauvao1,INPUT_PULLUP);
pinMode(dauvao2,INPUT_PULLUP);
pinMode(dauvao3,INPUT_PULLUP);
pinMode(dauvao4,INPUT_PULLUP);
pinMode(dauvao5,INPUT_PULLUP);
pinMode(dauvao6,INPUT_PULLUP);
pinMode(dauvao7,INPUT_PULLUP);
pinMode(dauvao8,INPUT_PULLUP);
pinMode(dauvao9,INPUT_PULLUP);
pinMode(dauvao10,INPUT_PULLUP);
pinMode(dauvao11,INPUT_PULLUP);
pinMode(dauvao12,INPUT_PULLUP);
pinMode(dauvao13,INPUT_PULLUP);
pinMode(dauvao14,INPUT_PULLUP);
pinMode(dauvao15,INPUT_PULLUP);
pinMode(dauvao16,INPUT_PULLUP);
pinMode(dauvao17,INPUT_PULLUP);
pinMode(dauvao18,INPUT_PULLUP);
pinMode(dauvao19,INPUT_PULLUP);
pinMode(dauvao20,INPUT_PULLUP);
pinMode(denmang,OUTPUT);
Timer1.initialize(1000000);
Timer1.attachInterrupt(motgiay); // blinkLED to run every 1 seconds
Timer1.stop();
manhinh.begin(); // initialize the lcd
manhinh.backlight();
if (! dongho.begin()) {
Serial.println("Couldn't find RTC");
Serial.flush();
}
Gsm_Init();
if (!myDFPlayer.begin(Serial2)) { //Use softwareSerial to communicate with mp3.
Serial.println("Unable to begin:");
Serial.println("1.Please recheck the connection!");
Serial.println("2.Please insert the SD card!");
}else{
Serial.println("DFPlayer Mini online.");
}
myDFPlayer.volume(EEPROM.read(amluong)); //Set volume value. From 0 to 30 EEPROM.read(903)
myDFPlayer.play(1); //Play the first mp3
dht.begin(); // Khởi động cảm biến
//lấy thông số hệ thống
tgtd=EEPROM.read(thoigiantamdung);
nhietdoCB=EEPROM.read(onhietdoCB);
giay=0;
trangthai=0;
cogoitam=0;
cogoi=0;
bitgoi=0;
thoigian=0;
moctggoidien=0;
timedauvaoND=0;
timedvphatxung=0;
CDid=0;CTDtendauvao=0;CTDcaucanhbao=0;CTDfilethongbao=0;CTDdonhay=0;CTDthoigianCB=0;
CDtendauvao=0;CDcaucanhbao=0;CDfilethongbao=0;CDdonhay=0;CDthoigianCB=0;
for(int i=0;i<15;i++){
timedauvaoDN[i]=0;
timedauvaoCB[i]=0;
cocanhbao[i]=0;
}
taomangten();
taomangcauCB();
taomangdonhay();
taomangthoigianCB();
taomangfilethongbao();
for(int i= 2100;i<2110;i++){
myphone1 += String(EEPROM.read(i));
delay(30);
}
for(int i= 2110;i<2120;i++){
myphone2 += String(EEPROM.read(i));
delay(30);
}
Serial.println("sdt1 da cai dat: "+myphone1);
Serial.println("sdt2 da cai dat: "+myphone2);
//kết thuc lấy thông số hệ thống
Blynk.begin(auth, "blynk.cloud", 80);
for(int i=0;i<2;i++){
Blynk.connect();
if(Blynk.connected()==1){
break;
}
}
if(Blynk.connected()==1){
internet=1;
}
else{
internet=0;
}
//Gsm_MakeCall(myphone2);
giay=0;
//Blynk.logEvent("restart", "he thong canh bao trung tam da restart");
}
void loop()
{
h = dht.readHumidity();
t = dht.readTemperature();
duytriketnoi();
hienthiLCD();
ham4khunggiokhoidong();
hamtamdung();
hamCDnhanh();
hamCDtendauvao();
hamCDcaucanhbao();
hamCDfilethongbao();
hamCDthoigianCB();
hamCDdonhay();
hienthilichsu();
hienthiled();
quytrinhcanhbao();
chongtreo();
Blynk.run();
}
void motgiay()
{
giay=giay+1;
Serial.println(giay);
}
void Gsm_Init()
{
Serial1.println("AT"); // Tat che do phan hoi (Echo mode)
delay(2000);
Serial1.println("AT+IPR=9600"); // Dat toc do truyen nhan du lieu 9600 bps
delay(2000);
Serial1.println("AT+CMGF=1"); // Chon che do TEXT Mode
delay(2000);
Serial1.println("AT+CLIP=1"); // Hien thi thong tin nguoi goi den
delay(2000);
Serial1.println("AT+CNMI=2,2"); // Hien thi truc tiep noi dung tin nhan
delay(2000);
}
void Gsm_MakeCall(String phone)
{
Serial1.println("ATD" + phone + ";"); // Goi dien
for(int i=0;i<20;i++){
manhinh.clear();
manhinh.setCursor(0,0);
manhinh.print("dang goi khoi dong");
delay(1000);
Blynk.run();
}
Serial1.println("AT+CHUP"); // Ngat cuoc goi
}
void Gsm_MakeSMS(String phone,String content){
Serial1.println("AT+CMGS=\"" + phone + "\""); // Lenh gui tin nhan
delay(1000);
Blynk.run();
delay(1000);
Blynk.run();
delay(1000);
Blynk.run();
Serial1.print(content); // Gui noi dung
Serial1.print((char)26); // Gui Ctrl+Z hay 26 de ket thuc noi dung tin nhan va gui tin di
delay(1000);
Blynk.run();
delay(1000);
Blynk.run();
delay(1000);
Blynk.run();
}
void hamtamdung() {
if(tamdungblynk==1){
Timer1.stop();
giay=0;
for(int i=0;i<15;i++){
timedauvaoDN[i]=0;
timedauvaoCB[i]=0;
cocanhbao[i]=0;
}
for(int i=0;i<tgtd;i++){
int tgtdcl=(tgtd-1)-i;
Blynk.virtualWrite(V48, tgtdcl);
for(int g=0;g<60;g++){
Blynk.virtualWrite(V49, 60-g);
hienthiLCDtamdung(tgtdcl,60-g);
delay(1000);
Blynk.run();
chongtreo();
}
}
tamdungblynk=0;
}
}
void duytriketnoi() {
if (Blynk.connected() == 1) {
Blynk.run();
digitalWrite(denmang,HIGH);
}else{
digitalWrite(denmang,LOW);
}
if (internet == 1) {
if (Blynk.connected() == 0) {
if (thoigian == 0) {
thoigian = millis();
//Serial.print("thoigian:");Serial.println(thoigian);
}
//Serial.println(millis()-thoigian);
if (millis() - thoigian > 30000) {
Blynk.begin(auth, "blynk.cloud", 80);
Blynk.connect();
thoigian = 0;
}
}
}
}
void hienthiLCD() {
DateTime now = dongho.now();
static unsigned long timepoint = millis();
if (millis() - timepoint > 1000U) {
timepoint = millis();
Blynk.virtualWrite(V0, now.hour());
Blynk.virtualWrite(V1, now.minute());
Blynk.virtualWrite(V2, now.second());
manhinh.clear();
manhinh.setCursor(0, 0);
manhinh.print("HE THONG GIAM SAT TT");
manhinh.setCursor(0, 1);
manhinh.print("NHIET DO TU: " + String(t));
manhinh.setCursor(0, 2);
manhinh.print(now.hour());
manhinh.print(":");
manhinh.print(now.minute());
manhinh.print(":");
manhinh.print(now.second());
manhinh.print(" ");
if (Blynk.connected() == 1) {
manhinh.print("ONL");
} else {
manhinh.print("OFF");
}
manhinh.print(" ");
manhinh.print(giay);
}
}
void hienthiLCDtamdung(int phuttd,int giaytd){
manhinh.clear();
manhinh.setCursor(0, 0);
manhinh.print("HE THONG GIAM SAT TT");
manhinh.setCursor(1, 1);
manhinh.print("TAM DUNG CANH BAO");
manhinh.setCursor(0, 3);
manhinh.print("thoi gian TD ");
manhinh.print(phuttd);
manhinh.print(":");
manhinh.print(giaytd);
}
void hienthiCB(String chuoihienthi) {
manhinh.setCursor(0, 3);
manhinh.print(chuoihienthi);
}
void ham4khunggiokhoidong() {
DateTime now = dongho.now();
if (Blynk.connected() == 1) {
if (minute() != now.minute()) {
dongho.adjust(DateTime(year(), month(), day(), hour(), minute(), second()));
}
}
if (Blynk.connected() == 0) {
if (now.hour() == 8 && now.minute() == 1) { //reset theo dong ho
if (now.second() >= 1 && now.second() < 3) {
digitalWrite(khoidonglai, HIGH);
delay(500);
digitalWrite(khoidonglai, LOW);
}
}
}
if (Blynk.connected() == 0) {
if (now.hour() == 12 && now.minute() == 1) { //reset theo dong ho
if (now.second() >= 1 && now.second() < 3) {
digitalWrite(khoidonglai, HIGH);
delay(500);
digitalWrite(khoidonglai, LOW);
}
}
}
if (Blynk.connected() == 0) {
if (now.hour() == 22 && now.minute() == 1) { //reset theo dong ho
if (now.second() >= 1 && now.second() < 3) {
digitalWrite(khoidonglai, HIGH);
delay(500);
digitalWrite(khoidonglai, LOW);
}
}
}
if (now.hour() == EEPROM.read(giokhoidong) && now.minute() == EEPROM.read(phutkhoidong)) { //reset theo dong ho
if (now.second() >= 1 && now.second() < 3) {
digitalWrite(khoidonglai, HIGH);
delay(500);
digitalWrite(khoidonglai, LOW);
}
}
}
//hàm mới
void goidien(String sodienthoai){
if(bitgoi==0){
if(moctggoidien==0){
Serial1.println("ATD" + sodienthoai + ";"); // Goi dien
Serial.println("gui lenh goi");
moctggoidien= millis();
bitgoi=1; // bật cờ thực hiện cuộc gọi
}
}
}
void ngatcuocgoi(){
if(bitgoi==1){
if(moctggoidien!=0){
if(millis()-moctggoidien>30000){
Serial1.println("AT+CHUP");
Serial.println("gui lenh ket thuc goi");
bitgoi=0; //tắt cờ thực hiện cuộc gọi
moctggoidien=0;
}
}
}
}
void luuchuoi(String chuoi,int vitribatdauluu,int soluongbyte){
int n = chuoi.length();
Serial.println(n);
byte bupfer1[n+1]={};
chuoi.getBytes(bupfer1,n+1);
for(int i=0;i<soluongbyte;i++){
if(i<n){
//Serial.print(char(tendauvao[i]));
EEPROM.update(vitribatdauluu+i,bupfer1[i]);
delay(20);
}else{
//Serial.println("255");
EEPROM.update(vitribatdauluu+i,255);
delay(20);
}
}
}
void luutendauvao(String chuoiten,int iddauvao1){
// vung nhớ luu ten dau vao 1 đến 300
int vitriluu = (15*iddauvao1)-14;
luuchuoi(chuoiten,vitriluu,15);
}
void luucaucanhbao(String chuoicauCB,int iddauvao2){
// vung nhớ luu ten dau vao 301 đến 700
int vitriluu = (20*iddauvao2)-19+300;
luuchuoi(chuoicauCB,vitriluu,20);
}
String noichuoi(int vitribatdaunoi,int soluongcannoi){
String chuoiketqua="";
for(int i=0;i<soluongcannoi;i++){
byte tam = EEPROM.read(vitribatdaunoi+i);
if(tam != 255){
chuoiketqua+=char(tam);
//Serial.print(chuoitendauvao);
}else{
break;
}
}
return(chuoiketqua);
}
String noichuoiten(int iddauvao3){
String chuoiten;
int vitridoc = (15*iddauvao3)-14;
chuoiten = noichuoi(vitridoc,15);
return(chuoiten);
}
String noichuoiCB(int iddauvao4){
String chuoiCB;
int vitridoc = (20*iddauvao4)-19+300;
chuoiCB = noichuoi(vitridoc,20);
return(chuoiCB);
}
void taomangten(){
for(int i=0;i<20;i++){
tendauvao[i]=noichuoiten(i+1);
Serial.println("ten dau vao "+ String(i+1)+" : " +tendauvao[i]);
}
}
void taomangcauCB(){
for(int i=0;i<20;i++){
caucanhbao[i]=noichuoiCB(i+1);
Serial.println("cau CB dau vao "+ String(i+1)+" : " +caucanhbao[i]);
}
}
void taomangdonhay(){
for(int i=0;i<20;i++){
donhay[i]=EEPROM.read(2020+i);
Serial.println("do nhay dau vao "+ String(i+1)+" : " +String(donhay[i]));
}
}
void taomangthoigianCB(){
for(int i=0;i<20;i++){
thoigianCB[i]=EEPROM.read(2000+i);
Serial.println("thoi gian CB dau vao "+ String(i+1)+" : " +String(thoigianCB[i]));
}
}
void taomangfilethongbao(){
for(int i=0;i<20;i++){
filethongbao[i]=EEPROM.read(2040+i);
Serial.println("file thong bao dau vao "+ String(i+1)+" : " +String(filethongbao[i]));
}
}
void luulichsu(int iddauvao){
DateTime now = dongho.now();
byte manglichsu[7] = {iddauvao,now.hour(),now.minute(),now.second(),now.day(),now.month(),now.year()-2000};
for(int i=0;i<7;i++){
Serial.println(manglichsu[i]);
}
for(int i=1;i<50;i++){
for(int j=0;j<7;j++){
byte tam=EEPROM.read(701+j+(i*7));
EEPROM.update(701+j+((i-1)*7),tam);
delay(30);
}
}
for(int j=1044;j<1051;j++){
EEPROM.update(j,manglichsu[j-1044]);
delay(30);
}
}
void hienthilichsu(){
if(hienthilsblynk==1){
String manglshienthi[7];
int stt=0;
for(int i=1044;i>700;i=i-7){
stt=stt+1;
if(i==1044){
manhinhterminal.clear();
}
String lshienthi=tendauvao[EEPROM.read(i)-1]+" ";
for(int j=0;j<7;j++){
manglshienthi[j]=String(EEPROM.read(i+j));
if(j>0){
lshienthi+=manglshienthi[j]+" ";
}
}
manhinhterminal.println(String(stt)+". "+lshienthi);
Serial.println(String(stt)+". "+lshienthi);
manhinhterminal.flush();
Blynk.run();
chongtreo();
}
hienthilsblynk=0;
}
}
void hamCDnhanh(){
if(CDnhanh==1){
WidgetTerminal manhinhterminal(V33);
luutendauvao(tendauvaotam,id);
tendauvao[id-1]=noichuoiten(id);
Serial.println("ten dau vao "+ String(id)+" : " +tendauvao[id-1]);
manhinhterminal.println("ten dau vao "+ String(id)+" : " +tendauvao[id-1]);
manhinhterminal.flush();
luucaucanhbao(caucanhbaotam,id);
caucanhbao[id-1]=noichuoiCB(id);
Serial.println("cau CB dau vao "+ String(id)+" : " +caucanhbao[id-1]);
manhinhterminal.println("cau CB dau vao "+ String(id)+" : " +caucanhbao[id-1]);
manhinhterminal.flush();
filethongbao[id-1]=filethongbaotam;
EEPROM.update(2040+id-1,filethongbaotam);
Serial.println("file thong bao dau vao "+ String(id)+" : " +String(filethongbaotam));
manhinhterminal.println("file thong bao dau vao "+ String(id)+" : " +String(filethongbaotam));
manhinhterminal.flush();
thoigianCB[id-1]=thoigianCBtam;
EEPROM.update(2000+id-1,thoigianCBtam);
Serial.println("thoi gian canh bao dau vao "+ String(id)+" : " +String(thoigianCBtam)+" giay");
manhinhterminal.println("thoi gian canh bao dau vao "+ String(id)+" : " +String(thoigianCBtam)+" giay");
manhinhterminal.flush();
donhay[id-1]=donhaytam;
EEPROM.update(2020+id-1,donhaytam);
Serial.println("do nhay dau vao "+ String(id)+" : " +String(donhaytam)+" giay");
manhinhterminal.println("do nhay dau vao "+ String(id)+" : " +String(donhaytam)+" giay");
manhinhterminal.flush();
CDnhanh=0;
CTDtendauvao=0;
CTDcaucanhbao=0;
CTDfilethongbao=0;
CTDthoigianCB=0;
CTDdonhay=0;
CDid=0;
delay(1000);
}
}
void hamCDtendauvao(){
if(CDtendauvao==1){
WidgetTerminal manhinhterminal(V33);
luutendauvao(tendauvaotam,id);
tendauvao[id-1]=noichuoiten(id);
Serial.println("ten dau vao "+ String(id)+" : " +tendauvao[id-1]);
manhinhterminal.println("ten dau vao "+ String(id)+" : " +tendauvao[id-1]);
manhinhterminal.flush();
CDtendauvao=0;
CTDtendauvao=0;
CDid=0;
delay(1000);
}
}
void hamCDcaucanhbao(){
if(CDcaucanhbao==1){
WidgetTerminal manhinhterminal(V33);
luucaucanhbao(caucanhbaotam,id);
caucanhbao[id-1]=noichuoiCB(id);
Serial.println("cau CB dau vao "+ String(id)+" : " +caucanhbao[id-1]);
manhinhterminal.println("cau CB dau vao "+ String(id)+" : " +caucanhbao[id-1]);
manhinhterminal.flush();
CDcaucanhbao=0;
CTDcaucanhbao=0;
CDid=0;
delay(1000);
}
}
void hamCDfilethongbao(){
if(CDfilethongbao==1){
WidgetTerminal manhinhterminal(V33);
filethongbao[id-1]=filethongbaotam;
EEPROM.update(2040+id-1,filethongbaotam);
Serial.println("file thong bao dau vao "+ String(id)+" : " +String(filethongbaotam));
manhinhterminal.println("file thong bao dau vao "+ String(id)+" : " +String(filethongbaotam));
manhinhterminal.flush();
CDfilethongbao=0;
CTDfilethongbao=0;
CDid=0;
delay(1000);
}
}
void hamCDthoigianCB(){
if(CDthoigianCB==1){
WidgetTerminal manhinhterminal(V33);
thoigianCB[id-1]=thoigianCBtam;
EEPROM.update(2000+id-1,thoigianCBtam);
Serial.println("thoi gian canh bao dau vao "+ String(id)+" : " +String(thoigianCBtam)+" giay");
manhinhterminal.println("thoi gian canh bao dau vao "+ String(id)+" : " +String(thoigianCBtam)+" giay");
manhinhterminal.flush();
CDthoigianCB=0;
CTDthoigianCB=0;
CDid=0;
delay(1000);
}
}
void hamCDdonhay(){
if(CDdonhay==1){
WidgetTerminal manhinhterminal(V33);
donhay[id-1]=donhaytam;
EEPROM.update(2020+id-1,donhaytam);
Serial.println("do nhay dau vao "+ String(id)+" : " +String(donhaytam)+" giay");
manhinhterminal.println("do nhay dau vao "+ String(id)+" : " +String(donhaytam)+" giay");
manhinhterminal.flush();
CDdonhay=0;
CTDdonhay=0;
CDid=0;
delay(1000);
}
}