Pages

10 July, 2012

Controlling a lamp using PC or Android


I have been searching for a 230V AC dimmer that I can control using a microcontroller, but I still haven't find any. What I wanted to do was to make the ceiling lamp fade on to wake me up in the morning. Basically I wanted to do the same as the wake up lights produced by companies such as Philips, but I wanted to use the ceiling lamp since it is allready installed. Unfortunatelly I ended up using a simple relay to make the lights go on and off, although it's not that spectacular. I found out that the most convenient thing to do was to set a wake up time and sleep time using my phone. This made it a whole lot easier to change the wake up time, and this also let med control the state of the light by just pressing a button on the phone. I now want to show you how you can do the exact same thing.

Hardware
First you should take a look at my earlier tutorial on how to set up the bluetooth device and program the Arduino. We will be using the exact same code for this project.

The LED and the resistor that was used in the earlier tutorial, will now be replaced by circuit that is a bit more complicated. We use this circuit to change the state of our relay, which will be working as our light switch.

WARNING: 230V AC can do a lot of damage to both you and your circuit. If you don't use components that are rated for the correct voltage and current, they might go up in flames. In some countries it is illegal for unauthorized personnel to do this kind of modification to the electrical system of the building.
Software - PC
After you are done with the wiring, you can try it out like we did in the earlier tutorial. Use the Arduino software to set the state of the pin to HIGH or LOW. I wanted to make it a bit more advanced, so I opened Microsoft Visual C# 2012 Express and created a new WPF project.


MainWindow.xaml:
<Window x:Class="WPF_Serialport_Bluetooth.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="142" Width="150" Loaded="Window_Loaded" Closing="Window_Closing" WindowStyle="None" AllowsTransparency="True" BorderBrush="Black" BorderThickness="1" SizeToContent="WidthAndHeight">
    <Grid Background="#FFF0F0F0">
        <Button Content="On" Height="23" HorizontalAlignment="Left" Margin="8" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
        <Rectangle Height="23" HorizontalAlignment="Left" Margin="90,8,0,0" Name="rectangle1" Stroke="Black" VerticalAlignment="Top" Width="23" Fill="#FF343434" MouseLeftButtonDown="rectangle1_MouseLeftButtonDown" />
        <Image Height="16" HorizontalAlignment="Left" Margin="93,12,0,0" Name="image1" Stretch="Fill" VerticalAlignment="Top" Width="16" Source="/WPF_SerialPort_Bluetooth;component/Images/1323476719_cancel.png" MouseLeftButtonDown="image1_MouseLeftButtonDown" Opacity="0.5" />
        <TextBox Height="23" HorizontalAlignment="Left" Margin="60,38,0,0" Name="textBoxOnHour" VerticalAlignment="Top" Width="23" IsEnabled="False" />
        <TextBox Height="23" HorizontalAlignment="Left" Margin="90,38,0,0" Name="textBoxOnMinute" VerticalAlignment="Top" Width="23" IsEnabled="False" />
        <Label Content="On" Margin="27,36,0,0" Name="labelOn" HorizontalAlignment="Left" VerticalAlignment="Top" IsEnabled="False" />
        <Label Content="Off" Height="28" HorizontalAlignment="Left" Margin="27,62,0,0" Name="labelOff" VerticalAlignment="Top" Width="27" IsEnabled="False" />
        <TextBox Height="23" HorizontalAlignment="Left" Margin="60,64,0,0" Name="textBoxOffHour" VerticalAlignment="Top" Width="23" IsEnabled="False" />
        <TextBox Height="23" HorizontalAlignment="Left" Margin="90,64,8,8" Name="textBoxOffMinute" VerticalAlignment="Top" Width="23" IsEnabled="False" />
        <CheckBox Height="16" HorizontalAlignment="Left" Margin="12,41,0,0" Name="checkBoxOn" VerticalAlignment="Top" Unchecked="checkBoxOn_Unchecked" Checked="checkBoxOn_Checked" />
        <CheckBox Height="16" HorizontalAlignment="Left" Margin="12,67,0,0" Name="checkBoxOff" VerticalAlignment="Top" Unchecked="checkBoxOff_Unchecked" Checked="checkBoxOff_Checked" />
        <Grid.ContextMenu>
            <ContextMenu>
                <MenuItem x:Name="xContextMenuTop" Click="xContextMenuItem_Top" Header="Always on top" IsCheckable="True">
                    <MenuItem.Icon>
                        <Image Height="16" Width="16" Source="/WPF_SerialPort_Bluetooth;component/Images/1323476910_arrow_top.png" />
                    </MenuItem.Icon>
                </MenuItem>
            </ContextMenu>
        </Grid.ContextMenu>
    </Grid>
