RDM630 125KHz RFID reading with the Arduino Mega 2560 R3

This howto is for the RDM630 125Khz RFID module – UART board. Seeedstudio and Spec Sheet.

There are a few chunks of code on the internet that will get the RDM630 up and running on an Uno, but those don’t seem to work on the Mega. There are also a few examples of code that sort-of works, but not reliably, and does not check the checksum etc. I stated with these and then kept on hacking until I got it to work 100%.

RDM630ArduinoMega

So, firstly, a few things that you should know before we start:

  • Many of the code examples work fine with an Uno (using Software Serial) but I’m starting to learn that the Mega doesn’t like Software Serial. I’ve found a few instances of people saying (anecdotally) that Software Serial is “not supported” on the Mega, and even though it works, it’s buggy, and there are patches blah blah. Luckily we have 4 hardware serial ports, so lets use those.
  • You only need to use +5V, GND and one pin (TX) to connect the board to your Mega.
  • You will notice the delay(20); in my code. That gives the board time to bring up the serial connection before trying to read data for it. Without that you’ll get garbage 90% of the time.
  • There are various methods for reading. I’m using a hybrid of various approaches from the internets that uses Serial1.available() as a signal that a tag has been swiped and explicitly reads 14 bytes.
  • I am also explicitly closing and restarting the the Serial1 connection after reading a tag. I do this because the code was working until I left a tag in range for longer than about 5 seconds, at which point Serial1 would get confused and the counter would overrun. This approach does slow things down, but since you can still scan about 4 tags a second (way more than you’ll need to in real life) you’ll be fine.
  • This approach uses pointers, buffers and some confusing snprintf and sscanf functions to extract and convert the 14 bytes from the tag into the various bits and pieces (RFID Tags have checksums, and the unique number itself is stored in HEX). That stuff is hard to grok, but luckily you can chose to either make an effort to understand it or just use the code as-is.

Right. Wiring.

Look at the spec sheet. On pin-set one, pins 4 and 5 go to GND and +5V respectively, with  pin 1 going to Pin 19 on your Arduino Mega (RX for Serial1). That’s all.

Now the code:

uint8_t buffer[14];
uint8_t* buffer_at;
uint8_t* buffer_end = buffer + sizeof(buffer);

String checksum;
boolean tagfound = false;

void setup()
{
    Serial.begin(9600);
    Serial.println("Serial Ready");

    Serial1.begin(9600);
    Serial.println("RFID Ready");
}

void loop()
{
    if (Serial1.available()){
        delay(20);
        buffer_at = buffer;

        while ( buffer_at < buffer_end )
        {
            *buffer_at++ = Serial1.read();
        }
        tagfound = true;
        Serial1.end();
        Serial1.begin(9600);
    }

    if (tagfound){
        buffer_at = buffer;
        uint32_t result = 0;

        // Skip the preamble
        ++buffer_at;
        // Accumulate the checksum, starting with the first value
        uint8_t checksum = rfid_get_next();
        // We are looking for 4 more values
        int i = 4;
        while(i--)
        {
            // Grab the next value
            uint8_t value = rfid_get_next();
            // Add it into the result
            result <<= 8;
            result |= value;
            // Xor it into the checksum
            checksum ^= value;
        }
        // Pull out the checksum from the data
        uint8_t data_checksum = rfid_get_next();

        // Print the result
        Serial.print("Tag: ");
        Serial.print(result);
        if ( checksum == data_checksum )
            Serial.println(" OK");
        else
            Serial.println(" CHECKSUM FAILED");
        // We're done processing, so there is no current value

        tagfound = false;
    }

}

uint8_t rfid_get_next(void)
{
    // sscanf needs a 2-byte space to put the result but we
    // only need one byte.
    uint16_t hexresult;
    // Working space to assemble each byte
    static char byte_chars[3];
    // Pull out one byte from this position in the stream
    snprintf(byte_chars,3,"%c%c",buffer_at[0],buffer_at[1]);
    sscanf(byte_chars,"%x",&hexresult);
    buffer_at += 2;
    return static_cast<uint8_t>(hexresult);
}

