Bluetooth based load control using LinkIt ONE and Android

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

<?’resource-id’=’state’>

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

<?ln1=0>

to set the resource ‘ln2’ to 1

<?ln2=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.

<?resource-id>

to get the status of resource ln2, we need send the following command

<?ln2>

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()

void 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.

#include "Arduino.h"
#include "LBT.h"
#include "LBTServer.h"
#include <stdlib.h>

#include <LinkItOneBluetoothPublish.h>

#define ln1Pin 6
#define ln2Pin 7
#define ln3Pin 8
#define ln4Pin 9
#define ln5Pin 10
#define ln6Pin 11
#define ln7Pin 12
#define ln8Pin 13
LinkItOneBluetoothPublish bt;

float ln[9];
long milli=0;

void setup()
{
int i=0;
Serial.begin(9600);
Serial.println("starting the application");
bt.beginBT();
//Serial.println("BT Started");
for(i=6;i<14;i++)
pinMode(13, OUTPUT);

bt.publish("ln1", &ln[1]); //publishing variable with a key
bt.publish("ln2", &ln[2]);
bt.publish("ln3", &ln[3]);
bt.publish("ln4", &ln[4]);
bt.publish("ln5", &ln[5]);
bt.publish("ln6", &ln[6]);
bt.publish("ln7", &ln[7]);
bt.publish("ln8", &ln[8]);
}

void loop()
{

digitalWrite(ln1Pin, (uint8_t)ln[1]);
digitalWrite(ln2Pin, (uint8_t)ln[2]);
digitalWrite(ln3Pin, (uint8_t)ln[3]);
digitalWrite(ln4Pin, (uint8_t)ln[4]);
digitalWrite(ln5Pin,(uint8_t) ln[5]);
digitalWrite(ln6Pin, (uint8_t)ln[6]);
digitalWrite(ln7Pin,(uint8_t) ln[7]);
digitalWrite(ln8Pin,(uint8_t) ln[8]);
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.

 

Add a Comment

Your email address will not be published. Required fields are marked *