Sample Code for lettest version of SDK
# | Date | Name | Download |
---|---|---|---|
1 | 01/20/2020 | Symmetric Key Sample -v2.0.0 | Download |
2 | 01/20/2020 | Symmetric Key Gateway Sample -v2.0.0 | Download |
3 | 01/20/2020 | Self Sign Sample -v2.0.0 | Download |
4 | 01/20/2020 | Self Sign Gateway Sample -v2.0.0 | Download |
5 | 01/20/2020 | CA Sign Sample -v2.0.0 | Download |
6 | 01/20/2020 | CA Sign Gateway Sample -v2.0.0 | Download |
How to run the JAVA sample code?
Prerequisite tools
- Java 8 (or above )
- Eclipse IDE
Installation
- Extract the “iotconnect-sdk-java-v2.0.0.zip” (You can download SDK file from here)
- Open Eclipse and create a new JAVA project.
- Create lib folder inside the newly created project.
- Put JAR(You can get JAR file from iotconnect-sdk-java-v2.0.0.zip) file inside lib
- Put config.properties file inside the project(You can get properties file from iotconnect-sdk-java-v2.0.0.zip).
- Configure JAR file to the project.Right-click on the project and click on Build Path and then click on Configure Build Path.
- Click on Java Build Path and then go to Libraries Tab and then click on Add JARs
- Now Select sdk-2.0.jar from lib folder and then click on OK button.
- Create new Java Class file inside src folder(You can get sample file(Sample.java) code from iotconnect-sdk-java-v2.0.0.zip).
Public Method and Implementations
Prerequisite input data
uniqueId = <<uniqueId>>; //It's your device uniqueId cpid = <<CPID>>; // You can get it from portal with admin user env = <<env>>;
Create a new to implement callback interface SDKCallback. You can use “Sample.java” file as a reference that is available in the extracted zip file.
//Callback class class CBImplementor implements SDKCallback, TwinCallback { //Implement a method to receive the command from Cloud @Override public void sdkCallbackMethod(CloudResponse cloudResponse) { if(cloudResponse!=null) { System.out.println("ACK : " + cloudResponse.getAck()); System.out.println("ACK ID : " + cloudResponse.getAckId()); System.out.println("Command : " + cloudResponse.getCommand()); System.out.println("UniqueId : " + cloudResponse.getUniqueId()); System.out.println("Value : " + cloudResponse.getValue()); } } @Override public void twinCallBackMethod(Map<String, String> arg0) { if(arg0.size() > 0){ System.out.println(arg0.toString()); System.out.println("Twin Property Updated"); } } }
Create SDK instance using prerequisite date and connect to IoTConnect
//Create iotconnect sdk instance IoTConnectSDK ioTConnectSDK = new IoTConnectSDKImpl(cpId, uniqueId, env, callerClassObject, callerClassObject);
To send the data from Device To Cloud(D2C)
//Date time will be in ISO 8601 format. Example: YYYY-MM-DDTHH:MM:SS.sssZ ioTConnectSDK.SendData("[{'data':{'humidity':'1211'}, 'time':'2020-02-28T10:49:37.658Z','uniqueId':'<<uniqueId>>'}]")
SDK Configuration
- “config.properties” is required to give additional information regarding certificates
- You can find “config.properties” in sample folder i.e. “sample/config.properties”, if SDK doesn’t find this file then it will give “Config file not found” error. Means you are missing this file at root location in case you are integrating with firmware
- Standard file format for the config.properties file is as below:
filePath= <filepath> caFile= <filename>/ca.cert.pem clientCrtFile= <filename>/cert..pem clientKeyFile= <filename>/key.pem clientKeyPassword= <password>
- If your device is using certificate base authentication then you have to provide bellow information else you can keep it blank. Make sure each file path are valid and accessible:
- caFile : Root server CA certificate file path
- clientCrtFile : Device certificate file path
- clientKeyFile : Device key file path
- clientKeyPassword : Key password
NOTE: It will cause an issue if property name got changed so please make sure none of the property get renamed
To update the Twin Property
Map<String,String> twinPropertyMap = new HashMap<>(); twinPropertyMap.put("humidity", "100"); ioTConnectSDK.updateTwin(twinPropertyMap);
Sample code SDK integration
//Import iotconnect package package sdk; //Import required classes import sdk.dao.d2c.CloudResponse; import sdk.service.IoTConnectSDK; import sdk.service.IoTConnectSDKImpl; import sdk.service.SDKCallback; //Sample Class public class demo { public static void main(String[] args) throws Exception { CBImplementor cb = new CBImplementor(); String env = "<<env>>"; //Replace your environment here String uniqueId = "<<uniqueId>>"; //Replace device uniqueid here String cpId = "<<cpid>>"; //Replace CPID here IoTConnectSDK ioTConnectSDK = new IoTConnectSDKImpl(cpId, uniqueId, env, cb, cb); ioTConnectSDK.sendData("[{'data':{'humidity':'1211'}, 'time':'2020-02-28T10:49:37.658Z','uniqueId':'<<uniqueId>>'}]"); // Replace device uniqueid here } } //Callback class class CBImplementor implements SDKCallback, TwinCallback { @Override public void sdkCallbackMethod(CloudResponse cloudResponse) { if(cloudResponse!=null) { System.out.println("ACK : " + cloudResponse.getAck()); System.out.println("ACK ID : " + cloudResponse.getAckId()); System.out.println("Command : " + cloudResponse.getCommand()); System.out.println("UniqueId : " + cloudResponse.getUniqueId()); System.out.println("Value : " + cloudResponse.getValue()); } } @Override public void twinCallBackMethod(Map<String, String> arg0) { if(arg0.size() > 0){ System.out.println(arg0.toString()); System.out.println("Twin Property Updated"); } } }
Troubleshooting
Device not found
Message: ERROR sdk.service.IoTConnectSDKImpl – Device not found
Reason: Device not registered on IoTConnect cloud
Solution:
– It should be valid and respective to the entered CPID and ENV
– The device should be registered with entered uniqueId on IoTConnect cloud respective to the selected CPID and ENV
– If your device name is wrong then you need to stop the script and run again after uniqueId correction.
CPID not found
Message: ERROR sdk.service.IoTConnectSDKImpl – Unidentified Error
Reason: Wrong CPID entered
Solution:
– CPID should be valid. You can take it from here (Reference link to get cpid)
– Check the combination of CPID and ENV (IoTConnect platform environment)
Device not registered
Message: ERROR sdk.service.IoTConnectSDKImpl – Device not registered
Reason: Device not registered on cloud
Solution:
– If your uniqueId is right and till you getting the above error then verify below process
– It should be registered on IoTConnect Platform respective to the selected CPID and ENV
– Its status should be “Acquired” (registered on the cloud)