-
Notifications
You must be signed in to change notification settings - Fork 39
/
MQTTViewController.m
91 lines (73 loc) · 3.06 KB
/
MQTTViewController.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
//
// MQTTViewController.m
// MQTTExample
//
// Created by Jeff Mesnil on 15/02/2014.
// Copyright (c) 2014 jmesnil.net. All rights reserved.
//
#import "MQTTViewController.h"
#import <MQTTKit.h>
#define kMQTTServerHost @"iot.eclipse.org"
#define kTopic @"MQTTExample/LED"
@interface MQTTViewController ()
// this UISwitch will be used to display the status received from the topic.
@property (weak, nonatomic) IBOutlet UISwitch *subscribedSwitch;
// create a property for the MQTTClient that is used to send and receive the message
@property (nonatomic, strong) MQTTClient *client;
@end
@implementation MQTTViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// create the MQTT client with an unique identifier
NSString *clientID = [UIDevice currentDevice].identifierForVendor.UUIDString;
self.client = [[MQTTClient alloc] initWithClientId:clientID];
// keep a reference on the switch to avoid having a reference to self in the
// block below (retain/release cycle, blah blah blah)
UISwitch *subSwitch = self.subscribedSwitch;
// define the handler that will be called when MQTT messages are received by the client
[self.client setMessageHandler:^(MQTTMessage *message) {
// extract the switch status from the message payload
BOOL on = [message.payloadString boolValue];
// the MQTTClientDelegate methods are called from a GCD queue.
// Any update to the UI must be done on the main queue
dispatch_async(dispatch_get_main_queue(), ^{
[subSwitch setOn:on animated:YES];
});
}];
// connect the MQTT client
[self.client connectToHost:kMQTTServerHost completionHandler:^(MQTTConnectionReturnCode code) {
if (code == ConnectionAccepted) {
// The client is connected when this completion handler is called
NSLog(@"client is connected with id %@", clientID);
// Subscribe to the topic
[self.client subscribe:kTopic withCompletionHandler:^(NSArray *grantedQos) {
// The client is effectively subscribed to the topic when this completion handler is called
NSLog(@"subscribed to topic %@", kTopic);
}];
}
}];
}
- (void)dealloc
{
// disconnect the MQTT client
[self.client disconnectWithCompletionHandler:^(NSUInteger code) {
// The client is disconnected when this completion handler is called
NSLog(@"MQTT is disconnected");
}];
}
#pragma mark - IBActions
// This method is called when the "published LED" switch status changes
- (IBAction)switchUpdated:(id)sender {
BOOL on = [sender isOn];
NSString *payload = [NSNumber numberWithBool:on].stringValue;
// use the MQTT client to send a message with the switch status to the topic
[self.client publishString:payload
toTopic:kTopic
withQos:AtMostOnce
retain:YES
completionHandler:nil];
// we passed nil to the completionHandler as we are not interested to know
// when the message was effectively sent
}
@end