NFC: Event erzeugen und Tag-ID übertragen wenn ein Tag erkannt wird...

Begonnen von Ranseyer, 11 Februar 2018, 18:28:31

Vorheriges Thema - Nächstes Thema

Ranseyer

Hi,

der folgende Sketch läuft gerade auf einem Arduino Nano. Der hängt am RS485 Bus und verfügt über einen NFS-Reader. PDF-Doku.

...
// Enable debug prints to serial monitor
#define MY_DEBUG

#define MY_NODE_ID 22
// Enable and select radio type attached
//#define MY_RADIO_NRF24
//#define MY_RADIO_RFM69
// Enable RS485 transport layer
#define MY_RS485
#define MY_TRANSPORT_WAIT_READY_MS 1000

// Define this to enables DE-pin management on defined pin
#define MY_RS485_DE_PIN 2

// Set RS485 baud rate to use
#define MY_RS485_BAUD_RATE 38400

// Enable this if RS485 is connected to a hardware serial port
// Not possible with Arduino Nano !!
//#define MY_RS485_HWSERIAL Serial


#include <SPI.h>
#include <MySensors.h> 
#include <Wire.h>
#include <PN532_I2C.h>
#include <PN532.h>


// Add your valid rfid keys here. To find you your key just run sketch; hold your new RFID tag in fron ot the reader;
// and copy the key from serial output of this sketch.
const uint8_t maxKeyLength = 7;
uint8_t validKeys[][maxKeyLength] = {
                    { 0xB3, 0xC6, 0xD9, 0x80, 0x00, 0x00, 0x00 },
                    { 0, 0, 0, 0, 0, 0, 0 },    // ADD YOUR KEYS HERE!
                    { 0, 0, 0, 0, 0, 0, 0 }};
int keyCount = sizeof validKeys / maxKeyLength;


#define CHILD_ID 99   // Id of the sensor child

// Pin definition
const int lockPin = 4;         // (Digital 4) The pin that activates the relay/solenoid lock.
bool lockStatus;
MyMessage lockMsg(CHILD_ID, V_LOCK_STATUS);
PN532_I2C pn532i2c(Wire);
PN532 nfc(pn532i2c);

void setup() {
 
  pinMode(lockPin, OUTPUT);

  nfc.begin();
  uint32_t versiondata = nfc.getFirmwareVersion();
  if (! versiondata) {
    Serial.print("Couldn't find PN53x board");
    while (1); // halt
  }
  Serial.print("Found NFC chip PN5"); Serial.println((versiondata>>24) & 0xFF, HEX);
  Serial.print("Firmware ver. "); Serial.print((versiondata>>16) & 0xFF, DEC);
  Serial.print('.'); Serial.println((versiondata>> & 0xFF, DEC);
  // Set the max number of retry attempts to read from a card
  // This prevents us from waiting forever for a card, which is
  // the default behaviour of the PN532.
  nfc.setPassiveActivationRetries(0x3);
 
  // configure board to read RFID tags
  nfc.SAMConfig();

  lockStatus = loadState(0);    // Read last lock status from eeprom
  setLockState(lockStatus, true); // Now set the last known state and send it to controller
}

void presentation()  {
  sendSketchInfo("RFID Lock", "1.0");
  present(CHILD_ID, S_LOCK);
}

void loop() {
  bool success;
  uint8_t key[] = { 0, 0, 0, 0, 0, 0, 0 };  // Buffer to store the returned UID
  uint8_t currentKeyLength;                        // Length of the UID (4 or 7 bytes depending on ISO14443A card type)

 
  // Wait for an ISO14443A type cards (Mifare, etc.).  When one is found
  // 'uid' will be populated with the UID, and uidLength will indicate
  // if the uid is 4 bytes (Mifare Classic) or 7 bytes (Mifare Ultralight)
  success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, &key[0], &currentKeyLength);
 
  if (success) {
    Serial.print("Found tag id: ");
    for (uint8_t i=0; i < currentKeyLength; i++)
    {
      if (i>0) Serial.print(",");
      Serial.print("0x");Serial.print(key[i], HEX);
    }
    for (uint8_t i=currentKeyLength; i < maxKeyLength; i++)
    {
      Serial.print(",0x00");
    }


    Serial.println("");

    bool valid = false;
    // Compare this key to the valid once registered here in sketch
    for (int i=0;i<keyCount && !valid;i++) {
      for (int j=0;j<currentKeyLength && !valid;j++) {
        if (key[j] != validKeys[i][j]) {
          break;
        }
        if (j==currentKeyLength-1) {
          valid = true;
        }
      }
    }
    if (valid) {
      // Switch lock status
      setLockState(!lockStatus, true);       
    }
   
    // Wait for card/tag to leave reader   
    while(nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, &key[0], &currentKeyLength));
  }
}



