Author Topic: Moteino + Raspberry pi controlled door lock  (Read 14375 times)

goryri

  • Newbie
  • *
  • Posts: 35
Re: Moteino + Raspberry pi controlled door lock
« Reply #30 on: March 07, 2014, 03:28:44 PM »
Tank you !  ;) By the way doo you have any experience using the SoftwareSerial Libary and RFM12B Libary together ? Im tring to connect RFID Reader ID-12LA tx line to pin 9 on the Moteino it works when i don't include the RFM12B libary... Maybe both libary uses the same Interrupts?

Code: [Select]
#include <SoftwareSerial.h>
#include <RFM12B.h>

#define SERIAL_BAUD 115200
#define NETWORKID       100
#define NODEID            1                 // network ID used for this unit
#define DOORNODEID  3
#define SAFENODEID  4

// The RFID module's TX pin needs to be connected to the Arduino.

#define rxPin 9
#define txPin 7
#define MAX_ID 20

//char input[RF12_MAXDATA];
#define KEY  "ABCDABCDABCDABCD"
RFM12B radio;
byte readSerialLine(char* input, char endOfLineChar=10, byte maxLength=64, uint16_t timeout=50);

// Create a software serial object for the connection to the RFID module
SoftwareSerial rfid = SoftwareSerial( rxPin, txPin );

// note, retype rather than copy and paste or you get a funny extra character
const char * allowedTags [] =
{
  "4500BE8CC3B4",  // 1 - Gøran Yri
  "8E40736E6073",  // 2 - Dick
  "A609CA0E4311",  // 3 - Harry
  "A3EC57520837",  // 4 - visitor

  NULL  // end of table marker
};

// List of names to associate with the matching tag IDs
char* tagName[] = {
  "DORUNL",      // Tag 1
  "DORLOC",      // Tag 2
  "NAME 3",       // Tag 3
};

// Check the number of tags defined
int numberOfTags = sizeof(allowedTags)/sizeof(allowedTags[0]);

void setup()
{
    radio.Initialize(NODEID, RF12_433MHZ, NETWORKID);
    radio.Encrypt((uint8_t*)KEY);
    Serial.begin(SERIAL_BAUD);
    Serial.println("Listening...");
    rfid.begin(9600);      // Serial port for connection to RFID module
}


#define MAX_ID 20

char cur_id [MAX_ID] = "";
int len = 0;

byte recvCount = 0;
byte ackCount=0;
byte inputLen=0;
char input[64];

void loop()
{
  //process any serial input
  inputLen = readSerialLine(input);
     
  if (inputLen >= 6)
  {
       if (input[0]=='D' && input[1]=='O' && input[2]=='R' && input[3]=='U' && input[4]=='N' && input[5]=='L')
      {
        Serial.print("UNL ... ");
        if (radio.sendWithRetry(DOORNODEID, "UNL", 3))
          Serial.println("ok ... ");
        else Serial.println("nothing ... ");
      }
      if (input[0]=='D' && input[1]=='O' && input[2]=='R' && input[3]=='L' && input[4]=='O' && input[5]=='C')
      {
        Serial.print("LOC ... ");
        if (radio.sendWithRetry(DOORNODEID, "LOC", 3))
          Serial.println("ok ... ");
        else Serial.println("nothing ... ");
      }
      if (input[0]=='D' && input[1]=='O' && input[2]=='R' && input[3]=='S' && input[4]=='T' && input[5]=='S')
      {
        Serial.print("STS ... ");
        if (radio.sendWithRetry(DOORNODEID, "STS", 3))
          Serial.println("ok ... ");
        else Serial.println("nothing ... ");
      }
      if (input[0]=='D' && input[1]=='O' && input[2]=='R' && input[3]=='N' && input[4]=='A' && input[5]=='T')
      {
        Serial.print("NAT ... ");
        if (radio.sendWithRetry(DOORNODEID, "NAT", 3))
          Serial.println("ok ... ");
        else Serial.println("nothing ... ");
      }
      if (input[0]=='D' && input[1]=='O' && input[2]=='R' && input[3]=='M' && input[4]=='O' && input[5]=='R')
      {
        Serial.print("MOR ... ");
        if (radio.sendWithRetry(DOORNODEID, "MOR", 3))
          Serial.println("ok ... ");
        else Serial.println("nothing ... ");
      }
      if (input[0]=='L' && input[1]=='E' && input[2]=='D' && input[3]=='S' && input[4]=='T' && input[5]=='S')
      {
        Serial.print("LEDSTS ... ");
        if (radio.sendWithRetry(DOORNODEID, "LEDSTS", 6))
          Serial.println("ok ... ");
        else Serial.println("nothing ... ");
      }
      if (input[0]=='L' && input[1]=='E' && input[2]=='D' && input[3]=='T' && input[4]=='O' && input[5]=='G')
      {
        Serial.print("Toggle led ... ");
        if (radio.sendWithRetry(DOORNODEID, "LEDTOG", 6))
          Serial.println("ok ... ");
        else Serial.println("nothing ... ");
      }
     
      if (input[0]=='S' && input[1]=='A' && input[2]=='F' && input[3]=='O' && input[4]=='P' && input[5]=='N')
      {
        Serial.print("OPEN SAFE ... ");
        if (radio.sendWithRetry(SAFENODEID, "SAFOPN", 6))
          Serial.println("ok ... ");
        else Serial.println("nothing ... ");
      }
      if (input[0]=='S' && input[1]=='A' && input[2]=='F' && input[3]=='S' && input[4]=='T' && input[5]=='S')
      {
        Serial.print("GETTING SAFE STATUS ... ");
        if (radio.sendWithRetry(SAFENODEID, "STS", 3))
          Serial.println("ok ... ");
        else Serial.println("nothing ... ");
      }
      if (input[0]=='S' && input[1]=='A' && input[2]=='F' && input[3]=='O' && input[4]=='N' && input[5]=='N')
      {
        Serial.print("TURNING ALL LIGHT ON ... ");
        if (radio.sendWithRetry(SAFENODEID, "ON", 2))
          Serial.println("ok ... ");
        else Serial.println("nothing ... ");
      }
      if (input[0]=='S' && input[1]=='A' && input[2]=='F' && input[3]=='O' && input[4]=='F' && input[5]=='F')
      {
        Serial.print("TURNING ALL LIGHT OFF ... ");
        if (radio.sendWithRetry(SAFENODEID, "OFF", 3))
          Serial.println("ok ... ");
        else Serial.println("nothing ... ");
      }
  }
   
    if (radio.ReceiveComplete())
    {
      if (radio.CRCPass())
      {
        digitalWrite(9,1);
        Serial.print('[');Serial.print(radio.GetSender(), DEC);Serial.print("] ");
        for (byte i = 0; i < *radio.DataLen; i++)
          Serial.print((char)radio.Data[i]);
        //Serial.print("   [RSSI:");Serial.print(radio.RSSI);Serial.print("]");
        //Serial.print("   [RSSI:-105");Serial.print("]");
        if (radio.ACKRequested())
        {
          radio.SendACK();
          Serial.print(" [ACK-sent]");
        }
        delay(5);
        digitalWrite(9,0);
      }
      else Serial.print(" [BAD-CRC]");
      Serial.println();
    }

  char inByte;
  int tagId = 0;
  if (rfid.available() > 0) {
    // get incoming byte:
    inByte = rfid.read();

    switch (inByte)
    {
    case 2:    // start of text
      len = 0;
      break;

    case 3:   // end of text
      cur_id [len] = 0;
      Serial.println (cur_id);
     
      // Search the tag database for this particular tag
      tagId = findTag(cur_id);
     
      // Only fire the strike plate if this tag was found in the database
      if( tagId > 0 )
      {
        Serial.print("Authorized tag ID ");
        Serial.print(tagId);
        Serial.print(": unlocking for ");
        Serial.println(tagName[tagId - 1]);   // Get the name for this tag from the database
        char buff[10];
        sprintf(buff, tagName[tagId - 1]);
        byte len = strlen(buff); 
        radio.sendWithRetry(DOORNODEID, buff, len);
      } else {
        Serial.println("Tag not authorized");
      }
      Serial.println();     // Blank separator line in output

      Serial.flush (); 
      len = 0; 
      break;

    case 10:   // newline
    case 13:   // carriage return
      break;

    default:
      if (len >= (MAX_ID - 1))
        break;

      cur_id [len++] = inByte;
      break;

    }  // end of switch

  }  // end of incoming data
}  // end of loop


