I recently installed the famous messaging application “WhatsApp” on my smartphone. After a while, I decided to create a device that can remotely switch two relays On or Off using messages sent through WhatsApp. Obviously, you must have two active accounts (and two smartphones) for remotely control the relays. In this description we call them the transmitter and the receiver. This device needs for a specific circuit (the relays board) and for a special Android application that works together with WhatsApp on receiver smartphone. Let’s start with the description of the circuit (the hardware), then the Android application (the software) will follow.
The hardware is based on PIC12F635 microcontroller from Microchip. It’s a small 8 pin device. In the picture you can see the prototype, realized on a 50x70mm proto board. The smd micro has been placed on small adapter (the red one on the left).
The working prototype
The schematic is relatively simple. We have the micro, a DTMF tone decoder (MT8870), a couple of relays and a switching regulator from 12V to 5V. That’s all.
Schematic diagram; click to enlarge
If you want a more readable copy, download the PDF file at this link. Please note the switching regulator module KIS-3R33S. I purchased a lot of (used) modules on ebay, at very low price. The problem is that the module is rated for 3.3V -3A max output, but I need for 5V out, so I modified the module removing a couple of components: one zener diode and one 51K resistor. It’s a very simple operation, please look at the picture:
The switching regulator and the parts that must be removed. (click to enlarge)
This switching regulator is needed only if you want to have an USB output that can recharge the smartphone that you use as receiver. In other cases, you can simply use a 5V linear regulator capable of 100-200 mA output current. A reduced (easy) schematic will be like this: (please, note that also ICSP section has been removed, that means you must program the micro off-board).
A reduced (easy) version of the schematic (click to enlarge)
A pdf version of this schematic can be downloaded at this link.
The circuit detects a sequence of four DTMF tones. The first three tones are “the activation key” and are fixed to ‘1’, ‘3’, ‘7’, while the fourth tone is “the command” and can have the values: ‘2’ for R1-ON R2-OFF, ‘5’ for R1-OFF R2-ON, ‘6’ for R1-ON R2-ON and finally ‘8’ for R1-OFF R2-OFF. Once built up, the circuit can be tested with every device capable of playing MP3 files. The test files 1372.mp3, 1375.mp3,1376.mp3 and 1378.mp3 can be downloaded as a zip file from this link. Connect a stereo jack from the circuit to the player and play one at a time the 4 files. The relays will follow the combination presented as DTMF tones.
To make hardware work, you must program the PIC micro with the .hex file that can be downloaded from this link (updated version:150409 – changed red LED behaviour and implemented timeout after valid key received). The Configuration Bits for the PIC12F635 in this application is shown here:
The configuration word for PIC12F635 (click to enlarge)
Using the ICD2 programmer under MPLAB, you can receive a warning like this:
ICD2 warning (click to enlarge)
On my prototype the device is correctly programmed if you click the “OK” button.
The WhatsApp (I will use WA abbreviation from now on) protocol is proprietary and I don’t want to hack the received text messages; so… how to decode a command for relays activation ? I have seen on my smartphone there is a folder named WhatsApp/Media/WhatsApp Images. When you receive an image as attachment to a message sent via WA, a copy of that image is saved on this folder. So, my way to control the relays is simply to poll that directory to see if a new IMG file is present, then I load that file in an imagebox of my Android App and analyze the contents in order to decode the relays command; after that the image is renamed (next polling doesn’t find it again). This way to operate is non-intrusive and co-operative with WA application.
The transmitter doesn’t need any additional application; you just need to store the command images on a folder that’s visible for WA when you try to send an image as attachment ( I use the Downloads folder on main storage). The commands are four (all the combinations of two relays) and are small and simple images:
The images that will be used as attachments for sending commands
The lower part is for the user (human readable), while the upper part will be read by the Android application. Note that the left two bits are the complement of the right two, this is just to have a validity check while analyzing the image. You can download all the files zipped at this link. Finally, when you want to activate a relay on the receiver, simply send from transmitter a WA empty message with one of the previous command images as attachment.
The receiver is a little bit more complicated. You must download, first of all, a special ringtone, that is the “key” to assign the receiver to a specific transmitter. After downloading the ringtone from this link (right button mouse click to download the ringtone if your browser tries to play it directly), you must store the audio file on the receiver smartphone, in a folder that makes it visible under the phone’s ringtones. On my old A5000 smartphone (Android Version 2.2.1) I created a folder on the main storage (/sdcard/) named Media/audio/ringtones, and inside that folder I stored the new ringtone named 137.ogg; after this operation and after rebooting, the file appeared in the list of ringtones. When this new ringtone is in the list, you must assign it to the specific contact (or contacts) in your phonebook that is (are) authorized to play with relays; then you must set WA preferences to play the notification tone using the contact’s ringtone. To test this settings, send a WA message from transmitter to receiver and hear if the played notification tone is the one just assigned. After this, send a WA message from another phone (not authorized) to the receiver and verify that the notification tone is different (or absent).
Now it’s time to load and install the WhatRelay application from my page on Google Play Store. Once installed, at first run, the program asks for the working directory of WA. On my devices (both of them) this folder is on the main storage (/sdcard/) with this path: WhatsApp/Media/WhatsApp Images/ (take care of capital letters and last slash). Insert this path and accept. Please, note that all files IMG-xxxx.jpg already present on such folder will be renamed by the application in .IMG-xxxx.jpg (hidden), one every 5 + 2 seconds. If you want to preserve such files from renaming, move them to a new folder. In any case, every IMG-xxxx.jpg file present will be loaded and analyzed by the program, then renamed in .IMG-xxxx.jpg, so remove all such files before starting the App, or you will see them appear in the imagebox, be analyzed, then renamed at 5 + 2 seconds steps. The application sets the phone to stay always ON. Click the “quit” button to exit the application and restore the normal auto-turn-off time.
First run of WhatRelay App
When the default path is set, the application starts, polling every 5 seconds the working directory to look if any IMG-xxxx.jpg file has been received. In the picture below, there is a screenshot of what happens when an IMG file is found. The image is copied in the small box at the right and after 2 seconds the program analyzes the picture to attribute a code. If the code is valid, a DTMF tone is played. In any case (valid image or not) the IMG-xxxx.jpg file will be renamed in .IMG-xxxx.jpg.
Program running: one IMG file has been received
So, when WA receives a message, plays the notification tone that is the 137.ogg audio file, containing three DTMF tones that are the “key” to enable the circuit, then the WhatRelay App detects a new image, decodes it and plays the fourth DTMF tone (after less than 15 seconds) and the electronic circuits has received the key and the command, so can switch the relays. Job done.
Please, note that this is a release 0.1. This release will be revised many times. Look at this page or on Google Play Store to see if there is something new (and better). A special note regarding the volumes: remember that the circuit connects to the smartphone headphone plug, so put the volumes around 2/3 of the maximum and remove any notification that isn’t needed for the relays control. Do some tests without connecting the circuit, just to hear if all the tones are played with a good audio level. Use the local command buttons for other tests or for manual control of relays.