// Unlocks the door.
void setLockState(bool state, bool doSend){
  if (state)
     Serial.println("open lock");
  else
     Serial.println("close lock");
  if (doSend)
    send(lockMsg.set(state));
  digitalWrite(lockPin, state);
  saveState(0,state);
  lockStatus = state;
}

void receive(const MyMessage &message) {
  // We only expect one type of message from controller. But we better check anyway.
  if (message.type==V_LOCK_STATUS) {
     // Change relay state
     setLockState(message.getBool(), false);
 
     // Write some debug info
     Serial.print("Incoming lock status:");
     Serial.println(message.getBool());
   }
}


Auf der Debug-Konsole bekomme ich dann solche Meldungen:
(leicht verfremdet)
ZitatFound tag id: 0x4,0x15,0x99,0x49,0xDD,0xDD,0x81
Found tag id: 0x65,0xDD,0xDD,0x6B,0x00,0x00,0x00



Ziel ist
A) Der Sketch sollte einen Pin ansteuern können. Das zu Nutzen mach aber bestenfalls Sinn wenn der Arduino gut geschützt ist...
B) Ich möchte einen Event im FHEM erhalten und die ID des Tags sehen. Daraufhin will ich dann FHEM entscheiden lassen was zu tun ist.


Hintergrund: Die Steuersignale für Garagentore ((Türöffner) usw. will ich keinesfalls außen an Gebäuden erreichbar haben.


Was mir helfen würde wäre eine Idee wie man das am besten coded. Noch besser ein Codeschnipsel, gerne auch ungetestet!
FHEM mit FTUI. Homematic-Funk für Thermostate und Licht. MySensors als Basis für eigene HW.
Zentrale ist der MAPLE-CUL mit RFM69+HModUART-AddOn.
Doku zu meinen Projekten: Github/Ranseyer. Platinen falls verfügbar gerne auf Anfrage.
Support: gerne wenn ich Zeit+Lust habe im Forum. Nicht per PN!

localhosthack0r

Hi,
nicht getestet, aber als Gedankenanstoß:

Vielleicht kann man die ID als String, per V_TEXT S_INFO an FHEM schicken, siehe:

MySensors API
https://www.mysensors.org/download/serial_api_20
und
https://forum.fhem.de/index.php/topic,75374.0.html

Ranseyer

Danke, ich fürchte das ist die falsche Richtung. V_Text denke ist um ein Display anzusteuern. (Oder geht das dann in beide Richtungen?)

Ich wäre eher gewesen bei:
ZitatV_IR_RECEIVE   33   This message contains a received IR-command   S_IR

Im Moment brauche ich erst noch einen String mit der ID des RFID Tags. Denke das bekomme ich hier vor Ort hin. Würde mich dann nochmals melden wegen einem Tipp um das zu übertragen und einen Event im FHEM zu erzeugen...

FHEM mit FTUI. Homematic-Funk für Thermostate und Licht. MySensors als Basis für eigene HW.
Zentrale ist der MAPLE-CUL mit RFM69+HModUART-AddOn.
Doku zu meinen Projekten: Github/Ranseyer. Platinen falls verfügbar gerne auf Anfrage.
Support: gerne wenn ich Zeit+Lust habe im Forum. Nicht per PN!

Ranseyer

Der Sketch zeigt schon mal die ID in der Debug-Konsole:

/**
* The MySensors Arduino library handles the wireless radio link and protocol
* between your home built sensors/actuators and HA controller of choice.
* The sensors forms a self healing radio network with optional repeaters. Each
* repeater and gateway builds a routing tables in EEPROM which keeps track of the
* network topology allowing messages to be routed to nodes.
*
* Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
* Copyright (C) 2013-2015 Sensnology AB
* Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
*
* Documentation: http://www.mysensors.org
* Support Forum: http://forum.mysensors.org
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 2 as published by the Free Software Foundation.
*
*******************************
*
* REVISION HISTORY
* Version 1.0 - Henrik Ekblad
*
* DESCRIPTION
* RFID Lock sensor/actuator
*
* Use RFID tag to lock/unlock a door or trigger a scene on your controller.
* This example sketch allows you to add an optional relay or solenoid
* which can be activated/opened by RFID or controller. 
*
* Use the I2C wiring option for your RFID module and connect to the following Arduino pins.
*
* RFID       Arduino
* -----      -------
* GND   ->   GND
* VCC   ->   +5V
* SCL   ->   A5
* SDA   ->   A4
*
* Use normal wiring for NRF24L01 radio
*
* Attach a optional relay or solonoid lock to pin 4
* http://www.mysensors.org/build/rfid
*/

// Enable debug prints to serial monitor
#define MY_DEBUG
#define MY_TRANSPORT_WAIT_READY_MS 1000

#define MY_NODE_ID 22
// Enable and select radio type attached
//#define MY_RADIO_NRF24
//#define MY_RADIO_RFM69
// Enable RS485 transport layer
#define MY_RS485


// Define this to enables DE-pin management on defined pin
#define MY_RS485_DE_PIN 2

// Set RS485 baud rate to use
#define MY_RS485_BAUD_RATE 38400

// Enable this if RS485 is connected to a hardware serial port
// Not possible with Arduino Nano !!
//#define MY_RS485_HWSERIAL Serial

#define RF_INIT_DELAY   125



#include <SPI.h>
#include <MySensors.h> 
#include <Wire.h>
#include <PN532_I2C.h>
#include <PN532.h>





// Add your valid rfid keys here. To find you your key just run sketch; hold your new RFID tag in fron ot the reader;
// and copy the key from serial output of this sketch.
const uint8_t maxKeyLength = 7;
uint8_t validKeys[][maxKeyLength] = {
                    { 0xB3, 0xC6, 0xD9, 0x80, 0x00, 0x00, 0x00 },
                    { 0, 0, 0, 0, 0, 0, 0 },    // ADD YOUR KEYS HERE!
                    { 0, 0, 0, 0, 0, 0, 0 }};
int keyCount = sizeof validKeys / maxKeyLength;
String tagid = String();


#define CHILD_ID_LOCK  1   // Id of the sensor child
#define CHILD_ID_WRONG 2   // Id of the sensor child
#define CHILD_ID_ALARM 3   // Id of the sensor child
#define CHILD_ID_TAGID 4   // Id of the sensor child
//#define CHILD_ID 99   // Id of the sensor child

// Pin definition
const int lockPin = 4;         // (Digital 4) The pin that activates the relay/solenoid lock.
bool lockStatus;
MyMessage  lockMsg(CHILD_ID_LOCK, V_LOCK_STATUS);
MyMessage  armMsg(CHILD_ID_WRONG, V_ARMED);
MyMessage  wrongMsg(CHILD_ID_ALARM, V_TRIPPED);
MyMessage  tagMsg(CHILD_ID_TAGID,  V_IR_RECEIVE);
//xxx


PN532_I2C pn532i2c(Wire);
PN532 nfc(pn532i2c);

void setup() {

  pinMode(lockPin, OUTPUT);

  nfc.begin();
  uint32_t versiondata = nfc.getFirmwareVersion();
  if (! versiondata) {
    Serial.print("Couldn't find PN53x board");
    while (1); // halt
  }
  Serial.print("Found NFC chip PN5"); Serial.println((versiondata>>24) & 0xFF, HEX);
  Serial.print("Firmware ver. "); Serial.print((versiondata>>16) & 0xFF, DEC);
  Serial.print('.'); Serial.println((versiondata>>8) & 0xFF, DEC);
  // Set the max number of retry attempts to read from a card
  // This prevents us from waiting forever for a card, which is
  // the default behaviour of the PN532.
  nfc.setPassiveActivationRetries(0x3);

  // configure board to read RFID tags
  nfc.SAMConfig();

  lockStatus = loadState(0);    // Read last lock status from eeprom
  setLockState(lockStatus, true); // Now set the last known state and send it to controller
}

void presentation()  {
  sendSketchInfo("RFID Lock", "0.0.2");
  present(CHILD_ID_LOCK, S_LOCK);      delay(RF_INIT_DELAY);
  present(CHILD_ID_WRONG, S_DOOR);   delay(RF_INIT_DELAY);
  present(CHILD_ID_ALARM, S_MOTION);   delay(RF_INIT_DELAY);
  present(CHILD_ID_TAGID, S_IR);     delay(RF_INIT_DELAY);
}




void loop() {
  bool success;
  uint8_t key[] = { 0, 0, 0, 0, 0, 0, 0 };  // Buffer to store the returned UID
  uint8_t currentKeyLength;                        // Length of the UID (4 or 7 bytes depending on ISO14443A card type)

  // Wait for an ISO14443A type cards (Mifare, etc.).  When one is found
  // 'uid' will be populated with the UID, and uidLength will indicate
  // if the uid is 4 bytes (Mifare Classic) or 7 bytes (Mifare Ultralight)
  success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, &key[0], &currentKeyLength);
//  Serial.print(key, BYTE);
//    Serial.println(uid);


 
  if (success) {
    tagid+=("Found tag id: ");
    for (uint8_t i=0; i < currentKeyLength; i++)
    {
      if (i>0) tagid+=(",");
      tagid+=("0x");tagid+=(key[i]);
    }
    for (uint8_t i=currentKeyLength; i < maxKeyLength; i++)
    {
      tagid+=(",0x00");
    }

//    Serial.println("Start");
    Serial.println(tagid);
//    Serial.println("Ende");
      bool tripped = 1;
      Serial.println(tripped);
      send(wrongMsg.set(tripped?"1":"0"));  // Send tripped value to gw
      send(tagMsg.set(tagid));  // Send id of the rfid-tag  to gw
      //V_IR_RECEIVE  33  This message contains a received IR-command S_IR
//MyMessage  lockMsg(CHILD_ID_LOCK, V_LOCK_STATUS);
//MyMessage  armMsg(CHILD_ID_WRONG, V_ARMED);
//MyMessage  wrongMsg(CHILD_ID_ALARM, V_TRIPPED);
//MyMessage  tagMsg(CHILD_TAGID, V_TRIPPED);




    Serial.println("");

    bool valid = false;
    // Compare this key to the valid once registered here in sketch
    for (int i=0;i<keyCount && !valid;i++) {
      for (int j=0;j<currentKeyLength && !valid;j++) {
        if (key[j] != validKeys[i][j]) {
          break;
        }
        if (j==currentKeyLength-1) {
          valid = true;
        }
      }
    }
    if (valid) {
      // Switch lock status
      setLockState(!lockStatus, true);       
    }

    // Wait for card/tag to leave reader   
    while(nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, &key[0], &currentKeyLength));
  }
}



