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!

Advertisements

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 install CyanogenMod 7 on an HTC Desire

Quick post because I wrote most of this out for a friend and figure it was internets worthy.

Firstly, why would you want to do this?

  1. CyanogenMod gives you Apps2SD functionality which means you can store apps on your SD card regardless of whether they support being moved to SD. The Desire has some or other issue with storage space and you’re forever running out.
  2. Android 2.3.3 is slick and should almost double your battery life.
  3. CyanogenMod has hundreds of tiny tweaks that make your phone better and faster.

Warning: This should be obvious but following the steps below will essentially factory reset your phone and you will lose all apps etc. Since your contacts are probably backed up with Google and all the apps are available via Market, you shouldn’t need to stress.

From start to finish this entire process should take less than an hour.

  1. Plug your phone into a computer and copy everything off your SD card. This is only necessary if you care about your photos and movies etc.
  2. Use unrevoked to root your phone. Don’t stress if it automatically reboots once or twice. It does that… no idea why.
  3. Install Clockwork ROM manager from the Market. (the free one is fine)
  4. From inside the Rom Manager, “Flash ClockworkMod Recovery”. Takes a minute and will give you a success message when done.
  5. From inside the Rom Manager, “Partition SD Card”. This partitions and formats your SD card to give you Apps2SD functionality and also a bit of swap memory. I used a 512mb Ext partition and a 32mb swap partition. (Your phone will reboot and do the partitioning in the recovery mode)
  6. From inside the ROM manager, use “Download ROM” to get the latest Stable CyanogenMod. It’ll ask if you want the Google apps as well, tick the box to say yes.
  7. Wait for it to finish downloading (might take a while since it’s about 90mb of downloads)
  8. When done it it will ask if you want Backup Existing ROM and Wipe Data and Cache. If this is your first CyanogenMod you’ll want to tick the last two (Data and Cache). Additionally you probably also want to backup your existing rom. (it backs it up to the SD Card)
  9. Your phone will reboot into recovery and start doing stuff. It’ll do the backup first (take about 10 minutes) and then it will install the new ROM (takes about 8 minutes) and then it will reboot.
  10. The first reboot is slow. Don’t stress.
  11. Boom, you have CyanogenMod 7 with Android 2.3.3 (at time of writing)

Who invented the lightbulb?

Lets get one thing straight. Thomas Alva Edison did not invent the lightbulb.

This story requires context:

  • In 600AD the greek writer Thales of Miletus started writing about Electricity. Mostly this was all about rubbing Amber together and noticing static charge
  • In 1760 French physicist Charles-Augustin Coulomb starts actually making sense of electricity.
  • In 1779 Alessandro Volta builds the first true battery.
  • In the late 1700s and early 1800s every scientist, tinkerer and hacker is playing with electricity.
  • 30 years later, in 1809, Humphry Davy builds the first true electric lamp. This is 38 years before Edison is even born.
  • 50 years later, in 1860, Sir Joseph Wilson Swan builds an electric light bulb and starts tinkering with carbon-filament incandescent electric bulbs (The same stuff Edison eventually used).
  • Ten years later in 1877, the American Charles Francis Brush manufactured carbon arcs and was lighting public parks and office blocks.
  • In 1879, three years after Brush already had electric light in office blocks, Thomas Alva Edison discovers that a carbon filament in an oxygen-free bulb was able to stay glowing for up to 40 hours.
  • Two years later some dude named Lewis Howard Latimer makes a better filament.
  • 20 years later another guy called Willis R. Whitney invents a process to stop the globe getting dimmer as it got older
  • Seven years later, in 1910, a guy called William David Coolidge invents a tungsten filament that lasts a lot longer than the carbon ones.
Thomas Alva Edison - Douchebag Extraordinaire

So all that Edison ever “invented” was that a vacuum increased the lifespan of the filament.

So, the next obvious question is, why does everyone think that Edison invented the light bulb? The answer to that question is an interesting one because it has more to do with propaganda than it does to do with invention.

A lightbulb is useless without electricity, Edison knew that… and, like any clever businessman, Edison knew that real inventors are generally very bad at making money out of their inventions because they’re always too busy working on version 2.

Edison realised that the only way to make real money was to get electricity into people’s houses so that you could sell them lightbulbs, and electricity… This was brilliant because it’s the “Give away the razor, sell the razorblades” plan except both lightbulbs and electricity are consumables… so its more like “Give away the connection and sell the lightbulbs and the electricity”. Edison also wasn’t the first person to figure this out, he was just really really good at marketing… Or really really bad at marketing, depending on how you feel about Elephants.

