-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path01-mqtt_test.ino
107 lines (91 loc) · 3 KB
/
01-mqtt_test.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
Author : Teeraphat Kullanankanjana
Version : 1.0
Date : 31/07/2024
Description : Connects to a specified WiFi network, publishes user-entered text to a specified MQTT topic,
and subscribes to the same topic to listen for incoming messages. The MQTT connection is maintained
and messages are processed via serial input and MQTT callbacks. The code handles WiFi connection,
MQTT connection, publishing messages, and receiving messages using the Arduino MKR WiFi 1010.
Copyright (C) 2024 Teeraphat Kullanankanjana. All rights reserved.
*/
// Include necessary libraries
#include <WiFiNINA.h>
#include <PubSubClient.h>
// Replace with your network credentials
const char* ssid = "your_ssid";
const char* password = "your_password";
// MQTT broker address
const char* mqtt_server = "test.mosquitto.org";
// MQTT topic
const char* mqtt_topic = "test/test";
// Create instances for WiFi and MQTT clients
WiFiClient espClient;
PubSubClient client(espClient);
// Function to handle incoming messages
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++) {
Serial.print((char)payload[i]);
}
Serial.println();
}
// Function to connect to MQTT broker
void connectMQTT() {
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Generate a random client ID
String clientId = "ArduinoClient-";
clientId += String(random(0xffff), HEX);
// Attempt to connect
if (client.connect(clientId.c_str())) {
Serial.println("connected");
// Subscribe to the topic
client.subscribe(mqtt_topic);
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
delay(5000);
}
}
}
void setup() {
// Initialize serial communication
Serial.begin(9600);
// Connect to WiFi
while (WiFi.status() != WL_CONNECTED) {
Serial.print("Connecting to ");
Serial.print(ssid);
Serial.print("...");
WiFi.begin(ssid, password);
delay(5000);
}
Serial.println("Connected to WiFi");
Serial.print("IP Address: ");
Serial.println(WiFi.localIP()); // Print the local IP address
Serial.print("Signal Strength (dBm): ");
Serial.println(WiFi.RSSI()); // Print the signal strength of the connected network
// Set up MQTT server and callback
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
// Connect to MQTT broker
connectMQTT();
}
void loop() {
// Ensure the client is connected
if (!client.connected()) {
connectMQTT();
}
client.loop();
// Check if serial data is available
if (Serial.available() > 0) {
String message = Serial.readStringUntil('\n');
message.trim(); // Remove any leading or trailing whitespace
// Publish the message to the MQTT topic
client.publish(mqtt_topic, message.c_str());
Serial.print("Published: ");
Serial.println(message);
}
}