If you have ever been to the United Arab Emirates in summer, you will probably know about the heat there. But when you are indoors, in your house or room, and the air conditioner is switched on, you do not feel the heat at all.
Now imagine you have to go out for some time, say two or three hours. You will have to switch off the air conditioner, to save electricity and cut down costs before going. But by the time you come back, there will be absolutely no cooling left in your room and you will have to wait for probably more than half an hour after switching on the air conditioner, for it to get the room's temperature to a bearable point. Looking at the above scene, there is a solution for this, using an Arduino UNO, along with some shields and peripheral devices (bluetooth module,etc) by using the WiFi shield, and a relay, by switching the relay on when you are about to return to your house, approximately half an hour before your arrival. But MediaTek's LinkIt ONE can provide an even better and robust solution, considering its features.
You will certainly say there is not much difference between the UNO and LinkIt ONE (other than the brilliant color scheme, of course!), but when you know the actual features, you will be dazzled.
The main features of this board, which make it the perfect candidate for this project is WiFi, Bluetooth, GPS, GSM and a micro SD card slot, all integrated in a compact package, comparable to the Arduino UNO. And along with that, you get ample space for coding and a generous amount of RAM (for a microcontroller of its size) to execute your code, without lagging, as there is on the Arduino UNO, when used with with bigger and more complex codes. So, given a hint of the awesome features present in this board, let us proceed to the main project. I had initially thought of using the Arduino UNO for this project, but was unable to do so by the cost (Shields were expensive than the UNO itself). But then came the LinkIt ONE, a board designed for wearables and IoT and so, I planned to make the above proposed project with the LinkIt ONE, with additional features, such as being able to receive the temperature and humidity, along with values from a smoke sensor which can alert me when there is a fire, or smoke present in the room.
Note: This project uses a relay, to control a high voltage appliance with the LinkIt ONE. To ensure your safety (and others too!), I would advise you to carry this out only if you know what you are doing, and have had previous experience with high voltage electricity. I am not responsible for any type of damage caused to anything by this project.
Step 1: Ultimate IoT and Its Meaning
At this point, you should be thinking that what exactly is 'Ultimate IoT' and how will it work.
Let me clearly explain my idea to you.
I will be using the MediaTek Cloud Sandbox service to save the data captured by sensors, in the cloud, along with the option of connecting to the LinkIt ONE via integrated Bluetooth in the LinkIt, AND also the option of using GSM, which can be used where-ever your network provider signals are available. So you can be sure that you will be able to send the command to switch on or off your AC and also can request variables from there and receive them too!
But for this, it is necessary that there is a stable WiFi connection in the place where you will fix this system. By 'stable', I mean one which is fast and also located near your router so that it does not disconnect due to weak signals. This is because WiFi is the main medium for connectivity, and Bluetooth and GSM are alternates, and without the presence of WiFi, the code will not move forward. In spite of that, you can accomplish the same thing as done with WiFi, with GSM and Bluetooth. This is what I meant by 'Ultimate IoT'.
I will be using the relay to control an AC, but you can use it control anything, be it a heater, a motor or literally anything. This just depends on your wiring.
So now that you have an idea of what we are about to do, move forward to the next step.
Step 2: Setting Up the LinkIt ONE' Firmware
Unlike the Arduino UNO, where you can just install the Arduino IDE and the required drivers, and just plug in the UNO to your PC and start using it, the LinkIt ONE requires you to do some homework.
In short, you will have to upgrade the firmware of the LinkIt ONE using the Arduino IDE and also add LinkIt ONE to your board manager. Detailed instructions for this are given on the MediaTek Labs website.
If you have already downloaded the compatible Arduino IDE version, choose that one, or download the latest compatible version shown on the website, for example 'Arduino IDE 1.6.5' in the screenshot above.
Then, click on your preferred operating system for that IDE and you will be shown a detailed set of instructions for your particular setup.
You should not have any problems in this step but if you do, the MediaTek forum is a great place for support; just browse through the questions there or google your problem and you will be fine. Once you can run the 'Blink' sketch successfully on the LinkIt ONE, you can then proceed to the next step.
Step 3: Moving On
One of the short-comings of the LinkIt ONE is that it provides only 3.3v in its digital pins. This means that you cannot directly plug in components which take more than 3.3v as that would damage the LinkIt ONE board. So an alternate method is required to accomplish that.
At this point, I would like to tell you about relays and how they work, and why their function makes them suitable to use in my project.
circuitstoday.com has a very good explaination on their website for the working of relays here. I would advise you to go there and have a look at it.
Now that you have an idea of what is a relay and how it works, you need to know that relays come in different specifications.
For example, the Grove relay from SeeedStudio, shown above, can safely be used to control appliances which take a minimum of current, for example a 220v lamp or LED. From its documentation and the printing on the relay we can see that its peak voltage capability is 250v at 10 amps.
Another relay is also shown above. This one is again available from SeeedStudio.
It is larger in size and also the current it can handle (Notice the 30 amps printed on it). So, it can be used to control appliances which take much more current than the smaller relay shown above can handle, for example, motors. But you cannot just say that because the relay is big in size and provides more current, you can use it to control a specific appliance, without confirming the peak current of both, the relay and the appliance you want to control.
To sum up, in selecting relays for your appliances, it is always better to use a relay that can handle a larger amount of current than the appliance so that you stay on the safe side.
You can commonly find relays in which the control circuit (coil) can be operated using 12v and the load circuit can handle higher currents. But, normally, it is not easy to find relays in which the control circuit (coil) can be operated using 3.3v and the load circuit handles a considerably large amount of current, say enough to control an AC. I did not find one like that so I had to find an alternate method.
The relay I found can handle 20 amps and the coil worked with 12 volts. I had salvaged it from an AC main board. It was used to switch the compressor of the AC on and off, so this should suffice. Images of that are given above.
After thinking about this, I had two choices:
- Using two relays. A smaller one (whose coil works with 3.3v) will be used to switch on the big relay (whose coil works with 12v and the current is 20 amps), which will be connected to the air conditioner. Images present above.
- Using an electronic circuit along with the big relay. The electronic circuit is a transistor switching circuit which is used in place of the small relay. It is easy to build and I have described it in detail later. Again, refer to the image above for information on this:
From the above two options, I chose option 2. This was because the switching circuit is very easy to build and I had the parts available too. Another thing is that relays are considerably slower as compared to electronic circuits.
But you can use option 1 too. or if you manage to find a relay that works with 3.3v and can handle a large amount of current, you may just connect that directly. That depends on you.
Step 4: Wiring the Relay to the LinkIt
This step depends on the parts you have available or the method you wish to accomplish this.
There should be three scenarios at this point:
- You will use one relay only, whose coil works at 3.3v and the load circuit can handle enough current to control an AC.
- You will use two relays, one small and one big. The small one will switch on the larger one.
- You will do as I did, make a transistor switching circuit and use that to switch on the main relay.
And here are the solutions:
- You have already done the hard work by finding the relay, so just move on to the next step.
- Wire up the relays so that when the coil of the smaller one is switched on, via the LinkIt ONE's digital pin 9, the coil of the bigger one gets switched on, and with the load circuit of that, you have to connect your AC. In short, wire it so that just switching on digital pin 9 on the LinkIt ONE will switch on your AC. You can look at the image posted earlier for this and that should give you and idea. I will not explain this in more detail, because if you do not understand this, this project is not for you and it will do you good by leaving this project and coming back when you are more confident about your skill of electronics.
- If you found a relay which can handle a large amount of current, but its coil works at a higher voltage level than the LinkIt ONE, and you want to make a transistor switching circuit, read on. I will tell you how to make a transistor switching circuit and with that you can switch on the higher voltage relay.
Transistor Switching Circuit:
You will need these commonly available components for this:
- TIP120 Transistor
- 2.2k resistor
- 1N4001 diode
You can test it on a breadboard first and then build it on perfboard. The breadboard Fritzing schematic is given above. I had tried a lot to find the Fritzing schematic for the LinkIt ONE but was unable to do so. Then I made an SVG file of the LinkIt ONE, using the picture given in the official MediaTek SD Card Tutorial as a reference.
It is very easy to build and the best part is that you can use the same circuit to connect other high voltage components to the LinkIt ONE. This is why it is better to solder it on perfboard and make it permanent rather than using it on the breadboard.
If you followed the above diagram, when you set the LinkIt ONE's digital pin 9 as an output and set it to high, you will hear a 'click' sound. If yes, then this means that the relay switch is on and you can use it to switch on your AC. Then you should move on to the next step. But if you do not, check your wiring, and confirm that the wires from the TIP120 are connected to the coil of the relay.
Step 5: Wiring the AC to the Relay
In the last step, you wired the relay to the LinkIt, but in this one, you will need to wire the AC to the relay.
In this step, you will need to work with high voltage electricity, and it will be different for everyone, so I will just show you how I wired up mine. I will expect you to wire this up yourself. If you have previous experience in working with high voltage electricity, you can understand from the images below, and also those who are unfamiliar with this, you should not attempt this tutorial until you are confident about what you are doing. I am again saying that I am not responsible for any damage caused by this project.
I highly recommend you to make an enclosure for this relay and circuit. If you own or have access to a laser printer, you can make a beautiful enclosure, but I do not so I just made it out of a project box.
Above are pictures of the complete project. I have put them here just because you can have an idea of the wiring of the AC to the relay. To know more about the sensors, just read on.
Step 6: Sensors
In the start of this tutorial, I had told you that along with controlling the AC in the room, I also wanted a method of receiving the temperature, humidity and presence of of smoke. So to do that, I needed sensors. I chose the:
- DHT11 sensor for measuring temperature and humidity
- MQ2 gas sensor for measuring smoke
There was no specific reason for choosing these, other than the fact that I already had them so because the code is set to use this, you will also have to use them.
But because the library which I used for the DHT11 sensor supports using the DHT22 sensor, I think it is possible to use that with some minor modification to the code. I myself think that I will use the DHT22 (Once I receive it), as from what I heard, it is more accurate than the DHT11, because with the DHT11, you can only receive values in whole numbers, not decimal places. But the DHT22 is accurate enough to display values in decimal points. But for now, let us continue using DHT11. The schematic diagram is attached below to wire the sensors. Just use that as a reference and attach them to the LinkIt ONE.
Once done, do not forget to set the switches on the LinkIt ONE in the following positions as shown on the picture above.
And attach the WiFi/BT and GSM antennae to the LinkIt ONE. There is no use for GPS, so it is not necessary to attach that. Also attach the SIM Card to the LinkIt ONE. At this point, your LinkIt ONE should look like mine in the picture above.
Step 7: Setting Up Mediatek Cloud Sandbox
To use the MediaTek Cloud Sanbox system with your LinkIt ONE, you will have to set it up. I will try to describe each step of this in detail, but it is better if you go through Mediatek's tutorial for the LinkIt ONE. In this way, you will already be familiar with the process which we are about to do. I am explaining each step in detail below:
Making an account at MediaTek Labs
Log in, or make an account at MediaTek Labs or MediaTek Cloud Sandbox, if you do not have one. The account made at MediaTek Labs can be used to login at the MediaTek Cloud Sandbox, so it is better to use or make that one only.
Creating the prototype and making data channels
You will need to create the prototype and the data channels exactly as I say, or your data points will not be uploaded to MCS. Images of each step are uploaded above. Look at them and you will be able to make this easily. After logging into your account, you will be directed to your MCS homepage.
- Click on 'Development' and then on 'Prototype'
- Click on 'Create' and enter the data on the page
Note: If you want to have an image for the project, take any picture you want and upload that to the MCS page.
Click on 'Detail' and then on 'Add' in the next page. Then select 'Display'
Then fill in the details exactly as shown above
Click 'Save' and after doing that, make other data channels for humidity and the smoke sensor, selecting 'Display' as the type, and filling the information as in the images above
Then add another data channel for displaying the relay state (Select 'Display' for this one too) and fill in the information
Then add another data channel but this time, instead of 'Display', select 'Controller'. Then add the information as in the image above
If your data point page looks exactly like mine in the image above, move on to make a test device for your prototype.
Creating a test device for the prototype
Click 'Test Device' and there, click 'Add Test Device'. Once done, copy the 'Device name' as in the image below and click on 'Create'. Images included above for reference:
'When the test device has been created successfully, click on 'Go to detail', and you will see a page similar to the one above.
You need to copy the 'DeviceId' and 'DeviceKey' save it somewhere as you will need it later.
Congratulations, you have successfully set up your MCS account and can move on to the next step.
Step 8: The Code
At this point, I would like to tell you about the code itself. I have created different functions, and at the end of each one, I have commented out what that function does. Each of them is linked to the other. In this way, it helps you make changes to the code and also understand it.
You need to make a few changes to the code before you can use it. You will need:
- WiFi connection SSID and its password
- Your 'Device ID' and 'Device Key' from the Mediatek Cloud Sandbox, after making the test device.
- The Bluetooth MAC address of the device you want to use your IoT system with.
- A phone number, from which any commands sent via SMS will work.
You will need the Bluetooth MAC Address of your device and the phone number because I wanted to have security in my system. Because of these, the LinkIt will only connect to a specific MAC address and only carry out SMS commands from a specific phone number. Any number not authorized will be rejected and the SMS received from that number will automatically be deleted. This is because no one would want others to be able to control their house appliances and also get sensor variables whenever they wanted to.
Below, I will tell you in detail how to the get the information stated above:
WiFi SSID and password:
You should yourself know where to get this. I won't tell you more about this.
'Device ID' and 'Device Key':
At the end of the previous step, I had told you to copy and save the 'Device ID' and'Device Key' as you will need it later. So you know where to get this too.
Bluetooth MAC Address:
To get the Bluetooth MAC Address, take out your Android phone or tablet. First, switch on Bluetooth. Then open 'Settings' and from there, go to 'About phone'. Click on 'Status' and there you will see your MAC address under 'Bluetooth Address'. Be careful as 'WiFi MAC address' is also there but we do not need that. Look at the images above for reference.
For this one, you will just need the phone number which you want to authorize with your system. Only SMS's sent from this phone number, will be checked by the LinkIt ONE. All other SMS's from other numbers will be deleted, just decide on that number, but be careful about how you enter it. You will need to enter it in the international number format, the one which appears in your 'Messenger' , when you receive a message from someone. For example, "+123456789", or something like that.
Changing the code
Once you have gotten the information stated above, download the attached code and look at the image above and change the values accordingly.
Turn off the LinkIt ONE, if it is on, and set the small switch next to the battery plug on the LinkIt ONE to 'MS' and plug it in your PC again. Then open 'My Computer' and open the internal flash of the LinkIt ONE. There, create two different text files by right clicking in an empty location and selecting 'Text Document' from 'New'. Name it "wifi.txt". Open it and there, enter your WiFi SSID and password separated by a comma. Do not put spaces until and unless there is a space in your password or SSID, just for the sake of formatting. Images included above for reference.
Note: You can change the WiFi SSID and password, but not the Device ID, Device Key, phone number and MAC address. If you need to change that, you will have to modify that in the code and upload the modified code again.
Uploading the code
Because you have previously uploaded code to your LinkIt ONE (The 'Blink' example), this step should be easy. Just open the code you modified earlier and select the correct COM port (MTK USB Modem Port) by verifying it first from the 'Device Manager'. Then press 'Upload'. Wait for the red reset light to flash once on the LinkIt ONE when you see 'Done Uploading', move on to the next step.
Step 9: The Working of the Code
It is necessary that you know how the code works.
It will send the datapoints to the MediaTek Cloud Sandbox, then it will start the bluetooth server and try to establish a connection with the specified address if available. If not then it will check if any SMS command is received, and then loop through this order.
But this had a problem, not with the WiFi or GSM, but with bluetooth connectivity. The problem was that as the as the LinkIt ONE only initializes the bluetooth server when it finishes uploading all the datapoints to MCS. So if anyone tries to connect the phone to the LinkIt ONE via Bluetooth, it just wont connect. But once it does connect, then it wont disconnect. So this meant that user had to keep on trying to connect to the LinkIt; simply put: Trial and error. But I did not want that it my project. I wanted it to be as smooth and efficient as possible.
Then I looked at other methods of doing that, for example using threads. I found out from the web that the LinkIt ONE does support threading but could not find a proper example for that and then left that too.
In the end, the solution which I used for this problem was that I modified the code to establish a bluetooth server and look for clients after uploading each datapoint to the MCS. While testing, in this way the bluetooth connection was stable enough for me to only retry the bluetooth connection a maximum of one time.
So this idea was very good, compared to the older version and is the final version at the moment.
Step 10: The WiFi Part
It is more efficient to use the MCS Android app for this. You can access it on a computer but because nowadays, everyone has a smartphone, and probably you will be using the bluetooth and GSM part on your smartphone only, so this is better.
Go to the website here and download it. Make sure you have checked the option of installing applications from other sources than the Play Store otherwise the application will not be installed if you are downloading the apk directly. Or you can download it via the Play Store itself.
Once installed, login with your MCS account or with your MediaTek Labs account. I was having trouble logging in using the MediaTek Labs account on my phone so I contacted them and those helpful people sent me step by step instructions of how to login. I am posting those below, in case you have any problem logging in
- MCS Mobile Login Page, click ”Labs@mediatek.com”
- Input credential and click “Sign In”
- Prompt for Authorization, click “Authorize”
- Login page will appear during the login transition please be patient for 2-3 seconds
Look at the images above.
Once you are able to successfully log in, move on to the next step.
Step 11: The Bluetooth Part
For the Bluetooth part, I have made an application using Android Studio. Download it:
- From the Play Store here
- By scanning the QR Code above
Or, if you are too lazy, just download the attached apk file
If you are downloading the apk file, make sure that you have enabled installing third party applications in your Android settings.
Note: This will app will only work on Android 4.0.3 and above
Once the application is successfully installed, read on.
Step 12: Testing and Usage
Note: As you may know, switching on and off an AC without a minimum delay of three minutes will damage it. I have not yet set a timer of that sort in my code, which will wait for some time (Say 3 minutes), before switching on or off the relay pin, once it has been switched on or off by the user. So do not constantly switch the AC on or off, as I have already said it before, and am saying it now that I am not responsible for any damage you cause by attempting this project.
It is a good idea to first set the relay pin to pin 13 in the code. In this way, the LED on pin 13 on the LinkIt ONE will light up when the relay is on, but when you change this, you will have to upload the code again to the LinkIt ONE.
You will need to open the serial monitor to initialize the program. To do that, take your LinkIt ONE (which already has the modified code uploaded) and connect it to your PC. Open the Arduino IDE and click the magnifier icon on the top right. If it says board not available or something like that, just re-plug the LinkIt ONE and try again. Once you get the serial monitor open, wait for a few seconds. You will see output like in the image below, if not, read the troubleshooting section below. If you see the correct output, skip the troubleshooting and start reading from after that.
Look at the images above. There are different examples of errors and what to do if you encounter each one.
Using the WiFi Part
Open the MCS app, enter you credentials and login. Once you are successfully logged in, you should see a similar screen to the one above.
Click on 'LinkIt ONE Ultimate IoT'
You should be receiving the temperature, humidity and smoke sensor values from the LinkIt ONE and also the LED on pin 13 should light up (if you have set the relay pin as pin 13) when you change the switch's position to on.
You can control the relay pin, get sensor values and if click on the arrow on the top right of a data channel like 'Temperature', 'Humidity' or 'Smoke Sensor', you can view a graph showing all the previous received values too.
By clicking the calendar icon at the top of the page, you can even get data from a particular date and time.
The relay switch will take some time to get switched on or off and also to update the relay state in the Mediatek Cloud Sandbox. This is because the LinkIt ONE moves stepwise through the code and when it reaches that part of the code, it gets automatically updated.
Images are present above.
Using the Bluetooth Part
First, you will need to pair your Android device with your LinkIt ONE. The LinkIt ONE's server name will be 'My_BTServer'. This should be easy, and because it varies for everyone, based on their device, I will not be posting details of how to do this. But just make sure that:
- The serial monitor is open. My code is set up to work only when the serial monitor is open. I will tell you how to make it start without opening the serial monitor later, but for now, you will need to open it.
- You are trying to connect the same Android device whose MAC address you found out earlier. If you try to connect another device, or entered the wrong MAC address, this wont work no matter what.
A thing which I noticed during testing is that two devices named 'My_BTServer' appear. You can pair both of them, but only one of them works with my application. So if you get stuck somewhere and it seems that you have tried out everything, you should unpair your paired device, and pair with the other one and try again.
Once you are successfully paired with the LinkIt ONE, open my application 'LinkIt Ultimate IoT' (which you previously downloaded) on your Android device (You need to use that device whose MAC address you saved earlier). You will see a screen like the one above if Bluetooth is switched off.
Touch 'Allow' and you will see the homescreen of my app (Pictured above)
To make it display the values, you will first need to connect it to the LinkIt ONE's server. Click on the Bluetooth icon on the top right of the screen. It will open another screen showing your paired Bluetooth devices. Pressing the blue floating button will scan for other available devices.
You should touch 'My_BTServer' and then you will be redirected to the app home screen and a pop-up will come telling you if the connection was successful or not.
If you get 'Unable to connect device', do not panic, just try connecting again.
When there is a successful connection, values start getting uploaded to it and appear in their respective places. The LinkIt ONE sends a formatted string with the values and the app automatically places them in their respective locations.
You can use the relay switch to control the relay and 'State' to tell the current status of the relay.
I have also created an option of saving the formatted string from the LinkIt ONE as a text file on your phone's storage. You can enable that option by touching the button next to the Bluetooth icon and clicking 'Settings'. Then check 'Enable Data Logging' in the settings page which opens.
I told you before that the LinkIt ONE sends a formatted string to the Android device. When data logging is enabled, the formatted string which comes from the LinkIt ONE is saved as a text file, in a folder in your Android devices root storage, named 'LinkIt Ultimate IoT'. A separate text file is created for each day, and it even stores the time with each particular string.
Note for Android 6.0 users: The data logging feature will not work until and unless you have provided permissions to the app, by going in your device settings.
You will say this is pretty much the Bluetooth part, but there one surprise feature left. Swipe to left on the home-screen of the app and graph mode will open. Using graph mode, the app automatically plots the received values in the form of a graph but the best thing about this is that it is real time.
Look at the video in step one and the images above for more information on how to use the bluetooth part.
Using the GSM Part
You can even use SMS's to control the relay and request sensor variables, at times when you do not have access to WiFi or Bluetooth. To do that, simply take out your Android device. If you send an SMS to the SIM in your LinkIt, saying "Switch on the relay", after some time, you will receive an SMS from the LinkIt's SIM that the relay is on.
Below is a list of the commands you can send and what you will receive after sending each command (Do not send the inverted commas too!)
- "Send variables": You will receive an SMS from the LinkIt ONE, with all the variables (Temperature, Humidity, Smoke sensor percentage as well as the relay state)
- "Switch on the relay" and "Switch off the relay": Does what it says on the tin! A confirmation SMS will be sent to you after switching the relay on or off.
Note: Using the GSM part may have costs and those depend on your service provider. The LinkIt ONE will not be able to send you an SMS if there is insufficient balance in the SIM Card. But output will be given in the serial monitor, so be sure to familiarize yourself with the way it works. Also, SMS commands generally take longer to execute in this system, compared to WiFi and Bluetooth. But I can assure you that it should take less than a minute at the most.
Above is an example of a conversation between the LinkIt ONE and my smartphone
Now that you are here, hopefully, you have used my system and will know about how it will work. Now, I will give you ideas about to install this system in your house. First, when you have set up everything and know how it works, open my code and comment out the line which is:
while (!Serial) delay(1000);
Which is present at the start of
This will cause, the code to start without requiring the serial port to be initialized before. Upload this modified code to your LinkIt ONE. You can also use the included battery but I have not tested how much it will work using that.
Then you can just plug it in using any adapter, for example a mobile charger rated at 5v. to power the LinkIt ONE continuously.
I will strongly advise you to make an enclosure for the relay. You can make it out of a project box or if you have a laser cutter or 3D printer, you can make a beautiful enclosure with that. I do not own or have access to one so I will not give you any CAD files or something like that.
Make sure that you place your LinkIt ONE in a place where it is in good range of your WiFi access point because if your WiFi does not work, the code will not start.
Step 13: Thanks and Future Updates
I would like to thank you for reading through the project, and if you have any ideas/comments/problems, feel free to leave a comment and I will surely help you out.
I will be adding more future updates to the project. This is because I want to install this permanently in my house
- Using another accurate and better temperature and humidity sensor in place of the DHT11
- Adding more sensors - Flame sensor, etcImproving the working of the full system - making it even more efficient and usable
- Adding more relays, to control more devices
- Making a Fritzing layout for the LinkIt ONE