-
Notifications
You must be signed in to change notification settings - Fork 0
/
mqtt.ino
86 lines (77 loc) · 2.15 KB
/
mqtt.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// MQTT topic
String samePrefix = "home/"+numberOfFloor+"/"+roomName;
String topicFromSystem = samePrefix+"/required-temperature-from-system";
String topicFromDevice = samePrefix+"/required-temperature-from-device";
String topicTemperature = samePrefix+"/temperature";
String mqttUserMessage = "home/user-message";
String topicActualTime = samePrefix+"/actual-time";
// For MQTT Ethernet
char data[100];
void setSubscribe()
{
if(typeOfConnection == WIFI)
{
asyncMqttClient.subscribe(topicFromSystem.c_str(), 2);
asyncMqttClient.subscribe(mqttUserMessage.c_str(), 2);
}
else
{
mqttClientEthernet.subscribe(topicFromSystem.c_str(), 1);
mqttClientEthernet.subscribe(mqttUserMessage.c_str(), 1);
}
}
void sendActualTime(String actualTime)
{
if(typeOfConnection == WIFI)
{
asyncMqttClient.publish(topicActualTime.c_str(), 2, true, actualTime.c_str());
}
else
{
sprintf(data, "%s", actualTime);
mqttClientEthernet.publish(topicActualTime.c_str(), data);
}
}
void sendTemperature(float temperature)
{
if(typeOfConnection == WIFI)
{
asyncMqttClient.publish(topicTemperature.c_str(), 2, true, String(temperature).c_str());
}
else
{
sprintf(data, "%f", temperature);
mqttClientEthernet.publish(topicTemperature.c_str(), data);
}
}
void sendRequiredTemperature(float requiredTemperature)
{
if(typeOfConnection == WIFI)
{
asyncMqttClient.publish(topicFromDevice.c_str(), 2, true, String(requiredTemperature).c_str());
}
else
{
sprintf(data, "%f", requiredTemperature);
mqttClientEthernet.publish(topicFromDevice.c_str(), data);
}
}
void parseTopic(char* topic, String payload)
{
String topicString = String(topic);
if(topicString == mqttUserMessage)
{
String stringPayload = String(payload);
userMessage = stringPayload;
isNewUserMessage = true;
}
if(topicString == topicFromSystem)
{
String stringPayload = String(payload);
float value = stringPayload.toFloat();
int intValue = (int)( value * 10);
oldRequiredTemperature = requiredTemperature;
requiredTemperature = (float)intValue/10;
isNewRequiredTemperatureFromSystem = true;
}
}