asandford
Posts: 1999
Joined: Mon Dec 31, 2012 12:54 pm
Location: Waterlooville

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

Mon Jan 22, 2018 12:24 am

[quote=GhostRaider post_id=1261545 time=1516408691 user_id=174543

How would that work? It's very interesting indeed but how would clients confirm to the mqtt broker that it received the message if brokers aren't allowed to subscribe or publish to anything other than just send messages to their destination? Unless its a modified protocol. I've researched this before, for example someone wanted to create a chat application but wanted to have a feature that the message was received by the other client. The answer was to use a workaround, meaning to use the subscriber/publisher model because you can never confirm that the client handled the message appropriately unless you manually check that message from the client side.
[/quote]

There are other brokers available that have a great deal of added functionality and extend that to the MQTT protocol.

kingofthenorth
Posts: 6
Joined: Mon Nov 20, 2017 11:18 pm

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

Thu Jan 25, 2018 4:56 pm

axopax wrote:
Sat Jan 20, 2018 12:47 am
Hey,

Thanks for the great tutorial. I just had a few issues I wanted to verify.

After i wrote make in the mosquitto terminal, I got so many warnings. Mainly :

../lib/tls_mosq.c:130:5: warning: ‘ASN1_STRING_data’ is deprecated [-Wdeprecated-declarations]
data = ASN1_STRING_data(nval->d.dNSName);
^~~~

Second, when I added the "mosquitto -v" command, I got :1516409129:

mosquitto version 1.4.14 (build date 2018-01-20 00:39:52+0000) starting
1516409129: Using default config.
1516409129: Opening ipv4 listen socket on port 1883.
1516409129: Error: Address already in use

Could you please help as I am a beginner
Open a terminal and type

Code: Select all

ps -ef | grep mosquitto
then execute

Code: Select all

sudo kill 123
where 123 is the process ID. Then try executing

Code: Select all

mosquitto -v

Francklin
Posts: 8
Joined: Wed Jan 24, 2018 7:47 pm

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

Thu Jan 25, 2018 6:06 pm

Great guide, congratulations!

I will ask something "idiot"...I see the ip to make a communication. So, can I use, for example, 10 iphones into the same server (raspberry pi) ?


Att;
Francklin Souza

kingofthenorth
Posts: 6
Joined: Mon Nov 20, 2017 11:18 pm

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

Thu Jan 25, 2018 6:59 pm

GhostRaider wrote:
Sat Jan 20, 2018 12:14 am
kingofthenorth wrote:
Fri Jan 19, 2018 8:53 pm

So you have to make the Raspberry Pi publish to a topic that the iOS device is subscribed to, and then send the appropriate message like "filedone" or something like that. In iOS, you would have a function that receives messages, decodes it and does a string comparison. So if message == "filedone", then do something.

Thanks for the feedback everyone! It's been very helpful. I'm still not able to receive the message from the Pi. I'm obviously missing something.

Here's a little test app I have in place.

RPi:

Code: Select all

import paho.mqtt.client as MQTT

def connectionStatus(client, userdata, flag, rc):
    print("Connecting...")
    mqttClient.subscribe("rpi/hello")

def messageDecoder(client, userdata, msg):
    message = msg.payload.decode(encoding='UTF-8')
    
    if message == "Hello":
        print("Sending reply...")
        mqttClient.publish("rpi/world", "World")

clientName = "Pi"
serverAddress = ip
mqttClient = MQTT.Client(clientName)
mqttClient.on_connect = connectionStatus
mqttClient.on_message = messageDecoder
mqttClient.connect(serverAddress)
mqttClient.loop_forever()
iOS:

Code: Select all

let client = CocoaMQTT(clientID: "iOS Device", host: ip, port: 1883)

@IBAction func connect(_ sender: UIButton) {
  client.connect()
}

@IBAction func disconnect(_ sender: UIButton) {
  client.disconnect()
}
  
@IBAction func send(_ sender: UIButton) {
  client.publish("rpi/hello", withString: "Hello")
  client.subscribe("rpi/world")
}

func mqtt(_ mqtt: CocoaMQTT, didReceiveMessage message: CocoaMQTTMessage, id: UInt16 ) {
  print(String(bytes: message.payload, encoding: .utf8)!)
}

MQTT Server:

Code: Select all

New Connection from ip on port 1883
New client connection from ip as iOS Device (c1, k60)
Sending CONNACK to iOS Device (0,0)
Received PUBLISH from iOS Device (d0, q1, r0, m8, 'rpi/hello', ...(5 bytes))
Received SUBSCRIBE from iOS Device
rpi/word (QoS 2)
iOS Device 2 rpi/world
Sending SUBACK to iOS Device

I appreciate any help!

kingofthenorth
Posts: 6
Joined: Mon Nov 20, 2017 11:18 pm

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

Thu Jan 25, 2018 7:02 pm

Francklin wrote:
Thu Jan 25, 2018 6:06 pm
Great guide, congratulations!

I will ask something "idiot"...I see the ip to make a communication. So, can I use, for example, 10 iphones into the same server (raspberry pi) ?


Att;
Francklin Souza

Are you asking if 10 iPhones can publish to the same MQTT server? If so, yes I believe so. As long as 10 iPhone are creating a client to the same IP.

Francklin
Posts: 8
Joined: Wed Jan 24, 2018 7:47 pm

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

Thu Jan 25, 2018 7:13 pm

kingofthenorth wrote:
Thu Jan 25, 2018 7:02 pm
Francklin wrote:
Thu Jan 25, 2018 6:06 pm
Great guide, congratulations!

I will ask something "idiot"...I see the ip to make a communication. So, can I use, for example, 10 iphones into the same server (raspberry pi) ?


Att;
Francklin Souza

Are you asking if 10 iPhones can publish to the same MQTT server? If so, yes I believe so. As long as 10 iPhone are creating a client to the same IP.
Exactly!

My situation:

I have a raspberry pi running mosquitto and a program python-based which power on or off a GPIO in accord with a string who receive from iphone. So, I need to make an app for everyone (any iphone) connect on raspberry to control a led, for example.
Is it possible?

kingofthenorth
Posts: 6
Joined: Mon Nov 20, 2017 11:18 pm

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

Thu Jan 25, 2018 7:19 pm

Francklin wrote:
Thu Jan 25, 2018 7:13 pm
kingofthenorth wrote:
Thu Jan 25, 2018 7:02 pm
Francklin wrote:
Thu Jan 25, 2018 6:06 pm
Great guide, congratulations!

I will ask something "idiot"...I see the ip to make a communication. So, can I use, for example, 10 iphones into the same server (raspberry pi) ?


Att;
Francklin Souza

Are you asking if 10 iPhones can publish to the same MQTT server? If so, yes I believe so. As long as 10 iPhone are creating a client to the same IP.
Exactly!

My situation:

I have a raspberry pi running mosquitto and a program python-based which power on or off a GPIO in accord with a string who receive from iphone. So, I need to make an app for everyone (any iphone) connect on raspberry to control a led, for example.
Is it possible?

Yes. This is assuming all 10 iPhones are on the same network as the Pi. I've never tried it on different networks. If so, I assume that might be a little more involved.

Francklin
Posts: 8
Joined: Wed Jan 24, 2018 7:47 pm

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

Thu Jan 25, 2018 7:21 pm

kingofthenorth wrote:
Thu Jan 25, 2018 7:19 pm
Francklin wrote:
Thu Jan 25, 2018 7:13 pm
kingofthenorth wrote:
Thu Jan 25, 2018 7:02 pm



Are you asking if 10 iPhones can publish to the same MQTT server? If so, yes I believe so. As long as 10 iPhone are creating a client to the same IP.
Exactly!

My situation:

I have a raspberry pi running mosquitto and a program python-based which power on or off a GPIO in accord with a string who receive from iphone. So, I need to make an app for everyone (any iphone) connect on raspberry to control a led, for example.
Is it possible?

Yes. This is assuming all 10 iPhones are on the same network as the Pi. I've never tried it on different networks. If so, I assume that might be a little more involved.
At the same network.
Thank you for your help!

GhostRaider
Posts: 49
Joined: Fri Jan 22, 2016 9:04 pm

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

Fri Jan 26, 2018 5:33 am

kingofthenorth wrote:
Thu Jan 25, 2018 6:59 pm
GhostRaider wrote:
Sat Jan 20, 2018 12:14 am
kingofthenorth wrote:
Fri Jan 19, 2018 8:53 pm

So you have to make the Raspberry Pi publish to a topic that the iOS device is subscribed to, and then send the appropriate message like "filedone" or something like that. In iOS, you would have a function that receives messages, decodes it and does a string comparison. So if message == "filedone", then do something.

Thanks for the feedback everyone! It's been very helpful. I'm still not able to receive the message from the Pi. I'm obviously missing something.

Here's a little test app I have in place.

RPi:

Code: Select all

import paho.mqtt.client as MQTT

def connectionStatus(client, userdata, flag, rc):
    print("Connecting...")
    mqttClient.subscribe("rpi/hello")

def messageDecoder(client, userdata, msg):
    message = msg.payload.decode(encoding='UTF-8')
    
    if message == "Hello":
        print("Sending reply...")
        mqttClient.publish("rpi/world", "World")

clientName = "Pi"
serverAddress = ip
mqttClient = MQTT.Client(clientName)
mqttClient.on_connect = connectionStatus
mqttClient.on_message = messageDecoder
mqttClient.connect(serverAddress)
mqttClient.loop_forever()
iOS:

Code: Select all

let client = CocoaMQTT(clientID: "iOS Device", host: ip, port: 1883)

@IBAction func connect(_ sender: UIButton) {
  client.connect()
}

@IBAction func disconnect(_ sender: UIButton) {
  client.disconnect()
}
  
@IBAction func send(_ sender: UIButton) {
  client.publish("rpi/hello", withString: "Hello")
  client.subscribe("rpi/world")
}

func mqtt(_ mqtt: CocoaMQTT, didReceiveMessage message: CocoaMQTTMessage, id: UInt16 ) {
  print(String(bytes: message.payload, encoding: .utf8)!)
}

MQTT Server:

Code: Select all

New Connection from ip on port 1883
New client connection from ip as iOS Device (c1, k60)
Sending CONNACK to iOS Device (0,0)
Received PUBLISH from iOS Device (d0, q1, r0, m8, 'rpi/hello', ...(5 bytes))
Received SUBSCRIBE from iOS Device
rpi/word (QoS 2)
iOS Device 2 rpi/world
Sending SUBACK to iOS Device

I appreciate any help!
Does the Raspberry Pi ever receive the message "Hello" from the iOS device? If it doesn't then that could be why.
Guides I have created on the Raspberry Pi Forums:

1) [GUIDE] Raspbian Lite with PIXEL/LXDE/XFCE/MATE/Openbox/i3
2) [GUIDE] Communicate between iOS and Raspberry Pi using MQTT

kingofthenorth
Posts: 6
Joined: Mon Nov 20, 2017 11:18 pm

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

Fri Jan 26, 2018 6:17 am

GhostRaider wrote:
Fri Jan 26, 2018 5:33 am
kingofthenorth wrote:
Thu Jan 25, 2018 6:59 pm
GhostRaider wrote:
Sat Jan 20, 2018 12:14 am



Thanks for the feedback everyone! It's been very helpful. I'm still not able to receive the message from the Pi. I'm obviously missing something.

Here's a little test app I have in place.

RPi:

Code: Select all

import paho.mqtt.client as MQTT

def connectionStatus(client, userdata, flag, rc):
    print("Connecting...")
    mqttClient.subscribe("rpi/hello")

def messageDecoder(client, userdata, msg):
    message = msg.payload.decode(encoding='UTF-8')
    
    if message == "Hello":
        print("Sending reply...")
        mqttClient.publish("rpi/world", "World")

clientName = "Pi"
serverAddress = ip
mqttClient = MQTT.Client(clientName)
mqttClient.on_connect = connectionStatus
mqttClient.on_message = messageDecoder
mqttClient.connect(serverAddress)
mqttClient.loop_forever()
iOS:

Code: Select all

let client = CocoaMQTT(clientID: "iOS Device", host: ip, port: 1883)

