Skip to content

Commit

Permalink
Add summon timestamp logging, adjust Arduino sketch code for reconnect
Browse files Browse the repository at this point in the history
  • Loading branch information
0Calories committed Jun 6, 2018
1 parent 167d2ea commit 9d13df1
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
16 changes: 14 additions & 2 deletions arduino/Callbell/Callbell.ino
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,22 @@ void setup() {

void loop() {

// Check the status of the WiFi
// If we have lost connection, set LED to red and attempt to reconnect every 10 seconds
while (WiFi.status() == WL_DISCONNECTED) {
Serial.println("Lost connection the WiFi!");
setColour(255, 0, 0);
delay(10000);
WiFi.begin(ssid, pass);
}

if (WiFi.status() == WL_CONNECTED && canSummon) {
setColour(0, 255, 0);
}

// If cannot currently summon, block thread for 6 secs
if (!canSummon) {
delay(6000);
setColour(0, 255, 0);
canSummon = true;
}

Expand All @@ -81,8 +93,8 @@ void summonRequest() {
// Send POST request
client.println("POST /call");
client.println();
canSummon = false;
}
canSummon = false;
}
}

Expand Down
3 changes: 3 additions & 0 deletions server/callbell-server.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const notifier = require('node-notifier');
const express = require('express');
const {getDateTime} = require('./utils');

const PORT = 4200;
const app = express();
Expand All @@ -23,6 +24,8 @@ app.post('/call', (req, res) => {
wait: false
});

console.log(`${getDateTime()} Summon request received`);

// Anti spam timeout function
setTimeout(() => canSummon = true, SUMMON_DELAY);
res.status(200).send();
Expand Down
20 changes: 20 additions & 0 deletions server/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Courtesy of Ionică Bizău from https://stackoverflow.com/questions/7357734/

let getDateTime = () => {

let date = new Date();

let hour = date.getHours();
hour = (hour < 10 ? "0" : "") + hour;

let min = date.getMinutes();
min = (min < 10 ? "0" : "") + min;

let sec = date.getSeconds();
sec = (sec < 10 ? "0" : "") + sec;

return "[" + hour + ":" + min + ":" + sec + "]";

}

module.exports = { getDateTime };

0 comments on commit 9d13df1

Please sign in to comment.