-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSmartVentComm.cpp
More file actions
161 lines (112 loc) · 2.71 KB
/
SmartVentComm.cpp
File metadata and controls
161 lines (112 loc) · 2.71 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include "SmartVentComm.h"
SmartVentComm::SmartVentComm(uint16_t rp, uint16_t cp)
:regServer(rp), comServer(cp)
{
memset(hostName, '\0', sizeof(hostName));
WiFi.macAddress(macAddress);
sprintf(hostName, "sv%02x%02x%02x%02x%02x%02x", macAddress[0], macAddress[1], macAddress[2], macAddress[3], macAddress[4], macAddress[5]);
regPort = rp;
comPort = cp;
regServer.begin();
comServer.begin();
return;
}
bool SmartVentComm::registerVent(unsigned char t)
{
uint32_t elapsed;
WiFiClient client;
client.connect("thermostat", regPort);
elapsed = millis();
while(!client.connected()) {
if(millis() > elapsed + 30000) {
Serial.println("Connect failed after 30 seconds, aborting.");
return false;
}
delay(100);
}
client.print(hostName);
client.write(t);
client.flush();
client.stop();
return true;
}
bool SmartVentComm::getRegistration(RegisterStruct *r)
{
WiFiClient client = regServer.available();
if(!client)
return false;
memset(r->host, '\0', sizeof(r->host));
client.readBytes(r->host, sizeof(r->host)-1);
r->hasSensor = client.read();
client.stop();
return true;
}
bool SmartVentComm::receiveTemperature(TempStruct *t)
{
WiFiClient client = comServer.available();
unsigned char i;
unsigned char j;
if(!client)
return false;
client.readBytes(t->host, sizeof(t->host)-1);
t->host[sizeof(t->host)] = '\0';
do {
delay(50);
t->temp = client.read();
} while(t->temp == 255);
client.stop();
return true;
}
bool SmartVentComm::sendTemperature(unsigned char t)
{
uint32_t elapsed;
WiFiClient client;
TempStruct ts;
client.connect("thermostat", comPort);
elapsed = millis();
while(!client.connected()) {
if(millis() > elapsed + 30000) {
Serial.println("Connect failed after 30 seconds, aborting.");
return false;
}
delay(100);
}
strcpy(ts.host , hostName);
ts.temp = t;
Serial.print("Sending ");
Serial.print(ts.host);
Serial.print(" ");
Serial.println(ts.temp, DEC);
client.print(ts.host);
client.write(ts.temp);
client.flush();
client.stop();
return true;
}
bool SmartVentComm::getCommand(unsigned char *c)
{
WiFiClient client = comServer.available();
if(!client)
return false;
c = (unsigned char *)client.read();
client.stop();
return true;
}
bool SmartVentComm::sendCommand(const char *h, unsigned char c)
{
uint32_t elapsed;
WiFiClient client;
client.connect(h, comPort);
elapsed = millis();
while(!client.connected()) {
if(millis() > elapsed + 10000) {
Serial.println("Connect failed after 10 seconds, aborting.");
return false;
}
delay(100);
}
client.write(c);
client.flush();
client.stop();
return true;
}