@IBAction func connect(_ sender: UIButton) {
  client.connect()
}

@IBAction func disconnect(_ sender: UIButton) {
  client.disconnect()
}
  
@IBAction func send(_ sender: UIButton) {
  client.publish("rpi/hello", withString: "Hello")
  client.subscribe("rpi/world")
}

func mqtt(_ mqtt: CocoaMQTT, didReceiveMessage message: CocoaMQTTMessage, id: UInt16 ) {
  print(String(bytes: message.payload, encoding: .utf8)!)
}

MQTT Server:

Code: Select all

New Connection from ip on port 1883
New client connection from ip as iOS Device (c1, k60)
Sending CONNACK to iOS Device (0,0)
Received PUBLISH from iOS Device (d0, q1, r0, m8, 'rpi/hello', ...(5 bytes))
Received SUBSCRIBE from iOS Device
rpi/word (QoS 2)
iOS Device 2 rpi/world
Sending SUBACK to iOS Device

I appreciate any help!
Does the Raspberry Pi ever receive the message "Hello" from the iOS device? If it doesn't then that could be why.

Yes the Pi is receiving the message "Hello" from the iOS device.

When publish from the iOS device to the Pi, the Pi does a publish on topic /rpi/world. This is how the log looks:

Code: Select all

Received PUBLISH from iOS Device ('rpi/hello')
Sending PUBACK to iOS Device
Sending PUBLISH to Pi ('/rpi/hello')
Received PUBLISH from Pi ('/rpi/world')
Sending PUBLISH to iOS Device ('rpi/world')