</Window>


MainWindow.xaml.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO.Ports;
using System.Windows.Threading;

namespace WPF_Serialport_Bluetooth
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        SerialPort sp = new SerialPort("COM8", 9600);
        int btnState = 0;

        public MainWindow()
        {
            InitializeComponent();
            this.MouseLeftButtonDown += (o, e) => DragMove();

            DispatcherTimer dispatcherTimer = new DispatcherTimer();
            dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
            dispatcherTimer.Interval = TimeSpan.FromMilliseconds(1000);
            dispatcherTimer.Start();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            sp.Open();
        }

        private void dispatcherTimer_Tick(object sender, EventArgs e)
        {
            DateTime CurrTime = DateTime.Now;

            if (checkBoxOn.IsChecked == true && textBoxOnHour.Text.Count() > 0 && textBoxOnHour.Text.Count() <= 2 && textBoxOnMinute.Text.Count() > 0 && textBoxOnMinute.Text.Count() <= 2)
            {
                if (CurrTime.Hour == int.Parse(textBoxOnHour.Text) && CurrTime.Minute == int.Parse(textBoxOnMinute.Text) && CurrTime.Second < 1)
                {
                    sp.Write("H");
                    rectangle1.Fill = new SolidColorBrush(Colors.White);
                    button1.Content = "Off";
                    btnState = 1;
                }
            }

            if (checkBoxOff.IsChecked == true && textBoxOffHour.Text.Count() > 0 && textBoxOffHour.Text.Count() <= 2 && textBoxOffMinute.Text.Count() > 0 && textBoxOffMinute.Text.Count() <= 2)
            {
                if (CurrTime.Hour == int.Parse(textBoxOffHour.Text) && CurrTime.Minute == int.Parse(textBoxOffMinute.Text) && CurrTime.Second < 1)
                {
                    sp.Write("L");
                    rectangle1.Fill = new SolidColorBrush(Color.FromRgb(52, 52, 52));
                    button1.Content = "On";
                    btnState = 0;
                }
            }
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            if (btnState == 0)
            {
                sp.Write("H");
                rectangle1.Fill = new SolidColorBrush(Colors.White);
                button1.Content = "Off";
                btnState = 1;
            }
            else
            {
                sp.Write("L");
                rectangle1.Fill = new SolidColorBrush(Color.FromRgb(52, 52, 52));
                button1.Content = "On";
                btnState = 0;
            }
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            sp.Close();
        }

        private void rectangle1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            sp.Close();
            Application.Current.Shutdown();
        }
        private void image1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            sp.Close();
            Application.Current.Shutdown();
        }

        private void xContextMenuItem_Top(object sender, RoutedEventArgs e)
        {
            if (this.Topmost == true)
            {
                this.Topmost = false;
            }
            else if (this.Topmost == false)
            {
                this.Topmost = true;
            }
        }

        private void checkBoxOn_Checked(object sender, RoutedEventArgs e)
        {
            labelOn.IsEnabled = true;
            textBoxOnHour.IsEnabled = true;
            textBoxOnMinute.IsEnabled = true;
        }

        private void checkBoxOff_Checked(object sender, RoutedEventArgs e)
        {
            labelOff.IsEnabled = true;
            textBoxOffHour.IsEnabled = true;
            textBoxOffMinute.IsEnabled = true;
        }

        private void checkBoxOn_Unchecked(object sender, RoutedEventArgs e)
        {
            labelOn.IsEnabled = false;
            textBoxOnHour.IsEnabled = false;
            textBoxOnMinute.IsEnabled = false;
        }

        private void checkBoxOff_Unchecked(object sender, RoutedEventArgs e)
        {
            labelOff.IsEnabled = false;
            textBoxOffHour.IsEnabled = false;
            textBoxOffMinute.IsEnabled = false;
        }
    }
}