// Unlocks the door.
void setLockState(bool state, bool doSend){
  if (state)
     Serial.println("open lock");
  else
     Serial.println("close lock");
  if (doSend)
    send(lockMsg.set(state));
  digitalWrite(lockPin, state);
  saveState(0,state);
  lockStatus = state;
}

void receive(const MyMessage &message) {
  // We only expect one type of message from controller. But we better check anyway.
  if (message.type==V_LOCK_STATUS) {
     // Change relay state
     setLockState(message.getBool(), false);

     // Write some debug info
     Serial.print("Incoming lock status:");
     Serial.println(message.getBool());
   }
}


18608 TSF:MSG:SEND,22-22-0-0,s=4,c=1,t=33,pt=1,l=1,sg=0,ft=0,st=OK:1

Found tag id: 0x222,0x2222,0x37,0x59,0x00,0x00,0x00Found tag id: 0x2222,0x22,0x37,0x59,0x00,0x00,0x00
1
20432 TSF:MSG:SEND,22-22-0-0,s=3,c=1,t=16,pt=0,l=1,sg=0,ft=0,st=OK:1
20444 TSF:MSG:SEND,22-22-0-0,s=4,c=1,t=33,pt=1,l=1,sg=0,ft=0,st=OK:1



Nun muss ich noch die Übertragung zu FHEM hinbekommen...
FHEM mit FTUI. Homematic-Funk für Thermostate und Licht. MySensors als Basis für eigene HW.
Zentrale ist der MAPLE-CUL mit RFM69+HModUART-AddOn.
Doku zu meinen Projekten: Github/Ranseyer. Platinen falls verfügbar gerne auf Anfrage.
Support: gerne wenn ich Zeit+Lust habe im Forum. Nicht per PN!