/**
 * Search for a specific tag in the database
 */
int findTag(char tagValue[MAX_ID] ) {
  for (int thisCard = 0; thisCard < numberOfTags; thisCard++) {
    // Check if the tag value matches this row in the tag database
    if(strcmp(tagValue, allowedTags[thisCard]) == 0)
    {
      // The row in the database starts at 0, so add 1 to the result so
      // that the card ID starts from 1 instead (0 represents "no match")
      return(thisCard + 1);
    }
  }
  // If we don't find the tag return a tag ID of 0 to show there was no match
  return(0);
}

// reads a line feed (\n) terminated line from the serial stream
// returns # of bytes read, up to 255
// timeout in ms, will timeout and return after so long
byte readSerialLine(char* input, char endOfLineChar, byte maxLength, uint16_t timeout)
{
  byte inputLen = 0;
  Serial.setTimeout(timeout);
  inputLen = Serial.readBytesUntil(endOfLineChar, input, maxLength);
  input[inputLen]=0;//null-terminate it
  Serial.setTimeout(0);
  //Serial.println();
  return inputLen;
}

Felix

  • Administrator
  • Hero Member
  • *****
  • Posts: 6017
  • Country: us
    • LowPowerLab
Re: Moteino + Raspberry pi controlled door lock
« Reply #31 on: March 10, 2014, 09:02:11 AM »
Yes I have used software serial with RFM12B before, no issues at 57600baud. But if you are heavy on the interrupts side you might have to slow down the baud rate. The pins for software serial should not interfere with the RFM interrupt.

goryri

  • Newbie
  • *
  • Posts: 35
Re: Moteino + Raspberry pi controlled door lock
« Reply #32 on: March 10, 2014, 04:13:46 PM »
My code worked but I had forgot to set the correct baud rate on the serial terminal  :-[

goryri

  • Newbie
  • *
  • Posts: 35
Re: Moteino + Raspberry pi controlled door lock
« Reply #33 on: March 15, 2014, 05:52:42 AM »
I have made a new video have also made new intro let me now what you think about it.
« Last Edit: March 15, 2014, 06:30:14 AM by goryri »

goryri

  • Newbie
  • *
  • Posts: 35
Re: Moteino + Raspberry pi controlled door lock
« Reply #34 on: March 17, 2014, 06:02:04 PM »
Tomorrow do I go to military so have updated my blog and posted my code :) I have learned a lot the past two months ! http://yrielectro.com/ http://yrielectro.com/homeautomation.zip

Felix

  • Administrator
  • Hero Member
  • *****
  • Posts: 6017
  • Country: us
    • LowPowerLab
Re: Moteino + Raspberry pi controlled door lock
« Reply #35 on: March 17, 2014, 07:37:01 PM »
Thanks for sharing your project and enjoy your military service, if that is possible :)