-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWeb.h
159 lines (130 loc) · 3.53 KB
/
Web.h
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
// This file contains the web interface to this script.
//
// To read the sensor data:
// 10.0.2.208/arduino/sensors
//
// This will return a JSON object:
// {
// temperature : 76.23 // farenheit
// humidity : 5 // percent
// }
//
// To control the output:
// 10.0.2.208/arduino/heat/1
// 10.0.2.208/arduino/heat/0
#ifndef THERMOSTAT_WEB_H_
#define THERMOSTAT_WEB_H_
#include <YunServer.h>
#include <YunClient.h>
class Web
{
public:
Web();
void Setup();
void Update();
float GetPrevHeatSetPoint() const
{
return prevHeatSetPoint;
}
float GetPrevCoolSetPoint() const
{
return prevCoolSetPoint;
}
private:
// Listen on default port 5555, the webserver on the Yun
// will forward there all the HTTP requests for us.
YunServer server;
// time since the last read
unsigned long lastReadTimeMillis;
// time since the last update.
unsigned long lastUpdateTimeMillis;
float prevHeatSetPoint;
float prevCoolSetPoint;
void Process(YunClient& client);
void Read(YunClient& client);
void Command(YunClient& client);
void Heartbeat(YunClient& client);
};
Web::Web() :
lastUpdateTimeMillis(0),
prevHeatSetPoint(HEAT_TEMP_DETACHED),
prevCoolSetPoint(COOL_TEMP_DETACHED)
{
}
void Web::Setup()
{
// Listen for incoming tcp connections (port 5555) only from localhost. The
// linino will forward connections from everyone else.
server.listenOnLocalhost();
server.begin();
// restart this timer
lastReadTimeMillis = millis();
}
void Web::Update()
{
if ((millis() - lastUpdateTimeMillis) < 1000)
{
return;
}
lastUpdateTimeMillis = millis();
// Get clients coming from server
YunClient client = server.accept();
// There is a new client?
if (client)
{
// Process request
Process(client);
// Close connection and free resources.
client.stop();
}
}
void Web::Process(YunClient& client)
{
// read the command
String command = client.readStringUntil('/');
command.trim(); // remove trailing whitespace
if (command == "command")
{
Command(client);
}
else if (command == "heartbeat")
{
Heartbeat(client);
}
else
{
client.print(F("invalid REST command: '"));
client.print(command);
client.println(F("'"));
}
}
void Web::Command(YunClient& client)
{
// Read desired state
float heatSetPoint = client.parseFloat();
float coolSetPoint = client.parseFloat();
if ((heatSetPoint != prevHeatSetPoint) ||
(coolSetPoint != prevCoolSetPoint))
{
prevHeatSetPoint = heatSetPoint;
Bridge.put("heatSetPoint",String(heatSetPoint));
prevCoolSetPoint = coolSetPoint;
Bridge.put("coolSetPoint",String(coolSetPoint));
Bridge.put("lcdOverride", "false");
}
// Send feedback to client
client.print(F("The setPoint range is "));
client.print(BridgeGetFloat("heatSetPoint"));
client.print(F(" ... "));
client.println(BridgeGetFloat("coolSetPoint"));
}
void Web::Heartbeat(YunClient& client)
{
// put the amount of time since the last time this was called in a variable called lastUpdateTime
Bridge.put("lastUpdateTime", String(millis() - BridgeGetULong("pythonUpdateTime")));
Bridge.put("pythonUpdateTime", String(millis()));
client.print(F("{\"uptime_ms\": "));
client.print(BridgeGetULong("uptime_ms"));
client.println(F("}"));
}
#endif // INCLUDE GUARD