Now connect it up, open your Serial Monitor and swipe a tag. You should see the tags being read, with their decimal value (often the number that’s printed on them) printed out.

My three tags look like so:

Serial Ready
RFID Ready
Reading: 695592 OK
Reading: 721129 OK
Reading: 1430936 OK

Credits:
I stole a large chunk of code from ManicBug’s blog post, and got a lot of help from the great people in #arduino on Freenode.  Thanks!

Seeed Studio GPRS Shield 1.4 and Arduino Mega 2560

Recently I battled to get the Seeed Studio GPRS Shield 1.4 and Arduino Mega 2560 to talk to each other. I eventually discovered that they are not actually compatible when stacked (without some ugly pin jumping, and even then, not really compatible because the Software Serial can’t seem to really handle 19200 baud rates).

So here’s how to treat it as a breakout board. (my notes in blue):Image

This only requires 5 connections. The blue labels represent the Arduino pins to connect to.

  1. 5v and GND – These should be pretty obvious.
  2. Pin 9 – This pin is used to turn the SIM900 on and off.
  3. Pin 18 and 19 – These are the HW serial pins

Finally note the position of the jumpers (HW Serial) and the position of the Power Switch (Internal).

Which, when connected up should look something like this:Image

Then finally you can load up the following sketch to get serial access to the GPRS modem via the Arduino’s serial monitor.

#include <SoftwareSerial.h> 
#define terminator 10 // DEC value for a LF(line feed) to skip while loop

/*
SETUP
 
 See https://arbitraryuser.com/2013/04/07/seeed-studio-gprs-shield-and-arduino-mega-2560/
 
 This code is partially "borrowed" from idS2001 at http://www.seeedstudio.com/forum/viewtopic.php?p=12939#p12939
 */

String IncDataSerial = "";

void setup()
{
  delay(1000);
  Serial.begin(19200);
  Serial1.begin(19200);

  // Automatically power up the SIM900.
  pinMode(9, OUTPUT);
  digitalWrite(9,LOW);
  delay(1000);
  digitalWrite(9,HIGH);
  delay(2500);
  digitalWrite(9,LOW);
  delay(3500);
  // End of SIM900 power up.
}

void loop()
{
  if (Serial1.available()>0)  // if date is comming from softwareserial port ==> data is comming from gprs shield
  {
    boolean getLF = false;
    while(Serial1.available()>0 && !getLF)  // reading data into string if activity is on port and getLF is false ==> no LF have been send
    {
      char buffer=Serial1.read();  // writing data into char
      IncDataSerial += buffer;
      if (buffer == terminator) {
        getLF = true;
      }
    }

    Serial.print(IncDataSerial);  // send string ( char array ) to hardware serial
    Serial.print("\r");   // send a CR because it is missing
    IncDataSerial = "";
  }
  if (Serial.available()>0) // if data is available on hardwareserial port ==> data is comming from PC or notebook
    Serial1.write(Serial.read());  // write it to the GPRS shield
}

In Serial Monitor (or similar app) you will then see it start to talk to you after the initial ~5 second startup delay.

RDY

+CFUN: 1

+CPIN: READY

Call Ready

AT
OK

AT+CSQ
+CSQ:18,0
OK

20 Pieces of Startup Advice I Should Have Posted A Long Time Ago

Disclaimer: I am not a successful tech entrepreneur, so you probably shouldn’t read this.

  1. Build what people need and build it in the quickest and easiest (read hackiest) way possible that is barely acceptable to them. There are a lot of very bad implementations out there making millions right now.
  2. Do not build what you want (or what you think people need/want). As a tech entrepreneur you are an anomaly. Most people don’t care about the things you care about. eg. “I want a way to sync my scrobbles to my own server in case LastFM gets taken down by the FBI.” – Only 5 people care about this.
  3. Build products around use cases, NOT use cases around products.
    If you can’t explain what your product does in 20 seconds then you don’t have a product, you have a big idea. Unless you have unlimited resources and funding you’re going to need to tame your idea. Find a specific implementation of your big idea in action that resonates with the masses and run with that. If building that specific instance of your idea doesn’t sound sexy enough, think about how sexy it is going to be when you go back to your old boss and ask for a job. Once you’re making a profit off your not-so-sexy idea you can start self-funding your big idea.
  4. Big ideas don’t get funding. Google was not a big idea, it was a vastly better search engine in a market flooded with search engines. It was a product. Angels and VCs need to be able to understand your idea and then be able to communicate your idea to other people who will also understand it and immediately see how it will make money.
  5. Don’t let VCs lead you down the garden path and never commit. They’re doing you a disservice. If they truly like your idea and believe in you they can do their due diligence in 2 weeks and have (some) money in your bank account in a month. Too often it seems that VCs who don’t really “get” an idea are too scared to tell the founders to go away, just in case their idea starts to make sense. (No one wants to be the record exec who told the Beatles to go away). But this can give youa false  impression of how good your idea is, because if a VC seems interested, then surely your idea is a good one, right?
  6. Don’t make friends with VCs. Friends don’t want to tell friends that they “don’t really get it“, or more specifically that they “get it, but don’t see how you will be able to sell enough of it“. This kind of feedback can too easily come off as a personal insult for anyone to ever say it… so they’ll lead you on in the hope that one day you’ll say something to convince them because they really want you to do well.
  7. Don’t get too personal or precious about your idea. You are a smart, attractive person with great hair and a wonderful personality, you don’t need your product to validate your worth. Getting too personal about your product leaves you unable to change anything because it’s like gazing into the eyes of your beautiful new born baby and wishing they had been born with with nicer ears. You need to be ready to dump that baby in the dumpster at a moments notice.
  8. It’s all about cash, sales and runways. Building the product is the easy bit. Any nerd with a laptop can build a product. Selling it is HARD. You need to realise up front that your “tech startup” is 90% on-the-street-corner-sales. If you think you’re immune to this you’re a fool. If you aren’t earning 50% of what you need to break even after 50% of your runway you are in trouble.
  9. If your runway is 100 meters long, you need to be selling your product at 25m. The next 75m is refine, sell, refine, sell, repeat.
  10. Make sure know how long your runway is from day 1. Count down in days, have it up on the wall in big print.
  11. If you can’t build a product that people would pay for in 25m, make it simpler. If you don’t think you can sell this new simplified product then charge less for it or try and find some more runway… But figure this all out before you start.
  12. If you think you have a longer runway because you will obviously get more funding, don’t quit your day job. Negotiate all your funding before you quit your job. You might need to develop an MVP to get this funding. Do that at night or on weekends.
  13. Selling isn’t sexy but don’t avoid it. Rather get your hands dirty from day one so that you get used to the smell. (You’ll also get better at not stinking up the room every time you try)
  14. You need to realise that there is a difference between what people are impressed by and what they will pay for. If you’re removing some significant pain or frustration from their life, they might not be impressed but they will pay for it. People pay for lots of very unsexy things all the time.
  15. Design and field-test products until something resonates. Mock something up in photoshop and then go and see if you can sell it. You need to get to the point where someone is willing to give you cash out of their wallet in order to go home and use your thing.
  16. Sell to people you don’t know and who don’t know you. If you’re going to be successful then 99.9999% of your market is going to be people who have never met you, so why would test your sales on people who know you? Firstly they’re biased (they want to help you and may even give you their hard earned money out of guilt/pity/just-to-be-nice) and secondly, you have insider knowledge– you know who to sell to and which of your friends to not even bother with. That’s not reality.
  17. If there is more than one of you in the startup, don’t assume roles like “sales guy” and “coder”. Send the coder out to sell (especially when you’re still faking it)… He/She might just surprise you, and, at the very least they’ll learn more about how the product fits in the real world.
  18. Get a simple office (or even a room in the back of someone else’s office). Be there every day from 9 until 5 (or 10 till 6, or 11 till 7 etc) from day 1. Stick things on the walls, decorate your corner… get a crappy coffee machine. There is something about a humble office that will bring out the best in you. Working from someone’s home, even if you all work together just doesn’t have the same effect.
  19. Don’t believe everything you read on the internet.
  20. Read The Personal MBA before you start. Even if you have an MBA.