When using the code above, be sure to enter the right serial port (COMx). If you don't know what port to use, take a look at the earlier tutorial on how to set up the bluetooth device and program the Arduino.

Software - Android
After testing that the C# WPF program worked, I opened Eclipse and begun the process of making an Android application.


MainActivity.java:

package com.example.test;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.TextView;
import android.widget.EditText;  
import android.widget.Button;
import android.widget.ToggleButton;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.GregorianCalendar;
import java.util.Set;
import java.util.Timer;
import java.util.TimerTask;
import java.util.UUID;
import java.util.Calendar;

public class MainActivity extends Activity
{
private static final int REQUEST_ENABLE_BT = 0;
Button openButton;
Button sendButton;
Button closeButton;
Button highButton;
Button lowButton;
ToggleButton onoffButton;
EditText editTextOnHour;
EditText editTextOnMinute;
EditText editTextOffHour;
EditText editTextOffMinute;
Calendar calendar;
EditText editTextDeviceName;
int hour;
int minute;

   TextView myLabel;
   EditText myTextbox;
   BluetoothAdapter mBluetoothAdapter;
   BluetoothSocket mmSocket;
   BluetoothDevice mmDevice;
   OutputStream mmOutputStream;
   InputStream mmInputStream;
   Thread workerThread;
   
   BluetoothAdapter bluetoothAdapter;
   
   byte[] readBuffer;
   int readBufferPosition;
   int counter;
   volatile boolean stopWorker;
   
   @Override
   public void onCreate(Bundle savedInstanceState)
   {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);
       
       MyTimerTask myTask = new MyTimerTask();
       Timer myTimer = new Timer();
       myTimer.schedule(myTask, 500, 500);
       
       bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
               
       openButton = (Button)findViewById(R.id.open);
       sendButton = (Button)findViewById(R.id.send);
       closeButton = (Button)findViewById(R.id.close);
       highButton = (Button)findViewById(R.id.high);
       lowButton = (Button)findViewById(R.id.low);
       onoffButton = (ToggleButton)findViewById(R.id.onoff);
       myLabel = (TextView)findViewById(R.id.label);
       myTextbox = (EditText)findViewById(R.id.editTextEntry);
       editTextOnHour = (EditText)findViewById(R.id.editTextOnHour);
       editTextOnMinute = (EditText)findViewById(R.id.editTextOnMinute);
       editTextOffHour = (EditText)findViewById(R.id.editTextOffHour);
       editTextOffMinute = (EditText)findViewById(R.id.editTextOffMinute);
       editTextDeviceName = (EditText)findViewById(R.id.editTextDeviceName);
       
       CheckBlueToothState();
       
       onoffButton.setText("Off");
       
       //Open Button
       openButton.setOnClickListener(new View.OnClickListener()
       {
           public void onClick(View v)
           {
               try 
               {
                   findBT();
                   openBT();
                   openButton.setEnabled(false);
                   sendButton.setEnabled(true);
                   closeButton.setEnabled(true);
                   highButton.setEnabled(true);
                   lowButton.setEnabled(true);
                   onoffButton.setEnabled(true);
               }
               catch (IOException ex) { }
           }
       });
       
       //Send Button
       sendButton.setOnClickListener(new View.OnClickListener()
       {
           public void onClick(View v)
           {
               try 
               {
                   sendData();
               }
               catch (IOException ex) { }
           }
       });
       
       //Close button
       closeButton.setOnClickListener(new View.OnClickListener()
       {
           public void onClick(View v)
           {
               try 
               {
                   closeBT();
                   openButton.setEnabled(true);
                   sendButton.setEnabled(false);
                   closeButton.setEnabled(false);
                   highButton.setEnabled(false);
                   lowButton.setEnabled(false);
                   onoffButton.setEnabled(false);
               }
               catch (IOException ex) { }
           }
       });
       
       //High button
       highButton.setOnClickListener(new View.OnClickListener()
       {
           public void onClick(View v)
           {
               try 
               {
                mmOutputStream.write('H');
                onoffButton.setChecked(true);
                onoffButton.setText("On");
                highButton.setEnabled(false);
                lowButton.setEnabled(true);
               }
               catch (IOException ex) { }
           }
       });
       
