Android Interface 4 Switches
Welcome everyone to this new tutorial. Even though this will be a strict Android tutorial, we will be joining it with previous work: Four Switches (the first chapter of the Switches Series). I am going to show you how to set up a basic and simple configuration to have a set of buttons mirroring the switches we made. And how to send and receive UDP SIGNALS to toggle the on/off states. So let's get started.
Supplies
As you can see in the picture above, for this we will need an Android phone or tablet. To install or use Android Studio, in which, we will be coding and deploying our app. And at least a NodeMCU/ESP8266 or similar. You don't need to build the entire Four Switches or a complete and finished relay implementation. You can play around with just the simple board by debugging the Arduino IDE console or if you want to have more fun improvise simple connections with a few relays, the type you like the most. Or maybe a simple set of LEDs. But if you want to become an expert go through the switches tutorial.
Diagram
As a matter of fact, more than a diagram, the picture above demonstrates what this project is all about. As you can see, you will be able to control remote electric power sockets by sending and receiving UDP signals through your local network, with an interface representing each socket. That will show the state of each socket every time a switch status is toggled. In each signal sent, we will receive the status of all switches. So the system will be able to update the status constantly. If a switch has been changed by another device, this interface will reflect that change.
Check Out the Code
You can clone our repo or check out if you have previously worked with our tutorials, this branch:
Improved Architecture
As you can see we slightly introduce new features in this new branch. We used to have only the Main Activity class in which we coded all our needed functionalities just for simplicity reasons. Now that we have more knowledge we will be splitting the code into different units to make it more clean. We will be introducing concepts like dependency injection and polymorphism.
- Dependency Injection is a design pattern where the dependencies of a software component are injected from the outside rather than created within the component.
- Polymorphism is a programming concept that allows objects of different types to be treated as objects of a common type. In other words, it enables a single interface to represent various types, and a single operation can work on different types of objects.
So that being explained let's see the code:
Please take a look at this for an overall view of the complete structure of an Android app. We will be explaining the components related exclusively to the Java code.
Android tutorial 1 (Getting Started with Android Development: Understanding a Basic App)
Android tutorial 2 (The basics of how an Android app works!)
Android tutorial 3 (Android app from scratch with Android Studio)
Android tutorial 4 (Learn how to download an Android app from Git and generate an installable file)
We will have 3 main components:
- MainActivity.java contains the basic configuration, and the well-known onCreate method that contains the construction of the objects required for controlling the UDP communication, and the buttons toggling.
- UdpTransceiver.java is in charge of the UDP emitting and receiving.
- ButtonHandler.java that will represent each button or switch.
So basically when the app starts the onCreate method will loop through a fixed list of details describing the buttons we will be dealing with:
This in future improvements could be managed by a configuration phase of the app and stored in an internal db or a persistent resource. Then we create an object from the UdpTransceiver.java to handle all UDP communication from now on. This will be used to check the first state of the buttons and we will be injecting it to all the buttons we will be creating.
Notice that with this action we will be able to split the code and reuse features created by "passing it" to other components.
Then we create each button and pass the information required to handle their states like ID, NAME, and of course the UDP transceiver and the list itself of all buttons. This last one gives the capability for each button to check and update the status of all. And finally, we add the created button to the Map of key values called buttons.
Finally, the toggle of each button in the view will trigger this function:
activity_main.xml extract:
MainActivity.java extract:
Basically, and thanks to this pattern we created, we simply get the ID of the button toggled, retrieve the button identified by that ID, and execute the function toggle. ButtonHandler.java will have a main method toggle that will be:
- Sending the toggle signal defined by the actual status of the button and receiving the update status.
- Handling possible errors.
- Changing the label of the button on/off
- Checking the updated status for the rest and updating them ( this is in case another device changes the status of the switch).
- Returning the response with the info about what button has changed to be printed in Main.
Now take notice that this is not exactly a polymorphic structure since is the same class for the same behavior. What is changing is the information inside each button that makes it act differently. The idea with this structure is to define an interface and complete different classes implementing that interface. But for this case, I think is fair enough. We will deal with more complex scenarios in the future.
And last but not least, UdpTransceiver.java. This one is simple. It can send a UDP package and wait at most 5 seconds to receive the updated status.
Coming Next
Regarding what we just built here, I feel there is still space to improve. Regarding for example the activity_main.xml the buttons are hardcoded, in our example we used 4 switches. The idea would be to make the buttons dynamically. In this way, you will be able to configure all the switches you want in your home or office. So you can control as many switches as you want.
On the other hand, there is a future tutorial in this series to control the switches with your voice. And why not add this feature to this same app? We will see.
See you in the next one and have a good one.