In this post we will build a wireless home load controlling system using LinkIt ONE and Android mobile. Android mobile will have a client application to switch on/off the devices and LinkIt ONE will act as base device which will receive commands from mobile and controls the relays. This project involves very little wiring as LinkIt ONE has inbuilt bluetooth. We can test the project without any wiring or relays by controlling the on board LED.
The android application supports controlling of 8 IO lines.
Android application working:
App is made of two activities one is for controlling the loads and other is to select the bluetooth device. The communication is synchronous between LinkIt ONE and mobile. Android application sends a control command to the LinkIt ONE ,expects a reply from the LinkIt ONE within specified amount of time. if there is no repay from the LinkIt ONE then operation will timeout and considered as command failed.
The idea is very simple we will have some float variables on LinkIt ONE (will be mentioned as resources from know onwards). we will be able to change the value of these resources from a remote program running on other device here it andorid mobile. in LinkIt ONE code, we can monitor these resource values to change the state of IO or for any other purpose.
Command format to change the state of a load
where device id is id of the load and state will be 0 or 1.(you can replace the state with any value ,in this project we have only on/off so two states are enough)
for example to set the resource ‘ln1’ to 0, LinkIt ONE expects following command
to set the resource ‘ln2’ to 1
The resource id’s are hardcoded in the android application, so if you are using this android application for your own project on LinkIt ONE, you need to stick with these id’s (ln1,ln2…ln8). But on LinkIt ONE you are free to map the id’s to any of the IO’s.(we will see that in a minute)
To get the status of a IO ,we just need to send deviceid.
to get the status of resource ln2, we need send the following command
for all the commands we send the LinkIt ONE responds with the value of the resource in the received command. The LinkIt ONE will just send only the value
for example(assume that ln1 value is 1 now), if we send <?ln2> the LinkIt ONE will respond with 1.00\r\n
We can include the resource id in the response, which will allow us to implement the asynchronous communication model.
We will use SPP of the LinkIt ONE(LinkIt ONE also supports GATT), The LinkIt ONE provides libraries to access the Bluetooth functionalities. The response time of the LinkIt ONE is not as fast as that of HC-05. The Android application may not respond immediately at times because of the communication is synchronous.
LinkItOneBluetoothPublish library takes care of the communication part and updating the published variables.
You just need to know about the two methods about this library, to be able to publish your variables.
int publish(String key,float *ptr);
This method just works like a dictionary, which takes a key and associated value is address of the variable that you want publish.This method will returns negative value if it fails to publish the variable, zero or positive value for successful publish. we will use this key when sending query to Arduino from Android application. You might want keep this key as short as possible because we don’t have much RAM on Arduino.
The other method is sync()
This method serves the requests from Android application . You need to call this method from your main loop as frequently as possible otherwise you may miss some requests.
#define ln1Pin 6
#define ln2Pin 7
#define ln3Pin 8
#define ln4Pin 9
#define ln5Pin 10
#define ln6Pin 11
#define ln7Pin 12
#define ln8Pin 13
Serial.println("starting the application");
bt.publish("ln1", &ln); //publishing variable with a key
bt.sync();//This method will check for any incoming messages in Bluetooth
As shown in the code , you can map the published variables to any IO pin or to control any other functionalities.you can download the library from here,and place it in arduino library folder. The Arduino sketch and the android apk are present in example folder of the library. if you haven’t setup the environment for linkIt ONE ,checkout this blog.