I have an app which I am building using the MQTT_Client_Framework. I and log onto my server, and subscribe to my topics and even get any updated data when new data is published to the server. However I would like to be able to subscribe and get the existing topic values when the app is launched. Right now the only time I get values is when they are updated on the server by another client.
I am building the app on an iPhone 11 Pro running IOS 14.2. I used Cocoapods to install the Frameworks. My Broker(server) is Adafruit.com. They say they are not a Broker but a Client.
There is not an issue with the code per say, I just want to be able to get the values when I launch the app.
Here is my code in the app's ViewController
#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 = @"xxxxxxxx";
session.password = @"xxxxxxxxxxxxxxxxxxxxxxx";
session.transport = transport;
session.delegate = self;
[session connectWithConnectHandler:^(NSError *error) {
if(!error){
[session subscribeToTopic:@"xxxxxxx/feeds/+" atLevel:1 subscribeHandler:^(NSError *error, NSArray *gQoss){
if (error) {
NSLog(@"Subscription failed %@", error.localizedDescription);
} else {
NSLog(@"Subscription sucessfull! Granted Qos: %@", gQoss);
//I GET TO HERE FINE JUST NEED TO KNOW WHAT TO PUT HERE TO GET THE CURRENT VALUES ON SERVER
}
}];
}
else {NSLog(@"[connectWithConnectHandler]Error Connect %@", error.localizedDescription);}
}];
}
//THIS WORKS FINE TO GET UPDATED VALUES
- (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:@"gbenna/feeds/level"]){
self.levelValueLabel.text = dataString;}
if ([topic isEqualToString:@"xxxxxxx/feeds/date"]){
self.dateValueLabel.text = dataString;}
if ([topic isEqualToString:@"xxxxxx/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:@"xxxxxxx/feeds/trip"]){
self.tripValueLabel.text = dataString;}
}
//THIS WORKS TO POST A VALUE TO SERVER
-(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:@"xxxxx/feeds/trip" retain:NO qos:1 publishHandler:nil];
NSLog(@"%@", trip);
NSLog(@"%d",trigger);
}
@end
If someone could help I would really appreciate it.
I have an app which I am building using the MQTT_Client_Framework. I and log onto my server, and subscribe to my topics and even get any updated data when new data is published to the server. However I would like to be able to subscribe and get the existing topic values when the app is launched. Right now the only time I get values is when they are updated on the server by another client. I am building the app on an iPhone 11 Pro running IOS 14.2. I used Cocoapods to install the Frameworks. My Broker(server) is Adafruit.com. They say they are not a Broker but a Client. There is not an issue with the code per say, I just want to be able to get the values when I launch the app. Here is my code in the app's ViewController
If someone could help I would really appreciate it.