Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
I
IoT Temp
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
louis.colclough
IoT Temp
Commits
e3ceb40e
Commit
e3ceb40e
authored
Apr 12, 2024
by
louis.colclough
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add new file
parent
75bb0870
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
86 additions
and
0 deletions
+86
-0
TempMonSub
TempMonSub
+86
-0
No files found.
TempMonSub
0 → 100644
View file @
e3ceb40e
#include <ArduinoMqttClient.h>
#include <WiFiNINA.h>
#include "arduino_secrets.h"
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
char ssid[] = SECRET_SSID; // your network SSID
char pass[] = SECRET_PASS; // your network password
WiFiClient wifiClient;
MqttClient mqttClient(wifiClient);
const char broker[] = "test.mosquitto.org";
int port = 1883;
const char topic[] = "st_john_iot_temperature_value_topic";
void setup() {
// put your setup code here, to run once:
//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 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();
Serial.print("Attempting to connect to the MQTT broker: ");
Serial.println(broker);
if (!mqttClient.connect(broker, port)) {
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.println("Subscribing to topic: ");
Serial.println(topic);
Serial.println();
// subscribe to a topic
mqttClient.subscribe(topic);
// topics can be unsubscribed using:
// mqttClient.unsubscribe(topic);
Serial.println("Topic: ");
Serial.println(topic);
Serial.println();
}
void loop() {
// put your main code here, to run repeatedly:
// call poll() regularly to allow the library to receive MQTT messages and
// send MQTT keep alive which avoids being disconnected by the broker
mqttClient.poll();
}
void onMqttMessage(int messageSize) {
// we received a message, print out the topic and contents
Serial.println("Recorded Temperature value is '");
Serial.println(mqttClient.messageTopic());
// use the Stream interface to print the contents
while (mqttClient.available()) {
Serial.print((char)mqttClient.read());
}
Serial.println();
Serial.println();
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment