Mosquitto - added Mosquitto to changelog
I have it installed and up and running, I have an Arduino MKR WiFi 1010 with a MKRENV shield running of battery power publishing data to Mosquitto and its working without issue. I can connect to Mosquitto using MQTT explorer and can confirm that the data is, indeed, being published. I tried a mobile app to connect and subscribe to topics and can subscribe and get live data as its published. All of this is only done via local LAN, not externally.
I have Mosquitto running on an authentication basis which requires Username and Password. no TTS has been added, may look into that for future to improve security for the connection
Mosquitto Running on pi-top[4]
Viewing Data via MQTT Explorer
Viewing Data as a Subscriber to topic via Mobile Phone
Mosquitto Is a success and working as expected, this is not running automatically, only manual running to begin with
Arduino Code
Here is the code used for the Arduino MKR WiFi 1010 and ENV shield for those that is interested, not hiding username and password for the MQTT server because its the same as RPiOS default but removed the WiFi SSID and password.
Data is published to Mosquitto every 1 min
#include <PubSubClient.h> // Connect and publish to the MQTT broker
#include <WiFiNINA.h> // Enables the WiFiNina to connect to the local network
#include <Arduino_MKRENV.h> // communicate with the MKRENV Shield
// WiFi
const char* ssid = "SSID_NAME_HERE"; // Your personal network SSID
const char* wifi_password = "WIFI_PASSWORD"; // Your personal network password
// MQTT
const char* mqtt_server = "192.168.1.135"; // IP of the MQTT broker
const char* humidity_topic = "home/test/humidity";
const char* temperature_topic = "home/test/temperature";
const char* pressure_topic = "home/test/pressure";
const char* illuminance_topic = "home/test/illuminance";
const char* uvIndex_topic = "home/test/uvIndex";
const char* mqtt_username = "pi"; // MQTT username
const char* mqtt_password = "raspberry"; // MQTT password
const char* clientID = "client_ArduinoMKR1010"; // MQTT client ID
// Initialise the WiFi and MQTT Client objects
WiFiClient wifiClient;
// 1883 is the listener port for the Broker
PubSubClient client(mqtt_server, 1883, wifiClient);
// Custom function to connect to the MQTT broker via WiFi
void connect_MQTT(){
Serial.print("Connecting to ");
Serial.println(ssid);
// Connect to the WiFi
WiFi.begin(ssid, wifi_password);
// Wait until the connection has been confirmed before continuing
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
// Debugging - Output the IP Address of the ESP8266
Serial.println("WiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
// Connect to MQTT Broker
// client.connect returns a boolean value to let us know if the connection was successful.
// If the connection is failing, make sure you are using the correct MQTT Username and Password (Setup Earlier in the Instructable)
if (client.connect(clientID, mqtt_username, mqtt_password)) {
Serial.println("Connected to MQTT Broker!");
}
else {
Serial.println("Connection to MQTT Broker failed...");
}
}
void setup() {
Serial.begin(9600);
while (!Serial);
if (!ENV.begin()) {
Serial.println("Failed to initialize MKR ENV shield!");
while (1);
}
}
void loop() {
connect_MQTT();
Serial.setTimeout(5000);
float t = ENV.readTemperature();
float h = ENV.readHumidity();
float p = ENV.readPressure();
float i = ENV.readIlluminance();
float u = ENV.readUVIndex();
Serial.print("Temperature = ");
Serial.print(t);
Serial.println(" °C");
Serial.print("Humidity = ");
Serial.print(h);
Serial.println(" %");
Serial.print("Pressure = ");
Serial.print(p);
Serial.println(" kPa");
Serial.print("Illuminance = ");
Serial.print(i);
Serial.println(" lx");
Serial.print("UV Index = ");
Serial.println(u);
// MQTT can only transmit strings
String ts="Temp: "+String((float)t)+" C ";
String hs="Humi: "+String((float)h)+" % ";
String ps="Pres: "+String((float)t)+" kPa ";
String is="Illu: "+String((float)t)+" lx ";
String us="UV : "+String((float)t)+;
// PUBLISH to the MQTT Broker (topic = Temperature, defined at the beginning)
if (client.publish(temperature_topic, String(t).c_str())) {
Serial.println("Temperature sent!");
}
// Again, client.publish will return a boolean value depending on whether it succeded or not.
// If the message failed to send, we will try again, as the connection may have broken.
else {
Serial.println("Temperature failed to send. Reconnecting to MQTT Broker and trying again");
client.connect(clientID, mqtt_username, mqtt_password);
delay(10); // This delay ensures that client.publish doesn't clash with the client.connect call
client.publish(temperature_topic, String(t).c_str());
}
// PUBLISH to the MQTT Broker (topic = Humidity, defined at the beginning)
if (client.publish(humidity_topic, String(h).c_str())) {
Serial.println("Humidity sent!");
}
// Again, client.publish will return a boolean value depending on whether it succeded or not.
// If the message failed to send, we will try again, as the connection may have broken.
else {
Serial.println("Humidity failed to send. Reconnecting to MQTT Broker and trying again");
client.connect(clientID, mqtt_username, mqtt_password);
delay(10); // This delay ensures that client.publish doesn't clash with the client.connect call
client.publish(humidity_topic, String(h).c_str());
}
// PUBLISH to the MQTT Broker (topic = Humidity, defined at the beginning)
if (client.publish(pressure_topic, String(p).c_str())) {
Serial.println("Air Pressure sent!");
}
// Again, client.publish will return a boolean value depending on whether it succeded or not.
// If the message failed to send, we will try again, as the connection may have broken.
else {
Serial.println("Air Pressure failed to send. Reconnecting to MQTT Broker and trying again");
client.connect(clientID, mqtt_username, mqtt_password);
delay(10); // This delay ensures that client.publish doesn't clash with the client.connect call
client.publish(pressure_topic, String(p).c_str());
}
if (client.publish(illuminance_topic, String(i).c_str())) {
Serial.println("Illuminance sent!");
}
// Again, client.publish will return a boolean value depending on whether it succeded or not.
// If the message failed to send, we will try again, as the connection may have broken.
else {
Serial.println("Illuminance failed to send. Reconnecting to MQTT Broker and trying again");
client.connect(clientID, mqtt_username, mqtt_password);
delay(10); // This delay ensures that client.publish doesn't clash with the client.connect call
client.publish(illuminance_topic, String(i).c_str());
}
if (client.publish(uvIndex_topic, String(u).c_str())) {
Serial.println("UV Index sent!");
}
// Again, client.publish will return a boolean value depending on whether it succeded or not.
// If the message failed to send, we will try again, as the connection may have broken.
else {
Serial.println("UV Index failed to send. Reconnecting to MQTT Broker and trying again");
client.connect(clientID, mqtt_username, mqtt_password);
delay(10); // This delay ensures that client.publish doesn't clash with the client.connect call
client.publish(uvIndex_topic, String(u).c_str());
}
client.disconnect(); // disconnect from the MQTT broker
delay(1000*60); // print new values every 1 Minute
}
Documentation completed and example Arduino code uploaded