tiwo85

Ich habe mal bei einem Sensor mit V_var1 Etc. rumexperementiert. Das ist bei jedem Device verfügbar und du kannst das auch in FHEM als Reading registrieren.

Gesendet von meinem Huawei P10 Plus mit Tapatalk


KarlHeinz2000

An der Stelle, wo die debug Ausgabe rauskommt, die ID nicht (nur) seriell ausgeben, sondern parallel in ein char[] kopieren.
Das char[] kannst du dann ganz normal als payload senden, egal mit welcher Variable (V_TEXT, V_VAR, V_ID,...). Ich würde wohl V_ID nehmen. Payloadgrenze von max 26(?) Bytes am Stück beachten (mit NRF24).
Wenn du eine andere Variable nutzt als von MYSENSORS vorgeschlagen, geht halt das autocreate nicht und du musst das in FHEM entsprechen per Hand anlegen (mapreading...).
Vermutlich brauchst du vor dem Senden auch noch eine Konvertierung nach HEX, sonst ist die ID dezimal.

Beta-User

Das mit den IR-typischen Variablen sollte schon klappen. V_ID würde ich (im Moment noch nicht) nehmen, das erfordert die Prästentation eines Temp-Sensors, dass das mit autocreate klappt.

Habe mal etwas an dem Sketch rumgebastelt, habe aber keinen solchen Leser (irgendwo liegt noch ein mrf522 rum, der auf seinen Einsatz wartet...) und kann daher nicht selbst testen:

/**
* The MySensors Arduino library handles the wireless radio link and protocol
* between your home built sensors/actuators and HA controller of choice.
* The sensors forms a self healing radio network with optional repeaters. Each
* repeater and gateway builds a routing tables in EEPROM which keeps track of the
* network topology allowing messages to be routed to nodes.
*
* Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
* Copyright (C) 2013-2015 Sensnology AB
* Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
*
* Documentation: http://www.mysensors.org
* Support Forum: http://forum.mysensors.org
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 2 as published by the Free Software Foundation.
*
*******************************
*
* REVISION HISTORY
* Version 1.0 - Henrik Ekblad
*
* DESCRIPTION
* RFID Lock sensor/actuator
*
* Use RFID tag to lock/unlock a door or trigger a scene on your controller.
* This example sketch allows you to add an optional relay or solenoid
* which can be activated/opened by RFID or controller.
*
* Use the I2C wiring option for your RFID module and connect to the following Arduino pins.
*
* RFID       Arduino
* -----      -------
* GND   ->   GND
* VCC   ->   +5V
* SCL   ->   A5
* SDA   ->   A4
*
* Use normal wiring for NRF24L01 radio
*
* Attach a optional relay or solonoid lock to pin 4
* http://www.mysensors.org/build/rfid
*/

