Arduino MKR1000 + Android + Relay = Christmas gift lock
Christmas is here! You’ve got your gift but you need to unlock it. Get your Arduino MKR1000 and your Android phone ready!
New 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 ArduinoMKR1000 over 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
- Sample Android program
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.
A 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.
The 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 the 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 the serial (the full code is attached to the 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)