OldPCGuy
Posts: 81
Joined: Mon Jan 02, 2017 4:53 pm
Location: Detroit Michigan

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

Mon Feb 26, 2018 4:21 pm

Dumb question; how do you take the iOS program you write from simulator to the actual phone?


Never mind figured it out ;)

OldPCGuy
Posts: 81
Joined: Mon Jan 02, 2017 4:53 pm
Location: Detroit Michigan

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

Sun Mar 04, 2018 9:15 pm

When I build the workspace under Xcode v9.2 I get the following error>100 times ;
"Block implicitly retains 'self'; explicitly mention 'self' to indicate this is intended behavior" in the GCDAsyncSocket.m file.

Can these warning be ignored or supressed or do they indicate some other issue?
Figured it out. On the move again! ;)
Last edited by OldPCGuy on Tue Mar 06, 2018 4:16 pm, edited 1 time in total.

axopax
Posts: 4
Joined: Sun Jan 14, 2018 1:24 am

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

Mon Mar 05, 2018 6:30 am

I am trying to link the raspberry pi to the android app MQTT Dashboard. I would like to do so to be able to publish certain messages. What client ID, server, and port should I be using for the MQTT Dashboard app to link them together?

User avatar
DougieLawson
Posts: 32984
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

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

Mon Mar 05, 2018 7:13 am

Where's your broker? Use the IP address for that and port 1883.
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

2012-18: 1B*5, 2B*2, B+, A+, Z, ZW, 3Bs*3, 3B+

Any DMs sent on Twitter will be answered next month.

mattmiller
Posts: 1861
Joined: Thu Feb 05, 2015 11:25 pm

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

Mon Mar 05, 2018 7:14 am

Assuming you've got mosquitto running on your Pi

ClientID can be anything you want (as long as its different to any other ID being used with the Pi)

Server is name or IP address of your pi - I suggest using using IP address if you know it

