Search code examples
iosmqttadafruit

How do I get MQTT topic data from adafruit upon device start up


I am building an iphone app which uses MQTT_Client_Foundation(https://github.com/novastone-media/MQTT-Client-Framework)to subscribe and publish to topics on Adafruit.com. I have it working that it connects and looks for updates when the values are updated on the server and it publishes to the server when a button is tapped. My problem is that I want to get the current topic values on the server when I open the app without having to wait for then to be updated. This is the code I have now:

#import "ViewController.h"
#import <MQTTClient/MQTTClient.h>
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *batteryValueLabel;
@property (strong, nonatomic) IBOutlet UIImageView *tempValueLabel;
@property (strong, nonatomic) IBOutlet UILabel *temperatureValueLabel;
@property (weak, nonatomic) IBOutlet UILabel *levelValueLabel;
@property (weak, nonatomic) IBOutlet UILabel *tripValueLabel;
@property (weak, nonatomic) IBOutlet UILabel *dateValueLabel;
@property(nonatomic, strong, readwrite) NSArray *certificates;
@property(nonatomic, weak) IBOutlet UIButton *Fill;


-(IBAction)Turnon:(id)sender;
@end
NSString *trip = @"0";
int trigger =1;
int B = 50;
int T = 90;
MQTTSession *session;
@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tempValueLabel.frame = CGRectMake(20, 250-(T+45),65, T+45);
    _batteryValueLabel.frame = CGRectMake(114, 287, B*1.7, 63);
    NSString* myNewString = [NSString stringWithFormat:@"%d", T];
    self.temperatureValueLabel.text = myNewString;
    MQTTCFSocketTransport *transport = [[MQTTCFSocketTransport alloc] init];
    transport.host = @"io.adafruit.com";
    transport.port = 1883;
   
    session = [[MQTTSession alloc] init];
    session.userName = @"xxxxxxxxx";
    session.password = @"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
    session.transport = transport;
    session.delegate = self;

//MODIFIED CODE HERE FOR /GET

     [session connectWithConnectHandler:^(NSError *error) {
   
        if(!error){
            
            [session subscribeToTopic:@"XXXXXX/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(), ^{

//SEEMS TO ME THAT THEY ARE PASSING A NUMBER NOT A STRING

            trip = @"0";
            NSData* data = [trip dataUsingEncoding:NSUTF8StringEncoding];
            [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);}
    
    
}];

}

//this is continuously called and gets data when server values are updated
- (void)newMessage:(MQTTSession *)session
              data:(NSData *)data
           onTopic:(NSString *)topic
               qos:(MQTTQosLevel)qos
          retained:(BOOL)retained
               mid:(unsigned int)mid {
    NSString *dataString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    NSLog(@"topic: %@", topic);
    NSLog(@"data:%@", dataString);
    if ([topic isEqualToString:@"xxxxxxx/feeds/temperature"]){
        T = [dataString intValue];
        self.temperatureValueLabel.text = dataString;
        self.tempValueLabel.frame = CGRectMake(20, 250-(T+45),65, T+45);
        if (T <= 69) {
            _tempValueLabel.image = [UIImage imageNamed:@"dk blue rectangle.png"];
        }
        else if ((69< B)&& (B <= 90)) {
            _tempValueLabel.image = [UIImage imageNamed:@"blue rectangle.png"];
        }
        else if (B > 90) {
            _tempValueLabel.image = [UIImage imageNamed:@"red rectangle.png"];
        }
    }
    if ([topic isEqualToString:@"xxxxxxxx/feeds/level"]){
        self.levelValueLabel.text = dataString;}
    if ([topic isEqualToString:@"xxxxxxxx/feeds/date"]){
        self.dateValueLabel.text = dataString;}
    if ([topic isEqualToString:@"xxxxxxxx/feeds/battery"]){
      
        B = [dataString intValue];
        _batteryValueLabel.frame = CGRectMake(114, 287, B*1.7, 63);
        if (B <= 20) {
            _batteryValueLabel.image = [UIImage imageNamed:@"red rectangle.png"];
        }
        else if ((20< B)&& (B <= 100)) {
            _batteryValueLabel.image = [UIImage imageNamed:@"green rectangle.png"];
        }
        else if (B > 100) {
            _batteryValueLabel.image = [UIImage imageNamed:@"red rectangle.png"];
        }
    }
    if ([topic isEqualToString:@"xxxxxxxxx/feeds/trip"]){
        self.tripValueLabel.text = dataString;}


}
//when I tap this button the value on the server is updated
-(IBAction)Turnon:(id)sender;{
    if(trigger ==1){
        trip = @"0";
        trigger = 2;
    }
    else if(trigger==2){
        trip = @"100";
        trigger = 1;
    }
    NSData* data = [trip dataUsingEncoding:NSUTF8StringEncoding];
    [session publishData:data onTopic:@"xxxxxxxxx/feeds/trip" retain:NO qos:1 publishHandler:nil];
    NSLog(@"%@", trip);
    NSLog(@"%d",trigger);
}
@end

Does anyone know what code and where to put it so that when I open the app it gets the current topic value on the server without waiting for an update?


Solution

  • I got this to work Here is my code

    #import "ViewController.h"
    #import <MQTTClient/MQTTClient.h>
    @interface ViewController ()
    @property (weak, nonatomic) IBOutlet UIImageView *batteryValueLabel;
    @property (strong, nonatomic) IBOutlet UIImageView *tempValueLabel;
    @property (strong, nonatomic) IBOutlet UILabel *temperatureValueLabel;
    @property (weak, nonatomic) IBOutlet UILabel *levelValueLabel;
    @property (weak, nonatomic) IBOutlet UILabel *tripValueLabel;
    @property (weak, nonatomic) IBOutlet UILabel *dateValueLabel;
    @property(nonatomic, strong, readwrite) NSArray *certificates;
    @property(nonatomic, weak) IBOutlet UIButton *Fill;
    
    
    -(IBAction)Turnon:(id)sender;
    -(IBAction)update:(id)sender;
    @end
    NSString *trip = @"0";
    int trigger =1;
    int B = 50;
    int T = 90;
    MQTTSession *session;
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        self.tempValueLabel.frame = CGRectMake(20, 250-(T+45),65, T+45);
        
        NSString* myNewString = [NSString stringWithFormat:@"%d", T];
        self.temperatureValueLabel.text = myNewString;
        MQTTCFSocketTransport *transport = [[MQTTCFSocketTransport alloc] init];
        transport.host = @"io.adafruit.com";
        transport.port = 1883;
        session = [[MQTTSession alloc] init];
        session.userName = @"xxxxxxxx";
        session.password = @"xxxxxxxxxxxxxxxxxxx";
        session.transport = transport;
        session.delegate = self;
        [session connectWithConnectHandler:^(NSError *error) {
                if(!error){
                    
                    [session subscribeToTopic:@"xxxxxxxx/feeds/temperature" atLevel:1 subscribeHandler:^(NSError *error, NSArray *gQoss){
                        
                if (error) {
                    NSLog(@"Subscription failed %@", error.localizedDescription);
                } else {
                    NSLog(@"Subscription sucessfull! Granted Qos: %@", gQoss);
                    
                    trip = @"0";
                    NSData* data = [trip dataUsingEncoding:NSUTF8StringEncoding];
                    [session publishData:data onTopic:@"gbenna/feeds/temperature/get" retain:NO qos:0 publishHandler:nil];
                    NSLog(@"%@", trip);
                    NSLog(@"%d",trigger);
                       }
            }];
                    [session subscribeToTopic:@"xxxxxxx/feeds/battery" atLevel:1 subscribeHandler:^(NSError *error, NSArray *gQoss){
                        
                if (error) {
                    NSLog(@"Subscription failed %@", error.localizedDescription);
                } else {
                    NSLog(@"Subscription sucessfull! Granted Qos: %@", gQoss);
                    
                    trip = @"0";
                    NSData* data = [trip dataUsingEncoding:NSUTF8StringEncoding];
                    [session publishData:data onTopic:@"xxxxxxx/feeds/battery/get" retain:NO qos:0 publishHandler:nil];
                    NSLog(@"%@", trip);
                    NSLog(@"%d",trigger);
                        }
            }];
                    [session subscribeToTopic:@"xxxxxxxx/feeds/level" atLevel:1 subscribeHandler:^(NSError *error, NSArray *gQoss){
                        
                if (error) {
                    NSLog(@"Subscription failed %@", error.localizedDescription);
                } else {
                    NSLog(@"Subscription sucessfull! Granted Qos: %@", gQoss);
                    
                    trip = @"0";
                    NSData* data = [trip dataUsingEncoding:NSUTF8StringEncoding];
                    [session publishData:data onTopic:@"xxxxxxx/feeds/level/get" retain:NO qos:0 publishHandler:nil];
                    NSLog(@"%@", trip);
                    NSLog(@"%d",trigger);
                        }
            }];
                    [session subscribeToTopic:@"gbenna/feeds/date" atLevel:1 subscribeHandler:^(NSError *error, NSArray *gQoss){
                
                if (error) {
                    NSLog(@"Subscription failed %@", error.localizedDescription);
                } else {
                    NSLog(@"Subscription sucessfull! Granted Qos: %@", gQoss);
                    
                    trip = @"0";
                    NSData* data = [trip dataUsingEncoding:NSUTF8StringEncoding];
                    [session publishData:data onTopic:@"xxxxxxx/feeds/date/get" retain:NO qos:0 publishHandler:nil];
                    NSLog(@"%@", trip);
                    NSLog(@"%d",trigger);
                       }
            }];
                    [session subscribeToTopic:@"xxxxxxx/feeds/trip" atLevel:1 subscribeHandler:^(NSError *error, NSArray *gQoss){
                
                if (error) {
                    NSLog(@"Subscription failed %@", error.localizedDescription);
                } else {
                    NSLog(@"Subscription sucessfull! Granted Qos: %@", gQoss);
                    
                    trip = @"0";
                    NSData* data = [trip dataUsingEncoding:NSUTF8StringEncoding];
                    [session publishData:data onTopic:@"xxxxxxx/feeds/trip/get" retain:NO qos:0 publishHandler:nil];
                    NSLog(@"%@", trip);
                    NSLog(@"%d",trigger);
                       }
            }];
                    }
                else {NSLog(@"[connectWithConnectHandler]Error Connect %@", error.localizedDescription);}
            
            
        }];
       
      
        
        
    }
    
    - (void)newMessage:(MQTTSession *)session
                  data:(NSData *)data
               onTopic:(NSString *)topic
                   qos:(MQTTQosLevel)qos
              retained:(BOOL)retained
                   mid:(unsigned int)mid {
        NSString *dataString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        NSLog(@"topic: %@", topic);
        NSLog(@"data:%@", dataString);
        if ([topic isEqualToString:@"xxxxxx/feeds/temperature"]){
            T = [dataString intValue];
            self.temperatureValueLabel.text = dataString;
            self.tempValueLabel.frame = CGRectMake(20, 250-(T+45),65, T+45);
            if (T <= 69) {
                _tempValueLabel.image = [UIImage imageNamed:@"dk blue rectangle.png"];
            }
            else if ((69< B)&& (B <= 90)) {
                _tempValueLabel.image = [UIImage imageNamed:@"blue rectangle.png"];
            }
            else if (B > 90) {
                _tempValueLabel.image = [UIImage imageNamed:@"red rectangle.png"];
            }
        }
        if ([topic isEqualToString:@"xxxxxx/feeds/level"]){
            self.levelValueLabel.text = dataString;}
        if ([topic isEqualToString:@"xxxxxx/feeds/date"]){
            self.dateValueLabel.text = dataString;}
        if ([topic isEqualToString:@"xxxxxxx/feeds/battery"]){
          
            B = [dataString intValue];
            self.batteryValueLabel.frame = CGRectMake(332, 250-(B*1.7), 65, B);
            if (B <= 20) {
                _batteryValueLabel.image = [UIImage imageNamed:@"red rectangle.png"];
            }
            else if ((20< B)&& (B <= 100)) {
                _batteryValueLabel.image = [UIImage imageNamed:@"green rectangle.png"];
            }
            else if (B > 100) {
                _batteryValueLabel.image = [UIImage imageNamed:@"red rectangle.png"];
            }
        }
        if ([topic isEqualToString:@"xxxxxxx/feeds/trip"]){
            self.tripValueLabel.text = dataString;}
    
    
    }
    -(IBAction)Turnon:(id)sender;{
        if(trigger ==1){
            trip = @"0";
            trigger = 2;
        }
        else if(trigger==2){
            trip = @"100";
            trigger = 1;
        }
        NSData* data = [trip dataUsingEncoding:NSUTF8StringEncoding];
        [session publishData:data onTopic:@"xxxxxxx/feeds/trip" retain:NO qos:1 publishHandler:nil];
        NSLog(@"%@", trip);
        NSLog(@"%d",trigger);
        
    }
    
    @end
    

    Once connected to the server I subscribed to a topic and then when I was subscribed to that topic I published the /get method for it. I did this for each topic. and they all update with the last current value for each topic when I launch my app. I am going to try and see what happens when I come back for being in the background and maybe add a button to update (/get) all topics also.

    Thanks for everyone's help.