Page 1 sur 1

Envoyer à domoticz la valeur d'une variable via send [RESOLU]

Publié : 15 mars 2020, 17:15
par titi62
Bonjour,
Je n'arrive pas à comprendre pourquoi mon code n'arrive pas à envoyer la valeur d'une variable à Domoticz.
Pouvez vous m'aidez

Principaux code :

MyMessage msgUSER(CHILD_ID_TAG, V_TEXT);

Serial.println(hex_value);
send(msgUSER.set(hex_value));


La variable hex_value est par exemple 2151442272
2151442272 est bien affiché dans la console série
Mais 1 est envoyé sur mon afficheur virtuel sur domoticz
Si je modifie send(msgUSER.set(hex_value)); par send(msgUSER.set("2151442272")); cela fonctionne !!

Ou est mon erreur ?

Le code entier :

Code : Tout sélectionner

// Enable debug prints to serial monitor
 #define MY_DEBUG

// Enable and select radio type attached
//#define MY_RADIO_RF24
//#define MY_RADIO_NRF5_ESB
//#define MY_RADIO_RFM69
//#define MY_RADIO_RFM95

// Enable gateway ethernet module type
#define MY_GATEWAY_W5100

// W5100 Ethernet module SPI enable (optional if using a shield/module that manages SPI_EN signal)
//#define MY_W5100_SPI_EN 4

// Enable Soft SPI for NRF radio (note different radio wiring is required)
// The W5100 ethernet module seems to have a hard time co-operate with
// radio on the same spi bus.
#if !defined(MY_W5100_SPI_EN) && !defined(ARDUINO_ARCH_SAMD)
#define MY_SOFTSPI
#define MY_SOFT_SPI_SCK_PIN 14
#define MY_SOFT_SPI_MISO_PIN 16
#define MY_SOFT_SPI_MOSI_PIN 15
#endif

// When W5100 is connected we have to move CE/CSN pins for NRF radio
#ifndef MY_RF24_CE_PIN
#define MY_RF24_CE_PIN 5
#endif
#ifndef MY_RF24_CS_PIN
#define MY_RF24_CS_PIN 6
#endif

// Enable UDP communication
//#define MY_USE_UDP  // If using UDP you need to set MY_CONTROLLER_IP_ADDRESS below

// Enable MY_IP_ADDRESS here if you want a static ip address (no DHCP)
#define MY_IP_ADDRESS 192,168,3,200

// If using static ip you can define Gateway and Subnet address as well
#define MY_IP_GATEWAY_ADDRESS 192,168,3,1
#define MY_IP_SUBNET_ADDRESS 255,255,255,0

// Renewal period if using DHCP
//#define MY_IP_RENEWAL_INTERVAL 60000

// The port to keep open on node server mode / or port to contact in client mode
#define MY_PORT 5003

// Controller ip address. Enables client mode (default is "server" mode).
// Also enable this if MY_USE_UDP is used and you want sensor data sent somewhere.
//#define MY_CONTROLLER_IP_ADDRESS 192, 168, 3, 103

// The MAC address can be anything you want but should be unique on your network.
// Newer boards have a MAC address printed on the underside of the PCB, which you can (optionally) use.
// Note that most of the Arduino examples use  "DEAD BEEF FEED" for the MAC address.
#define MY_MAC_ADDRESS 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED

// Enable inclusion mode
#define MY_INCLUSION_MODE_FEATURE
// Enable Inclusion mode button on gateway
//#define MY_INCLUSION_BUTTON_FEATURE
// Set inclusion mode duration (in seconds)
#define MY_INCLUSION_MODE_DURATION 60
// Digital pin used for inclusion mode button
//#define MY_INCLUSION_MODE_BUTTON_PIN  3

// Set blinking period
#define MY_DEFAULT_LED_BLINK_PERIOD 300

