#define CAYENNE_DEBUG
#define CAYENNE_PRINT Serial
#define TINY_GSM_MODEM_A6
#define SerialMon Serial
#define TINY_GSM_RX_BUFFER 512
#define gsmSerial Serial1
#define TINY_GSM_DEBUG SerialMon
#define GSM_PIN ""
#include <CayenneMQTTGSM.h>
#include <CayenneMQTTGSMClient.h>
#define REMOTEXY_MODE__HARDSERIAL
#include <RemoteXY.h>
#define REMOTEXY_SERIAL Serial2
#define REMOTEXY_SERIAL_SPEED 115200
#include <Wire.h>
#include <OneWire.h>
#define ONE_WIRE_BUS 7
OneWire oneWire(ONE_WIRE_BUS);
#include <DallasTemperature.h>
DallasTemperature ds(&oneWire);
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
Adafruit_BME280 bme;
#include <iarduino_RTC.h>
iarduino_RTC time(RTC_DS3231);
#include <UTFT.h>
UTFT myGLCD(TFT32MEGA_2, 38, 39, 40, 41);
#include <URTouch.h>
URTouch myTouch(52, 53, 51, 50, 44);
#include "GyverWDT.h"
extern uint8_t SevenSegNumFont[];
extern uint8_t Grotesk16x32[];
extern uint8_t Grotesk24x48[];
extern uint8_t Arial_round_16x24[];
double tempH, presH, humH, tempL, tempR, tempOut = 0.0;
byte qty;
uint8_t D, d, M, Y ;
uint8_t h, m, s ;
unsigned char L, H, B, F = 0;
int x, y;
char incomingByte;
String inputString;
unsigned char N0, N1, N2, N3, K, Status, Signal, State = 0;
unsigned char led_1_r;
unsigned char led_1_g;
unsigned char led_2_r;
unsigned char led_2_g;
unsigned char led_3_r;
unsigned char led_3_g;
unsigned char led_4_r;
unsigned char led_4_g;
unsigned char switch_1;
unsigned char switch_2;
unsigned char switch_3;
unsigned char switch_4;
#define Liht 2
#define Heater 3
#define Boiler 4
#define Fridge 5
#define Power 6
String Message;
char SMStxt[161];
char SMSnum[ 13];
char SMStim[ 18];
uint16_t SMSlongID;
uint8_t SMSlongSUM;
uint8_t SMSlongNUM;
#pragma pack(push, 1)
uint8_t RemoteXY_CONF[] =
{ 255, 4, 0, 41, 0, 12, 1, 10, 178, 1,
65, 38, 52, 89, 9, 9, 2, 1, 2, 88,
22, 11, 79, 26, 135, 36, 79, 78, 0, 79,
70, 70, 0, 2, 1, 2, 62, 22, 11, 78,
26, 135, 37, 79, 78, 0, 79, 70, 70, 0,
2, 1, 2, 75, 22, 11, 78, 26, 135, 36,
79, 78, 0, 79, 70, 70, 0, 2, 1, 2,
49, 22, 11, 78, 26, 135, 36, 79, 78, 0,
79, 70, 70, 0, 65, 38, 52, 63, 9, 9,
65, 38, 52, 77, 9, 9, 67, 1, 43, 15,
26, 9, 31, 26, 11, 67, 1, 43, 24, 26,
9, 31, 26, 11, 129, 0, 1, 27, 22, 6,
31, 72, 85, 77, 44, 32, 37, 0, 129, 0,
1, 18, 22, 6, 31, 84, 77, 80, 44, 32,
194, 176, 67, 0, 65, 38, 52, 50, 9, 9,
129, 0, 1, 36, 38, 6, 31, 80, 82, 69,
83, 44, 32, 109, 109, 72, 71, 0, 67, 1,
43, 34, 22, 9, 31, 26, 11, 129, 0, 26,
52, 13, 6, 31, 76, 73, 72, 84, 0, 129,
0, 26, 65, 24, 6, 31, 72, 69, 65, 84,
69, 82, 0, 129, 0, 26, 78, 23, 6, 31,
66, 79, 73, 76, 69, 82, 0, 129, 0, 26,
91, 23, 6, 31, 70, 82, 73, 68, 71, 69,
0, 129, 0, 2, 6, 31, 6, 31, 79, 85,
84, 68, 79, 79, 82, 0, 129, 0, 35, 6,
26, 6, 31, 67, 76, 73, 77, 65, 84, 69,
0, 130, 2, 0, 13, 63, 3, 31, 130, 2,
0, 43, 64, 3, 31
};
struct {
uint8_t switch_4;
uint8_t switch_2;
uint8_t switch_3;
uint8_t switch_1;
uint8_t led_4_r;
uint8_t led_4_g;
uint8_t led_2_r;
uint8_t led_2_g;
uint8_t led_3_r;
uint8_t led_3_g;
char text_1[11];
char text_2[11];
uint8_t led_1_r;
uint8_t led_1_g;
char text_3[11];
uint8_t connect_flag;
} RemoteXY;
#pragma pack(pop)
char apn[] = "internet";
char gprsLogin[] = "gdata";
char gprsPassword[] = "gdata";
char pin[] = "";
char username[] = "45df2b50-7020-11eb-b767-3f1a8f1211ba";
char password[] = "9d0202fe177ad42e667cdd2e26389ba1cf7ff7ac";
char clientID[] = "11161260-734c-11eb-a2e4-b32ea624e442";
void setup()
{
RemoteXY_Init ();
Serial.begin(115200);
Wire.begin();
time.begin();
bme.begin(0x76);
ds.begin();
qty = ds.getDeviceCount();
myGLCD.InitLCD(LANDSCAPE);
myTouch.InitTouch(LANDSCAPE);
myTouch.setPrecision(PREC_MEDIUM);
pinMode(Power, OUTPUT);
digitalWrite(Power, LOW);
delay (2000);
digitalWrite(Power, HIGH);
delay (3000);
digitalWrite(Power, LOW);
pinMode(Fridge, OUTPUT);
digitalWrite(Fridge, HIGH);
pinMode(Liht, OUTPUT);
digitalWrite(Liht, HIGH);
pinMode(Heater, OUTPUT);
digitalWrite(Heater, HIGH);
pinMode(Boiler, OUTPUT);
digitalWrite(Boiler, HIGH);
TinyGsmAutoBaud(gsmSerial);
Cayenne.begin(username, password, clientID, gsmSerial, apn, gprsLogin, gprsPassword, pin);
myGLCD.fillScr(VGA_BLUE);
myGLCD.setFont(Arial_round_16x24);
myGLCD.setBackColor(VGA_BLUE);
myGLCD.setColor(VGA_YELLOW);
myGLCD.print("Initialization, please wait...", 0, 5);
delay(5000);
myGLCD.clrScr();
}
void(* resetFunc) (void) = 0;
void loop()
{
Watchdog.enable(RESET_MODE, WDT_PRESCALER_1024);
Cayenne.loop();
RemoteXY_Handler ();
Info();
Infoext();
while (myTouch.dataAvailable())
{
myTouch.read();
x = myTouch.getX();
y = myTouch.getY();
if ((x >= 0 && x <= 50) && (y >= 0 && y <= 50))
{
MainMenu ();
}
}
State = RemoteXY_isConnected();
if (State == 1)
{
Bluetooth ();
}
Watchdog.reset();
}
void Bluetooth ()
{
Watchdog.disable ();
myGLCD.setColor(VGA_RED);
myGLCD.fillRoundRect (0, 0, 480, 160);
myGLCD.setFont(Arial_round_16x24);
myGLCD.setBackColor(VGA_RED);
myGLCD.setColor(VGA_WHITE);
myGLCD.print ("DEVICE UNDER", CENTER, 5);
myGLCD.print ("BLUETOOTH CONTROL!!!", CENTER, 35);
myGLCD.print ("FOR RETURN TO MANUAL CONTROL", CENTER, 65);
myGLCD.print ("TURN OFF THE BLUETOOTH", CENTER, 95);
myGLCD.print ("CONTROL DEVICE!", CENTER, 125);
Bluetooth:
tempH = bme.readTemperature();
presH = bme.readPressure() * 0.00750062;
humH = bme.readHumidity();
Infoext ();
RemoteXY_Handler ();
State = RemoteXY_isConnected();
if (State == 1)
{
if (RemoteXY.switch_1 != 0)
{
digitalWrite(Liht, LOW);
RemoteXY.led_1_r = 0;
RemoteXY.led_1_g = 255;
}
else
{
digitalWrite(Liht, HIGH);
RemoteXY.led_1_r = 255;
RemoteXY.led_1_g = 0;
}
if (RemoteXY.switch_2 != 0)
{
digitalWrite(Heater, LOW);
RemoteXY.led_2_r = 0;
RemoteXY.led_2_g = 255;
}
else
{
digitalWrite(Heater, HIGH);
RemoteXY.led_2_r = 255;
RemoteXY.led_2_g = 0;
}
if (RemoteXY.switch_3 != 0)
{
digitalWrite(Boiler, LOW);
RemoteXY.led_3_r = 0;
RemoteXY.led_3_g = 255;
}
else
{
digitalWrite(Boiler, HIGH);
RemoteXY.led_3_r = 255;
RemoteXY.led_3_g = 0;
}
if (RemoteXY.switch_4 != 0)
{
digitalWrite(Fridge, LOW);
RemoteXY.led_4_r = 0;
RemoteXY.led_4_g = 255;
}
else
{
digitalWrite(Fridge, HIGH);
RemoteXY.led_4_r = 255;
RemoteXY.led_4_g = 0;
}
dtostrf( tempH, 0, 0, RemoteXY.text_1);
dtostrf( humH, 0, 0, RemoteXY.text_2);
dtostrf( presH, 0, 0, RemoteXY.text_3);
goto Bluetooth;
}
if (State == 0)
{
myGLCD.clrScr();
}
}
void Info()
{
myGLCD.setBackColor(VGA_BLACK);
myGLCD.setColor(VGA_WHITE);
time.gettime();
h = time.Hours;
m = time.minutes;
s = time.seconds;
d = time.day;
M = time.month;
Y = time.year;
tempH = bme.readTemperature();
presH = bme.readPressure() * 0.00750062;
humH = bme.readHumidity();
myGLCD.setFont(SevenSegNumFont);
if (h <= 9)
{
myGLCD.printNumI(0, 240, 5);
myGLCD.printNumI(h, 270, 5);
}
if (h >= 10)
{
myGLCD.printNumI(h, 235, 5);
}
if (m <= 9)
{
myGLCD.printNumI(0, 325, 5);
myGLCD.printNumI(m, 355, 5);
}
if (m >= 10)
{
myGLCD.printNumI(m, 325, 5);
}
if (s <= 9)
{
myGLCD.printNumI(0, 410, 5);
myGLCD.printNumI(s, 440, 5);
}
if (s >= 10)
{
myGLCD.printNumI(s, 410, 5);
}
if (d <= 9)
{
myGLCD.printNumI(0, 235, 65);
myGLCD.printNumI(d, 265, 65);
}
if (d >= 10)
{
myGLCD.printNumI(d, 235, 65);
}
if (M <= 9)
{
myGLCD.printNumI(0, 325, 65);
myGLCD.printNumI(M, 355, 65);
}
if (M >= 10)
{
myGLCD.printNumI(M, 325, 65);
}
myGLCD.printNumI (Y, 410, 65);
byte daY = d;
byte month = M;
unsigned int Year = Y;
byte a = f_div((14 - month), 12);
unsigned int Y = Year - a;
byte m = month + 12 * a - 2;
unsigned int Y4 = f_div(Y, 4);
byte Y100 = f_div(Y, 100);
byte Y400 = f_div(Y, 400);
byte X = f_div(31 * m, 12);
byte D = ((daY + Y + Y4 - Y100 + Y400 + X) % 7);
if (D == 1)
{
myGLCD.setFont(Grotesk16x32);
myGLCD.print ("MONDAY", 300, 125);
}
if (D == 2)
{
myGLCD.setFont(Grotesk16x32);
myGLCD.print ("TUESDAY", 298, 125);
}
if (D == 3)
{
myGLCD.setFont(Grotesk16x32);
myGLCD.print ("WEDNESDAY", 290, 125);
}
if (D == 4)
{
myGLCD.setFont(Grotesk16x32);
myGLCD.print ("THURSDAY", 298, 125);
}
if (D == 5)
{
myGLCD.setFont(Grotesk16x32);
myGLCD.print ("FRIDAY", 298, 125);
}
if (D == 6)
{
myGLCD.setFont(Grotesk16x32);
myGLCD.print ("SATURDAY", 300, 125);
}
if (D == 7)
{
myGLCD.setFont(Grotesk16x32);
myGLCD.print ("SUNDAY", 300, 125);
}
myGLCD.setFont(Grotesk24x48);
myGLCD.print (":", 300, 2);
myGLCD.print (":", 388, 2);
myGLCD.print ("/", 298, 65);
myGLCD.print ("/", 388, 65);
myGLCD.fillRoundRect (0, 160, 480, 165);
myGLCD.fillRoundRect (219, 0, 224, 160);
myGLCD.drawLine(0, 57, 224, 57);
myGLCD.drawLine(0, 58, 224, 58);
myGLCD.setFont(Arial_round_16x24);
myGLCD.print ("(TMP, C)", 45, 30);
myGLCD.drawCircle(130, 36, 3);
myGLCD.print ("HOUSE CLIMATE", 3, 5);
myGLCD.print ("HALLWAY....", 3, 67);
myGLCD.printNumI (tempH, 180, 70);
myGLCD.print ("LAVATORY...", 3, 100);
myGLCD.printNumI (tempL, 180, 100);
myGLCD.print ("ROOM.......", 3, 130);
myGLCD.printNumI (tempR, 180, 130);
myGLCD.setFont(SevenSegNumFont);
myGLCD.setColor(VGA_WHITE);
}
void Infoext()
{
myGLCD.setBackColor(VGA_BLACK);
myGLCD.setColor(VGA_WHITE);
tempH = bme.readTemperature();
presH = bme.readPressure() * 0.00750062;
humH = bme.readHumidity();
myGLCD.fillRoundRect (0, 160, 480, 165);
myGLCD.fillRoundRect (219, 160, 224, 320);
myGLCD.drawLine(0, 220, 224, 220);
myGLCD.drawLine(0, 221, 224, 221);
myGLCD.drawLine(224, 198, 480, 198);
myGLCD.drawLine(224, 199, 480, 199);
myGLCD.setFont(Arial_round_16x24);
myGLCD.print ("OUTDOOR", 50, 172);
myGLCD.print ("CLIMATE", 50, 195);
myGLCD.print ("DEVICES STATUS", 242, 170);
myGLCD.print ("TMP, C.....", 3, 230);
myGLCD.drawCircle(73, 234, 3);
myGLCD.printNumI (tempOut, 180, 230);
myGLCD.print ("HUM, % ......", 3, 260);
myGLCD.printNumI (humH, 180, 260);
myGLCD.print ("PRES, mmHg.", 3, 290);
myGLCD.printNumI (presH, 164, 290);
L = digitalRead (Liht);
if (L == HIGH)
{
myGLCD.print ("Lihting.....", 230, 205);
myGLCD.setColor(VGA_RED);
myGLCD.print ("OFF", 428, 205);
}
else
{
myGLCD.print ("Lihting......", 230, 205);
myGLCD.setColor(VGA_GREEN);
myGLCD.print (" ON", 430, 205);
}
myGLCD.setColor(VGA_WHITE);
H = digitalRead (Heater);
if (H == HIGH)
{
myGLCD.print ("Heating.....", 230, 232);
myGLCD.setColor(VGA_RED);
myGLCD.print ("OFF", 428, 232);
}
else
{
myGLCD.print ("Heating......", 230, 232);
myGLCD.setColor(VGA_GREEN);
myGLCD.print (" ON", 430, 232);
}
myGLCD.setColor(VGA_WHITE);
B = digitalRead (Boiler);
if (B == HIGH)
{
myGLCD.print ("Boiler......", 230, 262);
myGLCD.setColor(VGA_RED);
myGLCD.print ("OFF", 428, 262);
}
else
{
myGLCD.print ("Boiler.......", 230, 262);
myGLCD.setColor(VGA_GREEN);
myGLCD.print (" ON", 430, 262);
}
myGLCD.setColor(VGA_WHITE);
F = digitalRead (Fridge);
if (F == HIGH)
{
myGLCD.print ("Fridge......", 230, 290);
myGLCD.setColor(VGA_RED);
myGLCD.print ("OFF", 428, 290);
}
else
{
myGLCD.print ("Fridge.......", 230, 290);
myGLCD.setColor(VGA_GREEN);
myGLCD.print (" ON", 430, 290);
}
myGLCD.setFont(SevenSegNumFont);
myGLCD.setColor(VGA_WHITE);
}
void MainMenu ()
{
Watchdog.disable ();
myGLCD.clrScr();
Main:
myGLCD.setFont(Grotesk16x32);
myGLCD.setBackColor(VGA_BLACK);
myGLCD.setColor(VGA_WHITE);
myGLCD.fillRect(0, 40, 480, 45);
myGLCD.print("MAIN MENU", CENTER, 5);
myGLCD.print("X", 5, 5);
myGLCD.print("1. CONTROL MENU", 5, 75);
myGLCD.print("2. SETUP MENU", 5, 155);
myGLCD.setColor(VGA_BLUE);
myGLCD.fillRoundRect (330, 65, 474, 112);
myGLCD.fillRoundRect (330, 145, 474, 192);
myGLCD.setColor(VGA_WHITE);
myGLCD.drawRoundRect (329, 64, 475, 113);
myGLCD.drawRoundRect (329, 144, 475, 193);
myGLCD.setFont(Grotesk24x48);
myGLCD.setBackColor(VGA_BLUE);
myGLCD.setColor(VGA_WHITE);
myGLCD.print("ENTER", 343, 65);
myGLCD.print("ENTER", 343, 145);
waitForTouch();
myTouch.read();
x = myTouch.getX();
y = myTouch.getY();
if ((x >= 330 && x <= 470) && (y >= 57 && y <= 105))
{
ControlMenu ();
}
if ((x >= 330 && x <= 470) && (y >= 124 && y <= 180 ))
{
SetupMenu ();
}
if ((x >= 0 && x <= 60) && (y >= 0 && y <= 60))
{
myGLCD.clrScr();
return;
}
}
void ControlMenu ()
{
Watchdog.disable ();
myGLCD.clrScr();
myGLCD.setFont(Grotesk16x32);
myGLCD.setBackColor(VGA_BLACK);
myGLCD.setColor(VGA_WHITE);
myGLCD.fillRect(0, 40, 480, 45);
myGLCD.print("CONTROL MENU", CENTER, 5);
myGLCD.print("X", 5, 5);
myGLCD.fillRect(0, 40, 480, 45);
myGLCD.print("1.LIHTING CONTROL", 5, 65);
myGLCD.print("2.HEATING CONTROL", 5, 135);
myGLCD.print("3.BOILER CONTROL", 5, 205);
myGLCD.print("4.FRIDGE CONTROL", 5, 275);
Control:
L = digitalRead (Liht);
if (L == HIGH)
{
myGLCD.setColor (VGA_GREEN);
myGLCD.fillRoundRect (310, 55, 475, 105);
myGLCD.drawRoundRect (309, 54, 476, 106);
myGLCD.setBackColor(VGA_GREEN);
myGLCD.setColor(VGA_WHITE);
myGLCD.drawRoundRect (309, 54, 476, 106);
myGLCD.print ("Switch ON", 321, 65);
}
else
{
myGLCD.setColor(VGA_RED);
myGLCD.fillRoundRect (310, 55, 475, 105);
myGLCD.setBackColor(VGA_RED);
myGLCD.setColor(VGA_WHITE);
myGLCD.drawRoundRect (309, 54, 476, 106);
myGLCD.print ("Switch OFF", 312, 65);
}
H = digitalRead (Heater);
if (H == HIGH)
{
myGLCD.setColor (VGA_GREEN);
myGLCD.fillRoundRect (310, 125, 475, 175);
myGLCD.setBackColor(VGA_GREEN);
myGLCD.setColor(VGA_WHITE);
myGLCD.drawRoundRect (309, 124, 476, 176);
myGLCD.print ("Switch ON", 321, 135);
}
else
{
myGLCD.setColor(VGA_RED);
myGLCD.fillRoundRect (310, 125, 475, 175);
myGLCD.setBackColor(VGA_RED);
myGLCD.setColor(VGA_WHITE);
myGLCD.drawRoundRect (309, 124, 476, 176);
myGLCD.print ("Switch OFF", 312, 135);
}
B = digitalRead (Boiler);
if (B == HIGH)
{
myGLCD.setColor (VGA_GREEN);
myGLCD.fillRoundRect (310, 195, 475, 245);
myGLCD.setBackColor(VGA_GREEN);
myGLCD.setColor(VGA_WHITE);
myGLCD.drawRoundRect (309, 194, 476, 246);
myGLCD.print ("Switch ON", 321, 205);
}
else
{
myGLCD.setColor(VGA_RED);
myGLCD.fillRoundRect (310, 195, 475, 245);
myGLCD.setBackColor(VGA_RED);
myGLCD.setColor(VGA_WHITE);
myGLCD.drawRoundRect (309, 194, 476, 246);
myGLCD.print ("Switch OFF", 312, 205);
}
F = digitalRead (Fridge);
if (F == HIGH)
{
myGLCD.setColor (VGA_GREEN);
myGLCD.fillRoundRect (310, 265, 475, 315);
myGLCD.setBackColor(VGA_GREEN);
myGLCD.setColor(VGA_WHITE);
myGLCD.drawRoundRect (309, 264, 476, 316);
myGLCD.print ("Switch ON", 321, 275);
}
else
{
myGLCD.setColor(VGA_RED);
myGLCD.fillRoundRect (310, 265, 475, 315);
myGLCD.setColor (VGA_WHITE);
myGLCD.setBackColor(VGA_RED);
myGLCD.setColor(VGA_WHITE);
myGLCD.drawRoundRect (309, 264, 476, 316);
myGLCD.print ("Switch OFF", 312, 275);
}
waitForTouch();
myTouch.read();
x = myTouch.getX();
y = myTouch.getY();
if ((x >= 316 && x <= 470) && (y >= 55 && y <= 97 ))
{
Lihting ();
}
if ((x >= 330 && x <= 470) && (y >= 125 && y <= 153 ))
{
Heating ();
}
if ((x >= 330 && x <= 470) && (y >= 196 && y <= 246))
{
BoilerF ();
}
if ((x >= 330 && x <= 470) && (y >= 271 && y <= 311))
{
FridgeF ();
}
if ((x >= 0 && x <= 50) && (y >= 0 && y <= 50))
{
myGLCD.clrScr();
return;
}
goto Control;
}
void SetupMenu ()
{
myGLCD.clrScr();
myGLCD.setFont(Grotesk16x32);
myGLCD.setBackColor(VGA_BLACK);
myGLCD.setColor(VGA_WHITE);
myGLCD.fillRect(0, 40, 480, 45);
myGLCD.print("SETUP MENU", CENTER, 5);
myGLCD.print("X", 5, 5);
myGLCD.print("1.TIME SETUP", 5, 60);
myGLCD.print("2.DATA SETUP", 5, 140);
myGLCD.print("3.RESET", 5, 220);
myGLCD.drawRoundRect (309, 50, 476, 100);
myGLCD.drawRoundRect (309, 130, 476, 180);
myGLCD.drawRoundRect (309, 209, 476, 259);
myGLCD.setColor(VGA_BLUE);
myGLCD.fillRoundRect (310, 51, 475, 99);
myGLCD.fillRoundRect (310, 131, 475, 179);
myGLCD.fillRoundRect (310, 210, 475, 258);
myGLCD.setFont(Grotesk24x48);
myGLCD.setBackColor(VGA_BLUE);
myGLCD.setColor(VGA_WHITE);
myGLCD.print("ENTER", 335, 51);
myGLCD.print("ENTER", 335, 131);
myGLCD.print("ENTER", 335, 210);
waitForTouch ();
myTouch.read();
x = myTouch.getX();
y = myTouch.getY();
if ((x >= 320 && x <= 470) && (y >= 51 && y <= 99))
{
TimeMenu();
}
if ((x >= 320 && x <= 470) && (y >= 131 && y <= 179))
{
DateMenu();
}
if ((x >= 320 && x <= 470) && (y >= 210 && y <= 258))
{
ResetMenu ();
}
}
void waitForTouch()
{
while (myTouch.dataAvailable() == false)
{}
}
void Lihting()
{
if (N0 == 0)
{
digitalWrite(Liht, LOW);
N0 = 1;
}
else
{
digitalWrite(Liht, HIGH);
N0 = 0;
}
}
void Heating()
{
if (N1 == 0)
{
digitalWrite(Heater, LOW);
N1 = 1;
}
else
{
digitalWrite(Heater, HIGH);
N1 = 0;
}
}
void BoilerF()
{
if (N2 == 0)
{
digitalWrite(Boiler, LOW);
N2 = 1;
}
else
{
digitalWrite(Boiler, HIGH);
N2 = 0;
}
}
void FridgeF()
{
if (N3 == 0)
{
digitalWrite(Fridge, LOW);
N3 = 1;
}
else
{
digitalWrite(Fridge, HIGH);
N3 = 0;
}
}
void ResetMenu()
{
myGLCD.clrScr();
digitalWrite(Power, LOW);
delay (3000);
digitalWrite(Power, HIGH);
resetFunc();
}
void TimeMenu ()
{
Watchdog.disable ();
H = time.Hours;
m = time.minutes;
s = time.seconds;
myGLCD.clrScr();
myGLCD.setFont(Grotesk16x32);
myGLCD.setBackColor(VGA_BLACK);
myGLCD.setColor(VGA_WHITE);
myGLCD.fillRect(0, 40, 480, 45);
myGLCD.print("SETUP TIME", CENTER, 5);
myGLCD.setBackColor(VGA_BLUE);
myGLCD.setColor(VGA_BLUE);
myGLCD.fillRoundRect(100 , 55 , 200 , 270); // Hour edit field
myGLCD.fillRoundRect(280 , 55 , 380 , 270); // Min edit field
myGLCD.fillRoundRect(161, 280 , 319, 310);
myGLCD.setColor(VGA_WHITE);
myGLCD.print("SAVE DATA", CENTER, 280);
myGLCD.drawRoundRect(160, 279 , 320, 311);
if (H <= 9)
{
myGLCD.setBackColor(VGA_BLUE);
myGLCD.setColor(VGA_WHITE);
myGLCD.print("0", 137, 144);
myGLCD.printNumI(H, 155, 144);
}
if (H > 9)
{
myGLCD.setBackColor(VGA_BLUE);
myGLCD.setColor(VGA_WHITE);
myGLCD.printNumI(H, 137, 144);
}
if (m <= 9)
{
myGLCD.setBackColor(VGA_BLUE);
myGLCD.setColor(VGA_WHITE);
myGLCD.print("0", 305, 144);
myGLCD.printNumI(m, 328, 144);
}
if (m > 9)
{
myGLCD.setBackColor(VGA_BLUE);
myGLCD.setColor(VGA_WHITE);
myGLCD.printNumI(m, 313, 144);
}
myGLCD.setColor(VGA_RED);
myGLCD.fillCircle (150, 100, 35); // +hour
myGLCD.fillCircle (330, 100, 35); // +min
myGLCD.fillCircle (150, 220, 35); // -hour
myGLCD.fillCircle (330, 220, 35); // -min
myGLCD.setFont(Grotesk24x48);
myGLCD.setBackColor(VGA_RED);
myGLCD.setColor(VGA_WHITE);
myGLCD.print(" + ", 140, 75); // +hour
myGLCD.print(" + ", 318, 75); // +min
myGLCD.print(" - ", 140, 195); // -hour
myGLCD.print(" - ", 318, 195); // -min
waitForTouch();
TimeSet();
}
void DateMenu ()
{
Watchdog.disable ();
d = time.day;
M = time.month;
Y = time.year;
myGLCD.clrScr();
myGLCD.setFont(Grotesk16x32);
myGLCD.setBackColor(VGA_BLACK);
myGLCD.setColor(VGA_WHITE);
myGLCD.fillRect(0, 40, 480, 45);
myGLCD.print("SETUP DATE", CENTER, 5);
myGLCD.setColor(VGA_BLUE);
myGLCD.fillRoundRect(40 , 55 , 140 , 270); // Date edit field
myGLCD.fillRoundRect(190 , 55 , 290 , 270); // Month edit field
myGLCD.fillRoundRect(340 , 55 , 440 , 270); // Year edit field
myGLCD.setColor(VGA_BLUE);
myGLCD.fillRoundRect(161, 280 , 319, 310);
myGLCD.setBackColor(VGA_BLUE);
myGLCD.setColor(VGA_WHITE);
myGLCD.print("SAVE DATA", CENTER, 280);
myGLCD.setColor(VGA_WHITE);
if (d <= 9)
{
myGLCD.setBackColor(VGA_BLUE);
myGLCD.setColor(VGA_WHITE);
myGLCD.print("0", 75, 144);
myGLCD.printNumI(d, 90, 144);
}
if (d > 9)
{
myGLCD.setBackColor(VGA_BLUE);
myGLCD.setColor(VGA_WHITE);
myGLCD.printNumI(d, 75, 144);
}
if (M <= 9)
{
myGLCD.setBackColor(VGA_BLUE);
myGLCD.setColor(VGA_WHITE);
myGLCD.print("0", 225, 144);
myGLCD.printNumI(M, 240, 144);
}
if (M > 9)
{
myGLCD.setBackColor(VGA_BLUE);
myGLCD.setColor(VGA_WHITE);
myGLCD.printNumI(M, 225, 144);
}
myGLCD.printNumI(Y, 375, 144);
myGLCD.setBackColor(VGA_RED);
myGLCD.setColor(VGA_RED);
myGLCD.fillCircle (90, 100, 35); // +date
myGLCD.fillCircle (240, 100, 35); // +month
myGLCD.fillCircle (390, 100, 35); // +Year
myGLCD.fillCircle (90, 220, 35); // -date
myGLCD.fillCircle (240, 220, 35); // -month
myGLCD.fillCircle (390, 220, 35); // -Year
myGLCD.setColor(VGA_WHITE);
myGLCD.print(" + ", 83, 83); // +date
myGLCD.print(" + ", 233, 83); // +month
myGLCD.print(" + ", 383, 83); // +Year
myGLCD.print(" - ", 83, 203); // -date
myGLCD.print(" - ", 232, 203); // -month
myGLCD.print(" - ", 383, 203); // -Year
waitForTouch();
DateSet();
}
void DateSet()
{
Labeldate:
myGLCD.setFont(Grotesk16x32);
myTouch.read();
x = myTouch.getX();
y = myTouch.getY();
if (( x > 80 && x < 100 ) && (y > 80 && y < 110 )) // +date button pressed
{
d = d + 1;
if (d >= 31)
{
d = 31;
}
if (d <= 9)
{
myGLCD.setBackColor(VGA_BLUE);
myGLCD.setColor(VGA_YELLOW);
myGLCD.print("0", 75, 144);
myGLCD.printNumI(d, 90, 144);
}
if (d >= 10)
{
myGLCD.setBackColor(VGA_BLUE);
myGLCD.setColor(VGA_YELLOW);
myGLCD.printNumI(d, 75, 144);
}
delay(200);
}
if (( x > 80 && x < 100 ) && (y > 210 && y < 230 )) // -date button pressed
{
d = d - 1;
if (d <= 1)
{
d = 1;
}
if (d <= 9)
{
myGLCD.setBackColor(VGA_BLUE);
myGLCD.setColor(VGA_YELLOW);
myGLCD.print("0", 75, 144);
myGLCD.printNumI(d, 90, 144);
}
if (d > 9)
{
myGLCD.setBackColor(VGA_BLUE);
myGLCD.setColor(VGA_YELLOW);
myGLCD.printNumI(d, 75, 144);
}
delay(200);
}
if (( x > 233 && x < 253 ) && (y > 80 && y < 110 )) // +month button pressed
{
M = M + 1;
if (M >= 12)
{
M = 12 ;
}
if (M <= 9)
{
myGLCD.setBackColor(VGA_BLUE);
myGLCD.setColor(VGA_YELLOW);
myGLCD.print("0", 225, 144);
myGLCD.printNumI(M, 240, 144);
}
if (M > 9)
{
myGLCD.setBackColor(VGA_BLUE);
myGLCD.setColor(VGA_YELLOW);
myGLCD.printNumI(M, 225, 144);
}
delay(200);
}
if (( x > 230 && x < 250 ) && (y > 210 && y < 230 )) // -month button pressed
{
M = M - 1;
if (M <= 1)
{
M = 1;
}
if (M <= 9)
{
myGLCD.setBackColor(VGA_BLUE);
myGLCD.setColor(VGA_YELLOW);
myGLCD.print("0", 225, 144);
myGLCD.printNumI(M, 240, 144);
}
if (M >= 10)
{
myGLCD.setBackColor(VGA_BLUE);
myGLCD.setColor(VGA_YELLOW);
myGLCD.printNumI(M, 225, 144);
}
delay(200);
}
if (( x > 390 && x < 410 ) && (y > 90 && y < 110 )) // +Year button pressed
{
Y = Y + 1;
if (Y >= 100)
{
Y = 21 ;
}
myGLCD.setBackColor(VGA_BLUE);
myGLCD.setColor(VGA_YELLOW);
myGLCD.printNumI(Y, 375, 144);
}
delay(200);
if (( x > 390 && x < 410 ) && (y > 210 && y < 230)) // -Year button pressed
{
Y = Y - 1;
if (Y <= 20)
{
Y = 21;
}
myGLCD.setBackColor(VGA_BLUE);
myGLCD.setColor(VGA_YELLOW);
myGLCD.printNumI(Y, 375, 144);
}
delay(300);
if (( x > 220 && x < 280 ) && (y > 270 && y < 310 )) // save button pressed
{
byte day = d; //число
byte month = M; //месяц
unsigned int year = Y; //год
byte a = f_div((14 - month), 12);
unsigned int y = year - a;
byte m = month + 12 * a - 2;
unsigned int y4 = f_div(y, 4);
byte y100 = f_div(y, 100);
byte y400 = f_div(y, 400);
byte x = f_div(31 * m, 12);
byte D = ((day + y + y4 - y100 + y400 + x) % 7);
time.settime (-1, -1, -1, d, M, Y, D);
resetFunc();
goto Labeldate;
}
}
void TimeSet()
{
Labeltime:
myGLCD.setFont(Grotesk16x32);
myTouch.read();
x = myTouch.getX();
y = myTouch.getY();
if (( x > 126 && x < 175 ) && (y > 79 && y < 110 )) // +hour button pressed
{
H = H + 1;
if (H >= 23)
{
H = 23;
}
if (H <= 9)
{
myGLCD.setBackColor(VGA_BLUE);
myGLCD.setColor(VGA_YELLOW);
myGLCD.print("0", 138, 144);
myGLCD.printNumI(H, 163, 144);
}
if (H > 9)
{
myGLCD.setBackColor(VGA_BLUE);
myGLCD.setColor(VGA_YELLOW);
myGLCD.printNumI(H, 138, 144);
}
delay (200);
}
if (( x > 140 && x < 170 ) && (y > 200 && y < 230 )) // -hour button pressed
{
H = H - 1;
if (H <= 0)
{
H = 0;
}
if (H <= 9)
{
myGLCD.setBackColor(VGA_BLUE);
myGLCD.setColor(VGA_YELLOW);
myGLCD.print("0", 138, 144);
myGLCD.printNumI(H, 163, 144);
}
if (H > 9)
{
myGLCD.setBackColor(VGA_BLUE);
myGLCD.setColor(VGA_YELLOW);
myGLCD.printNumI(H, 138, 144);
}
delay (200);
}
if (( x > 320 && x < 340 ) && (y > 80 && y < 100 )) // +min button pressed
{
m = m + 1;
if (m >= 59)
{
m = 59 ;
}
if (m <= 9)
{
myGLCD.setBackColor(VGA_BLUE);
myGLCD.setColor(VGA_YELLOW);
myGLCD.print("0", 330, 144);
myGLCD.printNumI(m, 338, 144);
}
if (m > 9)
{
myGLCD.setBackColor(VGA_BLUE);
myGLCD.setColor(VGA_YELLOW);
myGLCD.printNumI(m, 313, 144);
}
delay (200);
}
if (( x > 330 && x < 360 ) && (y > 210 && y < 240 )) // -min button pressed
{
m = m - 1;
if (m <= 0)
{
m = 0;
}
if (m <= 9)
{
myGLCD.setBackColor(VGA_BLUE);
myGLCD.setColor(VGA_YELLOW);
myGLCD.print("0", 313, 144);
myGLCD.printNumI(m, 338, 144);
}
if (m > 9)
{
myGLCD.setBackColor(VGA_BLUE);
myGLCD.setColor(VGA_YELLOW);
myGLCD.printNumI(m, 313, 144);
}
delay (200);
}
if (( x > 200 && x < 400 ) && (y > 270 && y < 315 )) // Save button pressed
{
time.settime (0, m, H);
resetFunc();
}
goto Labeltime;
}
unsigned int f_div(unsigned int X, unsigned int Y)
{
unsigned int result;
result = (X - (X % Y)) / Y;
return result;
}
CAYENNE_OUT(0)
{
tempH = bme.readTemperature();
Cayenne.virtualWrite(0, tempH);
}
CAYENNE_OUT(1)
{
humH = bme.readHumidity();
Cayenne.virtualWrite(1, humH);
}
CAYENNE_OUT(2)
{
presH = bme.readPressure() * 0.00750062;
Cayenne.virtualWrite(2, presH);
}
CAYENNE_IN(4)
{
CAYENNE_LOG("Channel %u, value %s", request.channel, getValue.asInt());
L = getValue.asInt();
if (L == 1)
{
digitalWrite(Liht, LOW);
}
else
{
digitalWrite(Liht, HIGH);
}
}
CAYENNE_IN(5)
{
CAYENNE_LOG("Channel %u, value %s", request.channel, getValue.asInt());
H = getValue.asInt();
if (H == 1)
{
digitalWrite(Heater, LOW);
}
else
{
digitalWrite(Heater, HIGH);
}
}
CAYENNE_IN(6)
{
CAYENNE_LOG("Channel %u, value %s", request.channel, getValue.asInt());
B = getValue.asInt();
if (B == 1)
{
digitalWrite(Boiler, LOW);
}
else
{
digitalWrite(Boiler, HIGH);
}
}
CAYENNE_IN(7)
{
CAYENNE_LOG("Channel %u, value %s", request.channel, getValue.asInt());
F = getValue.asInt();
if (F == 1)
{
digitalWrite(Fridge, LOW);
}
else
{
digitalWrite(Fridge, HIGH);
}
}