Skip to content

Commit

Permalink
Update MQTT examples
Browse files Browse the repository at this point in the history
- Update the discerptions
- Clear warnings
  • Loading branch information
M-ichae-l committed Jun 15, 2023
1 parent 588f47c commit 667f885
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
/*
Basic MQTT example with Authentication
- connects to an MQTT server, providing username
and password
MQTT Authentication example
- connects to an MQTT server, providing username and password
- publishes "hello world" to the topic "outTopic"
- subscribes to the topic "inTopic"
*/
Example guide:
https://www.amebaiot.com/en/amebad-arduino-mqtt-upload-listen/
*/

#include <WiFi.h>
#include <PubSubClient.h>

// Update these with values suitable for your network.
char ssid[] = "Network_SSID"; // your network SSID (name)
char pass[] = "Password"; // your network password
int status = WL_IDLE_STATUS; // Indicater of Wifi status

char mqttServer[] = "cloud.amebaiot.com";

char mqttServer[] = "cloud.amebaiot.com";
char clientId[] = "amebaClient";
char clientUser[] = "testuser";
char clientPass[] = "testpass";
Expand All @@ -25,18 +24,27 @@ char publishPayload[] = "hello world";
char subscribeTopic[] = "inTopic";

void callback(char* topic, byte* payload, unsigned int length) {
// handle message arrived
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (unsigned int i = 0; i < length; i++) {
Serial.print((char)(payload[i]));
}
Serial.println();
}

WiFiClient wifiClient;
PubSubClient client(mqttServer, 1883, callback, wifiClient);

void setup() {
//Initialize serial and wait for port to open:
Serial.begin(115200);
// wait for serial port to connect.
while (!Serial) {
;
}

//Attempt to connect to WiFi network
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
/*
Basic MQTT example
MQTT Basic example
This sketch demonstrates the basic capabilities of the library.
It connects to an MQTT server then:
- connects to an MQTT server
- publishes "hello world" to the topic "outTopic"
- subscribes to the topic "inTopic", printing out any messages
it receives. NB - it assumes the received payloads are strings not binary
- subscribes to the topic "inTopic", printing out any messages it receives. It assumes the received payloads are strings not binary
It will reconnect to the server if the connection is lost using a blocking
reconnect function. See the 'mqtt_reconnect_nonblocking' example for how to
Expand All @@ -33,7 +32,7 @@ void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i = 0; i < length; i++) {
for (unsigned int i = 0; i < length; i++) {
Serial.print((char)(payload[i]));
}
Serial.println();
Expand All @@ -45,7 +44,7 @@ PubSubClient client(wifiClient);
void reconnect() {
// Loop until we're reconnected
while (!(client.connected())) {
Serial.print("Attempting MQTT connection...");
Serial.print("\nAttempting MQTT connection...");
// Attempt to connect
if (client.connect(clientId)) {
Serial.println("connected");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/*
Publishing in the callback
- connects to an MQTT server
MQTT Publishing in the callback
- connects to an MQTT server, providing username and password
- subscribes to the topic "inTopic"
- when a message is received, republishes it to "outTopic"
Expand All @@ -12,12 +11,13 @@
This ensures the client reference in the callback function
is valid.
*/
Example guide:
https://www.amebaiot.com/en/amebad-arduino-mqtt-upload-listen/
*/

#include <WiFi.h>
#include <PubSubClient.h>

// Update these with values suitable for your network.
char ssid[] = "Network_SSID"; // your network SSID (name)
char pass[] = "Password"; // your network password
int status = WL_IDLE_STATUS; // Indicater of Wifi status
Expand All @@ -30,14 +30,18 @@ char publishTopic[] = "outTopic";
char publishPayload[] = "hello world";
char subscribeTopic[] = "inTopic";

// Callback function header
void callback(char* topic, byte* payload, unsigned int length);

WiFiClient wifiClient;
PubSubClient client(mqttServer, 1883, callback, wifiClient);

// Callback function
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
Serial.println();

// In order to republish this payload, a copy must be made
// as the orignal payload buffer will be overwritten whilst
// constructing the PUBLISH packet.
Expand All @@ -52,11 +56,14 @@ void callback(char* topic, byte* payload, unsigned int length) {
}

void setup() {
//Initialize serial and wait for port to open:
Serial.begin(115200);
// wait for serial port to connect.
while (!Serial) {
;
}

//Attempt to connect to WiFi network
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
Expand Down

0 comments on commit 667f885

Please sign in to comment.