eHouse Building Automation System – Android software to receive the status of controllers via UDP

Ethernet Home Automation eHouse allow you to automatically send a status broadcastu UDP (User Datagram Protocol) , which can receive all the devices in your LAN or WiFi and IP satisfying the condition – Found in the local mask 255.255.255.0, which is broadcast off the status the IP addresses of the controllers 192.168.0.x .

UDP Broadcast allows you to disable the download status of the TCP/IP client , which requires a connection to the controller.UDP Broadcast is a connectionless protocol , for propagation data and regardless of the number of the receiving devices does not load more the CPU or network links.

Simple UDP broadcast reception function with smartphones , panels and Android tablets is shown below.
It is therefore an infinite loop on the basis of this function, you need to create a thread (THREAD) working in the background , giving it the lowest priority work .

Otherwise, the execution of this function block the execution of other activities of the program .

The source code for receiving UDP Broadcast android platform:

public void udp(int port) // parameter port to pick up the default broadcast 6789 – status in binary controller

{

byte[] broadcastadr = new byte[4];

byte[] localadr = new byte[4];

WifiManager wifi = (WifiManager)getSystemService(Context.WIFI_SERVICE); // Wifi manager

DhcpInfo dhcp = wifi.getDhcpInfo(); // Get info from wifi dhcp

if (dhcp == null) // get default value is disabled if DHCP network of eHouse

{// No dhcp or an error for the device

broadcastadr[0]=(byte)192;

broadcastadr[1]=(byte)168;

broadcastadr[2]=(byte)0;

broadcastadr[3]=(byte)255;

}

else // DHCP

{

broadcast = int (dhcp.IPAddress & ; dhcp.netmask) | ~dhcp.netmask; // For udp broadcast – If you want to broadcast the data on the function
controller only receives status

for (int k=0 ; k<4; k++) broadcastadr[k]=(byte)((broadcast>>k*8)&0xFF);

local int=(dhcp.ipaddress); // Local ip address dhcp

for (int k=0;k<4;k++) localadr[k]=(byte)((local>>k*8)&0xFF); // For local ip address

}

try

{

ds = new DatagramSocket(port) ; /// Listening on udp port data from any ip

inside a mask – udp socket to create an instance of the local port number

}

catch (Exception e)

{

// Ehousecommunication.l(” Error Inet: get address ” + E.getMessage()); for test

}

try

{

ds.setBroadcast(true); // Enable send broadcast message – for transmission

ds.setReuseAddress (true) ; // Enable multiple socket use the same IP address

// Ds.setSoTimeout (400) ; // Reception timeout

}

catch (Exception io)

{// Ignore errors but we protect against application and system errors

}

// * Udp broadcast TEST – broadcastu test udp broadcast from your phone or tablet – future communication with the application of

/ * Try

{

DatagramPacket dd=new DatagramPacket(“Hello Word from Android UPD TEST “.getBytes() ,0,”Hello Word from Android UPD TEST”.length() ,

InetAddress.getByAddress(broadcastadr),port) ;

ds.send (dd) ; // Sends a DatagramPacket

}

catch(Exception e)

{

ehousecommunication.l ( ” Transmit Datagram packet error: ” + E . getMessage ()) ;

}

* /

MulticastLock ml = wifi.createMulticastLock(“Multicast enabled lock”) ;

// Create an instance of the block multicast and broadcastu

ml.Acquire() ; // Multicast lock enable udp broadcast reception – It can be disabled Certain Android smartphones for batery saving Purposes –
broadcastu UDP support activates the phone if it is locked and only disabled

while (!ehousecommunication.Terminate) // infinite loop for udp reception

{// Infinite loop receiving udp status

DatagramPacket dp = new DatagramPacket(message,message.length); // Create udp datagram packet buffer using data – makes use of the UDP packet
buffer ” message ”

try {

ds.receive (dp) ; // Udp packet reception from eHouse system – receives the packet udp port of the UDP a socket

if (dp.getLength()>0) // if the size of parcels greater than zero – something received

{

EhouseTCP.QueryBuff = dp.getData(); // Assign a udp broadcast reception query buffer – loaded into the buffer status eHouse

// Dp.setLength(0); – packet size is set to zero so as not to process the same data repeatedly

EhouseTCP.QueryReceived(); // Decode query data arrays and fill devices – decodes the status of controllers and loads the application status table

}

}

catch (Exception os)

{// Ignore errors

// Ehousecommunication.l ( “asdfasdfsafsa ” + Os . getMessage ()) ; // For test only

}

}

ml.release() ; // Multicast udp lock free for reception of eHouse application – releases the lock multicast

ds.close() ; // Close the datagram socket at the end – closes the socket at the end of

}

Multicast lock may be required in some Android devices .

Local Broadcast UDP in the case of mobile phones can be enabled or not depending on the mobile phone firmware .

It can also be permanently disabled for battery saving and for safety reasons or to reduce the CPU load on the network at work .
(Note the kernel configuration: CONFIG_IP_MULTICAST is not set) .

In addition, some models can be blocked by a local firewall and need to activate the transmission on a given port (default 6789) .

Outside Broadcast when using GPRS Internet connections , EDGE , 3G , 4G can be locked or unlocked by the network operator . More often, however, should be expected , that it is fixed in order to minimize the utilization of the link .

In some models of mobile phones , Software implementation status of controllers receiving UDP can be very difficult or even impossible . May require the use of special firmware non branding or smartphone .

In addition, care must be taken when upgrade firmware and check that all features are working properly out of the box , otherwise the diagnosis of the errors will be very difficult .

Based on some HTC phones and other products containing the Android operating system version lower than 2.3.3 observed a total block UDP reception broadcastu .

This was the version of android (2.1-2.3 of the period of 01.01.2010-14.06.2011)

If you can not receive UDP broadcastu download and install , New Android ROM version of the minimum 2.3.3 (June 14, 2011) , which tested the correctness of the work load UDP broadcast .

Kernel Version 2.6.35.10-htcg3ef43272-kernel@and18-2#1.

No.3software.14.405.1.

For other brands of phones with similar problems, please download Firmware Android Rom from the manufacturer, version of at least 2.3.3.

Note also the essential characteristics of UDP Packets .

They are asynchronous and require reading about what may cause data read errors , their loss (shedding), especially with slow equipment , links , combine high load and CPU occupation SmartPhones and tablets .
Udp status data is equipped in check-sum to check validity of data.

Home Automation Producer eHouse Home Automation eHouse

eHouse Controllers – Home Automation eHouse