pimansays
Posts: 13
Joined: Sun Dec 02, 2018 8:30 pm

understanding MQTT

Tue Dec 04, 2018 1:00 pm

Hi

I have been playing with MQTT using two Pi zero W. I have managed to transmit and receive MQTT messages using the command line and in python code using paho-mqtt. I have one Pi set up as the MQTT broker. However, what I want to do is have the two Pi be a standalone network with no other connections (there may be more Pi at a later date). So the code I have works fine as long as both the Pi are connected to my home network, but as soon as I disconnect one from the home network, they cannot talk to each other. (My initial assumption was that the MQTT broker would be handling all the wifi protocol etc). Presumably they are communicating via the home network. Is there a way to get MQTT to work without the home network? Do i somehow need to connect the two Pi to eachother first?

DirkS
Posts: 9936
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: understanding MQTT

Tue Dec 04, 2018 1:14 pm

Just set up 1 Pi (the one that also hosts as the broker), as an access point, leaving out the connection to the local network/Internet

scotty101
Posts: 3711
Joined: Fri Jun 08, 2012 6:03 pm

Re: understanding MQTT

Tue Dec 04, 2018 1:16 pm

MQTT won't sort out the WiFi connection for you. You will need to setup one of the Pi's as a Wifi Access point

https://www.raspberrypi.org/documentati ... s-point.md

Then the other Pi will connect to that Pi. You'll need to make sure that all of the MQTT clients point towards the correct IP address for the MQTT broker.
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

pimansays
Posts: 13
Joined: Sun Dec 02, 2018 8:30 pm

Re: understanding MQTT

Tue Dec 04, 2018 2:11 pm

Thanks DirkS and scotty101. I have followed the link and one Pi is now reconfigured as an access point and I can connect to it on my pc but I am unable to ssh into it. The wifi icon on the Pi tells me "wlan0:Configured 192.168.4.1/24. Do i need to change my ssh command in some way? I am using PuTTy

scotty101
Posts: 3711
Joined: Fri Jun 08, 2012 6:03 pm

Re: understanding MQTT

Tue Dec 04, 2018 2:22 pm

pimansays wrote:
Tue Dec 04, 2018 2:11 pm
Do i need to change my ssh command in some way?
What is your ssh command at the moment? Have you changed it to match the IP address of the Pi?
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

pimansays
Posts: 13
Joined: Sun Dec 02, 2018 8:30 pm

Re: understanding MQTT

Tue Dec 04, 2018 3:32 pm

What is your ssh command at the moment? Have you changed it to match the IP address of the Pi?
I am using 192.168.4.1/24 both with the laptop connected to the Pi wifi ("pinet" I called it) and without. I have tried other ports eg 20 and 22 also with no result

DirkS
Posts: 9936
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: understanding MQTT

Tue Dec 04, 2018 5:22 pm

pimansays wrote:
Tue Dec 04, 2018 3:32 pm
What is your ssh command at the moment? Have you changed it to match the IP address of the Pi?
I am using 192.168.4.1/24 both with the laptop connected to the Pi wifi ("pinet" I called it) and without. I have tried other ports eg 20 and 22 also with no result
Did you enable ssh on pinet?
If not then use 'sudo raspi-config' and enable it in the Interfacing submenu.

pimansays
Posts: 13
Joined: Sun Dec 02, 2018 8:30 pm

Re: understanding MQTT

Tue Dec 04, 2018 5:46 pm

Yes ssh is enabled. do i need to connect to "pinet" on my laptop (have tried connected and not connected - makes no difference)

The network properties on the laptop show

SSID: pinet
Protocol: 802.11g
Security type: WPA2-Personal
Network band: 2.4 GHz
Network channel: 7
IPv6 DNS servers: fec0:0:0:ffff::1%1
fec0:0:0:ffff::2%1
fec0:0:0:ffff::3%1
Manufacturer: Intel Corporation
Description: Intel(R) Centrino(R) Wireless-N 2230
Driver version: 15.16.0.2
Physical address (MAC): 84-A6-C8-B1-88-A4
Last edited by pimansays on Tue Dec 04, 2018 7:04 pm, edited 1 time in total.

PhatFil
Posts: 1363
Joined: Thu Apr 13, 2017 3:55 pm
Location: Oxford UK

Re: understanding MQTT

Tue Dec 04, 2018 6:25 pm

yes you must use wifi on your laptop to join the Pinet network hosted by your Pi

when connected with your laptop to the pinet net what does an

Code: Select all

ipconfig
cli command return (window$ os)

AFAIK if 192.168.4.1 is the AP addy, it should also be listed as the net gateway And be listed in the DNS servers list.

once connected to pinet can you ping either the ap or any other wifi clients?

What do you intend your mqtt clients to do ? I ask as a Pi could be overkill, within my 'under test' home automation set up with a Pi-0-W host for web server mqtt and node red, i use esp826/5 devices with a public project firmware available on github 'Tasmota'

Once you get the wrinkles ironed out of your wifi connection issues you should find all you need do is point your mqtt clients at the broker by address to get started..

pimansays
Posts: 13
Joined: Sun Dec 02, 2018 8:30 pm

Re: understanding MQTT

Tue Dec 04, 2018 7:01 pm

ipconfig on the laptop shows

Wireless LAN adapter WiFi:

