Ewon Technical Forum

Full Version: Getting Started with MQTT on your Flexy using JAVA.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Hi Guys,

Here is a JAVA project example that shows you how to publish and subscribe messages on a MQTT broker.
This example uses the MQTT broker of our ewonsupport.biz server and unencrypted communication.

This project is based on the new JAVA ETK 1.4.2 and the Eclipse IDE. See https://developer.ewon.biz/content/java-0

This is the equivalent of what is available in BASIC : https://techforum.ewon.biz/thread-473.html

See also : Connect your Flexy to AWS IoT through MQTT using JAVA

Simon
Hello Simon, hello Flexy users,

we want to use the Azure IotHub Device SDK for a Java Program running on the Flexy 205.
On the github page for the IotHubSDK (https://github.com/Azure/azure-iot-sdk-j...x-setup.md) it's stated that
"To use the SDK and run the samples you will need Java SE 8."
This means Java 1.8.x.

On the ewon programming for java page (https://developer.ewon.biz/content/java-0#dev-documents) it says
"The eWON JAVA toolkit (v1.4.2 | Release note) is designed around the J2SE (JAVA Standard Edition) technology. "

This means Java 1.4.x.

Does this mean, we should not even try to use the SDK because it will not work anyways?
Is there a known way to make it work?

If there is no way right now - when will an update be available which will enable the usage of the SDK?

Regards,
René
Hi Rene,

Yes, this SDK won't work with the Flexy.

However, you can use our MQTT apis to build your own client in BASIC (https://techforum.ewon.biz/thread-561.html) or JAVA (https://techforum.ewon.biz/thread-586.html)

Regarding the JVM update, it is something we consider for the future but most probably not on this hardware platform.

Simon
Hello Simon,

When I use the code in your zip file, as soon as the application is uploaded and started, the device reboots. Could it be a version issue? I am using JAVA ETK 1.4.4 on a flexy 205 with firmware 14.0s0.

Thanks
Hi,

Thanks for this feedback.

I just figured out that if you call "readMessages()" when the function is called for MQTT connection status changes (event = 1), the system crashes.

So an easy fix is to add
if (event == 1)
return;

in the MyMQTTClient.java class :

try {
if (event == 1)
return;

MqttMessage msg;

msg = readMessage();
if (msg != null) {
String str = new String(msg.getPayload(), "UTF-8");
System.out.println("MQTT Message Received: " + str);
msg.close();
}
} catch (Exception e) {
System.out.println("Error callMqttEvent: " + e.toString());
}


When event = 0, the event is fired for reading incoming messages. When event = 1, it is because the MQTT status changes (You can then read it with getStatus() ).

Simon
Hello ,
I have tried your code in the zip file . I have ewon flexy 102 series, firmware - 13.2s and javaetk1.4.4 (J2SE) . Now i am trying to publish the float tag in the ewon flexy using java.
In the Real Time Logs ,in the event table its showing me like :

java.lang.NoSuchMethodError: MQTT_Thread: no method readTagAsFloat with signature (Ljava/lang/StringWinkF found in referenced class com.ewon.ewonitf.IOManager or its superclasses

Is any some library is missing that i need to install ? Can you tell me where am i doing wrong.
(20-11-2019, 09:13 PM)simon Wrote: [ -> ]Hi,

Thanks for this feedback.

I just figured out that if you call "readMessages()" when the function is called for MQTT connection status changes (event = 1), the system crashes.

So an easy fix is to add
if (event == 1)
  return;

in the MyMQTTClient.java class :

try {
if (event == 1)
return;

MqttMessage msg;

msg = readMessage();
if (msg != null) {
String str = new String(msg.getPayload(), "UTF-8");
System.out.println("MQTT Message Received: " + str);
msg.close();
}
} catch (Exception e) {
System.out.println("Error callMqttEvent: " + e.toString());
}


When event = 0, the event is fired for reading incoming messages. When event = 1, it is because the MQTT status changes (You can then read it with getStatus() ).

Simon


Thanks, Simon!
(21-11-2019, 07:20 AM)Kanduri Shouri Wrote: [ -> ]Hello ,
I have tried your code in the zip file . I have ewon flexy 102 series, firmware - 13.2s and javaetk1.4.4 (J2SE) . Now i am trying to publish the float tag in the ewon flexy using java.
In the Real Time Logs ,in the event table its showing me like :

java.lang.NoSuchMethodError: MQTT_Thread: no method readTagAsFloat with signature (Ljava/lang/StringWinkF found in referenced class com.ewon.ewonitf.IOManager or its superclasses

Is any some library is missing that i need to install ? Can you tell me where am i doing wrong.

Kanduri,

This function has been implemented in ETK 1.4.4 and in firmware 14.0.(which is temporary unavailable)

Simon
Thank you simon .i got it . 

Shouri.
(21-11-2019, 07:20 AM)Kanduri Shouri Wrote: [ -> ]Hello ,
I have tried your code in the zip file . I have ewon flexy 102 series, firmware - 13.2s and javaetk1.4.4 (J2SE) . Now i am trying to publish the float tag in the ewon flexy using java.
In the Real Time Logs ,in the event table its showing me like :

java.lang.NoSuchMethodError: MQTT_Thread: no method readTagAsFloat with signature (Ljava/lang/StringWinkF found in referenced class com.ewon.ewonitf.IOManager or its superclasses

Is any some library is missing that i need to install ? Can you tell me where am i doing wrong.

Kanduri,

This function has been implemented in ETK 1.4.4 and in firmware 14.0.(which is temporary unavailable)

Simon

Hello Simon ,
Thank you for giving info regarding the previous topic .
I have tried publishing the tags from the ewon device which are coming from the plc to mqtt broker using your java code .
Now i want to control the tags from the other devices to ewon .  For testing i have used mosquitto broker to publish a message to the ewon flexy 102 gateway. and i executed the same program which consists of the subscriber java code by subscribing to the required topic 

All i get  is ,in the realtime events section of the ewon browser, i am just getting received publish log. The message what i published from the mosquitto broker is not printing. I mean  the print function inside the callbackMqtt event is not executing. 

I am not getting whether the callbackMqtt event is being called or not . Because i can see the received publish log , but i am not able to see the message which was published by mosquitto broker .

mosquitto publish message 
         mosquitto_pub -h 192.168.0.50 -t abcdef/PLC_TAGS/CONVEYOR_CONTROL -m "ON"

Ewon Log message:

         Log (16): Client 1922-0017-20 received PUBLISH (d0, q0, r0, m0, 'AXISCADES/PLC_TAGS/CONVEYOR_CONTROL', ... (2 bytes))

Code of MyMQTTClient.java

import com.ewon.ewonitf.IOManager;
import com.ewon.ewonitf.MqttClient;
import com.ewon.ewonitf.MqttMessage;
//import com.ewon.ewonitf.SysControlBlock;

public class MyMQTTClient extends MqttClient {

public MyMQTTClient(String MqttId, String Host) throws Exception {
super(MqttId, Host);
}

public void callMqttEvent(int event) {
//When a message is received through a subscribed topic,
//the function callMqttEvent is called
try {
System.out.println("Reached Call Back Function");
//com.ewon.ewonitf.SysControlBlock Sys = new SysControlBlock(SysControlBlock.INF);
MqttMessage msg = readMessage();
String msgString = new String();

if (msg != null) {
msgString = new String(msg.getPayload(), "UTF-8");
System.out.println("MQTT Message Received: " + msgString);

if(msgString.equalsIgnoreCase("ON")) {
IOManager.writeTag("MACHINE_RUNNING", 1.0);
}
else {
IOManager.writeTag("MACHINE_RUNNING", 0.0);
}
}
msg.close();
}
catch (Exception e) {
System.out.println("Error callMqttEvent: " + e.toString());
}
}
}



I am just receiving the Ewon log message as received publish. But whenever a message is received from the broker to the subscribed topic , this callbackMqtt function should be called automatically and display the message ,but i am not seeing the strings in the print functions of this callbackMqtt() function in the realtime events log or the message which was published .Could you please tell me where i am doing wrong?  
  
(22-11-2019, 12:29 PM)Kanduri ShouriHello ,Can someone help me with the subscriber problem that i mentioned ? Wrote: [ -> ]Thank you simon .i got it . 

Shouri.
(21-11-2019, 07:20 AM)Kanduri Shouri Wrote: [ -> ]Hello ,
I have tried your code in the zip file . I have ewon flexy 102 series, firmware - 13.2s and javaetk1.4.4 (J2SE) . Now i am trying to publish the float tag in the ewon flexy using java.
In the Real Time Logs ,in the event table its showing me like :

java.lang.NoSuchMethodError: MQTT_Thread: no method readTagAsFloat with signature (Ljava/lang/StringWinkF found in referenced class com.ewon.ewonitf.IOManager or its superclasses

Is any some library is missing that i need to install ? Can you tell me where am i doing wrong.

Kanduri,

This function has been implemented in ETK 1.4.4 and in firmware 14.0.(which is temporary unavailable)

Simon

Hello Simon ,
Thank you for giving info regarding the previous topic .
I have tried publishing the tags from the ewon device which are coming from the plc to mqtt broker using your java code .
Now i want to control the tags from the other devices to ewon .  For testing i have used mosquitto broker to publish a message to the ewon flexy 102 gateway. and i executed the same program which consists of the subscriber java code by subscribing to the required topic 

All i get  is ,in the realtime events section of the ewon browser, i am just getting received publish log. The message what i published from the mosquitto broker is not printing. I mean  the print function inside the callbackMqtt event is not executing. 

I am not getting whether the callbackMqtt event is being called or not . Because i can see the received publish log , but i am not able to see the message which was published by mosquitto broker .

mosquitto publish message 
         mosquitto_pub -h 192.168.0.50 -t abcdef/PLC_TAGS/CONVEYOR_CONTROL -m "ON"

Ewon Log message:

         Log (16): Client 1922-0017-20 received PUBLISH (d0, q0, r0, m0, 'AXISCADES/PLC_TAGS/CONVEYOR_CONTROL', ... (2 bytes))

Code of MyMQTTClient.java

import com.ewon.ewonitf.IOManager;
import com.ewon.ewonitf.MqttClient;
import com.ewon.ewonitf.MqttMessage;
//import com.ewon.ewonitf.SysControlBlock;

public class MyMQTTClient extends MqttClient {

public MyMQTTClient(String MqttId, String Host) throws Exception {
super(MqttId, Host);
}

public void callMqttEvent(int event) {
//When a message is received through a subscribed topic,
//the function callMqttEvent is called
try {
System.out.println("Reached Call Back Function");
//com.ewon.ewonitf.SysControlBlock Sys = new SysControlBlock(SysControlBlock.INF);
MqttMessage msg = readMessage();
String msgString = new String();

if (msg != null) {
msgString = new String(msg.getPayload(), "UTF-8");
System.out.println("MQTT Message Received: " + msgString);

if(msgString.equalsIgnoreCase("ON")) {
IOManager.writeTag("MACHINE_RUNNING", 1.0);
}
else {
IOManager.writeTag("MACHINE_RUNNING", 0.0);
}
}
msg.close();
}
catch (Exception e) {
System.out.println("Error callMqttEvent: " + e.toString());
}
}
}



I am just receiving the Ewon log message as received publish. But whenever a message is received from the broker to the subscribed topic , this callbackMqtt function should be called automatically and display the message ,but i am not seeing the strings in the print functions of this callbackMqtt() function in the realtime events log or the message which was published .Could you please tell me where i am doing wrong?  
  
- Have you used the script from my post (with "if (event==1) return;") : https://techforum.ewon.biz/thread-586-po...ml#pid3697
- Have you subscribed to a topic to get the message ?

Simon
(03-12-2019, 04:29 PM)Hello simon,Yes i have subscribed to a topic in MQTT_Thread.java fileand i have tried with  "if (event='=1) return;".  Now i am receiving messages  but i am not able to change the tag data in the ewon gateway . Can you tell me where am i doing wrong? Wrote: [ -> ]- Have you used the script from my post (with "if (event==1) return;") : https://techforum.ewon.biz/thread-586-po...ml#pid3697
- Have you subscribed to a topic to get the message ?

Simon
Hi
you know how I can connect an ewon 205 to mqtt on a localhost using JAVA?

I´ts my first time using Ewon and i´dont have any idea
Hello,

If you want your Flexy acts as a MQTT client (for any kind of broker/Server), you'll have to make a BASIC or JAVA program.
If you get started you can simply use our demo MQTT Broker + the BASIC script example (that you just need to copy/paste)
https://tools.ewonsupport.biz/mqtt/

Simon
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.

(03-12-2019, 04:29 PM)simon Wrote: [ -> ]- Have you used the script from my post (with "if (event==1) return;") : https://techforum.ewon.biz/thread-586-po...ml#pid3697
- Have you subscribed to a topic to get the message ?

Simon

Hi Simon,
I also have the problem, the broker published message, but i can not find the message in flexy Realtime Logs.

public class MyMQTTClient extends MqttClient{

public MyMQTTClient(String MqttId, String Host) throws Exception {
super(MqttId, Host);
}

//when a message is received through a subscribed topic the function callMqttEvent() is called
public void callMqttEvent(int event) {
System.out.println("MqttEvent:"+event);

try {
if (event == 1)
return;

MqttMessage msg;

msg = readMessage();
if (msg != null) {
String str = new String(msg.getPayload(), "UTF-8");
System.out.println("MQTT Message Received: " + str);
msg.close();
}
} catch (Exception e) {
System.out.println("Error callMqttEvent: " + e.toString());
}

}
}

I subscribed the topic already.

Mqtt_c.subscribe("/shadow/get/a1msNv3yPCw/mypc", 0);

I think Flexy got the message ,i mean i can not print this message!
Pages: 1 2