2012-04-04 03:44:45 -04:00
|
|
|
/*
|
2012-09-13 22:35:48 +01:00
|
|
|
Basic MQTT example with Authentication
|
2015-08-27 14:18:16 +01:00
|
|
|
|
2012-09-13 22:35:48 +01:00
|
|
|
- connects to an MQTT server, providing username
|
|
|
|
and password
|
2012-04-04 03:44:45 -04:00
|
|
|
- publishes "hello world" to the topic "outTopic"
|
|
|
|
- subscribes to the topic "inTopic"
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <SPI.h>
|
|
|
|
#include <Ethernet.h>
|
|
|
|
#include <PubSubClient.h>
|
|
|
|
|
|
|
|
// Update these with values suitable for your network.
|
|
|
|
byte mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED };
|
2015-08-27 14:18:16 +01:00
|
|
|
IPAddress ip(172, 16, 0, 100);
|
|
|
|
IPAddress server(172, 16, 0, 2);
|
2012-04-04 03:44:45 -04:00
|
|
|
|
|
|
|
void callback(char* topic, byte* payload, unsigned int length) {
|
|
|
|
// handle message arrived
|
|
|
|
}
|
|
|
|
|
2012-09-13 22:35:48 +01:00
|
|
|
EthernetClient ethClient;
|
|
|
|
PubSubClient client(server, 1883, callback, ethClient);
|
2012-04-04 03:44:45 -04:00
|
|
|
|
|
|
|
void setup()
|
|
|
|
{
|
|
|
|
Ethernet.begin(mac, ip);
|
2015-08-28 11:21:52 +01:00
|
|
|
// Note - the default maximum packet size is 128 bytes. If the
|
2017-04-30 10:43:18 +01:00
|
|
|
// combined length of clientId, username and password exceed this use the
|
|
|
|
// following to increase the buffer size:
|
|
|
|
// client.setBufferSize(255);
|
2015-08-28 11:21:52 +01:00
|
|
|
|
2012-04-04 03:44:45 -04:00
|
|
|
if (client.connect("arduinoClient", "testuser", "testpass")) {
|
|
|
|
client.publish("outTopic","hello world");
|
|
|
|
client.subscribe("inTopic");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void loop()
|
|
|
|
{
|
|
|
|
client.loop();
|
|
|
|
}
|