1
1
#include < Arduino.h>
2
+ #include < InternetConnection.h>
2
3
#include < MetheoData.h>
3
4
#include < OledDisplay.h>
4
- #include < InternetConnection .h>
5
+ #include < Thermostat .h>
5
6
#include < Ticker.h>
6
7
#include < EEPROM.h>
7
8
8
- const int relayPinAddress = D5;
9
9
const int readMetheoDataDisplayDataControllThermostatInterval = 10000 ;
10
10
const int sendDataToInternetInterval = 60000 ;
11
11
12
- const int redLed = D6;
13
- const int greenLed = D7;
14
- const int blueLed = D8;
15
-
16
12
MetheoData metheoData;
17
13
OledDisplay oledDisplay;
18
14
InternetConnection connection;
@@ -23,94 +19,40 @@ Ticker timerSendDataToInternet;
23
19
// Connections to APIs are OK
24
20
bool apisAreConnected = false ;
25
21
26
- void turnOffLed ()
27
- {
28
- // RGB LED is Anode type, so HIGH = turnOff, LOW = turnOn
29
- digitalWrite (redLed, HIGH);
30
- digitalWrite (greenLed, HIGH);
31
- digitalWrite (blueLed, HIGH);
32
- }
33
-
34
- void setupRGBLed ()
35
- {
36
- pinMode (redLed, OUTPUT);
37
- pinMode (greenLed, OUTPUT);
38
- pinMode (blueLed, OUTPUT);
39
-
40
- turnOffLed ();
41
- }
42
-
43
- void setLed (int ledPin, bool isTurnOn)
22
+ void readMetheoDataDisplayDataControllThermostat ()
44
23
{
45
- turnOffLed ();
46
- digitalWrite (ledPin, isTurnOn ? LOW : HIGH);
24
+ metheoData.setData ();
25
+ oledDisplay.printMetheoDataToDisplay (metheoData);
26
+ Thermostat::controllThermostat (metheoData);
47
27
}
48
28
49
- // Set thermostat ON/OFF
50
- void controllThermostat (MetheoData data)
29
+ void initializeInternetConnection ()
51
30
{
52
- // TODO: refactor, asi strcit do InternetConnection :-/
53
- if (data.dataAreValid ())
54
- {
55
- // heating is enabled
56
- if (EEPROM.read (1 ) == true )
57
- {
58
- int requiredTemperature = EEPROM.read (2 );
59
- if (requiredTemperature >= 10 && requiredTemperature <= 25 && data.shtTemperature <= requiredTemperature)
60
- {
61
- setLed (greenLed, true );
62
- digitalWrite (relayPinAddress, HIGH);
63
- InternetConnection::setStatusToBlynk (" Heating ON" , " #00FF00" );
64
- connection.setIsHeatingToBlynk (true );
65
- }
66
- else
67
- {
68
- setLed (blueLed, true );
69
- digitalWrite (relayPinAddress, LOW);
70
- InternetConnection::setStatusToBlynk (" Heating OFF" , " #FF0000" );
71
- connection.setIsHeatingToBlynk (false );
72
- }
73
- }
74
- else
75
- {
76
- setLed (redLed, true );
77
- digitalWrite (relayPinAddress, LOW);
78
- InternetConnection::setStatusToBlynk (" Heating not enabled" , " #FF0000" );
79
- connection.setIsHeatingToBlynk (false );
80
- }
81
- }
82
- else
31
+ if (connection.initializeThingSpeak ())
83
32
{
84
- setLed (redLed, true );
85
- digitalWrite (relayPinAddress, LOW);
86
- InternetConnection::setStatusToBlynk (" Data are invalid, heating OFF." , " #FF0000" );
87
- connection.setIsHeatingToBlynk (false );
33
+ apisAreConnected = connection.initializeBlynk ();
88
34
}
89
35
}
90
36
91
- void readMetheoDataDisplayDataControllThermostat ()
92
- {
93
- metheoData.setData ();
94
- oledDisplay.printMetheoDataToDisplay (metheoData);
95
- controllThermostat (metheoData);
96
- }
97
-
98
37
void sendDataToInternet ()
99
38
{
100
39
if (apisAreConnected)
101
40
{
102
41
connection.setMeteoDataToThingSpeakObject (metheoData);
103
- connection.sendDataToThingSpeakApi ();
104
- connection.sendDataToBlynk (metheoData);
105
- Serial.println (" Data was sended" );
106
- }
107
- }
42
+ bool successThingSpeak = connection.sendDataToThingSpeakApi ();
43
+ bool successBlynk = connection.sendDataToBlynk (metheoData);
108
44
109
- void initializeInternetConnection ()
110
- {
111
- if (connection.initializeThingSpeak ())
112
- {
113
- apisAreConnected = connection.initializeBlynk ();
45
+ if (successThingSpeak && successBlynk) {
46
+ Serial.println (" Data was sent" );
47
+ }
48
+ else {
49
+ Serial.println (" No internet connection, try initialize connection" );
50
+ apisAreConnected = false ;
51
+ initializeInternetConnection ();
52
+ }
53
+ }
54
+ else {
55
+ initializeInternetConnection ();
114
56
}
115
57
}
116
58
@@ -134,8 +76,7 @@ void setup()
134
76
Serial.begin (9600 );
135
77
delay (100 );
136
78
137
- pinMode (relayPinAddress, OUTPUT);
138
- setupRGBLed ();
79
+ Thermostat::initialize ();
139
80
initializeInternetConnection ();
140
81
setupTimers ();
141
82
}
0 commit comments