Port is normally 1883

pwrdwn
Posts: 1
Joined: Fri Mar 23, 2018 2:19 pm

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

Fri Mar 23, 2018 3:46 pm

Does anyone have any other troubleshooting tips besides double checking the IP address? Everything seems to be find, but nothing is happening when I press connect or flip the switch.

OldPCGuy
Posts: 81
Joined: Mon Jan 02, 2017 4:53 pm
Location: Detroit Michigan

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

Sun Apr 01, 2018 2:38 pm

I solved my issue but...

why would the Geany IDE resolve/import the paho.mqtt and the IDLE IDE would not?


should I have installed paho-mqtt under sudo? I get the following error :(

Code: Select all

====================== RESTART: /home/pi/MQTTDemo4.py =======================
Traceback (most recent call last):
  File "/home/pi/MQTTDemo4.py", line 4, in <module>
    import paho.mqtt.client as mqtt
ImportError: No module named 'paho'
>>> 

Code: Select all

[email protected]:~ $ pip install paho-mqtt
Collecting paho-mqtt
  Downloading paho-mqtt-1.3.1.tar.gz (80kB)
    100% |████████████████████████████████| 81kB 815kB/s 
Building wheels for collected packages: paho-mqtt
  Running setup.py bdist_wheel for paho-mqtt ... done
  Stored in directory: /home/pi/.cache/pip/wheels/20/d8/0d/acdc8f2890111b7be7de71deebef0642fb83be0313dfff0493
Successfully built paho-mqtt
Installing collected packages: paho-mqtt
Successfully installed paho-mqtt-1.3.1
[email protected]:~ $ ^C
[email protected]:~ $ 

asandford
Posts: 1999
Joined: Mon Dec 31, 2012 12:54 pm
Location: Waterlooville

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

Sun Apr 01, 2018 6:13 pm

OldPCGuy wrote:
Sun Apr 01, 2018 2:38 pm

should I have installed paho-mqtt under sudo?
Yes

HDST89
Posts: 1
Joined: Fri Apr 06, 2018 3:28 pm

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

Fri Apr 06, 2018 3:44 pm

Thanks for the tutorial, very helpful. Everything worked right away and I added subscribe and unsubscribe. When I subscribe to a published topic from iOS, the broker is receiving the message and forwarding to iOS so I would like to view on the app. What's the MQTT callback in swift to receive message? An example code would be appreciated (as you've done with this tutorial).

MartenKL
Posts: 1
Joined: Tue May 01, 2018 11:20 pm

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

Tue May 01, 2018 11:27 pm

Awesome guide, thanks. Too bad pahoo.mqtt isn't available in Pythonista on iOS. That would be a great test environment.

Pheaktra
Posts: 1
Joined: Wed May 16, 2018 9:27 am

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

Wed May 16, 2018 9:33 am

Hi ! So I have follow your post and find out everything work perfectly especially the iOS app but as you only focus on publish message from iOS, can you please help me with how to get iOS subscribed to any topic and display subscribed message on the screen, cuz I'm just the beginner level when talk about swift with mqtt
Thanks you and looking forward to your reply

prwiley
Posts: 1
Joined: Tue Jun 19, 2018 2:08 pm

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

Tue Jun 19, 2018 2:12 pm

I get to the step

Code: Select all

git clone https://github.com/eclipse/mosquitto.gi
t

and get

[email protected]:/ $ sudo git clone https://github.com/eclipse/mosquitto.git
Cloning into 'mosquitto'...
remote: Counting objects: 7380, done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 7380 (delta 0), reused 3 (delta 0), pack-reused 7376
Receiving objects: 100% (7380/7380), 2.08 MiB | 0 bytes/s, done.
Resolving deltas: 100% (5632/5632), done.
Checking connectivity... done.

but there is no mosquito directory in my home folder. Would it have gone someplace else?

Return to “Automation, sensing and robotics”

Who is online

Users browsing this forum: No registered users and 13 guests