1.1 If you want configure the MQTT sever information and the wifi information to connect for smart plug,you need make the smart plug into AP mode(Please refer to the MokoLife user manual):plug the smart plug into power socket,press the button for 10 seconds till the smart plug indicator blink amber light which indicate the smart plug into AP mode(Note:The timeout period for AP mode is 3 minutes,once you have configured the information to the smart plug,it will end AP mode).Enter the Wlan page and select the smart plug hotspot to connect ,when connect successfully,call connectDeviceWithHost:port:connectSucBlock:connectFailedBlock methods to connect smart plug.Following is the complete configuration of the smart plug process:
[[MKSocketManager sharedInstance] connectDeviceWithHost:@"192.168.4.1"
port:8266
connectSucBlock:^(NSString *IP, NSInteger port) {
//connect success
connectFailedBlock:^(NSError *error) {
//connect failed
}];
[[MKSocketManager sharedInstance] readSmartPlugDeviceInformationWithSucBlock:^(id returnData) {
//Read device info success
} failedBlock:^(NSError *error) {
//Read device info failed
}];
[[MKSocketManager sharedInstance] configMQTTServerHost:@"your MQTT Server host"
port:port
connectMode:mqttServerConnectTCPMode
qos:mqttQosLevelExactlyOnce
keepalive:60
cleanSession:YES
clientId:@"your device mac address"
username:@"your MQTT Server userName"
password:@"your MQTT Server password"
sucBlock:^(id returnData) {
//Config Success
}
failedBlock:^(NSError *error) {
//Config Failed
}];
[[MKSocketManager sharedInstance] configWifiSSID:your wifi ssid
password:wifi password
security:wifiSecurity_WPA2_PSK
sucBlock:^(id returnData) {
//Config Success
} failedBlock:^(NSError *error) {
//Config Failed
}];
@protocol MKMQTTServerManagerDelegate
-
(void)mqttServerManagerStateChanged:(MKMQTTSessionManagerState)state;//connect state delegate method
-
(void)sessionManager:(MKMQTTServerManager *)sessionManager didReceiveMessage:(NSData *)data onTopic:(NSString *)topic;//Receives the data from the MQTT server
@end
When the network is available,call [[MKMQTTServerManager sharedInstance] connectMQTTServer:port:tls:keepalive:clean:auth:user:pass:clientId:] to connect your MQTT server.
Please refer to the MQTT protocal document for the topic of the smart plug.Call - (void)subscriptions:(NSArray <NSString *>*)topicList
and - (void)unsubscriptions:(NSArray <NSString *>*)topicList
to subscribe and unsubscribe the topic separatrly.
Please refer to the MQTT protocal document for the topic of the smart plug.
- (void)sendData:(NSDictionary *)data topic:(NSString *)topic sucBlock:(void (^)(void))sucBlock failedBlock:(void (^)(NSError *error))failedBlock