mqtt_auth.ino 1.1 KB

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