How to cook steak

Here are 20 simple steps to cook a great steak.

  1. Buy good steak. It doesn’t have to be expensive, but it must be good. You want to select a piece of meat that has as much marbling as possible. Marbling is formed by lines of fat inside the meat. This fat dissolves during the cooking process and makes it awesome. Aged steaks are great. You should read up about dry aging if you’re interested.
  2. Don’t freeze it. Unless you have a blast freezer you’re going to be damaging the steak when you freeze it. As the cells freeze the ice inside them expands and eventually breaks the cells open. When you cook the steak all that moisture disappears.
  3. If you must freeze steak then make sure you defrost it gently and completely. Ice crystals inside the steak will evaporate and you’ll be boiling your steak from the inside out. I’d advise at least 5 hours outside the fridge to completely defrost a steak.
  4. Get the steak out of the fridge an hour before you want to cook it. Take it out of whatever packaging it is in, dab it dry and then leave it on a plate exposed to the air.
  5. Turn on your hotplate and start getting your pan or griddle pan hot. A griddle pan is ideal for steak because it allows the moisture to evaporate with steaming the meat. You want it to be as hot as humanly possible. If you can’t smell hot metal then it isn’t hot enough. Even on gas I leave the griddle pan on the flame for at least 10 minutes.
  6. Once you’ve let it temper (reach room temperature), dry the steak with a kitchen towel again. This removes any moisture from the outside of the steak. 
  7. Oil your steak. This will require you to get your hands oily. Massage the oil into the meat. While olive oil is fine, it does have a lower smoke temperature than sunflower oil, so sunflower oil is usually best.
  8. While your hands are oily, sprinkle some salt and pepper on your steak. You want to do this at the very last minute otherwise the salt will start to leach the moisture out of the steak.
  9. Put your steaks in the pan, but don’t crowd them. If you crowd the pan you’ll notice an excess of liquid will build up around your steaks and you will now be boiling your steak. Not ideal.
  10. (At this point a small word of warning. If you’ve done this correctly you’ll now start to see a large amount of “smoke” come off the steak and start filling the kitchen with a hazy layer of “mist”. Open some windows  to evacuate the smoke. Similarly you’ll have covered your stove and surrounding surfaces with a splattering of tiny oil bubbles. Feel free to cover everything with newspaper to minimise cleanup)
  11. Leave your steaks exactly as you placed them in the pan. Do not touch them, do not flip them. Do not talk about “sealing in the juices” because that’s been proven rubbish.
  12. Watch the side of the steak. You’ll start to see the line of “cooked” steak start rising from the bottom.  Wait until the line of “cooked” is half way up the side of the steak. Depending on how hot your pan is and how thick your meat is, this could take up to 5 minutes. Probably closer to 2 and a half minutes.
  13. Once the “cooked” line reaches the middle, flip the steak. If everything is going according to plan your steak should not be stuck and you should notice that the cooked side of the steak is nicely browned (we’ll brown it some more later). The steak is magically not-stuck because the Maillard reaction has caramelized the sugars, leaving the it perfectly brown but also not stuck to the pan.
  14. Watch the new “cooked” line rise until the two meet in the middle. This will take slightly less time than the first side did.
  15. Remove your steak and cover it with your favourite basting sauce. This can be anything from a home-made concoction of tomato sauce and chutney to a store bought steak sauce. “Lappies” sauce is my basting of choice, look for it in the Spar (the ones in the Northern Suburbs tend to be better at stocking it). I usually have my basting sauce ready on a plate and then use a spoon to make sure I cover it completely.
  16. Lift your steak out of the sauce, let any excess sauce drip off, and place it back in the hot pan. You are now cooking your meat to get the final temperature (rare, medium rare etc) and colour correct. If you leave it in the pan too long the sauce will burn, which is why we added the sauce at the end.
  17. Getting the “temperature” right is the hardest part of cooking a steak and the best advice I can give you now is to poke the meat with your finger to feel it and possibly use a meat thermometer. If your steaks are thick enough a meat thermometer pushed into the middle of the steak should hit between 54 to 56 degrees Celsius for medium rare. Medium rare feels deceptively soft. I usually cook my steaks for a minute each side after basting.
  18. It’s always better to under-do your steaks than overdo them. You can always put them back in the pan.
  19. Once you’re happy that your steaks are cooked (probably after being in the pan for between 5 and 8 minutes), take them out of the pan and place them on a room temperature plate to rest for at least 3 minutes.
  20. Serve your steaks on warm plates and enjoy.

