You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* added mqtt pub/sub example
* added mqtt pub/sub example
* removed unnecessary comments
* Move mqtt example to wifi folder
* Tidy up mqtt example
Fix review comments and build warnings.
Use MQTT_SERVER build variable instead of hard coding IP address.
Make a DNS request for the server.
Use async timer for publishing the temperature.
* Add support for username and password
* Add TLS support
Move the code down one level
* Add some more topics
print, just print the message to stdout
ping, publish uptime to pong
exit, quit the client
* Add will topic /online
Add some defines for some magic numbers
* Unique topic names per device
Disabled by default with MQTT_UNIQUE_TOPIC
* Some MQTT updates
Change /pong to /uptime
Display a message if MQTT_SERVER is not defined
Put picow_ in the name like the other examples
Add details to the readme!
---------
Co-authored-by: ellebi2000 <[email protected]>
Co-authored-by: Peter Harper <[email protected]>
To use this example you will need to install an MQTT server on your network.
4
+
To install the Mosquitto MQTT client on a Raspberry Pi Linux device run the following...
5
+
6
+
```
7
+
sudo apt install mosquitto
8
+
sudo apt install mosquitto-clients
9
+
```
10
+
11
+
Check it works...
12
+
13
+
```
14
+
mosquitto_pub -t test_topic -m "Yes it works" -r
15
+
mosquitto_sub -t test_topic -C 1
16
+
```
17
+
18
+
To allow an external client to connect to the server you will have to change the configuration. Add the following to /etc/mosquitto/conf.d/mosquitto.conf
19
+
20
+
```
21
+
allow_anonymous true
22
+
listener 1883 0.0.0.0
23
+
```
24
+
25
+
Then restart the service.
26
+
27
+
```
28
+
sudo service mosquitto restart
29
+
```
30
+
31
+
When building the code set the host name of the MQTT server, e.g.
32
+
33
+
```
34
+
export MQTT_SERVER=myhost
35
+
cmake ..
36
+
```
37
+
38
+
The example should publish its core temperature to the /temperature topic. You can subscribe to this topic from another machine.
39
+
40
+
```
41
+
mosquitto_sub -h $MQTT_SERVER -t '/temperature'
42
+
```
43
+
44
+
You can turn the led on and off by publishing messages.
45
+
46
+
```
47
+
mosquitto_pub -h $MQTT_SERVER -t '/led' -m on
48
+
mosquitto_pub -h $MQTT_SERVER -t '/led' -m off
49
+
```
50
+
51
+
# Security
52
+
53
+
If your server has a username and password, you can set these with the following variables.
54
+
55
+
```
56
+
export MQTT_USERNAME=user
57
+
export MQTT_PASSWORD=pass
58
+
```
59
+
60
+
Be aware that these details are sent in plain text unless you use TLS.
61
+
62
+
## Using TLS
63
+
64
+
To use TLS and client server authentication you need some keys and certificates for the client and server.
65
+
The `certs/makecerts.sh` script demonstrates a way to make these.
66
+
It creates a folder named after `MQTT_SERVER` containing all the required files.
67
+
From these files it generates a header file `mqtt_client.inc` included by the code.
68
+
Your server will have to be configured to use TLS and the port 8883 rather than the non-TLS port 1883.
69
+
70
+
```
71
+
listener 1883 127.0.0.1
72
+
73
+
listener 8883 0.0.0.0
74
+
allow_anonymous true
75
+
cafile /etc/mosquitto/ca_certificates/ca.crt
76
+
certfile /etc/mosquitto/certs/server.crt
77
+
keyfile /etc/mosquitto/certs/server.key
78
+
require_certificate true
79
+
```
80
+
81
+
To connect to your server with the mosquitto tools in linux you will have to pass extra parameters.
0 commit comments