Friday, January 30, 2015

First Ever IoT Community And First Ever IoT Meetup In Sri Lanka




Colombo IoT community was founded in November, 2014 with a vision of educating and supporting innovation of IoT in Sri Lanka.

Being true to the futuristic vision of Intel, Intel Sri Lanka was the driving force behind the first ever meetup conducted by the community on 21st January 2014 as the sole sponsor of the event.
We had a packed audience of more than 200 IoT enthusiasts with both industry and academia well represented.

Introductory session was conducted by Kasun Dilunika, Tech Lead at Intel Software. Kasun was instrumental in this initiative from the very beginning and is in the process of building an IoT enabled smart switch to solve a household problem he experienced.
He explained how the evolution of internet progressed from Internet of Places, Internet of People and finally to Internet of Things, with 40 billion devices expected to be connected by Year 2020.
Microcontrollers and sensors are the two main driving forces of IoT. There are many to choose from with different capabilities and varying sizes. Want to connect your house keys to internet, anyone?
Kasun answered a question that was surely in the minds of many who attended the meetup. There were home automation solutions etc years back. Isn’t it the same old thing that has come in a new dress as IoT?

IoT devices have 3 main capabilities; Control, Compute and Communication. We call them 3C (ccc) devices. Unlike their pre-IoT era siblings, today’s IoT devices have all 3Cs as a single unit in a small form factor.  Earlier devices mainly lacked in communication aspect. With Raspberry PI, Arduino, Intel Edison and Galileo, programming to hardware has become a child’s play. (We’re serious! Check this out http://www.raspberrypi.org/resources/make/)

Come up with an idea, it was said. It doesn’t have to be world changing, mind boggling complete with a nuclear reactor and a particle splitter. Kasun explained how our friend Jagath Makumbura came up with an idea for a Smart Susan after having chicken gravy spilt on him when using the turn table (lazy Susan) at a Chinese restaurant. Jagath’s Smart Susan was also on display during the meetup.


We’re sure many young minds were inspired by Kasun when he invited everyone to come up with their own project ideas and insisted on the fact that those who do will be guided by the Colombo IoT community. We truly hope that it will ultimately result in a product with “Made in Sri Lanka” label on it.
It was exciting news for the potential IoT innovators that 5 best IoT project proposals will be given Intel® Galileo Gen 2 microcontroller boards each.

Chamara Ratnaweera (Senior Architect at Intel Software), Chandana Kithalagama (Senior Architect at Intel Software) & Banuka Amarasinghe (Senior Software Engineer at Intel Software) presented “Kumana IoT Project”, a unique demonstration of IoT in a practical scenario.
Kumana is a national park in Sri Lanka which was severely affected by drought in 2014. Many animals were dying due to the lack of drinking water. Efforts taken by park authorities to deliver water to water holes from bowsers were not very effective. An initiative was taken by Intel Sri Lanka employees along with many other environmental enthusiasts to provide drinking water to the animals. Idea was to install a solar powered water pump to draw water from a tube well and fill a watering hole with it.

It was completed successfully. We were happy, animals were happy; but there was a small problem! The equipment was located in a remote area inside the thick jungle and there was no way of getting any information on how it was functioning. We didn’t want the water to be pumped when it was raining, we wanted to know if any of the equipment was malfunctioning and we knew Sri Lankan elephants are known to attack anything foreign! We couldn’t possibly get an elephant to telephone us if anything goes wrong.

Chamara got the bright idea of implementing an IoT solution to solve all of the above.


 A sensor array would gather environmental data (ambient light, temperature, humidity) and other relevant data such as voltage generated by the solar panel, amperage flowing through the system, water flow rate from motor to tank and water level of the main water hole for animals.
Sensor Data Aggregator (SDA) would periodically consolidate the sensor data at a particular point of time, process them and send it to onsite controller module. An Arduino board was used due to the ease of handling low level devices, low cost and low power consumption.
It was decided to use SMS as the communication channel since the onsite deployment is in a remote area.

Handling communication by sending out the data SMS and receiving and processing control SMS and also issuing control signals to the motor is the responsibility of Motor Control Module. A Raspberry Pi would run the show in this module and the decision to use a Pi was inspired by the fact that it’s better at working with high level languages like Python.
Central Processing Unit (CPU) hosted in cloud complete with an analytical module decided when to send a control signal to stop/start the motor and when to alert the stakeholders of a possible breakdown.

CPU provided an API to extract data and control the system.
A fancy UI application called the API exposed by the CPU and displayed it in a nice dashboard and provided control functionality, and of course you could access it via the internet on whatever smart device you choose to view it from.

Chamara, Banuka, Jagath, Sachindra, Chandana, Buddhi, Madhura, Kushan and many others contributed in successfully building up the prototype that was on display during the meetup.
Aruna Dissanayaka, Director of Engineering at Intel Software, took the audience on a global tour in his key note speech, highlighting on the need for IoT in major cities to cater their booming population. He explained that having enough IP addresses is not sufficient for the exponential growth in IoT devices. Standards would have to adapt, for example IP stack doesn’t suit most IoT devices.
Kolitha Ratwatte, General Manager, Intel Software Sri Lanka, whose immense support helped make this event a success, concluded the meetup speaking about the way forward for Colombo IoT community.

A Q&A session followed with many interesting questions raised by the audience.

This is the story of the first ever IoT meetup in Sri Lanka sponsored by Intel. It might be a small step, but as they say “A journey of a thousand miles must begins with a single step.”

Tuesday, January 20, 2015

Sending and receiving SMS using your dongle

I've almost forgotten I had a blog :|

I had to keep the following somewhere other than my memory since I know how unreliable it is.

There was a need to send and receive SMS for an app and people were wondering if we need to buy a separate GSM modem for that. Easier solution was to use a dongle (which is, well, basically a GSM modem J )

After a little research I thought of using SMSLib as recommended by many folks on internet. (http://smslib.org/)

SMSLib is a SMS messaging library. As mentioned in their home page two SMSLib versions currently available. The old, stable v3.x and the newer v4. SMSLib v4 is under development.

I used smslib-3.5.4.jar.

Downloads can be found at http://smslib.org/download/. Some of the paths given for artifacts in installation directory are not working, so I used the downloads page.

Used
  • HUAWEI Mobile HSPA+ dongle
  • Win64
  • smslib-3.5.4
  • java 7

1. Plug in dongle to USB port and find port number (Control Panel -> Device Manage -> Ports) It was COM11 in my case
2. Add RXTXcomm library as mentioned below
3. Create a sample java project and add smslib-3.5.4.jar in lib
4. Run the following java code. (Update port)

import org.smslib.*;
import org.smslib.modem.SerialModemGateway;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


public class SmsSenderReceiver {

    public void sendMessage() throws Exception {

        SerialModemGateway gateway = new SerialModemGateway("", "COM11", 9600, "", "");
        gateway.setInbound(true);
        gateway.setOutbound(true);

        OutboundNotification outboundNotification = new OutboundNotification();
        InboundNotification inboundNotification = new InboundNotification();

        Service service = Service.getInstance();
        service.setOutboundMessageNotification(outboundNotification);
        service.setInboundMessageNotification(inboundNotification);
        service.addGateway(gateway);
        service.startService();

        OutboundMessage msg = new OutboundMessage("0722452122", "test111");
        service.sendMessage(msg);
    }

    public class InboundNotification implements IInboundMessageNotification {
        @Override
//Get triggered when a SMS is received
        public void process(AGateway gateway, Message.MessageTypes messageTypes, InboundMessage inboundMessage) {

            System.out.println(inboundMessage);
            try {
                gateway.deleteMessage(inboundMessage);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public class OutboundNotification implements IOutboundMessageNotification {

//Get triggered when a SMS is sent
        public void process(AGateway gateway, OutboundMessage outboundMessage) {
            System.out.println(outboundMessage);
        }
    }

    public static void main(String args[]) {
        SmsSenderReceiver app = new SmsSenderReceiver();
        try {
            app.sendMessage();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

A prerequisite of using SMSLib is having Java Communications library as mentioned in http://smslib.org/doc/installation/.
They list two options

1.  Win32 :-> Java Comm v2
2.  For others :-> Java Comm v3 or RxTx

Java Comm didn't work for me. Use RxTx.


(Above are found at http://smslib.org/download/)

File RXTXcomm.jar should go under JDKDIR/jre/lib/ext/
The necessary library (e.g.the librxtxSerial.so) should go under JDKDIR/jre/bin/


In Linux
Tried to do the same in Linux environment, but unfortunately I kept receiving NoSuchPortException.

Tailed /var/log/messages to view updates.

Device was first identified as "usb-storage". usb_modeswitch was used

usb_modeswitch -H -v 0x12d1 -p 0x1506

View changes.
lsusb

View the port numbers and their permissions. Can see the ports that are mapped to GSM modem.
ls /dev/tty* -l

Added read write permissions
chmod o+rw /dev/ttyUSB*
chmod o+rw /dev/ttyS*

Can use minicom to see if the dongle is functioning as expected.

java.lang.UnsatisfiedLinkError: no rxtxSerial in java.library.path thrown while loading gnu.io.RXTXCommDriver
Exception in thread "Thread-3" java.lang.UnsatisfiedLinkError: no rxtxSerial in java.library.path
               at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1886)

librxtxSerial.so had to be added to JDKDIR/jre/lib/amd64 instead of  JDKDIR/jre/bin/ to solve the above.

As suggested in http://smslib.org/doc/smslib/troubleshooting/ added the soft links to something resembling a standard serial port.
ln -s /dev/ttyUSB_utps_diag /dev/ttyS20
ln -s /dev/ttyUSB_utps_modem /dev/ttyS21
ln -s /dev/ttyUSB_utps_pcui /dev/ttyS23

The above hack and more useful infor can be found at http://rxtx.qbang.org/wiki/index.php/Trouble_shooting too.
View "How does rxtx detect ports? Can I override it?" Section.

Kept on getting the following warning too.

check_group_uucp(): error testing lock file creation Error details:Permission deniedcheck_lock_status: No permission to create lock file.
please see: How can I use Lock Files with rxtx? in INSTALL

Adding my user to group uucp didn’t solve it L

usermod -aG uucp myUser

vim /etc/group

Sunday, September 29, 2013

Fuse ESB : Email Attachement Issue

I faced a very annoying issue when I tried to send an email with attachments using Fuse ESB. I created a simple project in Fuse IDE which sent a mail with attachments and it was working fine as a stand alone application but when the bundle was deployed in Fuse it was not sending the mail when there are attachments. There was no exception in logs, but then it was found that there is actually an exception and it was getting set to the exchange.

javax.activation.UnsupportedDataTypeException: no object DCH for MIME type multipart/mixed;

Many have come across this issue when dealing with javamail in other containers and for most of them it was because javamail jar was not found by the container.

Some solutions that have worked were found in this.
http://blog.hpxn.net/2009/12/02/tomcat-java-6-and-javamail-cant-load-dch/

But unfortunately it didn't work for me. 

javax.activation package exposed by the jre was not working. Removing it and adding an OSGI friendly version of this package created by Servicemix resolved the issue.

Solution
  1. Comment out the javax.activation export from jre.properties
  2. Install following bundle instead. install mvn:org.apache.servicemix.specs/org.apache.servicemix.specs.activation-api-1.1/2.2.0

Thursday, August 22, 2013

Entity-Attribute-Value

In the DB schema of a previous project (which was a 10 year old legacy system), there was a table with a set of attributes listed as rows instead of columns as shown below.


When asked why, it was told that there was a client request to add additional parameters to the product entity, but adding a new column was not feasible since the table contained millions of records. Instead they opted for this solution as a workaround. Two new tables were added and the "Product_Config_Param_Def" contained the new parameter definitions and "Product_Config_Param_Value" contained the corresponding values. This was flexible and allowed adding many more parameters without having to add columns. And the plus point is when there are products that don't have the entire set of parameters, we don't need to populate DB with nulls.

This is what is known as "Entity-Attribute-Value" Modeling (EAV) and it's a great way for us to get closer to the benefits offered by No SQL DBS with our relational DBs. No SQL DB of course offer us unlimited flexibility in adding dynamic attributes and is the way to go if our entities have large amount of varying attributes (In other words, when entities have different sets of attributes) and the relationships between entities is rather limited (Think FB accounts).

With EAV, data is modeled as attribute-value pairs.

EAV should be used with caution. Many identify it as an anti-pattern. It can be such a performance hit. If I may copy the example mentioned in http://tonyandrews.blogspot.com/2004/10/otlt-and-eav-two-big-design-mistakes.html

With no EAV:

select ename from emp where job=’CLERK’ and sal < 2000;

With EAV:

select ev1.name
from emp_values ev1, emp_values ev2 emp_values ev3
where ev1.code = ‘NAME’
and ev1.empno = ev2.empno
and ev2.code = ‘JOB’
and ev2.value = ‘CLERK’
and ev1.empno = ev3.empno
and ev3.code = ‘SAL’
and TO_NUMBER(ev3.value) < 2000;

Sunday, August 4, 2013

Camel-CXF: Java-First Service Implementation

Summarized from the documentation.

Web service interface is defined using a Service Endpoint Interface (SEI) and the WSDL is generated from it.

Steps
1. Implement & annotate the SEI.
2. Implement other requisite Java classes.
        - Any data types referenced by the SEI.
        - The implementation of the SEI. 3. Instantiate the Web service endpoint.
4. Generate the WSDL

1. Implement & annotate the SEI.

SEI is used as the;
• Base type of the Web service implementation (server side)—you define the Web service by implementing the SEI.
• Proxy type (client side)—on the client side, you use the SEI to invoke operations on the client proxy object.
• Basis for generating the WSDL contract—in the Java-first approach, you generate the WSDL contract by converting the SEI to WSDL.

In order to use the standard JAX-WS frontend, the SEI must be annotated with the @WebService annotation. Annotations are also used to customize the mapping from Java to WSDL.

import javax.jws.WebParam;
import javax.jws.WebService;

@WebService(targetNamespace = "http://demo.fusesource.com/wsdl/CustomerService/", name = "CustomerService", serviceName = "CustomerService", portName = "SOAPOverHTTP")
public interface CustomerService {
 public com.fusesource.demo.customer.Customer lookupCustomer(
   @WebParam(name = "customerId", targetNamespace = "") java.lang.String customerId);

 public void updateCustomer(
   @WebParam(name = "cust", targetNamespace = "") com.fusesource.demo.customer.Customer cust);

 public void getCustomerStatus(
   @WebParam(name = "customerId", targetNamespace = "") java.lang.String customerId,
   @WebParam(mode = WebParam.Mode.OUT, name = "status", targetNamespace = "") javax.xml.ws.Holder status,
   @WebParam(mode = WebParam.Mode.OUT, name = "statusMessage", targetNamespace = "") javax.xml.ws.Holder statusMessage);
}

2. Implement other requisite Java classes.

When you run the Java-to-WSDL compiler on the SEI, it converts not only the SEI, but also the classes referenced as parameters or return values. The parameter types must be convertible to XML, otherwise it would not be possible for WSDL operations to send or to receive those data types. In fact, when you run the Java-to-WSDL compiler, it is typically necessary to convert an entire tree of related classes to XML using the standard JAX-B encoding.

Each related class must have a default constructor .

public class Customer {
 protected String firstName;
 protected String lastName;
 protected String phoneNumber;
 protected String id;

 // Default constructor, required by JAX-WS
 public Customer() {
 }

 public Customer(String firstName, String lastName, String phoneNumber,
   String id) {
  super();
  this.firstName = firstName;
  this.lastName = lastName;
  this.phoneNumber = phoneNumber;
  this.id = id;
 }

 public String getFirstName() {
  return firstName;
 }

 public void setFirstName(String value) {
  this.firstName = value;
 }

 public String getLastName() {
  return lastName;
 }

 public void setLastName(String value) {
  this.lastName = value;
 }

 public String getPhoneNumber() {
  return phoneNumber;
 }

 public void setPhoneNumber(String value) {
  this.phoneNumber = value;
 }

 public String getId() {
  return id;
 }

 public void setId(String value) {
  this.id = value;
 }
}

3. Instantiate the Web service endpoint.

In Apache CXF, you create a WS endpoint by defining a jaxws:endpoint element in XML. The WS endpoint is effectively the runtime representation of the Web service: it opens an IP port to listen for SOAP/HTTP requests, is responsible for marshalling and unmarshalling messages (making use of the
generated Java stub code), and routes incoming requests to the relevant methods on the implementor class.

jaxws:endpoint => To integrate a WS endpoint with a Java implementation class.
cxf:cxfEndpoint => To integrate a WS endpoint with a Camel route.

Creating a Web service in Spring XML consists of the following two steps:
1. Create an instance of the implementor class, using the Spring bean element.
2. Create a WS endpoint, using the jaxws:endpoint element.


 
 
 
 
 
 
 

4. Generate the WSDL

To generate a WSDL contract from your SEI, you can use either the java2ws command-line utility or the cxf-java2ws-plugin Maven plug-in. Plug-in approached is given below.

Add plugin to the POM .


 ...
 
  2.4.2-fuse-00-05
 
 
  install
  
   ...
   
    org.apache.cxf
    cxf-java2ws-plugin
    ${cxf.version}
    
     
      org.apache.cxf
      cxf-rt-frontend-jaxws
      ${cxf.version}
     
     
      org.apache.cxf
      cxf-rt-frontend-simple
      ${cxf.version}
     
    
    
     
      process-classes
      process-classes
      
       org.fusesource.demo.camelcxf.ws.server.CustomerService
       
       
        ${basedir}/../src/main/resources/wsdl/CustomerService.wsdl
       
       true
       true
      
      
       java2ws