A few pictures:

Amazingly marbled steak. Note the difference between the marbling and the sinew.
Beef Marbling Standards – Top left is bland, bottom right = Awesome
Kobe is 5-6, Japanese Wagyu is 9-10. I don’t think 12 is actually possible.
Steak “Temperatures” – Medium Rare is what most chefs believe is best.

Ten things

Get your priorities straight
There is nothing more important than enjoying your life. Making sure that other people are enjoying their lives comes in at a close second. You are not a useful human being if you are not enjoying your own life.

Don’t sweat the small stuff
Gary Player famously said that the more he practiced the luckier he got. You can reprogram the way your brain reacts to truly stressful situations by practicing positive, stress-free, reactions to the little things that go wrong every day.

Get some perspective
Most of the stuff you worry about is simply not important. Your family and friends are what matter. They are irreplaceable. Your car getting stolen, your house burning down, losing your job, while all sad and frustrating, should not result in emotional trauma.

Emotional trauma is scar tissue
Years ago I broke my big toe by kicking a wall. It was stupid and every now and then my toe hurts for no reason. If you repeatedly kick a wall your toe is going to hurt all the time and you are not going to be able to enjoy your life.

It’s not over until you’re dead
Your health is important, but not more important than enjoying your life. I’m not suggesting you start a small heroin habit, but worrying about your health is futile unless you’re doing it while calling a doctor.

Put on your big girl panties when dealing with family
There are situations in life where you just need to make hay, even if you’re allergic and the sun isn’t shining. The normal rules of engagement do not apply for family. “Not talking” to some branch of your family is an incredibly sad outcome that should be avoided at all costs. No one is asking you to paint each other’s nails while watching Thelma and Louise on VHS, but for everyone’s mental health, including your own, sometimes you just need to get out there in the rain and start throwing around some hay.

Appreciate what you have
Be thankful for the things you have, not because one day they might be gone or because others don’t have them, but simply because you do have them.

Stretch
This is not a computer game. When you die your life is over. Try and be incredible or die trying.

Yourself is the best you you can be
Countless Facebook posts will encourage you to sing like no one is listening. That is rubbish. Being yourself is the only way to be happy. This is not rocket science. If you want to sing, sing. If you want to spend your weekend reading a book to your cat, do that.

Stop
Close the door, put away your phone, sit down and spend some time thinking. Call it whatever you want but just do it, daily if possible.

Leaving TrustFabric and joining Praekelt

This news is a few weeks old but I kept on meeting people who hadn’t heard so I figured a blog post was in order.

I have officially left TrustFabric and have joined Praekelt. Leaving TrustFabric was a hard decision. If Joe can pull of what he’s got planned, and I think he can, he will change the way we manage and share information online. I want that to be a reality.

My first few weeks at Praekelt have been great. I am now in a pure strategy role. Travelling back and forth between JHB and CPT, meeting amazing, talented people, having my mind expanded and learning constantly. My diverse background (travel, banking, advertising, telecoms, ISPs, online dating etc etc) is proving to be useful in ways that I never thought possible.

I’ll keep you posted.