Browse Source

Merged in faina09/espurna/dev (pull request #42)

Conflicts:
	code/espurna/data/index.html.gz
	code/espurna/static/index.html.gz.h
i18n
Xose Pérez 7 years ago
parent
commit
eee4c47658
8 changed files with 3305 additions and 3095 deletions
  1. +115
    -0
      code/espurna/ascheduler.ino
  2. +10
    -2
      code/espurna/config/hardware.h
  3. +1
    -1
      code/espurna/config/sensors.h
  4. BIN
      code/espurna/data/index.html.gz
  5. +5
    -0
      code/espurna/espurna.ino
  6. +3099
    -3088
      code/espurna/static/index.html.gz.h
  7. +11
    -3
      code/html/custom.css
  8. +64
    -1
      code/html/custom.js

+ 115
- 0
code/espurna/ascheduler.ino View File

@ -0,0 +1,115 @@
/*
A SCHEDULER MODULE
Copyright (C) 2017 by faina09
*/
#if SCHEDULER_SUPPORT
#include <NtpClientLib.h>
void _schWebSocketOnSend(JsonObject &root){
root["maxScheduled"] = MAX_SCHEDULED;
JsonArray &sch = root.createNestedArray("schedule");
for (byte i = 0; i < MAX_SCHEDULED; i++) {
if (!hasSetting("sch_switch", i))
break;
JsonObject &scheduler = sch.createNestedObject();
scheduler["sch_switch"] = getSetting("sch_switch", i, "");
scheduler["sch_operation"] = getSetting("sch_operation", i, "");
scheduler["sch_hour"] = getSetting("sch_hour", i, "");
scheduler["sch_minute"] = getSetting("sch_minute", i, "");
scheduler["sch_weekdays"] = getSetting("sch_weekdays", i, "");
}
}
void schSetup(){
// Update websocket clients
#if WEB_SUPPORT
wsOnSendRegister(_schWebSocketOnSend);
#endif
int i;
for (i = 0; i < MAX_SCHEDULED; i++) {
if (getSetting("sch_switch" + String(i)).length() == 0)
break;
String sch_weekdays = getSetting("sch_weekdays" + String(i));
int sch_switch = getSetting("sch_switch" + String(i)).toInt();
int sch_operation = getSetting("sch_operation" + String(i)).toInt();
int sch_hour = getSetting("sch_hour" + String(i)).toInt();
int sch_minute = getSetting("sch_minute" + String(i)).toInt();
DEBUG_MSG_P(PSTR("[SCH] Turn switch #%d %s at %02d:%02d on %s\n"), sch_switch, sch_operation ? "ON" : "OFF", sch_hour, sch_minute, (char *)sch_weekdays.c_str());
}
}
void schLoop(){
// Check if we should compare scheduled and actual times
static unsigned long last_update = 0;
static int sec = 0;
if ((millis() - last_update > ((SCH_UPDATE_SEC + 60 - sec)*1000)) || (last_update == 0)) {
last_update = millis();
if (!ntpConnected()){
time_t t = now();
sec = second(t);
DEBUG_MSG_P(PSTR("[SCH] no NTP, time now=%02d:%02d:%02d\n"),hour(t),minute(t),second(t));
}
else {
// compare at next minute and SCH_UPDATE_SEC seconds
sec = NTP.getTimeDateString().substring(6, 8).toInt();
}
int i;
for (i = 0; i < MAX_SCHEDULED; i++) {
if (getSetting("sch_switch" + String(i)).length() == 0)
break;
String sch_weekdays = getSetting("sch_weekdays" + String(i));
if (isThisWday(sch_weekdays)) {
int sch_switch = getSetting("sch_switch" + String(i)).toInt();
int sch_operation = getSetting("sch_operation" + String(i)).toInt();
int sch_hour = getSetting("sch_hour" + String(i)).toInt();
int sch_minute = getSetting("sch_minute" + String(i)).toInt();
//DEBUG_MSG_P(PSTR("[SCH] Today it will turn switch #%d %d @ %02d:%02d\n"), sch_switch, sch_operation, sch_hour, sch_minute);
int minToTrigger = diffTime(sch_hour, sch_minute);
if (minToTrigger == 0) {
relayStatus(sch_switch, sch_operation);
DEBUG_MSG_P(PSTR("[SCH] TRIGGERED!! switch #%d is %s\n"), sch_switch, sch_operation ? "ON" : "OFF");
}
if (minToTrigger < 0) {
//DEBUG_MSG_P(PSTR("[SCH] Time now: %s\n"), (char *)ntpDateTime().c_str()); // aaaa/mm/dd hh:mm:ss
DEBUG_MSG_P(PSTR("[SCH] Time now: %s, %d minutes to trigger %02d:%02d switch #%d %s\n"),
(char *)ntpDateTime().c_str(), -minToTrigger, sch_hour, sch_minute, sch_switch, sch_operation ? "ON" : "OFF");
}
}
}
}
}
bool isThisWday(String weekdays){
//Sunday = 1, Monday = 2, ...
int w = weekday(now());
//DEBUG_MSG_P(PSTR("[SCH] ntp weekday: %d\n"), w);
char * pch;
char * p = (char *)weekdays.c_str();
while ((pch = strtok_r(p, ",", &p)) != NULL) {
//DEBUG_MSG_P(PSTR("[SCH] w found: %d\n"), atoi(pch));
if (atoi(pch) == w) return true;
}
return false;
}
int diffTime(int schhour, int schminute){
if (!ntpConnected()){
time_t t = now();
DEBUG_MSG_P(PSTR("[SCH] no NTP time = %02d:%02d:%02d\n"),hour(t),minute(t),second(t));
return (hour(t) - schhour) * 60 + minute(t) - schminute;
}
else {
String value = NTP.getTimeDateString();
int hour = value.substring(0, 2).toInt();
int minute = value.substring(3, 5).toInt();
//DEBUG_MSG_P(PSTR("[SCH] ntp time: %02d:%02d\n"), hour, minute);
//DEBUG_MSG_P(PSTR("[SCH] cmp time: %02d:%02d\n"), schhour, schminute);
return (hour - schhour) * 60 + minute - schminute;
}
}
#endif

+ 10
- 2
code/espurna/config/hardware.h View File

@ -23,7 +23,7 @@
// -----------------------------------------------------------------------------
// Development boards
// -----------------------------------------------------------------------------
#define NODEMCU_LOLIN
#if defined(NODEMCU_LOLIN)
// Info
@ -36,8 +36,16 @@
#define BUTTON1_RELAY 1
// Relays
#define RELAY1_PIN 12
#define RELAY1_PIN 5
#define RELAY1_TYPE RELAY_TYPE_NORMAL
#define RELAY2_PIN 4
#define RELAY2_TYPE RELAY_TYPE_NORMAL
#define RELAY3_PIN 12
#define RELAY3_TYPE RELAY_TYPE_NORMAL
#define SCHEDULER_SUPPORT 1
#define SCH_UPDATE_SEC 5 //scheduler perform switch at hh:mm:05
#define MAX_SCHEDULED 10
// LEDs
#define LED1_PIN 2


+ 1
- 1
code/espurna/config/sensors.h View File

@ -2,7 +2,7 @@
// SENSORS - General data
// =============================================================================
#define SENSOR_DEBUG 0 // Debug sensors
#define SENSOR_DEBUG 1 // Debug sensors
#define SENSOR_READ_INTERVAL 6 // Read data from sensors every 6 seconds
#define SENSOR_READ_MIN_INTERVAL 6 // Minimum read interval


BIN
code/espurna/data/index.html.gz View File


+ 5
- 0
code/espurna/espurna.ino View File

@ -378,6 +378,9 @@ void setup() {
#if SENSOR_SUPPORT
sensorSetup();
#endif
#if SCHEDULER_SUPPORT
schSetup();
#endif
// Prepare configuration for version 2.0
migrate();
@ -435,6 +438,8 @@ void loop() {
#endif
#if THINGSPEAK_SUPPORT
tspkLoop();
#endif #if SCHEDULER_SUPPORT
schLoop();
#endif
// Power saving delay


+ 3099
- 3088
code/espurna/static/index.html.gz.h
File diff suppressed because it is too large
View File


+ 11
- 3
code/html/custom.css View File

@ -62,17 +62,23 @@
margin-left: 5px;
}
.button-add-network,
.button-add-schedule,
.button-rfb-learn {
background: rgb(28, 184, 65);
}
.button-del-network {
.button-del-network,
.button-del-schedule, {
background: rgb(202, 60, 60);
letter-spacing: 0px;
}
.button-more-network,
.button-more-schedule,
.button-rfb-send {
background: rgb(223, 117, 20);
}
#schedules input[type=number]{
max-width: 55px;
}
.button-settings-backup,
.button-settings-restore {
background: rgb(0, 202, 0);
@ -103,12 +109,14 @@ div.hint {
.break {
margin-top: 5px;
}
#networks .pure-g {
#networks .pure-g,
#schedules .pure-g {
padding: 10px 0 10px 0;
margin-bottom: 5px;
border-bottom: 2px dashed #e5e5e5;
}
#networks .more {
#networks .more,
#schedules .more {
display: none;
}
legend.module,


+ 64
- 1
code/html/custom.js View File

@ -1,6 +1,7 @@
var websock;
var password = false;
var maxNetworks;
var maxSchedules;
var messages = [];
var webhost;
@ -100,6 +101,7 @@ function validateForm(form) {
// These fields will always be a list of values
var is_group = [
"ssid", "pass", "gw", "mask", "ip", "dns",
"sch_switch","sch_operation","sch_hour","sch_minute","sch_weekdays",
"relayBoot", "relayPulse", "relayTime",
"mqttGroup", "mqttGroupInv",
"dczRelayIdx", "dczMagnitude",
@ -465,6 +467,39 @@ function moreNetwork() {
$(".more", parent).toggle();
}
// -----------------------------------------------------------------------------
// Relays scheduler
// -----------------------------------------------------------------------------
function delSchedule() {
var parent = $(this).parents(".pure-g");
$(parent).remove();
}
function moreSchedule() {
var parent = $(this).parents(".pure-g");
$("div.more", parent).toggle();
}
function addSchedule() {
var numSchedules = $("#schedules > div").length;
if (numSchedules >= maxSchedules) {
alert("Max number of schedules reached");
return;
}
var tabindex = 200 + numSchedules * 10;
var template = $("#scheduleTemplate").children();
var line = $(template).clone();
$(line).find("input").each(function() {
$(this).attr("tabindex", tabindex++);
});
$(line).find(".button-del-schedule").on('click', delSchedule);
$(line).find(".button-more-schedule").on('click', moreSchedule);
line.appendTo("#schedules");
return line;
}
// -----------------------------------------------------------------------------
// Relays
// -----------------------------------------------------------------------------
@ -820,6 +855,31 @@ function processData(data) {
return;
}
// -----------------------------------------------------------------------------
// Relays scheduler
// -----------------------------------------------------------------------------
if (key == "maxSchedules") {
maxSchedules = parseInt(data.maxSchedules);
return;
}
if (key == "schedule") {
var schedule = data.schedule;
for (var i in schedule) {
// add a new row
var line = addSchedule();
// fill in the blanks
var schedule = data.schedule[i];
Object.keys(schedule).forEach(function(key) {
var element = $("input[name=" + key + "]", line);
if (element.length) element.val(schedule[key]);
var elementsel = $("select[name=" + key + "]", line);
if (elementsel.length) elementsel.prop("value", schedule[key]);
});
}
return;
}
// ---------------------------------------------------------------------
// Relays
// ---------------------------------------------------------------------
@ -1055,7 +1115,10 @@ $(function() {
$(".button-add-network").on('click', function() {
$(".more", addNetwork()).toggle();
});
$(".button-add-schedule").on('click', function() {
$("div.more", addSchedule()).toggle();
});
$(document).on('change', 'input', hasChanged);
$(document).on('change', 'select', hasChanged);


Loading…
Cancel
Save