ENTS-tock
Environmental NeTworked Sensor firmware on Tock OS
Loading...
Searching...
No Matches
webserver.cpp
Go to the documentation of this file.
1#include "webserver.hpp"
2
3#include <Arduino.h>
4#include <ArduinoLog.h>
5#include <WebServer.h>
6
7static WebServer server(80);
8
9// Global variable to track solenoid state
10IrrigationCommand_State currentSolenoidState = IrrigationCommand_State_CLOSE;
11
12// Timer variables for timed operation
13unsigned long timedStartTime = 0;
14unsigned long timedDuration = 0;
15bool timedOperation = false;
16
17// Thresholds for auto irrigation
18extern float moisture_min_threshold;
19extern float moisture_max_threshold;
20extern unsigned long check_interval;
21
22// Current moisture reading from irrigation module
23float current_moisture = -1.0;
24
25extern bool auto_irrigation_enabled;
26unsigned long last_moisture_check = 0;
27const unsigned long MOISTURE_CHECK_INTERVAL = 300000; // 5 minutes
28
29// Implement the getter function
30IrrigationCommand_State GetSolenoidState() { return currentSolenoidState; }
31
32// Implement the setter function
33void SetSolenoidState(IrrigationCommand_State newState) {
34 currentSolenoidState = newState;
35 Log.noticeln("Solenoid state changed to: %d", newState);
36}
37
38// Getter and setter for moisture
40
41void SetCurrentMoisture(float moisture) {
42 current_moisture = moisture;
43 Log.noticeln("Received moisture reading: %.1f%%", moisture);
44}
45
46// Function to update timed operations
48 if (timedOperation && millis() - timedStartTime >= timedDuration) {
49 Log.noticeln("Timed operation completed, closing valve");
50 SetSolenoidState(IrrigationCommand_State_CLOSE);
51 timedOperation = false;
52 }
53}
54
60void HandleOpen();
61
67void HandleClose();
68
75void HandleTimed();
76
81
85void HandleAutoToggle();
86
92void HandleStatus();
93
99void HandleState();
100
101void HandleMoisture();
102
104 server.handleClient();
105 UpdateTimedOperation(); // Check for timed operations
106}
107
109 server.on("/open", HTTP_POST, HandleOpen);
110 server.on("/close", HTTP_POST, HandleClose);
111 server.on("/timed", HTTP_POST, HandleTimed);
112 server.on("/state", HTTP_GET, HandleState);
113 server.on("/auto", HTTP_POST, HandleAutoToggle);
114 server.on("/irrigation_setup", HTTP_POST, HandleAutoIrrigationSetup);
115 server.on("/status", HTTP_GET, HandleStatus);
116 server.on("/moisture", HTTP_POST, HandleMoisture);
117 server.begin();
118}
119
121 Log.noticeln("Opening valve");
122 SetSolenoidState(IrrigationCommand_State_OPEN);
123 timedOperation = false; // Cancel any timed operation
124 server.send(200, "text/plain", "Opening valve");
125}
126
128 Log.noticeln("Closing valve");
129 SetSolenoidState(IrrigationCommand_State_CLOSE);
130 timedOperation = false; // Cancel any timed operation
131 server.send(200, "text/plain", "Closing valve");
132}
133
135 int time = 0;
136
137 if (server.hasArg("time")) {
138 String time_str = server.arg("time");
139 time = time_str.toInt();
140 } else {
141 Log.errorln("Timed opening valve: no time specified");
142 server.send(400, "text/plain", "No time specified");
143 return;
144 }
145
146 if (time <= 0) {
147 Log.errorln("Invalid time specified: %d", time);
148 server.send(400, "text/plain", "Invalid time specified");
149 return;
150 }
151
152 Log.noticeln("Opening valve for %d seconds", time);
153
154 // Timed sequence
155 HandleOpen();
156 timedStartTime = millis();
157 timedDuration = time * 1000; // Convert to milliseconds
158 timedOperation = true;
159
160 server.send(200, "text/plain", "Valve opened for set duration");
161}
162
164 if (server.hasArg("min") && server.hasArg("max")) {
165 float min_thresh = server.arg("min").toFloat();
166 float max_thresh = server.arg("max").toFloat();
167
168 EnableAutoIrrigation(min_thresh, max_thresh);
169
170 if (server.hasArg("interval")) {
171 SetCheckInterval(server.arg("interval").toInt() * 60000);
172 }
173
174 Log.noticeln(
175 "Auto irrigation configured: Min=%.1f%%, Max=%.1f%%, Interval=%lu min",
176 min_thresh, max_thresh, GetCheckInterval() / 60000);
177 server.send(200, "text/plain", "Auto irrigation configured");
178 } else {
179 Log.errorln("Auto irrigation setup missing parameters");
180 server.send(400, "text/plain", "Missing min/max parameters");
181 }
182}
183
185 if (server.hasArg("enable")) {
186 bool enable = server.arg("enable") == "true";
187 if (enable) {
188 // Enable with current thresholds - let CheckAutoIrrigation handle the
189 // state
191 } else {
194 IrrigationCommand_State_CLOSE); // Still close when disabling
195 }
196 Log.noticeln("Auto irrigation %s", enable ? "enabled" : "disabled");
197 server.send(200, "text/plain", enable ? "Auto enabled" : "Auto disabled");
198 } else {
199 server.send(400, "text/plain", "Missing enable parameter");
200 }
201}
202
204 // Get current moisture reading from cache
205 float current_moisture_value = GetCurrentMoisture();
206
207 String json = "{";
208 json +=
209 "\"solenoid_state\":\"" +
210 String(GetSolenoidState() == IrrigationCommand_State_OPEN ? "open"
211 : "closed") +
212 "\",";
213 json += "\"auto_irrigation_enabled\":" +
214 String(IsAutoIrrigationEnabled() ? "true" : "false") + ",";
215 json += "\"min_threshold\":" + String(GetMinThreshold(), 2) + ",";
216 json += "\"max_threshold\":" + String(GetMaxThreshold(), 2) + ",";
217 json += "\"check_interval_seconds\":" + String(GetCheckInterval() / 1000) +
218 ","; // Convert ms to seconds
219 json += "\"current_moisture\":" + String(current_moisture_value, 2);
220 json += "}";
221
222 server.send(200, "application/json", json);
223}
224
226 IrrigationCommand_State currentState = GetSolenoidState();
227 String stateStr =
228 (currentState == IrrigationCommand_State_OPEN) ? "open" : "closed";
229 Log.noticeln("Current state: %s (%d)", stateStr.c_str(), currentState);
230 server.send(200, "text/plain", stateStr);
231}
232
234 if (server.hasArg("moisture")) {
235 float moisture = server.arg("moisture").toFloat();
236 SetCurrentMoisture(moisture);
237
238 // Trigger auto irrigation check when new moisture data arrives
240 Log.noticeln(
241 "New moisture reading received, triggering irrigation check");
243 }
244
245 server.send(200, "text/plain", "Moisture received");
246 } else {
247 Log.errorln("Moisture endpoint missing parameter");
248 server.send(400, "text/plain", "Missing moisture parameter");
249 }
250}
void HandleStatus()
Handles requests to /state.
IrrigationCommand_State GetSolenoidState()
Get the current solenoid state.
Definition webserver.cpp:30
void HandleMoisture()
void SetupServer()
Setup endpoints for the webserver.
unsigned long check_interval
float current_moisture
Definition webserver.cpp:23
bool auto_irrigation_enabled
void HandleAutoToggle()
Handles auto irrigation toggle.
void HandleOpen()
Handles POST requests to /on.
void SetCurrentMoisture(float moisture)
Definition webserver.cpp:41
float moisture_min_threshold
void HandleClose()
Handles POST requests to /off.
float GetCurrentMoisture()
Definition webserver.cpp:39
float moisture_max_threshold
void HandleAutoIrrigationSetup()
Handles auto irrigation configuration.
void SetSolenoidState(IrrigationCommand_State newState)
Set the solenoid state.
Definition webserver.cpp:33
bool timedOperation
Definition webserver.cpp:15
void HandleState()
Handles requests to /state.
unsigned long timedDuration
Definition webserver.cpp:14
const unsigned long MOISTURE_CHECK_INTERVAL
Definition webserver.cpp:27
unsigned long last_moisture_check
Definition webserver.cpp:26
void HandleClient()
Handle requests from the webserver.
IrrigationCommand_State currentSolenoidState
Definition webserver.cpp:10
void HandleTimed()
Handles POST requests to /timed.
unsigned long timedStartTime
Definition webserver.cpp:13
void UpdateTimedOperation()
Definition webserver.cpp:47
void SetCheckInterval(unsigned long interval_ms)
void EnableAutoIrrigation(float min_thresh, float max_thresh)
bool IsAutoIrrigationEnabled()
float GetMaxThreshold()
float GetCurrentMoistureFromCache()
unsigned long GetCheckInterval()
void CheckIrrigationConditions()
float GetMinThreshold()
void DisableAutoIrrigation()