       //Low button
       lowButton.setOnClickListener(new View.OnClickListener()
       {
           public void onClick(View v)
           {
               try 
               {
                mmOutputStream.write('L');
                onoffButton.setChecked(false);
                onoffButton.setText("Off");
                highButton.setEnabled(true);
                lowButton.setEnabled(false);
               }
               catch (IOException ex) { }
           }
       });
       
     //On-Off button
       onoffButton.setOnClickListener(new View.OnClickListener()
       {
           public void onClick(View v)
           {
            if(onoffButton.isChecked())
               {
            try 
               {
                mmOutputStream.write('H');
                highButton.setEnabled(false);
                lowButton.setEnabled(true);
               }
               catch (IOException ex) { }
            onoffButton.setText("On");
               }
               else
               {
                try 
               {
                mmOutputStream.write('L');
                highButton.setEnabled(true);
                lowButton.setEnabled(false);
               }
               catch (IOException ex) { }
                onoffButton.setText("Off");
               }
           }
       });
   }
   
   private void CheckBlueToothState(){
       if (bluetoothAdapter == null){
        myLabel.setText("Bluetooth NOT support");
          }else{
           if (bluetoothAdapter.isEnabled()){
            if(bluetoothAdapter.isDiscovering()){
            myLabel.setText("Bluetooth is currently in device discovery process.");
            }else{
            myLabel.setText("Bluetooth is Enabled");
            }
           }else{
            myLabel.setText("Bluetooth is NOT Enabled!");
            Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
               startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
           }
          }
      }
   
   void findBT()
   {
       mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
       if(mBluetoothAdapter == null)
       {
           myLabel.setText("No bluetooth adapter available");
       }
       
       if(!mBluetoothAdapter.isEnabled())
       {
           Intent enableBluetooth = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
           startActivityForResult(enableBluetooth, 0);
       }
       
       Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
       if(pairedDevices.size() > 0)
       {
           for(BluetoothDevice device : pairedDevices)
           {
            //Name of BT device
               if(device.getName().equals(editTextDeviceName.getText().toString())) 
               {
                   mmDevice = device;
                   break;
               }
           }
       }
       myLabel.setText("Bluetooth Device Found");
   }
   
   void openBT() throws IOException
   {
       UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb"); //Standard SerialPortService ID
       mmSocket = mmDevice.createRfcommSocketToServiceRecord(uuid);        
       mmSocket.connect();
       mmOutputStream = mmSocket.getOutputStream();
       mmInputStream = mmSocket.getInputStream();
       
       beginListenForData();
       
       myLabel.setText("Bluetooth Opened");
   }
   
   void beginListenForData()
   {
       final Handler handler = new Handler(); 
       final byte delimiter = 10; //This is the ASCII code for a newline character
       
       stopWorker = false;
       readBufferPosition = 0;
       readBuffer = new byte[1024];
       workerThread = new Thread(new Runnable()
       {
           public void run()
           {                
              while(!Thread.currentThread().isInterrupted() && !stopWorker)
              {
                   try 
                   {
                       int bytesAvailable = mmInputStream.available();                        
                       if(bytesAvailable > 0)
                       {
                           byte[] packetBytes = new byte[bytesAvailable];
                           mmInputStream.read(packetBytes);
                           for(int i=0;i<bytesAvailable;i++)
                           {
                               byte b = packetBytes[i];
                               if(b == delimiter)
                               {
                                   byte[] encodedBytes = new byte[readBufferPosition];
                                   System.arraycopy(readBuffer, 0, encodedBytes, 0, encodedBytes.length);
                                   final String data = new String(encodedBytes, "US-ASCII");
                                   readBufferPosition = 0;
                                   
                                   handler.post(new Runnable()
                                   {
                                       public void run()
                                       {
                                           myLabel.setText(data);
                                       }
                                   });
                               }
                               else
                               {
                                   readBuffer[readBufferPosition++] = b;
                               }
                           }
                       }
                   } 
                   catch (IOException ex) 
                   {
                       stopWorker = true;
                   }
              }
           }
       });

       workerThread.start();
   }
   
   void sendData() throws IOException
   {
       String msg = myTextbox.getText().toString();
       //msg += "\n";
       mmOutputStream.write(msg.getBytes());
       myLabel.setText("Data Sent");
   }
   
   void closeBT() throws IOException
   {
       stopWorker = true;
       mmOutputStream.close();
       mmInputStream.close();
       mmSocket.close();
       myLabel.setText("Bluetooth Closed");
   }
   
   class MyTimerTask extends TimerTask {
   public void run() {
  calendar = new GregorianCalendar();
  hour = calendar.get(Calendar.HOUR_OF_DAY);
       minute = calendar.get(Calendar.MINUTE);
  if (Integer.parseInt(editTextOnHour.getText().toString()) == hour && Integer.parseInt(editTextOnMinute.getText().toString()) == minute)
       {
        try 
                {
                mmOutputStream.write('H');
                onoffButton.setChecked(true);
                onoffButton.setText("On");
                highButton.setEnabled(false);
                lowButton.setEnabled(true);
                }
                catch (IOException ex) { }
       }
 
  if (Integer.parseInt(editTextOffHour.getText().toString()) == hour && Integer.parseInt(editTextOffMinute.getText().toString()) == minute)
       {
        try 
                {
                mmOutputStream.write('L');
                onoffButton.setChecked(false);
                onoffButton.setText("Off");
                highButton.setEnabled(true);
                lowButton.setEnabled(false);
                }
                catch (IOException ex) { }
       }
   }
  }
}


