All,
An example of a simple Java application to connect and push data to AWS IoT. This code is tested and works on a Flexy 201 with the new Java ETK 1.4.2.
This application will generate 20 random integers and push them to the "ewon/test" AWS IoT topic every 10 seconds for one minute.
Hopefully this is enough for anyone who needs this functionality to get started.
Good luck!
An example of a simple Java application to connect and push data to AWS IoT. This code is tested and works on a Flexy 201 with the new Java ETK 1.4.2.
This application will generate 20 random integers and push them to the "ewon/test" AWS IoT topic every 10 seconds for one minute.
Hopefully this is enough for anyone who needs this functionality to get started.
Good luck!
Code:
import java.util.Random;
import com.ewon.ewonitf.EWException;
import com.ewon.ewonitf.MqttClient;
import com.ewon.ewonitf.MqttMessage;
public class TestMain {
class EwonMqtt extends MqttClient {
public EwonMqtt(String endpoint, String clientID) throws Exception {
super(clientID, endpoint);
this.setOption("port", "8883");
this.setOption("log", "1");
this.setOption("keepalive", "30");
this.setOption("cafile", "/usr/root-CA.crt");
this.setOption("certfile", "/usr/my.cert.pem");
this.setOption("keyfile", "/usr/my.private.key");
this.connect();
}
public void callMqttEvent(int arg0) {
}
}
public void runTest() {
EwonMqtt client = null;
try {
client = new EwonMqtt("endpoint.iot.region.amazonaws.com", "eWON_Test");
} catch (Exception e) {
e.printStackTrace();
}
String json = "";
Random r = new Random();
for(int i = 0; i < 6; i++) {
json = "{";
for(int j = 0; j < 20; j++) {
json += "\"tag" + j + "\": " + r.nextInt(1000);
if(j < 19)
json += ", ";
}
json += "}";
MqttMessage message = new MqttMessage("ewon/test", json);
try {
client.publish(message, 0, false);
} catch (EWException e) {
e.printStackTrace();
}
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
TestMain t = new TestMain();
t.runTest();
}
}