Skip to content

Commit

Permalink
Updated HotelTagWriter and HotelNodeApp to work better together.
Browse files Browse the repository at this point in the history
  • Loading branch information
tigoe committed Sep 5, 2013
1 parent 3e4dffa commit fe66165
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 33 deletions.
22 changes: 13 additions & 9 deletions HotelNodeApp/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,20 +63,24 @@ app.post('/submit', function (request, response) {
// send it out the serial port:
myPort.write(JSON.stringify(record) + "\n");

// wait 750 ms before responding to browser, so that
// write the HTML head back to the browser:
response.writeHead(200, {'Content-Type': 'text/html'});
// send the data:
response.write("<p><a href=\"/\">Return to form</a></p>");
response.write("Sent the following to the writer device:<br>");
response.write(JSON.stringify(record) + "<p>");

// wait 3 seconds before closing the connection, so that
// you can get a response from the writer:
setTimeout(function() {
// write the HTML head back to the browser:
response.writeHead(200, {'Content-Type': 'text/html'});
// send the data:
response.write("Sent the following to the writer device:<br>");
response.write(JSON.stringify(record) + "<p>");
setTimeout(function() {
// if you got a response from the writer, send it too:
if (deviceMessage != "") {
response.write("response from writer device: " + deviceMessage + "<p>");
deviceMessage = "";
} else {
response.write("no tag present");
}
// send the link back to the index and close the link:
response.end("<a href=\"/\">Return to form</a>");
}, 750); // end of setTimeout()
response.end();
}, 3000); // end of setTimeout()
}); // end of app.post()
49 changes: 25 additions & 24 deletions HotelTagWriter/HotelTagWriter.ino
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@ void loop() {
char thisChar = Serial.read();
// add incoming character to the end of inputString:
inputString += thisChar;

if (thisChar == '{') {
// new message, reset buffer
inputString = "{";
readyToWrite = false;
} else if (thisChar == '}') {
}
else if (thisChar == '}') {
// end of message, ready to write to tag
Serial.println("Ready to write data to tag");
readyToWrite = true;
Expand All @@ -68,36 +68,37 @@ void loop() {
// keep looking for a tag to write to when
// you've got a string to write:
if (readyToWrite) {
lookForTag();
lookForTag();
}

if (millis() - lightOnTime > 3000 ) { // check every three seconds
digitalWrite(greenLed, LOW); // turn off pin 9
digitalWrite(redLed, LOW); // turn off pin 8
}
}

void lookForTag() {
if (millis() - lastReadTime > 3000) { // read every three seconds
if (nfc.tagPresent()) { // if there's a tag present
NdefMessage message; // make a new NDEF message
// add the input string as a record:
message.addMimeMediaRecord("text/hotelkey", inputString);
boolean success = nfc.write(message); // attempt to write to the tag

if (success) {
// let the desktop app know you succeeded:
Serial.println("Result: tag written.");
digitalWrite(greenLed, HIGH); // turn on the success light
lightOnTime = millis();
} else {
// let the desktop app know you failed:
Serial.println("Result: failed to write to tag");
digitalWrite(redLed, HIGH); // turn on the failure light
lightOnTime = millis();
}
if (nfc.tagPresent()) { // if there's a tag present
NdefMessage message; // make a new NDEF message
// add the input string as a record:
message.addMimeMediaRecord("text/hotelkey", inputString);
boolean success = nfc.write(message); // attempt to write to the tag

if (success) {
// let the desktop app know you succeeded:
Serial.println("Result: tag written.");
digitalWrite(greenLed, HIGH); // turn on the success light
lightOnTime = millis();
readyToWrite = false; // clear write flag
}
else {
// let the desktop app know you failed:
Serial.println("Result: failed to write to tag");
digitalWrite(redLed, HIGH); // turn on the failure light
lightOnTime = millis();
}
lastReadTime = millis();
}
}
lastReadTime = millis();
}


0 comments on commit fe66165

Please sign in to comment.