// Flash leds on rx/tx/err
// Uncomment to override default HW configurations
//#define MY_DEFAULT_ERR_LED_PIN 7  // Error led pin
//#define MY_DEFAULT_RX_LED_PIN  8  // Receive led pin
//#define MY_DEFAULT_TX_LED_PIN  9  // Transmit led pin

#if defined(MY_USE_UDP)
#include <EthernetUdp.h>
#endif
#include <Ethernet.h>
//#define MY_RF24_CE_PIN 49
//#define MY_RF24_CS_PIN 53
#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 },
                    { 0x73,0x90,0xE3,0x2,0x00,0x00,0x00 },    // 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
#define CHILD_ID_TAG 99 // Id du numero de tag

// 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);
MyMessage msgUSER(CHILD_ID_TAG, V_TEXT);
PN532_I2C pn532i2c(Wire);
PN532 nfc(pn532i2c);

void setup() {

  pinMode(lockPin, OUTPUT);
  nfc.begin();
  uint32_t versiondata = nfc.getFirmwareVersion();

  if (! versiondata) {
    Serial.print("Bug de la carte PN53x, reset dans 2s"); //SI BUG CARTE RFID RESET
 wait(2000); //SI BUG CARTE RFID RESET
  asm volatile ("  jmp 0"); // RESET
    
    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", "1.0");
 present(CHILD_ID_TAG, S_INFO);
 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"); 
    }





////  RAJOUT

    String hex_value = "";
    for (uint8_t i=0; i < currentKeyLength; i++) 

    {

     // Serial.print(" 0x");Serial.print(key[i], HEX);       
      

      hex_value += (String)key[i];

    }


    Serial.println(", value="+hex_value);
 

  Serial.println(hex_value);  //  TEST  TEST
    
send(msgUSER.set(hex_value));  // envois vers domoticz valeur du tag  NE FONCTIONNE PAS!!!!!!!!!!!!!!!!!!!!!!!!

    ////FIn RAJOUT


    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);       
    }

        if (!valid) {
      // CARTE NON VALIDE
      Serial.println("Non enregistré");
          digitalWrite(lockPin, true);
    wait(100);
        digitalWrite(lockPin, false);
    wait(100);
        digitalWrite(lockPin, true);
    wait(100);
        digitalWrite(lockPin, false);
    wait(100);
        digitalWrite(lockPin, true);
    wait(100);
        digitalWrite(lockPin, false);
    wait(100);
        digitalWrite(lockPin, true);
    wait(100);
        digitalWrite(lockPin, false);
    wait(100);      
    }


    // 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("On Bip");
  if (doSend)
    send(lockMsg.set(true));
    digitalWrite(lockPin, true);
    wait(1000);
    Serial.println("Off Bip");
  if (doSend)
    send(lockMsg.set(false));
    digitalWrite(lockPin, false);
  } else {
  if (doSend)
    send(lockMsg.set(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());
 //  } 
}
Mon but est via un capteur NFC d'envoyer la valeur d"un badge à Domoticz pour gerer un contrôle d'accé.
merci d'avance, je ne trouve pas mon erreur!

Re: Envoyer à domoticz la valeur d'une variable via send

Publié : 15 mars 2020, 18:30
par boum

Code : Tout sélectionner

send(msgUSER.set(hex_value.c_str()));  // envoie vers domoticz valeur du tag
C'est le problème d'utiliser String, dans ce cas, la chaîne est convertie en booléen. If faut utiliser c_str pour utiliser la bonne version de MyMessage::set().

Re: Envoyer à domoticz la valeur d'une variable via send

Publié : 15 mars 2020, 18:50
par titi62
boum a écrit : 15 mars 2020, 18:30

Code : Tout sélectionner

send(msgUSER.set(hex_value.c_str()));  // envoie vers domoticz valeur du tag
C'est le problème d'utiliser String, dans ce cas, la chaîne est convertie en booléen. If faut utiliser c_str pour utiliser la bonne version de MyMessage::set().
Merci énormément !!!