Switch to desktop version  
Connect your Flexy to AWS IoT through MQTT using JAVA - Printable Version

+- Ewon Technical Forum (https://techforum.ewon.biz)
+-- Forum: Development (https://techforum.ewon.biz/forum-50.html)
+--- Forum: Java (https://techforum.ewon.biz/forum-53.html)
+--- Thread: Connect your Flexy to AWS IoT through MQTT using JAVA (/thread-583.html)



Connect your Flexy to AWS IoT through MQTT using JAVA - ntnunk - 20-04-2018

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!


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();
    }

}



RE: New Java ETK and AWS IoT Example - simon - 24-04-2018

Noel,

Thanks for your example !

Here is another example. Indeed this example is based on the ETK 1.4.2 and the Eclipse IDE : See https://developer.ewon.biz/content/java-0

By the way, if someone starts with Amazon Web Services IOT, I suggest to read this : https://techforum.ewon.biz/attachment.php?aid=310
This explains how to connect a Flexy to AWS IOT but using BASIC.  The actions you need to do for creating a device, getting the certificates... in AWS IOT are the same.


Simon


RE: Connect your Flexy to AWS IoT through MQTT using JAVA - michauxb@helha.be - 18-12-2022

This thread has been marked as solved. If you have a similar issue, it would be better to post your own thread rather than bump this one, to help keep everybody's different issues separate.