// Enable debug prints to serial monitor
#define MY_DEBUG
#define MY_DEBUG_LOCAL
#define MY_TRANSPORT_WAIT_READY_MS 1000

#define MY_NODE_ID 22
// Enable and select radio type attached
//#define MY_RADIO_NRF24
//#define MY_RADIO_RFM69
// Enable RS485 transport layer
#define MY_RS485


// Define this to enables DE-pin management on defined pin
#define MY_RS485_DE_PIN 2

// Set RS485 baud rate to use
#define MY_RS485_BAUD_RATE 38400

// Enable this if RS485 is connected to a hardware serial port
// Not possible with Arduino Nano !!
//#define MY_RS485_HWSERIAL Serial

#define RF_INIT_DELAY   125



#include <SPI.h>
#include <MySensors.h>
#include <Wire.h>
#include <PN532_I2C.h>
#include <PN532.h>





// Add your valid rfid keys here. To find you your key just run sketch; hold your new RFID tag in fron ot the reader;
// and copy the key from serial output of this sketch.
const uint8_t maxKeyLength = 7;
uint8_t validKeys[][maxKeyLength] = {
                    { 0xB3, 0xC6, 0xD9, 0x80, 0x00, 0x00, 0x00 },
                    { 0, 0, 0, 0, 0, 0, 0 },    // ADD YOUR KEYS HERE!
                    { 0, 0, 0, 0, 0, 0, 0 }};
int keyCount = sizeof validKeys / maxKeyLength;
String tagid = String();


#define CHILD_ID_LOCK  1   // Id of the sensor child
#define CHILD_ID_WRONG 2   // Id of the sensor child
#define CHILD_ID_ALARM 3   // Id of the sensor child
#define CHILD_ID_TAGID 4   // Id of the sensor child
//#define CHILD_ID 99   // Id of the sensor child

// Pin definition
const int lockPin = 4;         // (Digital 4) The pin that activates the relay/solenoid lock.
bool lockStatus;
MyMessage  lockMsg(CHILD_ID_LOCK, V_LOCK_STATUS);
MyMessage  armMsg(CHILD_ID_WRONG, V_ARMED);
MyMessage  wrongMsg(CHILD_ID_ALARM, V_TRIPPED);
MyMessage  tagMsg(CHILD_ID_TAGID,  V_IR_RECEIVE);
//xxx


PN532_I2C pn532i2c(Wire);
PN532 nfc(pn532i2c);

void setup() {

  pinMode(lockPin, OUTPUT);

  nfc.begin();
  uint32_t versiondata = nfc.getFirmwareVersion();
  if (! versiondata) {
    Serial.print("Couldn't find PN53x board");
    while (1); // halt
  }
  Serial.print(F("Found NFC chip PN5")); Serial.println((versiondata>>24) & 0xFF, HEX);
  Serial.print(F("Firmware ver. ")); Serial.print((versiondata>>16) & 0xFF, DEC);
  Serial.print('.'); Serial.println((versiondata>>8) & 0xFF, DEC);
  // Set the max number of retry attempts to read from a card
  // This prevents us from waiting forever for a card, which is
  // the default behaviour of the PN532.
  nfc.setPassiveActivationRetries(0x3);

  // configure board to read RFID tags
  nfc.SAMConfig();

  lockStatus = loadState(0);    // Read last lock status from eeprom
  setLockState(lockStatus, true); // Now set the last known state and send it to controller
}

void presentation()  {
  sendSketchInfo("RFID Lock", "0.0.2");
  present(CHILD_ID_LOCK, S_LOCK);      delay(RF_INIT_DELAY);
  present(CHILD_ID_WRONG, S_DOOR);   delay(RF_INIT_DELAY);
  present(CHILD_ID_ALARM, S_MOTION);   delay(RF_INIT_DELAY);
  present(CHILD_ID_TAGID, S_IR);     delay(RF_INIT_DELAY);
}

