1 | /*
|
---|
2 | Basic MQTT example with Authentication
|
---|
3 |
|
---|
4 | - connects to an MQTT server, providing username
|
---|
5 | and password
|
---|
6 | - publishes "hello world" to the topic "outTopic"
|
---|
7 | - subscribes to the topic "inTopic"
|
---|
8 | */
|
---|
9 |
|
---|
10 | #include <SPI.h>
|
---|
11 | #include <Ethernet.h>
|
---|
12 | #include <PubSubClient.h>
|
---|
13 |
|
---|
14 | // Update these with values suitable for your network.
|
---|
15 | byte mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED };
|
---|
16 | IPAddress ip(172, 16, 0, 100);
|
---|
17 | IPAddress server(172, 16, 0, 2);
|
---|
18 |
|
---|
19 | void callback(char* topic, byte* payload, unsigned int length) {
|
---|
20 | // handle message arrived
|
---|
21 | }
|
---|
22 |
|
---|
23 | EthernetClient ethClient;
|
---|
24 | PubSubClient client(server, 1883, callback, ethClient);
|
---|
25 |
|
---|
26 | void setup()
|
---|
27 | {
|
---|
28 | Ethernet.begin(mac, ip);
|
---|
29 | // Note - the default maximum packet size is 128 bytes. If the
|
---|
30 | // combined length of clientId, username and password exceed this,
|
---|
31 | // you will need to increase the value of MQTT_MAX_PACKET_SIZE in
|
---|
32 | // PubSubClient.h
|
---|
33 |
|
---|
34 | if (client.connect("arduinoClient", "testuser", "testpass")) {
|
---|
35 | client.publish("outTopic","hello world");
|
---|
36 | client.subscribe("inTopic");
|
---|
37 | }
|
---|
38 | }
|
---|
39 |
|
---|
40 | void loop()
|
---|
41 | {
|
---|
42 | client.loop();
|
---|
43 | }
|
---|