Topsy the Elephant

Another person who realised that Electricity was going to make a lot of money was a rich businessman called Westinghouse. Westinghouse had become friends with an eccentric Yugoslav scientist called Nikola Tesla who had been experimenting with electricity his whole life. When Tesla wasn’t busy building Tesla Coils or trying to harness the power of lightning (or trying to harness energy from outer space, I shit you not) he worked with Westinghouse to build electricity generators and plan electricity distribution systems. Tesla knew more about electricity than pretty much anyone else alive at that point and early on had realised that Alternating Current (AC) was far better at distributing electricity than Direct Current (DC). So Westinghouse and Tesla started generating and distributing power to the rich and famous.

Edison didn’t like this. Not only were Tesla and Westinghouse competitors, but they were also proposing a different, better, system (AC) that Edison knew would eventually win the battle. Edison had managed to market himself as the father of electricity — a magician and folk hero — and he was getting incredibly rich.

Nikola Tesla - True Genius

So Edison did what any self respecting douche-bag marketer would do… he started publicly torturing animals… and filming it. Thomas Alva Edison, the “inventor of the lightbulb” went out into the streets and publicly electrocuted, to death, animals in a bid to show the public that Alternating Current was far too dangerous to be in their homes. And yes, in case you’re wondering, that reference to elephants earlier is because Edison even electrocuted, to death, an adult elephant.

Of course AC isn’t really any more dangerous than DC and Edison knew that… While there are some issues with AC’s 50-60 Hertz frequency being closer to that of your heart, both AC and DC are equally able to kill elephants and small children.

Sadly the public was gullible and Tesla was devastated… He went from being an eccentric socialite magician to being that guy who wants to murder small children and animals. Westinghouse had thicker skin and managed to keep his chin up, but Tesla became a recluse and started working in isolation on increasingly crazier ideas like harnessing power from the stratosphere. Tesla believed that it would be possible to get this power so cheaply that it would become free. Whether Tesla was onto something or whether his eccentric genius mind had finally snapped will never be known… In 1943 he died, alone, drowning in debt, in a hotel room.

Thomas Edison was no doubt a very clever man, but he was also ethically a disgusting person who thought nothing of destroying others to elevate his own fame.

Quick and Simple Server SMTP

I have a number of servers that I look after in various places on the intertubes. I like to have things like MDADM (Linux software RAID manager) be able to mail me when the something goes wrong like a disk dies etc.

Some of these machines are in places without reliable SMTP servers for me to send mail through and I’ve tried running my own postfix and delivering the mail directly, but invariably I run into situations where the servers that I’m trying to deliver mail to don’t like DSL IPs… and not getting a mail about a dead disk is kinda a big issue.

I also don’t trust a lot of ISP’s SMTP, and some of my servers move around, so one day it’ll be behind a DSL IP and the next behind a Verizon IP (where it can’t talk to smtp.dslprovider.net etc).

My solution is quite simple, use google. (This guide is for Ubuntu but I’m sure you’ll figure it out with other distros)

  1. Create a gmail account for monitoring. I do this because I don’t want my gmail password floating around in plaintext on various machines.
  2. Install the ca-certificates package

    $ sudo aptitude install ca-certificates
    $ sudo update-ca-certificates

  3. Install msmtp

    $ sudo apt-get install msmtp

  4. Configure msmtp

    $ sudo vim /etc/msmtprc

    Set it to something like

    account gmail
    host smtp.gmail.com
    from myemailaddress@gmail.com
    auth on
    tls on
    tls_trust_file /etc/ssl/certs/ca-certificates.crt
    user notifyemailaddress@gmail.com
    password mys3cr3tp455w0rd
    port 587

    account default : gmail

  5. Create a sendmail simlink

    $ sudo ln -s /usr/bin/msmtp /usr/sbin/sendmail

  6. Run a test

    $ echo “This is a an awesome test email” | msmtp youremail@domain.com

  7. If you want mdadm to mail you when something goes wrong

    $ sudo vim /etc/mdadm/mdadm.conf

    and put your email address on the line that reads something like

    MAILADDR youremail@domain.com

  8. And then run a mdadm test by running

    $ sudo mdadm –monitor –scan –test –oneshot

  9. If everything is working according to plan you should receive an email. You can now rest assured that any future MDADM issues will get to you.