void loop() {
  bool success;
  uint8_t key[] = { 0, 0, 0, 0, 0, 0, 0 };  // Buffer to store the returned UID
  uint8_t currentKeyLength;                        // Length of the UID (4 or 7 bytes depending on ISO14443A card type)

  /* Wait for an ISO14443A type cards (Mifare, etc.).  When one is found
    'uid' will be populated with the UID, and uidLength will indicate
    if the uid is 4 bytes (Mifare Classic) or 7 bytes (Mifare Ultralight)*/
  success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, &key[0], &currentKeyLength);

  if (success) {
#ifdef MY_DEBUG_LOCAL
    Serial.println(key, BYTE);
    Serial.println(uid);
#endif
    char buffer[24];
    sprintf(buffer, "0x%07lX", key);
    send(tagMsg.set(buffer));
/*tagid=(""); //Start with an empty string!
    for (uint8_t i=0; i < currentKeyLength; i++)
    {
      if (i>0) tagid+=(",");
      tagid+=("0x");tagid+=(key[i]);
    }
    for (uint8_t i=currentKeyLength; i < maxKeyLength; i++)
    {
      tagid+=(",0x00");
    }

//    Serial.println("Start");
    Serial.println(tagid);
//    Serial.println("Ende");*/

    bool tripped = 1;
#ifdef MY_DEBUG_LOCAL
    Serial.println(tripped);
#endif
    send(wrongMsg.set(tripped?"1":"0"));  // Send tripped value to gw
    //send(tagMsg.set(tagid));  // Send id of the rfid-tag  to gw
    //V_IR_RECEIVE  33  This message contains a received IR-command S_IR
    //MyMessage  lockMsg(CHILD_ID_LOCK, V_LOCK_STATUS);
    //MyMessage  armMsg(CHILD_ID_WRONG, V_ARMED);
    //MyMessage  wrongMsg(CHILD_ID_ALARM, V_TRIPPED);
    //MyMessage  tagMsg(CHILD_TAGID, V_TRIPPED);

#ifdef MY_DEBUG_LOCAL
    Serial.println("");
#endif

    bool valid = false;
    // Compare this key to the valid once registered here in sketch
    for (int i=0;i<keyCount && !valid;i++) {
      for (int j=0;j<currentKeyLength && !valid;j++) {
        if (key[j] != validKeys[i][j]) {
          break;
        }
        if (j==currentKeyLength-1) {
          valid = true;
        }
      }
    }
    if (valid) {
      // Switch lock status
      setLockState(!lockStatus, true);       
    }

    // Wait for card/tag to leave reader   
    while(nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, &key[0], &currentKeyLength));
  }
}

// Unlocks the door.
void setLockState(bool state, bool doSend){
#ifdef MY_DEBUG_LOCAL 
  if (state)
    Serial.println("open lock");
  else
    Serial.println("close lock");
#endif
  if (doSend)
    send(lockMsg.set(state));
    digitalWrite(lockPin, state);
    saveState(0,state);
    lockStatus = state;
}

void receive(const MyMessage &message) {
  // We only expect one type of message from controller. But we better check anyway.
  if (message.type==V_LOCK_STATUS) {
     // Change relay state
     setLockState(message.getBool(), false);

     // Write some debug info
     Serial.print("Incoming lock status:");
     Serial.println(message.getBool());
   }
}


