|
| 1 | +/* |
| 2 | + ArduinoMqttClient - WiFi Advanced Callback |
| 3 | +
|
| 4 | + This example connects to a MQTT broker and subscribes to a single topic, |
| 5 | + it also publishes a message to another topic every 10 seconds. |
| 6 | + When a message is received it prints the message to the serial monitor, |
| 7 | + it uses the callback functionality of the library. |
| 8 | +
|
| 9 | + It also demonstrates how to set the will message, get/set QoS, |
| 10 | + duplicate and retain values of messages. |
| 11 | +
|
| 12 | + The circuit: |
| 13 | + - Arduino MKR 1000, MKR 1010 or Uno WiFi Rev.2 board |
| 14 | +
|
| 15 | + This example code is in the public domain. |
| 16 | +*/ |
| 17 | + |
| 18 | +#include <ArduinoMqttClient.h> |
| 19 | +#include <WiFiNINA.h> // for MKR1000 change to: #include <WiFi101.h> |
| 20 | + |
| 21 | +#include "arduino_secrets.h" |
| 22 | +///////please enter your sensitive data in the Secret tab/arduino_secrets.h |
| 23 | +char ssid[] = SECRET_SSID; // your network SSID (name) |
| 24 | +char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP) |
| 25 | + |
| 26 | +WiFiClient wifiClient; |
| 27 | +MqttClient mqttClient(wifiClient); |
| 28 | + |
| 29 | +const char broker[] = "test.mosquitto.org"; |
| 30 | +const char willTopic[] = "arduino/will"; |
| 31 | +const char inTopic[] = "arduino/in"; |
| 32 | +const char outTopic[] = "arduino/out"; |
| 33 | + |
| 34 | +const long interval = 10000; |
| 35 | +unsigned long previousMillis = 0; |
| 36 | + |
| 37 | +int count = 0; |
| 38 | + |
| 39 | +void setup() { |
| 40 | + //Initialize serial and wait for port to open: |
| 41 | + Serial.begin(9600); |
| 42 | + while (!Serial) { |
| 43 | + ; // wait for serial port to connect. Needed for native USB port only |
| 44 | + } |
| 45 | + |
| 46 | + // attempt to connect to Wifi network: |
| 47 | + Serial.print("Attempting to connect to WPA SSID: "); |
| 48 | + Serial.println(ssid); |
| 49 | + while (WiFi.begin(ssid, pass) != WL_CONNECTED) { |
| 50 | + // failed, retry |
| 51 | + Serial.print("."); |
| 52 | + delay(5000); |
| 53 | + } |
| 54 | + |
| 55 | + Serial.println("You're connected to the network"); |
| 56 | + Serial.println(); |
| 57 | + |
| 58 | + // You can provide a unique client ID, if not set the library uses Arduin-millis() |
| 59 | + // Each client must have a unique client ID |
| 60 | + // mqttClient.setId("clientId"); |
| 61 | + |
| 62 | + // You can provide a username and password for authentication |
| 63 | + // mqttClient.setUsernamePassword("username", "password"); |
| 64 | + |
| 65 | + // set a will message, used by the broker when the connection dies unexpectantly |
| 66 | + // you must know the size of the message before hand, and it must be set before connecting |
| 67 | + String willPayload = "oh no!"; |
| 68 | + bool willRetain = true; |
| 69 | + int willQos = 1; |
| 70 | + |
| 71 | + mqttClient.beginWill(willTopic, willPayload.length(), willRetain, willQos); |
| 72 | + mqttClient.print(willPayload); |
| 73 | + mqttClient.endWill(); |
| 74 | + |
| 75 | + Serial.print("Attempting to connect to the MQTT broker: "); |
| 76 | + Serial.println(broker); |
| 77 | + |
| 78 | + if (!mqttClient.connect(broker, 1883)) { |
| 79 | + Serial.print("MQTT connection failed! Error code = "); |
| 80 | + Serial.println(mqttClient.connectError()); |
| 81 | + |
| 82 | + while (1); |
| 83 | + } |
| 84 | + |
| 85 | + Serial.println("You're connected to the MQTT broker!"); |
| 86 | + Serial.println(); |
| 87 | + |
| 88 | + // set the message receive callback |
| 89 | + mqttClient.onMessage(onMqttMessage); |
| 90 | + |
| 91 | + Serial.print("Subscribing to topic: "); |
| 92 | + Serial.println(inTopic); |
| 93 | + Serial.println(); |
| 94 | + |
| 95 | + // subscribe to a topic |
| 96 | + // the second paramter set's the QoS of the subscription, |
| 97 | + // the the library supports subscribing at QoS 0, 1, or 2 |
| 98 | + int subscribeQos = 1; |
| 99 | + |
| 100 | + mqttClient.subscribe(inTopic, subscribeQos); |
| 101 | + |
| 102 | + // topics can be unsubscribed using: |
| 103 | + // mqttClient.unsubscribe(inTopic); |
| 104 | + |
| 105 | + Serial.print("Waiting for messages on topic: "); |
| 106 | + Serial.println(inTopic); |
| 107 | + Serial.println(); |
| 108 | +} |
| 109 | + |
| 110 | +void loop() { |
| 111 | + // call poll() regularly to allow the library to receive MQTT messages and |
| 112 | + // send MQTT keep alives which avoids being disconnected by the broker |
| 113 | + mqttClient.poll(); |
| 114 | + |
| 115 | + // avoid having delays in loop, we'll use the strategy from BlinkWithoutDelay |
| 116 | + // see: File -> Examples -> 02.Digital -> BlinkWithoutDelay for more info |
| 117 | + unsigned long currentMillis = millis(); |
| 118 | + |
| 119 | + if (currentMillis - previousMillis >= interval) { |
| 120 | + // save the last time a message was sent |
| 121 | + previousMillis = currentMillis; |
| 122 | + |
| 123 | + String payload; |
| 124 | + |
| 125 | + payload += "hello world!"; |
| 126 | + payload += " "; |
| 127 | + payload += count; |
| 128 | + |
| 129 | + Serial.print("Sending message to topic: "); |
| 130 | + Serial.println(outTopic); |
| 131 | + Serial.println(payload); |
| 132 | + |
| 133 | + // send message, the Print interface can be used to set the message contents |
| 134 | + // in this case we know the size ahead of time, so the message payload can be streamed |
| 135 | + |
| 136 | + bool retained = false; |
| 137 | + int qos = 1; |
| 138 | + bool dup = false; |
| 139 | + |
| 140 | + mqttClient.beginMessage(outTopic, payload.length(), retained, qos, dup); |
| 141 | + mqttClient.print(payload); |
| 142 | + mqttClient.endMessage(); |
| 143 | + |
| 144 | + Serial.println(); |
| 145 | + |
| 146 | + count++; |
| 147 | + } |
| 148 | +} |
| 149 | + |
| 150 | +void onMqttMessage(int messageSize) { |
| 151 | + // we received a message, print out the topic and contents |
| 152 | + Serial.print("Received a message with topic '"); |
| 153 | + Serial.print(mqttClient.messageTopic()); |
| 154 | + Serial.print("', duplicate = "); |
| 155 | + Serial.print(mqttClient.messageDup() ? "true" : "false"); |
| 156 | + Serial.print(", QoS = "); |
| 157 | + Serial.print(mqttClient.messageQoS()); |
| 158 | + Serial.print(", retained = "); |
| 159 | + Serial.print(mqttClient.messageRetain() ? "true" : "false"); |
| 160 | + Serial.print("', length "); |
| 161 | + Serial.print(messageSize); |
| 162 | + Serial.println(" bytes:"); |
| 163 | + |
| 164 | + // use the Stream interface to print the contents |
| 165 | + while (mqttClient.available()) { |
| 166 | + Serial.print((char)mqttClient.read()); |
| 167 | + } |
| 168 | + Serial.println(); |
| 169 | + |
| 170 | + Serial.println(); |
| 171 | +} |
0 commit comments