SHARE
TWEET

hms_mqtt_example

adamundefined Apr 28th, 2018 (edited) 16 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  Basic MQTT example
  3.  
  4.  This sketch demonstrates the basic capabilities of the library.
  5.  It connects to an MQTT server then:
  6.   - publishes "hello world" to the topic "outTopic"
  7.   - subscribes to the topic "inTopic", printing out any messages
  8.     it receives. NB - it assumes the received payloads are strings not binary
  9.  
  10.  It will reconnect to the server if the connection is lost using a blocking
  11.  reconnect function. See the 'mqtt_reconnect_nonblocking' example for how to
  12.  achieve the same result without blocking the main loop.
  13.  
  14. */
  15.  
  16. #include <SPI.h>
  17. #include <Ethernet.h>
  18. #include <PubSubClient.h>
  19.  
  20. // Replace UNIQUE with the number you received in the workshop
  21. #define MQTTid "arduinoClient-PW-UNIQUE"
  22.  
  23. // If you do this project on your own you will need to create an account
  24. // on Adafruit.IO. You will populate your own username and AIO key
  25. // It's generally not good to share your key. I will be revoking this key
  26. // after the workshop.
  27. #define MQTTuser "REDACTED"
  28. #define MQTTpsw "REDACTED"
  29.  
  30. // MAC address for the Ethernet Shield. This needs to be unique on the network.
  31. // Replace 'UNIQUE' with the number you were given at the workshop
  32. byte mac[]    = {  0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xUNIQUE };
  33.  
  34. void callback(char* topic, byte* payload, unsigned int length) {
  35.   Serial.print("Message arrived [");
  36.   Serial.print(topic);
  37.   Serial.print("] ");
  38.   for (int i=0;i<length;i++) {
  39.     Serial.print((char)payload[i]);
  40.   }
  41.   Serial.println();
  42. }
  43.  
  44. EthernetClient ethClient;
  45. PubSubClient client(ethClient);
  46.  
  47. void reconnect() {
  48.   // Loop until we're reconnected
  49.   while (!client.connected()) {
  50.     Serial.print("Attempting MQTT connection...");
  51.     // Attempt to connect
  52.     if (client.connect(MQTTid, MQTTuser, MQTTpsw)) {
  53.       Serial.println("connected");
  54.       // Once connected, publish an announcement...
  55.       // Replace UNIQUE with the number you received in the workshop
  56.       client.publish("REDACTED/feeds/plant-waterer-sensors.pw-messagereceive","PW-UNIQUE connected");
  57.       // ... and resubscribe
  58.       client.subscribe("REDACTED/feeds/plant-waterer-sensors.pw-messagepublish");
  59.     } else {
  60.       Serial.print("failed, rc=");
  61.       Serial.print(client.state());
  62.       Serial.println(" try again in 5 seconds");
  63.       // Wait 5 seconds before retrying
  64.       delay(5000);
  65.     }
  66.   }
  67. }
  68.  
  69. void setup()
  70. {
  71.   Serial.begin(9600);
  72.  
  73.   client.setServer("io.adafruit.com", 1883);
  74.   client.setCallback(callback);
  75.  
  76.   Ethernet.begin(mac);
  77.   // Allow the hardware to sort itself out
  78.   delay(1500);
  79. }
  80.  
  81. void loop()
  82. {
  83.   if (!client.connected()) {
  84.     reconnect();
  85.   }
  86.   client.loop();
  87. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top