Skip to content
This repository has been archived by the owner on Jul 28, 2021. It is now read-only.
/ iot-java Public archive

Client libraries and samples for connecting to IBM Watson IoT using Java

License

Notifications You must be signed in to change notification settings

ibm-watson-iot/iot-java

Repository files navigation

Java for IBM Watson IoT Platform

Build Status Coverage Status GitHub issues GitHub Maven Central

Product Withdrawal Notice

Per the September 8, 2020 announcement IBM Watson IoT Platform (5900-A0N) has been withdrawn from marketing effective December 9, 2020. As a result, updates to this project will be limited.

Overview

Documentation

https://ibm-watson-iot.github.io/iot-java/

Usage

Add a dependency on com.ibm.wiotp.sdk to your project's pom.xml:

<dependency>
  <groupId>com.ibm.wiotp</groupId>
  <artifactId>com.ibm.wiotp.sdk</artifactId>
  <version>0.4.1</version>
</dependency>

Applications

Simple example that initialises an application client from environment variables, connects, sends 1 command to a device, and then disconnects.

import com.google.gson.JsonObject;
import com.ibm.wiotp.sdk.app.ApplicationClient;
import com.ibm.wiotp.sdk.codecs.JsonCodec;

class simpleTest {
	public static void main(String[] args) {
		ApplicationClient appClient = new ApplicationClient();
		appClient.registerCodec(new JsonCodec());
		appClient.connect();
		JsonObject data = new JsonObject();
		data.addProperty("distance", 10);
		appClient.publishCommand("myDeviceType", "myDeviceId", "myCommand", data);
		appClient.disconnect();
	}
}

Devices

Simple example that initialises a device client from environment variables, connects, sends 1 event, and then disconnects.

import com.google.gson.JsonObject;
import com.ibm.wiotp.sdk.device.DeviceClient;
import com.ibm.wiotp.sdk.codecs.JsonCodec;

class simpleTest {
	public static void main(String[] args) {
		DeviceClient deviceClient = new DeviceClient();
		deviceClient.registerCodec(new JsonCodec());
		deviceClient.connect();
		JsonObject data = new JsonObject();
		data.addProperty("distance", 10);
		deviceClient.publishEvent("myEvent", data);
		deviceClient.disconnect();
	}
}