//-------------------------------------------------------------------------- // Copyright 2017, RIoT International Pty Ltd // // @author Aaron Ardiri //-------------------------------------------------------------------------- #define RELAY_ENGINE_PIN 5 #define RELAY_HORN_PIN 6 #define RELAY_ON LOW #define RELAY_OFF HIGH #define HORN_DURATION 1000 // 1 second typedef struct xGlobals { struct { int active; bool state; long state_ts; } alarm; }; xGlobals xg; void microTLS_setup() { // we have two relays connected for the engine and horn pinMode(RELAY_ENGINE_PIN, OUTPUT); pinMode(RELAY_HORN_PIN, OUTPUT); // make sure the relays are off, normal operation digitalWrite(RELAY_HORN_PIN, RELAY_OFF); digitalWrite(RELAY_ENGINE_PIN, RELAY_OFF); // no alarm; assume everything is ok xg.alarm.active = false; } void microTLS_loop() { // has the alarm been activated? if (xg.alarm.active) { // has enough time passed for us to change our horn state if (millis() > xg.alarm.state_ts) { xg.alarm.state = !xg.alarm.state; xg.alarm.state_ts += HORN_DURATION; // the next time we should change } // blast the horn (turn it on or off, depending on state) digitalWrite(RELAY_HORN_PIN, xg.alarm.state ? RELAY_ON : RELAY_OFF); } } int microTLS_ping(uint8 *buf, int32 max_len) { strcpy((char *)buf, "ping"); return strlen((char *)buf)); } void microTLS_pong(uint8 *buf, int32 len) { // did we get anything back? if (len > 10) { if (strncmp((char *)buf, "activate=1", 10) == 0) { // the alarm is active; shut off engine and blast the horn repeatedly digitalWrite(RELAY_ENGINE_PIN, RELAY_ON); xg.alarm.active = true; } else { // the alarm isn't active - make sure the relays are off digitalWrite(RELAY_HORN_PIN, RELAY_OFF); digitalWrite(RELAY_ENGINE_PIN, RELAY_OFF); } } } //--------------------------------------------------------------------------