Black Lives Matter - Action and Equality.
0

Device Does not changes state with java Java program, but ok
Moderators: adafruit_support_bill, adafruit

Forum rules
If you're posting code, please make sure your code does not include your Adafruit IO Active Key or WiFi network credentials.
Please be positive and constructive with your questions and comments.

Device Does not changes state with java Java program, but ok

by andrebarone on Tue Apr 21, 2020 8:41 pm

Hi Guys,
first of all I'd like to say that I have searched in the forum for a similar question, and my apologies if is there and I couldn't reached.
:)

So,
my problem is that I made an small publishing program to change the value of a Feed on the dashboard using Java/PAHO, and therefore access the state of a LED on my ESP8266 :
Code: Select all | TOGGLE FULL SIZE
 public static void main(String[] args) {

   String topic = <MyUserName>/feeds/lednodemcu";
   String content = "OFF";
   int qos = 1;
   String broker = "tcp://io.adafruit.com";
   String clientId =  "<MyUserName>";
   String password =  "<MyKey>";
   MemoryPersistence persistence = new MemoryPersistence();
   
   try {
           MqttClient sampleClient = new MqttClient(broker, clientId, persistence);
           MqttConnectOptions connOpts = new MqttConnectOptions();
           connOpts.setMqttVersion(MqttConnectOptions.MQTT_VERSION_3_1);
           connOpts.setCleanSession(false);
           
           connOpts.setPassword(password.toCharArray());
           connOpts.setUserName(clientId);
           System.out.println("Connecting to broker: "+broker);
           sampleClient.connect(connOpts);
           System.out.println("Connected");
           System.out.println("Publishing message: "+content);
           MqttMessage message = new MqttMessage(content.getBytes());
           message.setQos(1);
           sampleClient.publish(topic, message);
           System.out.println("Message published");
           sampleClient.disconnect();
           System.out.println("Disconnected");
           System.exit(0);
   } catch (MqttException e) {
     e.printStackTrace();
   }
 }

I see the Feed state changing, bu Nothing happens at de ESP8266

What intrigues me is that I publish a simillar stuf using mosquitto, and work as expected, that is , both LED on my ESP8266 and Feed on Dashboard changes:

Code: Select all | TOGGLE FULL SIZE
mosquitto_pub -h io.adafruit.com -p 1883 -u '<MyUserName>' -P '<MyKey>' -t '<MyUserName>/feeds/lednodemcu' -m 'OFF'


Could somebody give me any clue?

Thanks!

andrebarone
 
Posts: 3
Joined: Tue Apr 21, 2020 8:07 pm

Re: Device Does not changes state with java Java program, bu

by brubell on Wed Apr 29, 2020 12:48 pm

Navigate to http://io.adafruit.com/monitor and then try sending data to the feed. Do you see any errors on this page?

brubell
 
Posts: 891
Joined: Fri Jul 17, 2015 10:33 pm

Re: Device Does not changes state with java Java program, bu

by andrebarone on Wed Apr 29, 2020 8:25 pm

Hi @Brubell,
Thnaks for replaying.
No errors appear on monitor, but some weird behavior.

When using Mosquitto, I can see 1 Connect and 1 Disconnect for each publishing request:
Image

For the java application, it does 1 connect, 2 disconnects and 1 connect again:
Image

So, I think I will review the code, although it is a pretty Straight Connect - Publish-Disconnect using Paho Library and MQTT version 3.1

andrebarone
 
Posts: 3
Joined: Tue Apr 21, 2020 8:07 pm

Re: Device Does not changes state with java Java program, bu

by andrebarone on Wed Apr 29, 2020 8:39 pm

Let me give an update.
I fixed the Problem.
I changed the ClientId on (String serverURI, String clientId, MqttClientPersistence persistence)class.

andrebarone
 
Posts: 3
Joined: Tue Apr 21, 2020 8:07 pm

Please be positive and constructive with your questions and comments.