Commit e3ceb40e authored by louis.colclough's avatar louis.colclough

Add new file

parent 75bb0870
#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();
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment