0

how do I add a retain to a feeds/topic for publishing?
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.

how do I add a retain to a feeds/topic for publishing?

by gbenna on Mon Dec 28, 2020 5:45 pm

I have a system that gets various sensor readings and hands them off to an ESP8266-01. This happens every two hrs when the ESP-01 is powered up. It then connects to the wifi and Adafruit and publishes the sensor values to 5 feed topics. I am building an iphone app which connects to
Adafruit and subscribe to the topics. The problem lies in that I don't want my phone to be on/ open/ and the app on and open all the time so I can get the updates. Is there anyway to add a retain to the published topics, or is there some way to have the phone app get the current topic values when it launches. I read something about /get but I don't know how to do that either. Can you point me to a tutorial on how to do either or both of these. Or do I need to look at another host server to be able to do this?
I like you service but I do need this ability!

gbenna
 
Posts: 6
Joined: Mon Sep 30, 2019 5:03 pm

Re: how do I add a retain to a feeds/topic for publishing?

by gbenna on Mon Dec 28, 2020 8:53 pm

So I am trying to use /get.
Here is what I have


Code: Select all | TOGGLE FULL SIZE
session = [[MQTTSession alloc] init];
    session.userName = @"123456";
    session.password = @"1234567890";
    session.transport = transport;
    session.delegate = self;
    [session connectWithConnectHandler:^(NSError *error) {
       
            if(!error){
               
                [session subscribeToTopic:@"12345/feeds/+" atLevel:1 subscribeHandler:^(NSError *error, NSArray *gQoss){
                   
            if (error) {
                NSLog(@"Subscription failed %@", error.localizedDescription);
            } else {
                NSLog(@"Subscription sucessfull! Granted Qos: %@", gQoss);
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                trip = @"0";
                NSData* data = [trip dataUsingEncoding:NSUTF8StringEncoding];
[u]//this is where I am trying to use the /get.  if I don't put /get the value does change so I know the
//publishing is correct.  I think it is because :data is a string and not an int.  Is there some way
//I can use this to publish an int?  am I correct in my assumption or is there another way?[/u]
                [session publishData:data onTopic:@"gbenna/feeds/temperature/get" retain:NO qos:1 publishHandler:nil];
                NSLog(@"%@", trip);
                NSLog(@"%d",trigger);
                    });}
        }]; }
            else {NSLog(@"[connectWithConnectHandler]Error Connect %@", error.localizedDescription);}
    }];}


I start to check for updates before this publish is sent. If I send it without /get it updates to 0 value but with /get nothing.

gbenna
 
Posts: 6
Joined: Mon Sep 30, 2019 5:03 pm

Re: how do I add a retain to a feeds/topic for publishing?

by brubell on Mon Jan 04, 2021 11:55 am

Is there anyway to add a retain to the published topics,

The retain publish flag is not supported by Adafruit IO's MQTT broker, we use a /get topic modifier instead (https://io.adafruit.com/api/docs/mqtt.h ... -get-topic).

You want to subscribe to the topic you'd like to publish to first, then publish to its /get modifier (https://io.adafruit.com/api/docs/mqtt.h ... -get-topic). I don't fully follow the code you posted above - could you shed some light on what it's doing?

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

Please be positive and constructive with your questions and comments.