In the previous post I wrote about how to setup an Arduino board in order to communicate with other devices using modbus and rs485.
This project is an example of DIY home automation system in which one (or more) Arduino boards can cooperate in order to effectively build the so-called smart homes. An Android touch screen is also used in this project in order to display data (for instance temperature, humidity etc) and devices status. The project could be easily used in other than smart homes field, like industrial automation.
The nice thing of this design is that the logic of the system could be distributed across all the Arduinos and even to the Android touch screen.
I’m now concernet about presenting you the Android code that deals with
The smart home project is an Android app like the one we usually use in our Android smartphones.
A good introduction on how to create and Android project could be found here.
The demo user interface is displayed in the picture on the right. It has some yellow/red bulb icons that account for the status of the bulb in the home, 4 toggle buttons and 2 analog statuses and a slider for the light intensity.
The main purpose of this and the previous articles was to give you an idea on how simply is to connect a set of Arduino boards to an Android touch screen using modbus and rs485.
I deliberately kept apart design principles for a moment, focusing firstly to descibe how simple is to connect a set of Arduino boards to an Android touch screen, leaving to you, reader, to express yourself and design the best interfaces that follow your needs.
The xml for the layout follows in the next block of source code. I used the AbsoluteLayout [LINK], even though more responsive layouts could be used.
http://developer.android.com/reference/android/widget/AbsoluteLayout.html
[sourcecode language=”xml”]
<AbsoluteLayout xmlns_android="http://schemas.android.com/apk/res/android"
xmlns_tools="http://schemas.android.com/tools"
android_layout_width="match_parent"
android_layout_height="match_parent"
android_background="@color/black">
<TextView
android_id="@+id/textView2"
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_layout_marginLeft="10dp"
android_layout_marginTop="10dp"
android_layout_x="12dp"
android_layout_y="20dp"
android_text="Switches status (Digital output):"
android_textColor="@color/white"
android_textColorHighlight="@color/white" />
<TextView
android_id="@+id/textView1"
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_layout_marginBottom="14dp"
android_layout_marginRight="20dp"
android_layout_x="376dp"
android_layout_y="10dp"
android_text="www.biemmeitalia.net"
android_textColorHighlight="@color/white"
android_textColor="@color/white"/>
<TextView
android_id="@+id/textView3"
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_layout_marginLeft="10dp"
android_layout_marginTop="180dp"
android_layout_x="12dp"
android_layout_y="210dp"
android_text="Analog input:"
android_textColor="@color/white" />
<ProgressBar
android_id="@+id/progressBar1"
style="?android:attr/progressBarStyleHorizontal"
android_layout_width="220dp"
android_layout_height="wrap_content"
android_layout_marginBottom="36dp"
android_layout_marginLeft="22dp"
android_layout_x="30dp"
android_layout_y="236dp"
android_max="1023" />
<ToggleButton
android_id="@+id/toggleButton3"
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_layout_x="282dp"
android_layout_y="54dp"
android_text="ToggleButton" />
<ToggleButton
android_id="@+id/toggleButton2"
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_layout_x="152dp"
android_layout_y="54dp"
android_text="ToggleButton" />
<ToggleButton
android_id="@+id/toggleButton4"
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_layout_x="414dp"
android_layout_y="54dp"
android_text="ToggleButton" />
<ToggleButton
android_id="@+id/toggleButton1"
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_layout_x="34dp"
android_layout_y="54dp"
android_text="ToggleButton" />
<ImageView
android_id="@+id/imageView1"
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_layout_x="25dp"
android_layout_y="152dp"
android_src="@drawable/red_bulb" />
<TextView
android_id="@+id/textView4"
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_layout_marginLeft="10dp"
android_layout_marginTop="180dp"
android_layout_x="274dp"
android_layout_y="125dp"
android_text="Light intensity (PWM):"
android_textColor="@color/white" />
<TextView
android_id="@+id/textView3"
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_layout_marginLeft="10dp"
android_layout_marginTop="115dp"
android_layout_x="18dp"
android_layout_y="121dp"
android_text="Bulb status (Digital input):"
android_textColor="@color/white" />
<ImageView
android_id="@+id/imageView3"
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_layout_x="147dp"
android_layout_y="152dp"
android_src="@drawable/red_bulb" />
<ImageView
android_id="@+id/imageView2"
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_layout_x="82dp"
android_layout_y="152dp"
android_src="@drawable/yellow_bulb" />
<ImageView
android_id="@+id/imageView4"
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_layout_x="205dp"
android_layout_y="152dp"
android_src="@drawable/red_bulb" />
<SeekBar
android_id="@+id/seekBar1"
android_layout_width="220dp"
android_layout_height="wrap_content"
android_layout_x="296dp"
android_layout_y="157dp"
android_max="128" />
<TextView
android_id="@+id/temperature"
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_layout_x="395dp"
android_layout_y="230dp"
android_text="°C"
android_textAppearance="?android:attr/textAppearanceLarge"
android_textColor="@color/white" />
<TextView
android_id="@+id/textView5"
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_layout_x="300dp"
android_layout_y="235dp"
android_text="Temperature:"
android_textColor="@color/white" />
</AbsoluteLayout>
[/sourcecode]
Colors that I used could be stored in string.xml file that has to be stored inside the res/values folder or your Android project.
[sourcecode language=”xml”]
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">BiemmeIOdemo</string>
<color name="white">#FFFFFF</color>
<color name="black">#000000</color>
</resources>
[/sourcecode]
(or eventually industrial automation)
The aim of the project was to use Arduino-based boards it in a home (or even industrial) automation context.
[sourcecode language=”java”]
package com.biemme.iodemo;
public class ModbusLib {
public native static long openCom();
public native static long ReadHoldingRegisters(int fd, int id, int address, int no_of_registers,int []holdingRegs);
public native static long WriteMultipleRegisters(int fd, int id, int address, int no_of_registers,int []holdingRegs);
public native static long closeCom(int fd);
static{
System.loadLibrary("com_biemme_iodemo_ModbusLib");
}
}
[/sourcecode]