Skip to content

Commit

Permalink
completed alarm system
Browse files Browse the repository at this point in the history
  • Loading branch information
cbrake committed May 23, 2012
1 parent 55c4c6b commit dc51b18
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 118 deletions.
145 changes: 88 additions & 57 deletions examples/RemoteAlarm/Monitor/Monitor.ino
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Receive sketch used to report quality of reception.
// Alarm system monitor
// 2012-03-22 <cbrake@bec-systems.com> http://opensource.org/licenses/mit-license.php

// based on https://github.com/jcw/jeelib/tree/master/examples/RF12/rfRangeRX
Expand All @@ -8,17 +8,12 @@

#include <JeeLib.h>

#define BUFFER_SIZE 64
#define DISPLAY_INTERVAL 500 // ms
#define LED_PIN 9 // activity LED, comment out to disable
#define BEEP_PIN 5 // P2

//PortI2C myI2C (3);
MilliTimer statTimer;

byte timeBuf [BUFFER_SIZE]; // index is time slot, value is last packet
byte seqBuf [BUFFER_SIZE]; // index is last packet, value is time slot
char history [11];
byte lastSeq;
MilliTimer beepTimer;
MilliTimer sensorAliveTimer;
int sensor_alive_minutes = 0;

static void activityLed (byte on) {
#ifdef LED_PIN
Expand All @@ -27,66 +22,102 @@ static void activityLed (byte on) {
#endif
}


static void gotPacket () {
byte tenths = millis() / 100;
// remember for each time slot what the last received packet was
timeBuf[tenths % BUFFER_SIZE] = lastSeq;
// remember for the last BUFFER_SIZE packets when they arrived
seqBuf[lastSeq % BUFFER_SIZE] = tenths;
}

static byte recvCount (byte period) {
// tenths and diff are bytes, so they are automatically modulo 256
byte tenths = millis() / 100;
byte n = 0;
for (byte i = 0; i < sizeof seqBuf; ++i) {
byte diff = tenths - seqBuf[i];
if (diff <= period)
++n;
}
return n;
}

static void updateHistory () {
for (byte i = 1; i < 10; ++i)
history[10-i] = history[9-i];
history[0] = '0';
// tenths and diff are bytes, so they are automatically modulo 256
byte tenths = millis() / 100;
for (byte i = 0; i < DISPLAY_INTERVAL / 100; ++i) {
byte pos = (tenths - i - 1) % BUFFER_SIZE;
byte diff = lastSeq - timeBuf[pos];
if (diff < 5)
++history[0];
}
}

void setup () {
Serial.begin(57600);
Serial.print("\n[JeenodeRangeRx]");
rf12_initialize('R', RF12_915MHZ);
}

enum state {
STATE_NORMAL,
STATE_SENSOR_ERROR,
STATE_ALARM,
};

int state;

bool sensor_ok = true;
bool received_sensor_data = false;

void loop () {
if (rf12_recvDone() && rf12_crc == 0 && rf12_len == 1) {
lastSeq = rf12_data[0];
gotPacket();
sensor_ok = rf12_data[0];
activityLed(1);
delay(5);
activityLed(0);
Serial.print("R ");
Serial.print(lastSeq);
Serial.print(sensor_ok);
Serial.print("\n");
received_sensor_data = true;
if (!sensor_ok) {
// turn on alarm
tone(BEEP_PIN, 2048, 500);
} else {
noTone(BEEP_PIN);
}
}

if (sensorAliveTimer.poll(60000) && ++sensor_alive_minutes >= 3) {
Serial.print("Checking if sensor is alive\n");
switch (state) {
case STATE_NORMAL: {
if (!received_sensor_data)
state = STATE_SENSOR_ERROR;
break;
}
case STATE_SENSOR_ERROR: {
if (received_sensor_data)
state = STATE_NORMAL;
break;
}
case STATE_ALARM: {
if (!received_sensor_data)
state = STATE_SENSOR_ERROR;
break;
}
}

sensor_alive_minutes = 0;
received_sensor_data = false;
}

if (statTimer.poll(DISPLAY_INTERVAL)) {
// number of packets received in the last 5 seconds, as percentage
//lcd3dig(0, 0, recvCount(50) * 2);
// number of packets received in the last second, as percentage
//lcd3dig(9, 0, recvCount(10) * 10);
// show number of packets received in the last 10 display intervals
updateHistory();
//lcd.print(history);


// do the fast loop processing
switch (state) {
case STATE_NORMAL: {
if (!sensor_ok)
state = STATE_ALARM;
break;
}
case STATE_ALARM: {
if (sensor_ok)
state = STATE_NORMAL;
break;
}
case STATE_SENSOR_ERROR: {
if (received_sensor_data)
state = STATE_NORMAL;
}
}

// beeper handling
if (beepTimer.poll(1000)) {
switch (state) {
case STATE_NORMAL: {
// do nothing
noTone(BEEP_PIN);
break;
}
case STATE_ALARM: {
// long beep
tone(BEEP_PIN, 2048, 500);
break;
}
case STATE_SENSOR_ERROR: {
// short beep
tone(BEEP_PIN, 2048, 50);
}
}
}
}

63 changes: 4 additions & 59 deletions examples/RemoteAlarm/Repeater/Repeater.ino
Original file line number Diff line number Diff line change
Expand Up @@ -8,64 +8,19 @@

#include <JeeLib.h>

#define BUFFER_SIZE 64
#define DISPLAY_INTERVAL 500 // ms
#define LED_PIN 9 // activity LED, comment out to disable

//PortI2C myI2C (3);
MilliTimer statTimer;

byte timeBuf [BUFFER_SIZE]; // index is time slot, value is last packet
byte seqBuf [BUFFER_SIZE]; // index is last packet, value is time slot
char history [11];
byte lastSeq;

static void activityLed (byte on) {
#ifdef LED_PIN
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, !on);
#endif
}


static void gotPacket () {
byte tenths = millis() / 100;
// remember for each time slot what the last received packet was
timeBuf[tenths % BUFFER_SIZE] = lastSeq;
// remember for the last BUFFER_SIZE packets when they arrived
seqBuf[lastSeq % BUFFER_SIZE] = tenths;
}

static byte recvCount (byte period) {
// tenths and diff are bytes, so they are automatically modulo 256
byte tenths = millis() / 100;
byte n = 0;
for (byte i = 0; i < sizeof seqBuf; ++i) {
byte diff = tenths - seqBuf[i];
if (diff <= period)
++n;
}
return n;
}

static void updateHistory () {
for (byte i = 1; i < 10; ++i)
history[10-i] = history[9-i];
history[0] = '0';
// tenths and diff are bytes, so they are automatically modulo 256
byte tenths = millis() / 100;
for (byte i = 0; i < DISPLAY_INTERVAL / 100; ++i) {
byte pos = (tenths - i - 1) % BUFFER_SIZE;
byte diff = lastSeq - timeBuf[pos];
if (diff < 5)
++history[0];
}
}

void setup () {
Serial.begin(57600);
Serial.print("\n[BEC Alarm Repeater]");
rf12_initialize('R', RF12_915MHZ);
rf12_initialize('S', RF12_915MHZ);
}

void send_payload(byte data)
Expand All @@ -77,24 +32,14 @@ void send_payload(byte data)

void loop () {
if (rf12_recvDone() && rf12_crc == 0 && rf12_len == 1) {
lastSeq = rf12_data[0];
gotPacket();
byte lastSeq = rf12_data[0];
activityLed(1);
delay(5);
activityLed(0);
Serial.print("R ");
Serial.print(lastSeq);
Serial.print("\n");
send_payload(rf12_data[0]);
}

if (statTimer.poll(DISPLAY_INTERVAL)) {
// number of packets received in the last 5 seconds, as percentage
//lcd3dig(0, 0, recvCount(50) * 2);
// number of packets received in the last second, as percentage
//lcd3dig(9, 0, recvCount(10) * 10);
// show number of packets received in the last 10 display intervals
updateHistory();
//lcd.print(history);
send_payload(lastSeq);
}
}

5 changes: 3 additions & 2 deletions examples/RemoteAlarm/Sensor/Sensor.ino
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ void send_payload()
rf12_sleep(RF12_WAKEUP);
while (!rf12_canSend())
rf12_recvDone();

bool switch_state = check_switches();
rf12_sendStart(0, &switch_state, sizeof switch_state);
rf12_sendWait(SLEEP_MODE_STANDBY);
Expand All @@ -78,14 +79,14 @@ void loop() {
sent = true;
}

if (sendTimer.poll(30000)) {
if (sendTimer.poll(20000)) {
sent = false;
//activityLed(1);
// debug_switch();
send_payload();
//activityLed(0);
}

Sleepy::loseSomeTime(10000);
Sleepy::loseSomeTime(5000);
}

0 comments on commit dc51b18

Please sign in to comment.