Connection-specific DNS Suffix . :
Link-local IPv6 Address . . . . . : fe80::2849:7799:9f33:cd4a%7
Autoconfiguration IPv4 Address. . : 169.254.215.74
Subnet Mask . . . . . . . . . . . : 255.255.0.0
Default Gateway . . . . . . . . . :

The first line is set to Default on my normal connection and the last line has a normal ip addtress. Guess this means there is something awry with my setup

DirkS
Posts: 9936
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: understanding MQTT

Tue Dec 04, 2018 7:06 pm

pimansays wrote:
Tue Dec 04, 2018 7:01 pm
Wireless LAN adapter WiFi:

Connection-specific DNS Suffix . :
Link-local IPv6 Address . . . . . : fe80::2849:7799:9f33:cd4a%7
Autoconfiguration IPv4 Address. . : 169.254.215.74
Subnet Mask . . . . . . . . . . . : 255.255.0.0
Default Gateway . . . . . . . . . :

The first line is set to Default on my normal connection and the last line has a normal ip addtress. Guess this means there is something awry with my setup
It's not getting an IP address from pinet.
Is the DHCP server on pinet correctly configured and running?

Just for testing you could set up a static address for Wifi on your Win notebook

PhatFil
Posts: 1363
Joined: Thu Apr 13, 2017 3:55 pm
Location: Oxford UK

Re: understanding MQTT

Tue Dec 04, 2018 8:05 pm

@Dirks beat me to it..

to select a static ip in win10 right click on your wifi/net taskbar icon and select "open network and internet settings"->"Change adaptor settings"
Then right click on the wifi icon and select properties.

Select (click on) the "Internet Protocol 4 tcp/ipv4" so it is highlighted, and then click on the properties button below.it to get to where you can set a static addy.. set the gateway and dns to your pinet AP address and use 255.255.255.0 as a submask

pimansays
Posts: 13
Joined: Sun Dec 02, 2018 8:30 pm

Re: understanding MQTT

Tue Dec 04, 2018 9:24 pm

Thank to all for your help with this :D .

PhatFil yes your right, a Pi is over kill but as I have 2 its ok to prove concept. The comms i need is very simple, just a server unit that sends a signal to the clients to start a function and the clients to send back a signal once complete. So esp8266/esp32 or similar would do. Ideal end state will be mobile phone as the server/master and simple microcontroller as the clients. But thats a long way away

I have selected a static IP address as per
to select a static ip in win10 right click on your wifi/net taskbar icon and select "open network and internet settings"->"Change adaptor settings"
Then right click on the wifi icon and select properties.

Select (click on) the "Internet Protocol 4 tcp/ipv4" so it is highlighted, and then click on the properties button below.it to get to where you can set a static addy.. set the gateway and dns to your pinet AP address and use 255.255.255.0 as a submask
I can now ssh into the Pi, but presumably need to do something to fix what I have done manually?

pimansays
Posts: 13
Joined: Sun Dec 02, 2018 8:30 pm

Re: understanding MQTT

Tue Dec 04, 2018 9:25 pm

Thank to all for your help with this :D .

PhatFil yes your right, a Pi is over kill but as I have 2 its ok to prove concept. The comms i need is very simple, just a server unit that sends a signal to the clients to start a function and the clients to send back a signal once complete. So esp8266/esp32 or similar would do. Ideal end state will be mobile phone as the server/master and simple microcontroller as the clients. But thats a long way away

I have selected a static IP address as per
to select a static ip in win10 right click on your wifi/net taskbar icon and select "open network and internet settings"->"Change adaptor settings"
Then right click on the wifi icon and select properties.

Select (click on) the "Internet Protocol 4 tcp/ipv4" so it is highlighted, and then click on the properties button below.it to get to where you can set a static addy.. set the gateway and dns to your pinet AP address and use 255.255.255.0 as a submask
I can now ssh into the Pi, but presumably need to do something to fix what I have done manually?

PhatFil
Posts: 1363
Joined: Thu Apr 13, 2017 3:55 pm
Location: Oxford UK

Re: understanding MQTT

Tue Dec 04, 2018 11:58 pm

well if your 'PiNet' is limited to 2x devices and your going to add to it 1 device at a time? whats wrong with running with static ip addys??

But if you want to get the slate clean its smelling very much like a dhcp server failure as suggested above,

im fairly sure systemd is used to manage dhcp as a service, https://www.raspberrypi.org/documentati ... systemd.md
so the systemctl command should give you a current status report on services running or halted. Any information gleaned from that regarding dhcp will indicate at what or where to look next, .- probably a typo somewhere ;) generally is this end..

pimansays
Posts: 13
Joined: Sun Dec 02, 2018 8:30 pm

Re: understanding MQTT

Fri Dec 07, 2018 1:03 pm

Ok Thanks. I am slowly making progress I think. I checked through everything but could not see anything wrong. Since then I have reinstalled Raspian several times and tried various other ways to set up an AP. All with not positive result. On the last attempt I again started from a clear install and followed the original instruction (from SurferTim's github rather than original link - basically the same thing) . I now seem to have an AP on some occasions ! Most of the time I am using a keyboard and mouse connected to the USB port via an adaptor and if these are connected the new network exists, but if I boot the pi with the usb adaptor disconnected, the new network does not exist.

Part of the process to set up the new network changes settings for eth0
Add a masquerade for outbound traffic on eth0:

sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
Eth0 doesn't exist as a standalone port on the PI zero, but there is an RJ45 connector on the usb adaptor so i am presuming this is having some effect. Any ideas?

Return to “Networking and servers”