main.xml (layout):

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="@dimen/padding_small"
        android:text="@string/type_here_"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/textView6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="@dimen/padding_small"
            android:layout_marginRight="@dimen/padding_small"
            android:text="@string/device_name"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <EditText
            android:id="@+id/editTextDeviceName"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="10"
            android:inputType="text"
            android:text="@string/name_of_bt_device" >

            <requestFocus />
        </EditText>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <EditText
            android:id="@+id/editTextEntry"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.13"
            android:ems="10" android:inputType="text"/>

        <Button
            android:id="@+id/send"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.16"
            android:enabled="false"
            android:text="@string/send" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="fill" >

        <Button
            android:id="@+id/high"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.19"
            android:enabled="false"
            android:text="@string/high" />

        <Button
            android:id="@+id/low"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.19"
            android:enabled="false"
            android:text="@string/low" />

        <Button
            android:id="@+id/close"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.19"
            android:enabled="false"
            android:text="@string/close" />

        <Button
            android:id="@+id/open"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.19"
            android:text="@string/open" />
    </LinearLayout>

    <ToggleButton
        android:id="@+id/onoff"
        android:layout_width="match_parent"
        android:layout_height="89dp"
        android:enabled="false"
        android:text="@string/togglebutton"
        android:textSize="40dp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="@dimen/padding_medium"
            android:text="@string/action"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/textView4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="40dp"
            android:text="@string/hour"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/textView5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="50dp"
            android:text="@string/minute"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textStyle="bold" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="@dimen/padding_small"
            android:layout_marginRight="@dimen/padding_small"
            android:text="@string/on"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <EditText
            android:id="@+id/editTextOnHour"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="10"
            android:inputType="time" />

        <EditText
            android:id="@+id/editTextOnMinute"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="10"
            android:inputType="time" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="@dimen/padding_small" >

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="@dimen/padding_small"
            android:text="@string/off"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <EditText
            android:id="@+id/editTextOffHour"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="10"
            android:inputType="time" />

        <EditText
            android:id="@+id/editTextOffMinute"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="10"
            android:inputType="time" />
    </LinearLayout>

</LinearLayout>


AndroidManifest.xml:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.test"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="7"
        android:targetSdkVersion="15" />
    <uses-permission android:name="android.permission.BLUETOOTH" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main"
            android:screenOrientation="portrait" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

If you got questions or suggestions for improvement, please let me know!

5 comments:

  1. I can copy this code, compile and install in my Android cell that will work?

    ReplyDelete
  2. Hi!From "openButton = (Button)findViewById(R.id.open);" to "editTextDeviceName = (EditText)findViewById(R.id.editTextDeviceName);" in MainActivity.java, I had the same problems - it cannot be resolved or is not a field.
    How do I solve it? Thanks!

    ReplyDelete
  3. Is there a way to connect the arduino to a switch power like this one. Notice the DC voltage in this power cord

    http://www.ekmmetering.com/ekm-metering-products/accessories/switch120.html?gclid=CPHJ5fPzy70CFSdn7Aod8HQAdA

    ReplyDelete