sketch: Convert from ethernet shield to USB serial comunication

Ethernet shield has been extremely unreliable, and we need a USB host nearby for a webcam anyway.
This commit is contained in:
Petr Baudis 2011-03-13 23:39:25 +01:00
parent ded3bd81f8
commit e2f813dd25
2 changed files with 31 additions and 40 deletions

7
TODO
View file

@ -4,13 +4,6 @@
* Potentially some prettier icons and richer javascript stubs. * Potentially some prettier icons and richer javascript stubs.
* Have the brmlab sketch do bi-directional TCP communication.
Currently, it only sends stuff and cannot receive any
instructions. This is pre-req for features below.
Alternatively, we may decide we need to switch back to
USB communication (with reliable USB host) if the ethernet
shield proves to be too unreliable.
* Way to override status from web page. * Way to override status from web page.
To keep status LED and web page in sync, bi-directional To keep status LED and web page in sync, bi-directional
communication will be required. communication will be required.

View file

@ -2,17 +2,17 @@
#define MUSIC 1 #define MUSIC 1
// if you are running Arduino0018 or older, comment the SPI.h include #include <NewSoftSerial.h>
#include <SPI.h>
#include <Ethernet.h>
// pins // pins
const int doorLock = 5; const int soundPin = 9; /* piezo in series with 100R */
const int soundPin = 9;
const int statusLed = 8; const int statusLed = 8;
const int statusBtn = 7; const int statusBtn = 7;
const int videoLed = 6; const int videoLed = 6;
const int videoBtn = 3; const int videoBtn = 5;
const int doorLock = 4;
const int rfidRx = 3;
const int rfidTx = 2;
int statusState = 0; int statusState = 0;
int videoState = 0; int videoState = 0;
@ -27,8 +27,11 @@ struct ACLdata {
#include "cardids.h" #include "cardids.h"
}; };
// run telnet server // comSerial for communication with the host
Server server(23); #define comSerial Serial
// rfidSerial for communication with the RFID reader
NewSoftSerial rfidSerial(rfidTx, rfidRx);
#if MUSIC #if MUSIC
@ -52,7 +55,7 @@ void toneManual(int pin, int frequency, int duration)
* It seems about right, but has not been tuned precisely for * It seems about right, but has not been tuned precisely for
* a 16MHz ATMega. */ * a 16MHz ATMega. */
delayMicroseconds(period - 50); delayMicroseconds(period - 50);
//server.print(pin, DEC); server.print(state, DEC); server.write(" "); server.print(period); server.write(" "); server.print(length); server.write("\n"); //comSerial.print(pin, DEC); comSerial.print(state, DEC); comSerial.write(" "); comSerial.print(period); comSerial.write(" "); comSerial.print(length); comSerial.write("\n");
} }
} }
@ -60,7 +63,7 @@ void playMelody(int *melody, int *noteDurations, int notes)
{ {
int i; int i;
for (i = 0; i < notes; i++) { for (i = 0; i < notes; i++) {
// server.print(melody[i]); server.write(" "); server.print(noteDurations[i]); server.write("\n"); // comSerial.print(melody[i]); comSerial.write(" "); comSerial.print(noteDurations[i]); comSerial.write("\n");
toneManual(soundPin, melody[i], noteDurations[i]); toneManual(soundPin, melody[i], noteDurations[i]);
delay(noteDurations[i] * 6/10); delay(noteDurations[i] * 6/10);
@ -95,26 +98,28 @@ void readCard()
byte RequestCardStatus[] = { 0xAA, 0x00, 0x03, 0x25, 0x26, 0x00, 0x00, 0xBB }; byte RequestCardStatus[] = { 0xAA, 0x00, 0x03, 0x25, 0x26, 0x00, 0x00, 0xBB };
byte NoCardResponse[] = { 0xAA, 0x00, 0x02, 0x01, 0x83, 0x80, 0xBB }; byte NoCardResponse[] = { 0xAA, 0x00, 0x02, 0x01, 0x83, 0x80, 0xBB };
byte buf[16]; byte buf[16];
int i;
// write query to serial // write query to serial
Serial.write(RequestCardStatus, 8); for (i = 0; i < 8; i++)
rfidSerial.print(RequestCardStatus[i]);
// wait for the result, while reblinking // wait for the result, while reblinking
delay(100); delay(100);
digitalWrite(statusLed, statusState); digitalWrite(statusLed, statusState);
delay(150); delay(150);
// read input from serial into the buffer // read input from serial into the buffer
int i = 0; i = 0;
while (Serial.available() > 0) { while (rfidSerial.available() > 0) {
if (i < sizeof(buf)) { if (i < sizeof(buf)) {
buf[i] = Serial.read(); buf[i] = rfidSerial.read();
} }
++i; ++i;
} }
// no card is detected // no card is detected
if (!memcmp(buf, NoCardResponse, 7)) { if (!memcmp(buf, NoCardResponse, 7)) {
server.write("NOCARD\n"); comSerial.write("NOCARD\n");
} }
// card detected - message has form AA0006xxxxxxxxxxxxxxBB where xxx... is the card ID // card detected - message has form AA0006xxxxxxxxxxxxxxBB where xxx... is the card ID
@ -125,9 +130,9 @@ void readCard()
// if there is a match - print known card ... // if there is a match - print known card ...
if (!memcmp(ACL[i].cardId, buf+3, 7)) { if (!memcmp(ACL[i].cardId, buf+3, 7)) {
known = true; known = true;
server.write("CARD "); comSerial.write("CARD ");
server.write(ACL[i].nick); comSerial.write(ACL[i].nick);
server.write("\n"); comSerial.write("\n");
// ... and open door for 5s // ... and open door for 5s
openDoorForTime(5000); openDoorForTime(5000);
break; break;
@ -135,12 +140,12 @@ void readCard()
} }
// card was not found in the ACL // card was not found in the ACL
if (!known) { if (!known) {
server.write("CARD UNKNOWN "); comSerial.write("CARD UNKNOWN ");
for (int i = 0; i < 7; ++i) { for (int i = 0; i < 7; ++i) {
if (buf[i+3] < 0xF) server.write("0"); if (buf[i+3] < 0xF) comSerial.write("0");
server.print(buf[i+3], HEX); comSerial.print(buf[i+3], HEX);
} }
server.write("\n"); comSerial.write("\n");
playMelodyNak(); playMelodyNak();
} }
} }
@ -150,12 +155,6 @@ void readCard()
void setup() void setup()
{ {
// constants for ethernet shield
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 1, 3 };
byte gateway[] = { 192, 168, 1, 1 };
byte subnet[] = { 255, 255, 255, 0 };
pinMode(doorLock, OUTPUT); pinMode(doorLock, OUTPUT);
pinMode(soundPin, OUTPUT); pinMode(soundPin, OUTPUT);
pinMode(statusLed, OUTPUT); pinMode(statusLed, OUTPUT);
@ -164,9 +163,8 @@ void setup()
digitalWrite(statusBtn, HIGH); digitalWrite(statusBtn, HIGH);
pinMode(videoBtn, INPUT); pinMode(videoBtn, INPUT);
digitalWrite(videoBtn, HIGH); digitalWrite(videoBtn, HIGH);
Serial.begin(9600); rfidSerial.begin(9600);
Ethernet.begin(mac, ip, gateway, subnet); comSerial.begin(9600);
server.begin();
} }
void loop() void loop()
@ -175,7 +173,7 @@ void loop()
videoState = !digitalRead(videoBtn); videoState = !digitalRead(videoBtn);
digitalWrite(statusLed, !statusState); // will be turned back in readCard() digitalWrite(statusLed, !statusState); // will be turned back in readCard()
digitalWrite(videoLed, videoState); digitalWrite(videoLed, videoState);
server.print(statusState, DEC); server.write(" "); comSerial.print(statusState, DEC); comSerial.write(" ");
server.print(videoState, DEC); server.write(" "); comSerial.print(videoState, DEC); comSerial.write(" ");
readCard(); readCard();
} }