Switch to desktop version  

Ewon TechForum
Providing technical solutions to technical requests



Connect your Flexy to AWS IoT through MQTT using JAVA
ntnunk
Junior Member
#1
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();
    }

}
Reply

simon
eWON Support
#2
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


Attached Files
  MQTT_AWS.zip (Size: 9,87 KB / Downloads: 203)
Reply

michauxb@helha.be
Junior Member
#3
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.
Reply




Possibly Related Threads…
12-03-2025, 11:19 PM
Last Post: simon
27-09-2024, 04:38 PM
Last Post: simon
  MQTT tls url check Started by davide
1 Replies - 1.539 Views
20-06-2024, 01:38 PM
Last Post: simon
  MQTT disconnection Started by Chris_M1
1 Replies - 1.194 Views
06-10-2023, 06:08 PM
Last Post: simon
04-08-2023, 09:20 AM
Last Post: simon
04-07-2023, 03:42 PM
Last Post: simon
04-05-2023, 05:18 PM
Last Post: simon
28-04-2023, 06:19 PM
Last Post: simon
16-03-2023, 11:03 AM
Last Post: ofitz
22-02-2023, 03:42 PM
Last Post: zconverse



Users browsing this thread:
1 Guest(s)



Theme © Ewon 2019 - Forum software by © MyBB - Cookie policy