Skip to content

Commit

Permalink
Initial release
Browse files Browse the repository at this point in the history
  • Loading branch information
sandeepmistry committed Jan 4, 2019
0 parents commit a19d994
Show file tree
Hide file tree
Showing 18 changed files with 2,160 additions and 0 deletions.
24 changes: 24 additions & 0 deletions README.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
= ArduinoMqttClient Library for Arduino =


image:https://travis-ci.org/arduino-libraries/ArduinoMqttClient.svg?branch=master["Build Status", link="https://travis-ci.org/arduino-libraries/ArduinoMqttClient"]

Allows you to send and receive MQTT messages using Arduino.

== License ==

Copyright (c) 2019 Arduino SA. All rights reserved.

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
171 changes: 171 additions & 0 deletions examples/WiFiAdvancedCallback/WiFiAdvancedCallback.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
/*
ArduinoMqttClient - WiFi Advanced Callback
This example connects to a MQTT broker and subscribes to a single topic,
it also publishes a message to another topic every 10 seconds.
When a message is received it prints the message to the serial monitor,
it uses the callback functionality of the library.
It also demonstrates how to set the will message, get/set QoS,
duplicate and retain values of messages.
The circuit:
- Arduino MKR 1000, MKR 1010 or Uno WiFi Rev.2 board
This example code is in the public domain.
*/

#include <ArduinoMqttClient.h>
#include <WiFiNINA.h> // for MKR1000 change to: #include <WiFi101.h>

#include "arduino_secrets.h"
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
char ssid[] = SECRET_SSID; // your network SSID (name)
char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)

WiFiClient wifiClient;
MqttClient mqttClient(wifiClient);

const char broker[] = "test.mosquitto.org";
const char willTopic[] = "arduino/will";
const char inTopic[] = "arduino/in";
const char outTopic[] = "arduino/out";

const long interval = 10000;
unsigned long previousMillis = 0;

int count = 0;

void setup() {
//Initialize serial and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}

// attempt to connect to Wifi network:
Serial.print("Attempting to connect to WPA SSID: ");
Serial.println(ssid);
while (WiFi.begin(ssid, pass) != WL_CONNECTED) {
// failed, retry
Serial.print(".");
delay(5000);
}

Serial.println("You're connected to the network");
Serial.println();

// You can provide a unique client ID, if not set the library uses Arduin-millis()
// Each client must have a unique client ID
// mqttClient.setId("clientId");

// You can provide a username and password for authentication
// mqttClient.setUsernamePassword("username", "password");

// set a will message, used by the broker when the connection dies unexpectantly
// you must know the size of the message before hand, and it must be set before connecting
String willPayload = "oh no!";
bool willRetain = true;
int willQos = 1;

mqttClient.beginWill(willTopic, willPayload.length(), willRetain, willQos);
mqttClient.print(willPayload);
mqttClient.endWill();

Serial.print("Attempting to connect to the MQTT broker: ");
Serial.println(broker);

if (!mqttClient.connect(broker, 1883)) {
Serial.print("MQTT connection failed! Error code = ");
Serial.println(mqttClient.connectError());

while (1);
}

Serial.println("You're connected to the MQTT broker!");
Serial.println();

// set the message receive callback
mqttClient.onMessage(onMqttMessage);

Serial.print("Subscribing to topic: ");
Serial.println(inTopic);
Serial.println();

// subscribe to a topic
// the second paramter set's the QoS of the subscription,
// the the library supports subscribing at QoS 0, 1, or 2
int subscribeQos = 1;

mqttClient.subscribe(inTopic, subscribeQos);

// topics can be unsubscribed using:
// mqttClient.unsubscribe(inTopic);

Serial.print("Waiting for messages on topic: ");
Serial.println(inTopic);
Serial.println();
}

void loop() {
// call poll() regularly to allow the library to receive MQTT messages and
// send MQTT keep alives which avoids being disconnected by the broker
mqttClient.poll();

// avoid having delays in loop, we'll use the strategy from BlinkWithoutDelay
// see: File -> Examples -> 02.Digital -> BlinkWithoutDelay for more info
unsigned long currentMillis = millis();

if (currentMillis - previousMillis >= interval) {
// save the last time a message was sent
previousMillis = currentMillis;

String payload;

payload += "hello world!";
payload += " ";
payload += count;

Serial.print("Sending message to topic: ");
Serial.println(outTopic);
Serial.println(payload);

// send message, the Print interface can be used to set the message contents
// in this case we know the size ahead of time, so the message payload can be streamed

bool retained = false;
int qos = 1;
bool dup = false;

mqttClient.beginMessage(outTopic, payload.length(), retained, qos, dup);
mqttClient.print(payload);
mqttClient.endMessage();

Serial.println();

count++;
}
}

