Efectivos invernaderos
demo-kit/ es lo que simula el dispositivo para la demostración en la página de inicio. El dispositivo en sí no lo necesita.
demo-kit/ es lo que simula el dispositivo para la demostración en la página de inicio. El dispositivo en sí no lo necesita.
// The pretend greenhouse for the demo (../../demo-kit/README.md): the same answers the sketch gives
// (../esp32/greenhouse/greenhouse.ino), with invented values. Time runs faster for the soil than in a real
// pot, so that a visitor sees it dry out and sees a watering help.
(function () {
var ctx = czDemo.context;
var S = ctx.state;
var INTERVAL_SECONDS = 900; // one history sample every 15 minutes, as on the device
var HISTORY_POINTS = 96; // 24 hours
var PUMP_MAX_SECONDS = 30;
var AUTO_WATER_SECONDS = 8;
var TANK_MIN_PERCENT = 5;
var CLOCK_AT_START = 10 * 3600 + 40 * 60; // the demo always starts on a morning, at 10:40
function clamp(value, min, max) {
return Math.max(min, Math.min(max, value));
}
function round1(value) {
return Math.round(value * 10) / 10;
}
// The air over a day: warmest at three in the afternoon, most humid before sunrise.
function dayWave(secondOfDay) {
return Math.sin((secondOfDay / 86400 - 0.375) * 2 * Math.PI);
}
function temperatureAt(secondOfDay) {
return 22.4 + 4.3 * dayWave(secondOfDay);
}
function humidityAt(secondOfDay) {
return 58 - 11 * dayWave(secondOfDay);
}
function lightAt(secondOfDay) {
var hour = (((secondOfDay % 86400) + 86400) % 86400) / 3600;
if (hour < 6 || hour > 20) return 2;
return 2 + 38000 * Math.pow(Math.sin((Math.PI * (hour - 6)) / 14), 1.6);
}
// The last 24 hours, oldest first, built backwards from now: the soil dries by a little under one percent
// per sample and jumps up where the pump ran.
var now = Date.now();
var soilNow = 43;
var soil = [];
var temperature = [];
var humidity = [];
var value = soilNow;
var peak = 76;
var samplesSinceWatering = -1;
for (var back = 0; back < HISTORY_POINTS; back++) {
var secondOfDay = CLOCK_AT_START - back * INTERVAL_SECONDS;
soil.unshift(round1(value));
temperature.unshift(round1(temperatureAt(secondOfDay) + ctx.between(-0.07, 0.07)));
humidity.unshift(round1(humidityAt(secondOfDay) + ctx.between(-0.35, 0.35)));
value += ctx.between(0.75, 1.05);
if (value >= peak) {
if (samplesSinceWatering < 0) samplesSinceWatering = back + 1;
value = ctx.between(33, 36);
peak = ctx.between(73, 79);
}
}
S.soil = soilNow;
S.soak = 0; // water that is in the pot but has not reached the sensor yet
S.tank = 68;
S.auto = true;
S.threshold = 35;
S.pump = false;
S.pumpUntil = 0;
S.lastWatered = now - (samplesSinceWatering * INTERVAL_SECONDS + 240) * 1000;
S.lastStep = now;
S.lastSample = now;
S.booted = now - (6 * 86400 + 3 * 3600 + 12 * 60) * 1000;
S.rssi = -58;
function startPump(seconds, at) {
S.pump = true;
S.pumpUntil = at + clamp(seconds, 1, PUMP_MAX_SECONDS) * 1000;
}
function stopPump(at) {
if (S.pump) S.lastWatered = at;
S.pump = false;
S.pumpUntil = 0;
}
// Moves the pretend pot forward to this moment, in steps of at most a quarter of a second.
function advance() {
var at = Date.now();
var from = Math.max(S.lastStep, at - 4000); // a demo that was paused does not catch up
for (var t = from; t < at; t += 250) {
var dt = Math.min(250, at - t) / 1000;
if (S.pump) {
S.soak += 4.4 * dt * (1 - S.soil / 115);
S.tank = Math.max(0, S.tank - 0.5 * dt);
if (t >= S.pumpUntil || S.tank < TANK_MIN_PERCENT) stopPump(t);
}
var absorbed = S.soak * 0.4 * dt;
S.soak -= absorbed;
S.soil = clamp(S.soil + absorbed - 0.03 * dt * (S.soil / 50), 4, 96);
if (S.auto && !S.pump && S.soil < S.threshold && S.tank >= TANK_MIN_PERCENT && t - S.lastWatered > 60000) {
startPump(AUTO_WATER_SECONDS, t);
}
}
S.lastStep = at;
S.rssi = clamp(S.rssi + ctx.between(-0.6, 0.6), -66, -52);
while (at - S.lastSample >= INTERVAL_SECONDS * 1000) {
S.lastSample += INTERVAL_SECONDS * 1000;
soil.push(round1(S.soil));
temperature.push(temperature[temperature.length - 1]);
humidity.push(humidity[humidity.length - 1]);
if (soil.length > HISTORY_POINTS) {
soil.shift();
temperature.shift();
humidity.shift();
}
}
}
function readings() {
var t = ctx.seconds();
var secondOfDay = CLOCK_AT_START + t;
return {
soil: round1(S.soil + 0.12 * Math.sin(t / 3)),
temperature: round1(temperatureAt(secondOfDay) + 0.15 * Math.sin(t / 11)),
humidity: round1(humidityAt(secondOfDay) + 0.6 * Math.sin(t / 17 + 1)),
light: Math.round(lightAt(secondOfDay) * (1 + 0.06 * Math.sin(t / 13) + 0.03 * Math.sin(t / 4.1))),
};
}
function state() {
advance();
var at = Date.now();
var now = readings();
return {
soil: now.soil,
temperature: now.temperature,
humidity: now.humidity,
light: now.light,
tank: round1(S.tank),
pump: S.pump,
pumpSecondsLeft: S.pump ? Math.ceil((S.pumpUntil - at) / 1000) : 0,
auto: S.auto,
threshold: S.threshold,
lastWateredSecondsAgo: Math.round((at - S.lastWatered) / 1000),
rssi: Math.round(S.rssi),
uptimeSeconds: Math.round((at - S.booted) / 1000),
};
}
czDemo.define({
device: { name: 'greenhouse', title: 'Greenhouse' },
routes: {
'GET /state': state,
'GET /history': function () {
advance();
// As on the device: the stored samples, and the reading of this moment as the last point.
var now = readings();
return {
intervalSeconds: INTERVAL_SECONDS,
soil: soil.slice(1).concat(now.soil),
temperature: temperature.slice(1).concat(now.temperature),
humidity: humidity.slice(1).concat(now.humidity),
};
},
'POST /pump': function (request, ctx) {
advance();
var body = request.json();
if (!body || (typeof body.on !== 'boolean' && typeof body.seconds !== 'number')) {
return ctx.respond(400, { error: "Send { 'on': true|false } or { 'seconds': n }." });
}
if (body.on === false) {
stopPump(Date.now());
} else {
if (S.tank < TANK_MIN_PERCENT) return ctx.respond(409, { error: 'The tank is empty.' });
startPump(typeof body.seconds === 'number' ? body.seconds : PUMP_MAX_SECONDS, Date.now());
}
return state();
},
'POST /config': function (request, ctx) {
var body = request.json();
if (!body) return ctx.respond(400, { error: "Send { 'auto': true|false, 'threshold': percent }." });
if (typeof body.auto === 'boolean') S.auto = body.auto;
if (typeof body.threshold === 'number') S.threshold = Math.round(clamp(body.threshold, 5, 90));
return state();
},
'GET /.cz/ui.json': function () {
return { name: 'Greenhouse', entry: '/.cz/ui.html', version: '1', theme: { background: '#0f1f17', foreground: '#e4eedd' } };
},
},
});
})();