Arduino MKR1000 + Android + Relay = Christmas gift lock

Santa is here but you need to guess your gift’s password! It’s a great starting point if you’re new to Arduino MKR1000, networking & Android

gimp-extract_YT0hAIh2As

Things used in this project

Hardware components

Arduino MKR1000
Arduino MKR1000
× 1
LED (generic)
LED (generic)
× 1
Jumper wires (generic)
Jumper wires (generic)
× 1
Resistor 330 ohm
Resistor 330 ohm
× 1
Relay (generic)
× 1

Story

b80de33b3719141bdba413c37cf1c789_GCF9qFxVIXNew to Arduino MKR1000? You’ve just hit the right place. This tutorial is meant to be a simple starting point that will introduce you to some cool stuff to make your projects shine.

We’ll also learn a little bit about UDP and Sockets. (Don’t worry if you’re not familiar with those guys, I’ll talk a bit about them; I’ll also mention some other stuff but will leave them for you to explore.)

What will we do?

We’ll use your Android phone to send the password to the ArduinoMKR1000over a network which will in turn check your password and unlock the gift that’s secured by a home lock connected to an AC Relay.

  • The Network (a little intro about what happens behind the scenes )
  • Arduino MKR1000 and what the Arduino does
  • Circuit
  • Sample Android program

Network

The Arduino MKR1000 will be connected to your home WiFi router. It’ll talk to the Android through the network using UDP sockets.

UDP (User Datagram Protocol*) is a network protocol that’s fast and very easy to use, though it doesn’t guarantee data transfer.

Our home network isn’t busy and thus won’t cause any problems regarding data loss.

*Protocol: simply, it’s a way for devices to talk to each other.

Sockets are like software doors; they connect your device (say, the Arduino ) to the whole world or your whole home! They take data from your device/Arduino in a Packet (bucket? Close enough…) and send it to the nearest “Router” and it will tell it where to go.

msg1281493660_LP3SCLrs13A Packet is a chunk of bytes. Packets travel all over the internet. They arrive at their correct destinations because they carry their destination IP address with them.

Arduino MKR1000

Our Arduino will be listening on its socket. Sockets have port numbers; each device may be opening many sockets simultaneously, and since each device has only one IP normally, port numbers denote which socket to go to.

An analogy for port numbers are houses on the same street: you know the street number/name, but you also must know the house number in order to reach your exact destination. Hence, the sender (your phone) must know not only the destination IP but also port number.

Your WiFi router will assign an IP to each newly connected device -umm, using DHCP- so we can’t know our Arduino’s IP until it’s already connected to the network. So, we’ll print it to serial (full code is attached to project).

IPAddress ip = WiFi.localIP(); 
Serial.print("IP Address: "); 
Serial.println(ip);

This will show something like this: (this IP is what you will use in the Android application)

tmp_file_kGcBNyLKK3

After connecting to our router and obtaining our IP, we start our UDP socket on a port that we define in a variable. A port number is a 16-bit value (Arduino int type is 16-bit).

unsigned int localPort = 2390;  // local port to listen on 

Opening a UDP socket is as simple as…

 Udp.begin(localPort); 

Now we need to check if something has arrived, and as I mentioned before, UDP is really simple to use. This line will return the number of available bytes to read. We can simply check that to know if something has arrived.

int packetSize = Udp.parsePacket(); 

To actually “read” data we need – you guessed it! – a read function. The function takes in the buffer size and returns the amount of actually read data. As you don’t know how much data is coming, you want to create a container that’s large enough to carry incoming data – the buffer . That’s just a fancy way to say an array of chars. Then you can slice and dice it depending on actual amount of bytes you received.

// len will contain number of bytes arrived at our Arduino
int len = Udp.read(packetBuffer, 255); // char packetBuffer[255]

255 bytes are large enough -really!- for a password string. You can change this to a larger value, but take care as UDP packets are limited in size by your network; this value is called maximum transfer unit (MTU).

After reading our data, we check if sent password matches passwords we defined on Arduino. If so, we’ll open our gift! Or else, we’ll make an LED scream.

full code is on Arduino Web Editor here.

Circuit

Our circuit is fairly simple; we’re using a relay module as it’s easier to hook up. If you’re not familiar with relays, check my project on that. If you don’t want to use a relay, it’s totally up to you. That’s just an example to add some ketchup and make it more fun. Arduino controls relay and the warning LED. The relay is connected to gift lock. (Assuming that you’re using a really strong AC lock for the Christmas gift!)

circuit_bb_XagKq1iy2Y

Schematics

ArduinoMKR100 circuit

Take care of the AC!
circuit_bb_aMA7ZXL4qF

Code

ArduinoMkr1000 code

Code for the ArduinoMKR1000 to control the locker and check the passwords


Android UDP Sender

It’s an application to let you send UDP messages, the best way to use the code is to import it using Android Studio

 


About The Author

Ibrar Ayyub

I am an experienced technical writer with a Master's degree in computer science from BZU Multan University. I have written for various industries, mainly home automation and engineering. My writing style is clear and simple, and I am skilled in using infographics and diagrams. I am a great researcher and am able to present information in a well-organized and logical manner.

Follow Us:
LinkedinTwitter
Scroll to Top