autobahn
Posts: 2
Joined: Sun Aug 05, 2018 9:00 am

Re: [GUIDE] Communicate between iOS and Raspberry Pi using MQTT

Sun Aug 05, 2018 9:14 am

Thanks for a great tutorial, got the led blinking from my iPhone!

I already have DHT11 temperature sensor connected to my rpi and tried to read the CocoaMQTT documentation about receiving messages but as I'm still a noob :geek: in swift I'd appreciate any suggestions to make iOS device to receive messages from MQTT server too as some other forum members have requested here also.

autobahn
Posts: 2
Joined: Sun Aug 05, 2018 9:00 am

Re: [GUIDE] Communicate between iOS and Raspberry Pi using MQTT

Mon Aug 06, 2018 5:32 pm

autobahn wrote:
Sun Aug 05, 2018 9:14 am
Thanks for a great tutorial, got the led blinking from my iPhone!

I already have DHT11 temperature sensor connected to my rpi and tried to read the CocoaMQTT documentation about receiving messages but as I'm still a noob :geek: in swift I'd appreciate any suggestions to make iOS device to receive messages from MQTT server too as some other forum members have requested here also.
I figured it out myself. To let you roast my code or just to help other noobs, I post it here:

ViewController Class

Code: Select all

import UIKit

class ViewController: UIViewController, didReceiveMessageDelegate {
    @IBOutlet weak var temperatureLabel: UILabel!
    
    let mqtt = MQTT()
    
    func setMessage(message: String) {
        temperatureLabel.text = message
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        mqtt.delegate = self
        let notificationCenter = NotificationCenter.default
        notificationCenter.addObserver(self, selector: #selector(appMovedToBackground), name: Notification.Name.UIApplicationWillResignActive, object: nil)
        notificationCenter.addObserver(self, selector: #selector(appMovedToForeground), name: Notification.Name.UIApplicationWillEnterForeground, object: nil)
        
        mqtt.connect()
    }
    
    @objc func appMovedToBackground() {
        print("App moved to background. Disconnecting.")
        mqtt.disconnect()
    }

    @objc func appMovedToForeground() {
        print("App moved to foreground. Connecting...")
        mqtt.connect()
    }
    @IBAction func ledSwitch(_ sender: UISwitch) {
        if sender.isOn {
            mqtt.publish(topic: "rpi/GPIO", message: "on")
        }
        else {
            mqtt.publish(topic: "rpi/GPIO", message: "off")
        }
    }
}
MQTT class

Code: Select all

import Foundation
import CocoaMQTT

protocol didReceiveMessageDelegate {
    func setMessage(message: String)
}

class MQTT: CocoaMQTTDelegate {
    
    var delegate: didReceiveMessageDelegate?
    let mqttClient = CocoaMQTT(clientID: "iOS Device", host: "192.168.1.111", port: 1883)
    
    func connect() {
        mqttClient.delegate = self
        mqttClient.connect()
        print("Connecting...")
    }
    
    func disconnect() {
        mqttClient.disconnect()
        print("Disconnected")
    }
    
    func publish(topic: String, message: String) {
        mqttClient.publish(topic, withString: message)
    }
    
    func mqtt(_ mqtt: CocoaMQTT, didConnectAck ack: CocoaMQTTConnAck) {
        print("Connected")
        mqttClient.subscribe("rpi/DHT")
    }
    
    func mqtt(_ mqtt: CocoaMQTT, didPublishMessage message: CocoaMQTTMessage, id: UInt16) {
        
    }
    
    func mqtt(_ mqtt: CocoaMQTT, didPublishAck id: UInt16) {
        
    }
    
    func mqtt(_ mqtt: CocoaMQTT, didReceiveMessage message: CocoaMQTTMessage, id: UInt16) {
        let messageDecoded = String(bytes: message.payload, encoding: .utf8)
        print("Did receive a message: \(messageDecoded!)")
        delegate?.setMessage(message: "\(messageDecoded!) ºC")
        
    }
    
    func mqtt(_ mqtt: CocoaMQTT, didSubscribeTopic topic: String) {
        print("Did subscribe to \(topic)")
    }
    
    func mqtt(_ mqtt: CocoaMQTT, didUnsubscribeTopic topic: String) {
        
    }
    
    func mqttDidPing(_ mqtt: CocoaMQTT) {
        
    }
    
    func mqttDidReceivePong(_ mqtt: CocoaMQTT) {

    }
    
    func mqttDidDisconnect(_ mqtt: CocoaMQTT, withError err: Error?) {
        
    }
}

krishdhar
Posts: 2
Joined: Wed Jul 25, 2018 10:26 pm

Re: [GUIDE] Communicate between iOS and Raspberry Pi using MQTT

Fri Aug 10, 2018 7:02 pm

Hi, thank you for the great tutorial

I am having trouble with one part
No matter what I do, it still says that there is an error with me importing CocoaMQTT and the build doesn't work
I have reinstalled cocoapods and even restarted my entire app and I haven't found any errors

Can anybody please help

Basouel
Posts: 1
Joined: Sat Sep 15, 2018 4:01 pm

Re: [GUIDE] Communicate between iOS and Raspberry Pi using MQTT

Sat Sep 15, 2018 4:16 pm

Hi,

Thanks a lot for that tutorial, really easy to follow and complete every steps.

My project is a bit different, I use all of that to control my camera from my phone and so far I managed to adapt all of that for it.
Now I would like it to work outside and it’s where I’m a bit stuck. I though that it could work if i just share the connexion from my phone, set the pi to connect automatically to it, change the IP address (for the app and for the python script) to the one used by the pi when connected to my phone, but the all server is now not working. The connect button doesn’t do anything with that so I wonder why and if you guys could know a solution.
(The IP address given by my phone is starting with 172.XXX.XX.XX)

Thanks

charbelsalloum
Posts: 1
Joined: Sat Oct 06, 2018 6:16 pm

Re: [GUIDE] Communicate between iOS and Raspberry Pi using MQTT

Sat Oct 06, 2018 6:20 pm

If we have two iOS devices connected, and one user changed the state of a pin will the state of the switch on the second iOS device changes too ?

informed
Posts: 103
Joined: Mon Oct 26, 2015 6:24 pm

Re: [GUIDE] Communicate between iOS and Raspberry Pi using MQTT

Wed Nov 21, 2018 1:08 pm

Hi,

looking to implement something similar using C#.

May I ask before I start to translate on what medium the messages are sent? I mean are you relying on the home users wireless hub to send and receive?

thanks

Return to “Automation, sensing and robotics”