I need help with 'rolling code' UART sensor

I have a RFID reader for cats, it’s UART and I know it’s wired up correctly, however the only example of a custom UART is text reader, my needs are different.

I’m using an ESP32.

The RFID reader sends 13 codes with values from 0 to 255, no LF, no CR. The feed isn’t constant, only when the tag near.

The first number is always 170 and the last is always 187.

I would like to be able to read these numbers and append them as one long number and export as a text_sensor.

That would involve:

  • Starting at 170 (although it should always start with 170)
  • Ending at 187 (or just a counter to 13)
  • Converting int to char
  • Adding the digits to the end of a char
  • Sending that back as a text sensor

I won’t lie, I’ve been trying to get this to work all weekend, I’ve got it to report the numbers one at a time as a sensor(not text). I just can’t get my head around it, I can do it in arduino, but not this C++ stuff.

Any assistant would be gratefully received.

1 Like

Maybe showing some sample messages will help explain.

Have you tinkered with different settings for the debugger?

Or potentially something like the approaches here?

Also, I suspect you may get more bites if you make your topic title more specific.

The information is getting through, I just need to change it.

I don’t understand what the second link is about to be honest, mask?

Thanks for the reply however :slight_smile:

Sorted, this does the job, only took me about 5 days! :slight_smile:

#include "esphome.h"

class BondcoTagReader : public Component, public UARTDevice, public TextSensor {
 public:
  BondcoTagReader(UARTComponent *parent) : UARTDevice(parent) {}

  void setup() override {
    // nothing to do here
  }

  int readtag(int dave, char *buffer) {
    static int len = 0;                 // create static int for buffer length
    if (dave == 170) {                  // if start of tag (170)
      len = 0;                          // reset buffer count
    }
    char chartemp[3];                   // create a temporary char array
  	sprintf(chartemp, "%d", dave);      // convert int to char
    if(dave < 10) {                     // if only 1 char
        chartemp[2] = chartemp[0];      // copy char2 to char0
        chartemp[1] = *"0";             // fill char1 with 0
        chartemp[0] = *"0";             // fill char0 with 0
    } else if (dave < 100){             // if only 2 chars
        chartemp[2] = chartemp[1];      // copy char2 to char1
        chartemp[1] = chartemp[0];      // copy char1 to char0
        chartemp[0] = *"0";             // fill char1 with 0
    }
    for (int j = 0; j < 3; j++) {       // copy from temporary chars to buffer array
        buffer[len+j] = chartemp[j];
    }
    len = len + 3;                      // add number of chars to buffer count
    if (dave == 187) {                  // if end of tag (187)
      if (len > 37)                     // if length of len > 37
      {                                 // all is good
        len = 0;                        // reset buffer count
        return 1;                       // return 1 to if statement below
      } else {                          // else
        return -1;                      // return -1 (failed)
      }
    }
    return -1;                          // return -1 (failed) to if statement below
  }

  void loop() override {
    static char buffer[40];
    while (available()) {
      if(readtag(read(), buffer) > 0) {
        publish_state(buffer);
        buffer[0] = '\0';
      }
    }
  }
};
1 Like