void onMqttMessage(int messageSize) {
// we received a message, print out the topic and contents
Serial.print("Received a message with topic '");
Serial.print(mqttClient.messageTopic());
Serial.print("', duplicate = ");
Serial.print(mqttClient.messageDup() ? "true" : "false");
Serial.print(", QoS = ");
Serial.print(mqttClient.messageQoS());
Serial.print(", retained = ");
Serial.print(mqttClient.messageRetain() ? "true" : "false");
Serial.print("', length ");
Serial.print(messageSize);
Serial.println(" bytes:");

// use the Stream interface to print the contents
while (mqttClient.available()) {
Serial.print((char)mqttClient.read());
}
Serial.println();

Serial.println();
}
2 changes: 2 additions & 0 deletions examples/WiFiAdvancedCallback/arduino_secrets.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#define SECRET_SSID ""
#define SECRET_PASS ""
130 changes: 130 additions & 0 deletions examples/WiFiEcho/WiFiEcho.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/*
ArduinoMqttClient - WiFi Echo
This example connects to a MQTT broker and subscribes to a single topic,
it also publishes a message to the same topic once a second.
When a message is received it prints the message to the serial monitor.
The circuit:
- Arduino MKR 1000, MKR 1010 or Uno WiFi Rev.2 board
This example code is in the public domain.
*/

#include <ArduinoMqttClient.h>
#include <WiFiNINA.h> // for MKR1000 change to: #include <WiFi101.h>

#include "arduino_secrets.h"
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
char ssid[] = SECRET_SSID; // your network SSID (name)
char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)

WiFiClient wifiClient;
MqttClient mqttClient(wifiClient);

const char broker[] = "test.mosquitto.org";
const char topic[] = "arduino/echo";

const long interval = 1000;
unsigned long previousMillis = 0;

int count = 0;

void setup() {
//Initialize serial and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}

// attempt to connect to Wifi network:
Serial.print("Attempting to connect to WPA SSID: ");
Serial.println(ssid);
while (WiFi.begin(ssid, pass) != WL_CONNECTED) {
// failed, retry
Serial.print(".");
delay(5000);
}

Serial.println("You're connected to the network");
Serial.println();

// You can provide a unique client ID, if not set the library uses Arduino-millis()
// Each client must have a unique client ID
// mqttClient.setId("clientId");

// You can provide a username and password for authentication
// mqttClient.setUsernamePassword("username", "password");

Serial.print("Attempting to connect to the MQTT broker: ");
Serial.println(broker);

if (!mqttClient.connect(broker, 1883)) {
Serial.print("MQTT connection failed! Error code = ");
Serial.println(mqttClient.connectError());

while (1);
}

Serial.println("You're connected to the MQTT broker!");
Serial.println();

Serial.print("Subscribing to topic: ");
Serial.println(topic);
Serial.println();

// subscribe to a topic
mqttClient.subscribe(topic);

// topics can be unsubscribed using:
// mqttClient.unsubscribe(topic);

Serial.print("Waiting for messages on topic: ");
Serial.println(topic);
Serial.println();
}

void loop() {
// check for incoming messages
int messageSize = mqttClient.parseMessage();
if (messageSize) {
// we received a message, print out the topic and contents
Serial.println("Received a message with topic '");
Serial.print(mqttClient.messageTopic());
Serial.print("', length ");
Serial.print(messageSize);
Serial.println(" bytes:");

// use the Stream interface to print the contents
while (mqttClient.available()) {
Serial.print((char)mqttClient.read());
}
Serial.println();

Serial.println();
}

// avoid having delays in loop, we'll use the strategy from BlinkWithoutDelay
// see: File -> Examples -> 02.Digital -> BlinkWithoutDelay for more info
unsigned long currentMillis = millis();

if (currentMillis - previousMillis >= interval) {
// save the last time a message was sent
previousMillis = currentMillis;

Serial.print("Sending message to topic: ");
Serial.println(topic);
Serial.print("echo ");
Serial.println(count);

// send message, the Print interface can be used to set the message contents
mqttClient.beginMessage(topic);
mqttClient.print("echo ");
mqttClient.print(count);
mqttClient.endMessage();

Serial.println();

count++;
}
}
2 changes: 2 additions & 0 deletions examples/WiFiEcho/arduino_secrets.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#define SECRET_SSID ""
#define SECRET_PASS ""
Loading

0 comments on commit a19d994

Please sign in to comment.