Skip to content

Commit 08081d1

Browse files
committed
Reinitialize internet connection, turn on heating immediately after Blynk set
1 parent 10078ef commit 08081d1

File tree

7 files changed

+153
-94
lines changed

7 files changed

+153
-94
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@
55
src/settings.cpp
66
.vscode/launch.json
77
.vscode/*.db
8+
.vscode/.browse.c_cpp.db*

lib/InternetConnection/InternetConnection.cpp

+19-9
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include "InternetConnection.h"
2+
23
#include "../../src/settings.cpp"
34
#include <BlynkSimpleEsp8266.h>
4-
#include <EEPROM.h>
55

66
WiFiClient client;
77
Settings settings;
@@ -21,24 +21,34 @@ void setToEEPROM(int address, int value)
2121
EEPROM.commit();
2222
}
2323

24+
void InternetConnection::callThermostatControll()
25+
{
26+
MetheoData metheoData = MetheoData();
27+
metheoData.setData();
28+
ThermostatStatus status = Thermostat::controllThermostat(metheoData);
29+
30+
InternetConnection::setStatusToBlynk(status.message, status.color);
31+
InternetConnection::setIsHeatingToBlynk(status.isHeating);
32+
}
33+
2434
// Enable/disable thermostat, set value to EEPROM to address 1
25-
BLYNK_WRITE(0)
35+
BLYNK_WRITE(V0)
2636
{
27-
// TODO: slo by okamzite pinkout metodu controllThermostat z mainu? Asi jedine presunout tu metodu sem :-/
2837
param.asInt() ? setToEEPROM(1, true) : setToEEPROM(1, false);
38+
InternetConnection::callThermostatControll();
2939
}
3040

3141
// Set temperature slider, write back to blynk to confirm show
3242
BLYNK_WRITE(V10)
3343
{
34-
// TODO: slo by okamzite pinkout metodu controllThermostat z mainu?
3544
int requiredTemp = param.asInt();
3645
Blynk.virtualWrite(V8, requiredTemp);
3746
Serial.println("Target Temperature is " + String(requiredTemp) + "°C");
3847
setToEEPROM(2, requiredTemp);
48+
InternetConnection::callThermostatControll();
3949
}
4050

41-
// Send message status to Blynk
51+
// Static method - send message status to Blynk
4252
void InternetConnection::setStatusToBlynk(String status, String color)
4353
{
4454
Blynk.virtualWrite(V9, status);
@@ -49,7 +59,6 @@ void InternetConnection::setStatusToBlynk(String status, String color)
4959
void InternetConnection::setIsHeatingToBlynk(bool isHeating)
5060
{
5161
Blynk.virtualWrite(V11, isHeating ? 1 : 0);
52-
// Blynk.setProperty(V11, "color", isHeating ? "#00FF00" : "#FF0000");
5362
}
5463

5564
// Initialize WiFi connection and ThingSpeak. Return true if connection is sucessfull.
@@ -111,7 +120,6 @@ void InternetConnection::setMeteoDataToThingSpeakObject(MetheoData metheoData)
111120
bool InternetConnection::sendDataToThingSpeakApi(void)
112121
{
113122
// Send data in one API call
114-
// TODO: co se stane pokud sluzba zrovna nejede, vypadne WIFi apod.
115123
int status = ThingSpeak.writeFields(thingSpeakChannelId, thingSpeakWriteApiKey);
116124
if (status == OK_SUCCESS)
117125
{
@@ -125,17 +133,19 @@ bool InternetConnection::sendDataToThingSpeakApi(void)
125133
return status;
126134
}
127135

128-
void InternetConnection::sendDataToBlynk(MetheoData metheoData)
136+
bool InternetConnection::sendDataToBlynk(MetheoData metheoData)
129137
{
130-
if (Blynk.connect())
138+
if (Blynk.connected())
131139
{
132140
Blynk.virtualWrite(V1, metheoData.shtTemperature);
133141
Blynk.virtualWrite(V2, metheoData.shtHumidity);
134142
Serial.println("Send data to Blynk OK");
135143
Blynk.run();
144+
return true;
136145
}
137146
else
138147
{
139148
Serial.println("Blynk is not connected.");
149+
return false;
140150
}
141151
}

lib/InternetConnection/InternetConnection.h

+6-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
#include <ESP8266WiFi.h>
55
#include <ThingSpeak.h>
66
#include <MetheoData.h>
7+
#include <Thermostat.h>
8+
#include <EEPROM.h>
79

810
class InternetConnection
911
{
@@ -13,9 +15,11 @@ class InternetConnection
1315
void setMeteoDataToThingSpeakObject(MetheoData);
1416
bool sendDataToThingSpeakApi(void);
1517
void runBlynk(void);
16-
void sendDataToBlynk(MetheoData);
18+
bool sendDataToBlynk(MetheoData);
1719
static void setStatusToBlynk(String, String);
18-
void setIsHeatingToBlynk(bool);
20+
static void setIsHeatingToBlynk(bool);
21+
static void callThermostatControll(void);
22+
static void processThermostatStatus(ThermostatStatus);
1923
};
2024

2125
#endif

lib/OledDisplay/OledDisplay.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ void OledDisplay::printMetheoDataToDisplay(MetheoData data)
1515
{
1616
prepareDisplay();
1717

18-
// temperature SHT
18+
// temperature
19+
display.println();
1920
display.println("T:" + String(data.shtTemperature) + " C");
2021
// humidity
2122
display.println("H:" + String(data.shtHumidity) + " %");

lib/Thermostat/thermostat.cpp

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#include "Thermostat.h"
2+
3+
// PINS
4+
const int relayPinAddress = D5;
5+
const int redLed = D6;
6+
const int greenLed = D7;
7+
const int blueLed = D8;
8+
9+
void Thermostat::turnOffLed()
10+
{
11+
// RGB LED is Anode type, so HIGH = turnOff, LOW = turnOn
12+
digitalWrite(redLed, HIGH);
13+
digitalWrite(greenLed, HIGH);
14+
digitalWrite(blueLed, HIGH);
15+
}
16+
17+
void Thermostat::initialize()
18+
{
19+
// set pinmodes to LED and relay
20+
pinMode(redLed, OUTPUT);
21+
pinMode(greenLed, OUTPUT);
22+
pinMode(blueLed, OUTPUT);
23+
pinMode(relayPinAddress, OUTPUT);
24+
25+
turnOffLed();
26+
}
27+
28+
void Thermostat::setLed(int ledPin, bool isTurnOn)
29+
{
30+
turnOffLed();
31+
digitalWrite(ledPin, isTurnOn ? LOW : HIGH);
32+
}
33+
34+
// Set thermostat ON/OFF and temperature
35+
ThermostatStatus Thermostat::controllThermostat(MetheoData data)
36+
{
37+
ThermostatStatus status;
38+
if (data.dataAreValid())
39+
{
40+
// heating is enabled
41+
if (EEPROM.read(1) == true)
42+
{
43+
int requiredTemperature = EEPROM.read(2);
44+
if (requiredTemperature >= 10 && requiredTemperature <= 25 && data.shtTemperature <= requiredTemperature)
45+
{
46+
setLed(greenLed, true);
47+
digitalWrite(relayPinAddress, HIGH);
48+
status = { (char *)"Heating ON", (char *)"#00FF00", true };
49+
// InternetConnection::setStatusToBlynk("Heating ON", "#00FF00");
50+
// connection.setIsHeatingToBlynk(true);
51+
}
52+
else
53+
{
54+
setLed(blueLed, true);
55+
digitalWrite(relayPinAddress, LOW);
56+
status = { (char *)"Heating OFF", (char *)"#FF0000", false };
57+
// InternetConnection::setStatusToBlynk("Heating OFF", "#FF0000");
58+
// connection.setIsHeatingToBlynk(false);
59+
}
60+
}
61+
else
62+
{
63+
setLed(redLed, true);
64+
digitalWrite(relayPinAddress, LOW);
65+
status = { (char *)"Heating not enabled", (char *)"#FF0000", false };
66+
// InternetConnection::setStatusToBlynk("Heating not enabled", "#FF0000");
67+
// connection.setIsHeatingToBlynk(false);
68+
}
69+
}
70+
else
71+
{
72+
setLed(redLed, true);
73+
digitalWrite(relayPinAddress, LOW);
74+
status = { (char *)"Data are invalid, heating OFF", (char *)"#FF0000", false };
75+
// InternetConnection::setStatusToBlynk("Data are invalid, heating OFF.", "#FF0000");
76+
// connection.setIsHeatingToBlynk(false);
77+
}
78+
return status;
79+
}

lib/Thermostat/thermostat.h

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#ifndef __Thermostat_H
2+
#define __Thermostat_H
3+
4+
#include <MetheoData.h>
5+
#include <EEPROM.h>
6+
7+
struct ThermostatStatus
8+
{
9+
char *message;
10+
char *color;
11+
bool isHeating;
12+
};
13+
14+
class Thermostat
15+
{
16+
public:
17+
static ThermostatStatus controllThermostat(MetheoData data);
18+
static void setLed(int ledPin, bool isTurnOn);
19+
static void turnOffLed();
20+
static void initialize();
21+
};
22+
23+
#endif

src/main.cpp

+23-82
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
11
#include <Arduino.h>
2+
#include <InternetConnection.h>
23
#include <MetheoData.h>
34
#include <OledDisplay.h>
4-
#include <InternetConnection.h>
5+
#include <Thermostat.h>
56
#include <Ticker.h>
67
#include <EEPROM.h>
78

8-
const int relayPinAddress = D5;
99
const int readMetheoDataDisplayDataControllThermostatInterval = 10000;
1010
const int sendDataToInternetInterval = 60000;
1111

12-
const int redLed = D6;
13-
const int greenLed = D7;
14-
const int blueLed = D8;
15-
1612
MetheoData metheoData;
1713
OledDisplay oledDisplay;
1814
InternetConnection connection;
@@ -23,94 +19,40 @@ Ticker timerSendDataToInternet;
2319
// Connections to APIs are OK
2420
bool apisAreConnected = false;
2521

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()
4423
{
45-
turnOffLed();
46-
digitalWrite(ledPin, isTurnOn ? LOW : HIGH);
24+
metheoData.setData();
25+
oledDisplay.printMetheoDataToDisplay(metheoData);
26+
Thermostat::controllThermostat(metheoData);
4727
}
4828

49-
// Set thermostat ON/OFF
50-
void controllThermostat(MetheoData data)
29+
void initializeInternetConnection()
5130
{
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())
8332
{
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();
8834
}
8935
}
9036

91-
void readMetheoDataDisplayDataControllThermostat()
92-
{
93-
metheoData.setData();
94-
oledDisplay.printMetheoDataToDisplay(metheoData);
95-
controllThermostat(metheoData);
96-
}
97-
9837
void sendDataToInternet()
9938
{
10039
if (apisAreConnected)
10140
{
10241
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);
10844

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();
11456
}
11557
}
11658

@@ -134,8 +76,7 @@ void setup()
13476
Serial.begin(9600);
13577
delay(100);
13678

137-
pinMode(relayPinAddress, OUTPUT);
138-
setupRGBLed();
79+
Thermostat::initialize();
13980
initializeInternetConnection();
14081
setupTimers();
14182
}

0 commit comments

Comments
 (0)