Smart Light SDK

From Atomation's Wiki
Jump to: navigation, search

Atomation’s mobile SDK is already integrated with Atomation's Hardware SDK wrapping it with a friendly API. The SDK will let you take advantage of all features included in the Hardware SDK (i.e. IMU, Temperature, Humidity, ADC read, RGB Led lighting, Motor controller, Transistors, Relays and more!). All can be easily controlled and the specific functions are up to you.

Getting Atomation's SDK

Register

Visit the Atomation website and click on Download SDK tab.

Login at Atomation

After registration, Login at the Atomation website and reach the Admin. On the Admin Home Page, you will find instructions and guidelines.

Add your Application

Once you have logged in you should start defining your first app:

Name 
Your application's name.
Description 
A brief description of your application.
Package 
The name of the application package (from the IDE).

Adding the SDK and Setting the Token

Add the following line to Module-level /app/build.gradle before dependencies:

repositories {
    jcenter()
}

Add the compile dependency in the build.gradle file:

 compile 'net.atomation.smartlight:sdk:1.0.0'

Add a meta-data element to the application element in the AndroidManifest.xml file:

<application android:label="@string/app_name" ...>
    ...
    <meta-data android:name="net.atomation.Token" android:value="ExampleToken" />
    ...
</application>

The meta-data value (ExampleToken) is Atomation's Token. The Token can be found in the center column in My Applications.

Basics

Atom Nucleus (the controller of the HW SDK) uses BLE to communicate with the mobile device. To work with this protocol you need to know and understand a few terms:

Central 
The Mobile device that handles the communication.
Peripheral 
The Hardware SDK.
MAC Address 
A six byte hex number which represents a physical address of the peripheral, two peripherals cannot share the same MAC Address.
RSSI 
Received Signal Strength Indicator, how powerful a signal is, measured in Dbm.
Advertise 
A state that the peripheral advertises itself when not connected.
Device Name 
The name advertised by the peripheral.
Write 
The way to send information to the peripheral device.
Read 
The way to get data once (for each read) from the peripheral.

Initialize Atomation's SDK

The Atomation SDK is initialized by using a simple method:

public static void init(android.content.Context context, net.atomation.atomationsdk.api.ISdkInitializationListener listener)

For example:

AtomationSdk.init(this.getApplicationContext(), new ISdkInitializationListener() {
            @Override
            public void onInitialized() {
                //do something
            }

            @Override
            public void onError(ErrorFactory.AtomationError error) {
                //do something
            }
        });

We recommend to implement the initialization in App.onCreate().

Creating Your First Atom

Atoms are objects that represent the Atom Nucleus and any additional skins. To create your Atom use the following methods:

public static net.atomation.atomationsdk.api.IRgbAtom getAtom(String macAddress)
public static net.atomation.atomationsdk.api.IRgbAtom createAtom(String deviceAddress)

The "macAddress" parameter is a String that represents the MAC address of the desired peripheral. For this example, we will set the MAC address of the peripheral to 80:30:DC:0C:EA:A6

macAddress = "80:30:DC:0C:EA:A6";

The implementation should be as follows:

IRgbAtom atom = AtomationRgbAtomManager.getAtom(macAddress);
if (atom == null) {
       atom = AtomationRgbAtomManager.createAtom(macAddress);
  }

Connecting to the Atom

There are two steps to connect to the Atom:

  1. Create a listener that implements the SDK's IConnectionStateListener.
  2. Send that listener to the the Atom's method "connect" as follows:
atom.connect(new MyConnectionStateListener());
   public class MyConnectionStateListener implements IConnectionStateListener{

        @Override
        public void onDeviceConnected() {
            doWhatIWantToDoOnConnection()
        }

        @Override
        public void onDeviceDisconnect() {
            doWhenDisconnec()

        }

        @Override
        public void onConnectionError(ErrorFactory.AtomationError error) {
            keepCalmAndReportError()
        }
    }

To disconnect, use the method "disconnect" in the same way as the "connect" method.

Using the Atom on Connection

After connecting to the Atom, you can control the LED colors by writing the desired RGB values using this method:

public abstract void writeRGB(int ledId,
                              int red,
                              int green,
                              int blue,
                              net.atomation.atomationsdk.api.IWriteListener listener)

The argument "ledId" is:
IRgbAtom.AtomationLedId.LED_1
IRgbAtom.AtomationLedId.LED_2
Use the first one to control the first LED, and the second one for the others.

"IWriteListener" is another listener provided by the Atomation SDK.

A possible implementation can be:

 atom.writeRGB(IRgbAtom.AtomationLedId.LED_2, 0x00, 0x00, 0xFF, new IWriteListener() {
                    @Override
                    public void onWriteExecuted() {
                        //do something
                    }

                    @Override
                    public void onWriteError(ErrorFactory.AtomationError error) {
                        //do something
                    }
                });