Switch relais on automatically on incoming updates

This commit is contained in:
Thomas Kolb 2017-04-29 16:04:49 +02:00
parent 7fcdc73137
commit 598255eaab
5 changed files with 31 additions and 7 deletions

View file

@ -111,7 +111,9 @@ void loop()
fader_loop++; fader_loop++;
} }
ws2801_udp_loop(); if(ws2801_udp_loop()) {
relais_heartbeat();
}
relais_loop(); relais_loop();
} }

View file

@ -4,6 +4,7 @@
#include "relais.h" #include "relais.h"
WiFiUDP relaisUDP; WiFiUDP relaisUDP;
uint32_t relais_off_timestamp;
#define RELAIS_PIN 5 #define RELAIS_PIN 5
@ -23,14 +24,30 @@ void relais_setup(void)
Serial.println("Relais: UDP server start FAILED!"); Serial.println("Relais: UDP server start FAILED!");
} }
relais_off_timestamp = 0;
Serial.println("Relais setup done"); Serial.println("Relais setup done");
} }
void relais_heartbeat(void)
{
relais_off_timestamp = millis() + 600000; // keep on for 10 more minutes
}
void relais_loop(void) void relais_loop(void)
{ {
unsigned char databyte; unsigned char databyte;
unsigned char answer; unsigned char answer;
// update the relais pin state
uint32_t now = millis();
if(now < relais_off_timestamp) {
digitalWrite(RELAIS_PIN, HIGH);
} else {
digitalWrite(RELAIS_PIN, LOW);
}
int packetSize = relaisUDP.parsePacket(); int packetSize = relaisUDP.parsePacket();
if(packetSize) if(packetSize)
{ {
@ -47,14 +64,14 @@ void relais_loop(void)
switch(databyte) { switch(databyte) {
case 0: // switch relais off case 0: // switch relais off
Serial.println("Relais: switch off."); Serial.println("Relais: force switch off.");
digitalWrite(RELAIS_PIN, LOW); relais_off_timestamp = 0;
answer = 0; answer = 0;
break; break;
case 1: // switch relais on case 1: // switch relais on
Serial.println("Relais: switch on."); Serial.println("Relais: force switch on (1 hour).");
digitalWrite(RELAIS_PIN, HIGH); relais_off_timestamp = now + 3600000;
answer = 1; answer = 1;
break; break;

View file

@ -2,6 +2,7 @@
#define RELAIS_H #define RELAIS_H
void relais_setup(void); void relais_setup(void);
void relais_heartbeat(void);
void relais_loop(void); void relais_loop(void);
#endif // RELAIS_H #endif // RELAIS_H

View file

@ -32,7 +32,7 @@ void ws2801_udp_setup(void)
Serial.println("WS2801UDP setup done"); Serial.println("WS2801UDP setup done");
} }
void ws2801_udp_loop(void) bool ws2801_udp_loop(void)
{ {
byte cmd[WS2801_CMD_LEN]; byte cmd[WS2801_CMD_LEN];
byte action, module, r, g, b; byte action, module, r, g, b;
@ -86,5 +86,9 @@ void ws2801_udp_loop(void)
ws2801UDP.write("OK\n"); ws2801UDP.write("OK\n");
*/ */
ws2801UDP.endPacket(); ws2801UDP.endPacket();
return true; // processed a packet
} else {
return false;
} }
} }

View file

@ -2,6 +2,6 @@
#define WS2801_UDP_H #define WS2801_UDP_H
void ws2801_udp_setup(void); void ws2801_udp_setup(void);
void ws2801_udp_loop(void); bool ws2801_udp_loop(void);
#endif // WS2801_UDP_H #endif // WS2801_UDP_H