Category: Main page » Microcontrollers

Lighting control with Android device through Bluetooth

Here we describe a prototype light control, or any other load using Android device, be it a tablet, smart phone, clock, etc. Communication is performed via Bluetooth link.

As a Bluetooth module uses cheap Chinese module HC-06, which has been described in this article.

The controller is used with .NET Micro Framework: FEZ Panda II, but any controller works with .NET Micro Framework core. Also, I would like to give an example of the transfer of entire rows instead of one character in both directions.

For this, we take the real task - control of two light bulbs in the apartment. As the bulbs I use halogen LED bulbs G4. Power of light bulbs is 12 volts and they will be powered by a separate power supply.

Parts:
Android device
Bluetooth module
.NET Micro Framework controller (FEZ Panda II or other)
Relay module

Wiring diagram:

Wiring diagram

Source code for .NET Micro Framework in a Visual C # Express

using System;
using System.Threading;
using System.IO.Ports;
using System.Text;

using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;

using GHIElectronics.NETMF.FEZ;                                     // library for FEZ

namespace CxemCAR
{
    public class Program
    {
        public static void Main()
        {
            byte[] My_Data = new byte[20];                          // array to store a string
            int My_index = 0;                                       // array index
            byte[] rx_data = new byte[1];                           // array UART data

            OutputPort relay1 = new OutputPort((Cpu.Pin)FEZ_Pin.Digital.Di8, false);
            OutputPort relay2 = new OutputPort((Cpu.Pin)FEZ_Pin.Digital.Di9, false);

            SerialPort UART1 = new SerialPort("COM1", 9600);        // new object UART1 (COM1)
            UART1.Open();
            UART1.Flush();

            while (true)
            {
                int read_count = UART1.Read(rx_data, 0, 1);         // read data
                if (read_count > 0)                                 // received data?
                {
                    My_Data[My_index] = rx_data[0];                 // write the byte to our array

                    if (rx_data[0] == '\r')                         // if, at the end of line
                    {
                        My_Data[My_index] = 0;                                               // remove the last element, in this case it "\r"

                        string strMy_Data = new string(Encoding.UTF8.GetChars(My_Data));     // create a string from a byte array

                        if (strMy_Data != null && strMy_Data.Equals("Start1"))               // crafted string compare with our string
                        {
                            byte[] buffer = Encoding.UTF8.GetBytes("LED 1 On\r\n");          // prepare a byte array "Led On" for output in UART
                            UART1.Write(buffer, 0, buffer.Length);                           // write in UART1 a byte array
                            relay1.Write(true);                                              // turn on relay 1
                        }
                        else if (strMy_Data != null && strMy_Data.Equals("Stop1"))
                        {
                            byte[] buffer = Encoding.UTF8.GetBytes("LED 1 Off\r\n");         // prepare a byte array "Led Off" for output in UART
                            UART1.Write(buffer, 0, buffer.Length);                           // write in UART1 a byte array
                            relay1.Write(false);                                             // turn off relay 1
                        }
                        else if (strMy_Data != null && strMy_Data.Equals("Start2"))
                        {
                            byte[] buffer = Encoding.UTF8.GetBytes("LED 2 On\r\n");          // prepare a byte array "Led On" for output in UART
                            UART1.Write(buffer, 0, buffer.Length);                           // write in UART1 a byte array
                            relay2.Write(true);                                              // turn on relay 2
                        }
                        else if (strMy_Data != null && strMy_Data.Equals("Stop2"))
                        {
                            byte[] buffer = Encoding.UTF8.GetBytes("LED 2 Off\r\n");         // prepare a byte array "Led Off" for output in UART
                            UART1.Write(buffer, 0, buffer.Length);                           // write in UART1 a byte array
                            relay2.Write(false);                                             // turn off relay 2
                        }
                        else
                        {
                            byte[] buffer = Encoding.UTF8.GetBytes("Command Error!\r\n");
                            UART1.Write(buffer, 0, buffer.Length);                           // write in UART1 a byte array
                        }

                        Array.Clear(My_Data, 0, My_Data.Length - 1);                         // clear array
                        My_index = -1;                                                       // clear array index
                        Thread.Sleep(100);
                    }
                    if (My_index < 19) My_index++;                                           // increment array index
                }
            }
        }
    }
}

In the program, the end of the line (command) is defined by line feed character "\r"

I defined four commands:
Start1 - turn on relay 1
Stop1 - turn off relay 1
Start2 - turn on relay 2
Stop2 - turn off relay 2

After the successfully receiving the command, Android device sent the relevant status messages: LED 1 On, LED 1 Off, etc. If the command is not recognized, an error message is transmitted "Command Error!"

Program for Android written in Java in Eclipse IDE. To work with the Bluetooth uses a class: Bluetooth.java. Receiving data from Bluetooth carried out a separate asynchronous thread. Received data and system messages are transmitted to the main activity through class Handler. To format a string from the controller uses the class StringBuilder.

Piece of code that is responsible for the formation of the string:

case cBluetooth.RECIEVE_MESSAGE:
   byte[] readBuf = (byte[]) msg.obj;
   String strIncom = new String(readBuf, 0, msg.arg1);
   sb.append(strIncom);                                                         // add new string to sb
   int endOfLineIndex = sb.indexOf("\r\n");                                     // define the end of the line
   if (endOfLineIndex > 0) {                                                    // if the end of the line
      String sbprint = sb.substring(0, endOfLineIndex);
      sb.delete(0, sb.length());                                                // clear sb
      text_answer_txt.setText("Answer: " + sbprint);                            // update TextView
   }
   break;

In Android-application, you can write a command from the keyboard (you is provided the input field and a submit button) and you can control relays with two ToggleButton. Below is a text field that displays the text recieved from FEZ Panda II.

Wiring diagram

Download APK application for Android

Download full project with source codes for Eclipse IDE and Visual C# 2010 Express

Author: Koltykov A.V.


This page no comment. You can be the first.

Your name:
Your Email:

Comment:
Type the characters: *
captcha
Refresh