Voice and Wi-Fi controlled Lamp

Today, I’m very excited to share a tutorial how to control your lamp or any electronic appliance with voice either cross-platform application created with Xamarin.Forms. So, did you find yourself a very lean person who don’t want to stand up to turn on or off the light or just wanna be the boss no matter what? If yes, follow the steps in this tutorial and any physical activity can be spared!

 

First of all, let’s see how it works!

Sure, no world-changing technology was invented here, however, imagine the feeling when you to tap a button on your phone or just say “Hello” to your #nomorestupid / #smart lamp:

Ok! Now I have your attention, so we can start making this dream come true.


Part I – Power Socket Control

Things you need

Making of Power Socket extension controlled with 5V Relay

Our lamp is going to be switched in a way of plugging / unplugging a device directly into / from a power socket. This can be achieved by creating a power socket extension, which will have a relay connected to our Raspberry Pi.

power-socket-relay-switch-2

Cut the power extension in the middle and connect a relay inside a junction box:

power-socket-relay-switch-4

In the next step, connect the diode, transistor, and resistor as following image shows:

power-socket-relay-switch-5

power-socket-relay-switch-1

Controlling the lamp from UWP application

Now we are ready to start coding UWP application for Raspberry Pi in order to control GPIO pin connected to our relay. It’s the very easy thing, but don’t forget to add the IoT extension reference to your project. Initializing the GPIO pin should look like this:

Now, when the pin is initialized,  we just need to set it’s value in order to switch the lamp on or off.

Aaand that’s it!


Part II – Speech Recognition

As you have already made it work, we’re now ready to add a speech recognition to be able to turn on or off the lamp by saying the command.

Things you need

Speech Recognition service

To detect voice commands we’re going to use a great, built-in SpeechRecognition API.

First of all, set the Microphone device capability in the package.appxmanifest file to get access to the microphone’s audio feed.

 

Next, create a new class called SpeechRecognitionService.

Basically, all you need to do is start a new SpeechRecognizer, define its grammar (vocabulary), compile it and listen for commands in a loop.

How simple is that? 🙂

 


Part III – Mobile Application

Yelling on your Raspberry is not always the best solution, so let’s take a look how to communicate between two C# applications.

When it comes to peer-to-peer communication between UWP and Xamarin apps, we can use TCP Sockets. Unlike UDP, it establishes a long-lasting and reliable connection. However, please note that this implementation is not secured and it’s aimed to use only on private networks.

UWP Server class

First of all, we need to implement server-side listener, which will listen for any incoming requests. Create a new class with TcpListener property, initialize it, and listen in a loop for any incoming connections. When the connection is established, listen for commands, process them and provide a relevant response back to the client. In this scenario, I’m dealing with strings, however, for bigger projects, it is more reasonable to use serializable classes for commands and responses of your own.

Xamarin Client

Now, we are ready to implement the client in order to reach our server. Because of different Mono Frameworks for mobile platforms, it’s not possible to create a single class client and use it on iOS, Windows, and Android. Fortunately, the code is 99.9% the same, so all we need to do is copy our class to each project and then use DependencyService to access it from PCL (portable class library).

PCL abstract client

Platform-specific client

The platform-specific class inherits from abstract client implemented in PCL project and is marked with assembly attribute.

Accessing platform-specific client from PCL


Conclusion

And that’s pretty much all from Voice and Wi-Fi controlled Lamp tutorial 🙂

We’ve seen how to control simple electronic device, either with your voice or mobile phone. Hope it helps to you!

Leave a Reply