Da jedenfalls ich den Leser mittelfristig mit RS485@HW-serial betreiben wollte, habe ich mal angefangen, sämtliche seriellen Ausgaben abschaltbar zu machen ;) . Übrigens schlucken so lange Texte viel Speicher, ergo ist das Motto: solange aussagefähig, ist kürzer besser, und am besten aus dem EEPROM geladen (F("))...

Als Merkposten noch:
- https://forum.mysensors.org/topic/2439/rfid-garage-door-opener (Schön strukturiert, leider noch V1.5, mit der Option, zulässige IDs zu programmieren)
- https://forum.mysensors.org/topic/2223/rfid-2-person-readout (auch V1.5, interessant: die dump_byte_array-Funktion)

Gruß, Beta-User
Server: HP-elitedesk@Debian 12, aktuelles FHEM@ConfigDB | CUL_HM (VCCU) | MQTT2: MiLight@ESP-GW, BT@OpenMQTTGw | MySensors: seriell, v.a. 2.3.1@RS485 | ZWave | ZigBee@deCONZ | SIGNALduino | MapleCUN | RHASSPY
svn: u.a MySensors, Weekday-&RandomTimer, Twilight,  div. attrTemplate-files

Ranseyer

Danke an alle für den Input.

Ich hab nochmals ganz von vorne angefangen beim Original Muster von MySensors.
Und schrittweise die Sachen übernommen.

Der Stand Ausgabe auf der Debug-Konsole:
ZitatFound tag id: 0x65,0x61,0xA9,0x6B,0x00,0x00,0x00
Start
0x101,0x97,0x169,0x107,0x00,0x00,0x00
Ende
19716 TSF:MSG:SEND,22-22-0-0,s=4,c=1,t=33,pt=0,l=10,sg=0,ft=0,st=OK:0x783008EA
1
19728 TSF:MSG:SEND,22-22-0-0,s=3,c=1,t=16,pt=0,l=1,sg=0,ft=0,st=OK:1


Der EventMonitor:
Zitat2018-03-03 10:00:31 MYSENSORS_DEVICE MYSENSOR_22 ir_receive4: 0x783008EA
2018-03-03 10:00:31 MYSENSORS_DEVICE MYSENSOR_22 tripped3: on


Der Code: https://github.com/ranseyer/home-automatics/blob/master/MySens-RS485-Lock-201802/MySens-RS485-Lock-201802.ino

Was mir unklar ist:

Was genau tut das (ja das ist bestimmte eine dumme Frage!)
    char buffer[24];
    sprintf(buffer, "0x%07lX", key);
    send(tagMsg.set(buffer));

Problem dabei: "key enthält nicht den ganzen Key.


Dieser wäre dezimal in tagid enthalten (wobei mir dezimal auch nicht gefällt):
tagid=(""); //Start with an empty string!
    for (uint8_t i=0; i < currentKeyLength; i++)
    {
      if (i>0) tagid+=(",");
      tagid+=("0x");tagid+=(key[i]);
    }
    for (uint8_t i=currentKeyLength; i < maxKeyLength; i++)
    {
      tagid+=(",0x00");
    }

    Serial.println("Start");
    Serial.println(tagid);
    Serial.println("Ende");

    char buffer[24];
    sprintf(buffer, "0x%07lX", key);
    send(tagMsg.set(buffer));
FHEM mit FTUI. Homematic-Funk für Thermostate und Licht. MySensors als Basis für eigene HW.
Zentrale ist der MAPLE-CUL mit RFM69+HModUART-AddOn.
Doku zu meinen Projekten: Github/Ranseyer. Platinen falls verfügbar gerne auf Anfrage.
Support: gerne wenn ich Zeit+Lust habe im Forum. Nicht per PN!

Beta-User

Zu was tut das:
Es wird erst eine Variable des Typs char definiert mit max. 24 Zeichen Länge. In diese werden dann aus der Variable "key" die ersten 7 Bytes als großgeschriebener HEX-Wert geschoben, wobei der String mit "0x" anfängt.

Eigentlich sollte in key aber wirklich der ganze Key enthalten sein, auch die Ausgangsfunktion aus dem MySensors-Sketch bedient sich ja daraus.

Wirf da mal das 0x raus und verlängere das, einfach die 7 ersetzen durch irgendwas, was weniger wie 24 ist...
Server: HP-elitedesk@Debian 12, aktuelles FHEM@ConfigDB | CUL_HM (VCCU) | MQTT2: MiLight@ESP-GW, BT@OpenMQTTGw | MySensors: seriell, v.a. 2.3.1@RS485 | ZWave | ZigBee@deCONZ | SIGNALduino | MapleCUN | RHASSPY
svn: u.a MySensors, Weekday-&RandomTimer, Twilight,  div. attrTemplate-files

Ranseyer

Cool, viel besser (Test mit zwei Tags...):

    char buffer[24];
    sprintf(buffer, "%23lX", key);
    send(tagMsg.set(buffer));



246=F6 das verstehe ich.

Events:
2018-03-03 13:28:54 MYSENSORS_DEVICE MYSENSOR_22 ir_receive4:                303108EA
2018-03-03 13:28:54 MYSENSORS_DEVICE MYSENSOR_22 tripped3: on
2018-03-03 13:28:57 MYSENSORS_DEVICE MYSENSOR_22 ir_receive4:                393508EA
2018-03-03 13:28:57 MYSENSORS_DEVICE MYSENSOR_22 tripped3: on


Debug:

Found tag id: 0x65,0x61,0xA9,0x6B,0x00,0x00,0x00
Start
0x101,0x97,0x169,0x107,0x00,0x00,0x00
Ende
24931 TSF:MSG:SEND,22-22-0-0,s=4,c=1,t=33,pt=0,l=23,sg=0,ft=0,st=OK:               303108EA
1
24944 TSF:MSG:SEND,22-22-0-0,s=3,c=1,t=16,pt=0,l=1,sg=0,ft=0,st=OK:1

Found tag id: 0xF6,0xEA,0x25,0x3B,0x00,0x00,0x00
Start
0x246,0x234,0x37,0x59,0x00,0x00,0x00
Ende
27575 TSF:MSG:SEND,22-22-0-0,s=4,c=1,t=33,pt=0,l=23,sg=0,ft=0,st=OK:               393508EA
1




(Ich werde die Zahlen später mal etwas editieren...)
FHEM mit FTUI. Homematic-Funk für Thermostate und Licht. MySensors als Basis für eigene HW.
Zentrale ist der MAPLE-CUL mit RFM69+HModUART-AddOn.
Doku zu meinen Projekten: Github/Ranseyer. Platinen falls verfügbar gerne auf Anfrage.
Support: gerne wenn ich Zeit+Lust habe im Forum. Nicht per PN!

Beta-User

Cool!

Irritierend ist nur, dass die beiden Ausgaben nicht im wesentlichen identisch ist, aber vermutlich habe ich da eine falsche Vorstellung von der HEX-Umrechnerei.

Jetzt müßte man also entscheiden, ob man lieber bei der "alten" Methode bleibt (in Hex-Schreibweise, damit es immer 2 Stellen sind), oder ob wir das buffer-char-array verwenden wollen. Ich würde doch eher zu letzterem tendieren, zumal ich nicht weiß, ob das Ergebnis der "neuen" Methode ebenso eindeutig ist wie das alte, sonst könnte man das komplett umstellen.

Aber wenn wir in der alten Vorgehensweise die Kommas und 0x-e drin lassen, wird das zu lang zum Versenden, dafür kann man den Vergleichscode lassen, wie er ist. Ich würde also jetzt das rausnehmen (aus den "=+"-Anweisungen).

Andere Meinungen?
Server: HP-elitedesk@Debian 12, aktuelles FHEM@ConfigDB | CUL_HM (VCCU) | MQTT2: MiLight@ESP-GW, BT@OpenMQTTGw | MySensors: seriell, v.a. 2.3.1@RS485 | ZWave | ZigBee@deCONZ | SIGNALduino | MapleCUN | RHASSPY
svn: u.a MySensors, Weekday-&RandomTimer, Twilight,  div. attrTemplate-files

Ranseyer

Denke die HEX Schreibweise macht am meisten Sinn. (Aber im Prinzip geht es ja nur darum einen eindeutigen Wert zu haben. Egal welchen.)
FHEM mit FTUI. Homematic-Funk für Thermostate und Licht. MySensors als Basis für eigene HW.
Zentrale ist der MAPLE-CUL mit RFM69+HModUART-AddOn.
Doku zu meinen Projekten: Github/Ranseyer. Platinen falls verfügbar gerne auf Anfrage.
Support: gerne wenn ich Zeit+Lust habe im Forum. Nicht per PN!

Beta-User

Schon, die Frage ist nur, welche:
Found tag id: 0x65,0x61,0xA9,0x6B,0x00,0x00,0x00
oder
303108EA
Tendiere eingentlich dazu, dass 6561A96B000000 die beste Variante wäre... Sind 14 Zeichen, was im Rahmen des übertagbaren liegt und man kann den Vergleich lassen, wie er ist ;) .
Server: HP-elitedesk@Debian 12, aktuelles FHEM@ConfigDB | CUL_HM (VCCU) | MQTT2: MiLight@ESP-GW, BT@OpenMQTTGw | MySensors: seriell, v.a. 2.3.1@RS485 | ZWave | ZigBee@deCONZ | SIGNALduino | MapleCUN | RHASSPY
svn: u.a MySensors, Weekday-&RandomTimer, Twilight,  div. attrTemplate-files

Ranseyer

Denke das ist auch die TagID: 0x65,0x61,0xA9,0x6B,0x00,0x00,0x00.

Denke ich würde es so machen:
  if (success) {
    Serial.print("Found tag id: ");
    for (uint8_t i=0; i < currentKeyLength; i++)
    {
      if (i>0) Serial.print(key[i], HEX);
    }
    for (uint8_t i=currentKeyLength; i < maxKeyLength; i++)
    {
      Serial.print("00");
    }
    Serial.println("");


das ergibt dann
ZitatFound tag id: EA253B000000
Start
0x246,0x234,0x37,0x59,0x00,0x00,0x00
Ende
4940 TSF:MSG:SEND,22-22-0-0,s=4,c=1,t=33,pt=0,l=23,sg=0,ft=0,st=OK:               393508EA
FHEM mit FTUI. Homematic-Funk für Thermostate und Licht. MySensors als Basis für eigene HW.
Zentrale ist der MAPLE-CUL mit RFM69+HModUART-AddOn.
Doku zu meinen Projekten: Github/Ranseyer. Platinen falls verfügbar gerne auf Anfrage.
Support: gerne wenn ich Zeit+Lust habe im Forum. Nicht per PN!

Beta-User

Bei Serial.print darf gerne eine "korrekte" Darstellung rauskommen, das sollte dann ja auch in die Liste der gültigen ID's so aufgenommen werden.

Aber der Sende-String/char* muß kürzer sein.

Hmmm. Kannst du mal etwas mit dem spritf rumspielen und das Ergebnis auch an Serial ausgeben? Insbesondere: Was ergibt:
sprintf(buffer, "%X", key);?

Server: HP-elitedesk@Debian 12, aktuelles FHEM@ConfigDB | CUL_HM (VCCU) | MQTT2: MiLight@ESP-GW, BT@OpenMQTTGw | MySensors: seriell, v.a. 2.3.1@RS485 | ZWave | ZigBee@deCONZ | SIGNALduino | MapleCUN | RHASSPY
svn: u.a MySensors, Weekday-&RandomTimer, Twilight,  div. attrTemplate-files