Ewon TechForum
Providing technical solutions to technical requests


Getting Started with MQTT on your Flexy using JAVA.
simon Offline
eWON Support
#1
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


Attached Files
.zip   MQTT_EWONSUPPORT.zip (Size: 9,08 KB / Downloads: 199)
Reply

rene.divossen Offline
Junior Member
#2
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é
Reply

simon Offline
eWON Support
#3
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
Reply

woodyjon Offline
Junior Member
#4
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
Reply

simon Offline
eWON Support
#5
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
Reply

Kanduri Shouri Offline
Junior Member
#6
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.


Attached Files
.png   ewon.PNG (Size: 100,56 KB / Downloads: 11)
Reply

woodyjon Offline
Junior Member
#7
(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!
Reply

simon Offline
eWON Support
#8
(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
Reply

Kanduri Shouri Offline
Junior Member
#9
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?  
  
Reply

Kanduri Shouri Offline
Junior Member
#10
(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?  
  
Reply

simon Offline
eWON Support
#11
- 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
Reply

Kanduri Shouri Offline
Junior Member
#12
(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
Reply



Possibly Related Threads…
Star MQTT Publish Problem Started by ntnunk
4 Replies - 100 Views
6 hours ago
Last Post: simon
  MQTT secure connection Started by Srdjan
3 Replies - 59 Views
26-03-2020, 03:20 PM
Last Post: Srdjan
  Java 1.4 update Started by tomT
12 Replies - 1.670 Views
24-03-2020, 09:08 PM
Last Post: hernfiry
13-03-2020, 01:12 PM
Last Post: simon
05-03-2020, 02:11 PM
Last Post: simon
03-03-2020, 11:35 PM
Last Post: simon
20-02-2020, 10:33 PM
Last Post: simon
31-01-2020, 05:27 PM
Last Post: simon
23-12-2019, 06:03 PM
Last Post: simon
  JAVA Azure IOTHub Connector Started by simon
1 Replies - 963 Views
11-12-2019, 12:19 PM
Last Post: simon



Users browsing this thread:
1 Guest(s)



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