Sensor Data with MQTT
Aquacloud provides sensor data capabilities thourg MQTT, a lightweight publish/subscribe protocol for machine-to-machine communication which has become
Aquacloud ingests and provides realtime sensor data through our mqtt.aquacloud.ai broker which enables environmetnal parameters such as water waility, temperature and oxygen level to be streamed directly from the farms into our systems.
We store the data in object storage dataset and create aggregate datasets for analytical consumption on top of that, or the datastream may be monitored realtime to enable alerts or real time monitoring of the temnperature at a particular site.
Aquacloud is providing mqtt connection through mqtt.aquacloud.ai on wither tcp 800 or wss (secure web sockets) at
connect to aquacloud from a server
YOu can connect to aquacloud and publish data using python with:
import paho.mqtt.client as mqtt
import json
import time
from datetime import datetime
# Connection configuration
MQTT_BROKER = "mqtt.aquacloud.ai"
MQTT_PORT = 8883
MQTT_USERNAME = "your_username"
MQTT_PASSWORD = "your_password"
# Create MQTT client
client = mqtt.Client(client_id=f"aquacloud_publisher_{int(time.time())}")
# Set credentials
client.username_pw_set(MQTT_USERNAME, MQTT_PASSWORD)
# Enable TLS/SSL for secure connection
client.tls_set()
# Connection callback
def on_connect(client, userdata, flags, rc):
if rc == 0:
print("Connected to AquaCloud MQTT broker")
else:
print(f"Connection failed with code {rc}")
# Set callback
client.on_connect = on_connect
# Connect to broker
try:
client.connect(MQTT_BROKER, MQTT_PORT, keepalive=60)
client.loop_start()
# Wait for connection
time.sleep(2)
# Publish temperature data
sites = ["site-001", "site-002", "site-003"]
for site in sites:
# Prepare temperature data
temperature_data = {
"temperature": 8.5 + (0.5 * sites.index(site)), # Example values
"unit": "°C",
"timestamp": datetime.utcnow().isoformat() + "Z",
"siteId": site,
"depth": 5, # meters
"status": "normal"
}
# Publish to topic
topic = f"salmon/{site}/temperature"
payload = json.dumps(temperature_data)
result = client.publish(topic, payload, qos=1, retain=False)
if result.rc == mqtt.MQTT_ERR_SUCCESS:
print(f"Published to {topic}: {temperature_data['temperature']}°C")
else:
print(f"Failed to publish to {topic}")
time.sleep(1) # Brief delay between publishes
# Disconnect
client.loop_stop()
client.disconnect()
print("Disconnected from MQTT broker")
except Exception as e:
print(f"Error: {e}")
Connect to aquacloud from the browser
Mqtt over websockets makes it possible to connecting toi aquacloud via web browsers for example to subscribe to a topic in a dashboard:
import * as mqtt from "mqtt";
// Connection configuration
const MQTT_URL = "wss://mqtt.aquacloud.ai:8084";
const OPTIONS: mqtt.IClientOptions = {
username: "your_username",
password: "your_password",
clientId: `aquacloud_client_${Math.random().toString(16).substr(2, 8)}`,
clean: true,
reconnectPeriod: 1000,
};
// Connect to MQTT broker
const client = mqtt.connect(MQTT_URL, OPTIONS);
// Connection event handlers
client.on("connect", () => {
console.log("Connected to AquaCloud MQTT broker");
// Subscribe to temperature topics for salmon sites
const topics = [
"salmon/+/temperature", // All sites
"salmon/site-001/temperature", // Specific site example
];
client.subscribe(topics, (err) => {
if (err) {
console.error("Subscription error:", err);
} else {
console.log("Subscribed to temperature topics");
}
});
});
// Handle incoming messages
client.on("message", (topic: string, payload: Buffer) => {
try {
const data = JSON.parse(payload.toString());
console.log(`Temperature update from ${topic}:`, {
value: data.temperature,
unit: data.unit || "°C",
timestamp: data.timestamp,
site: topic.split("/")[1],
});
} catch (error) {
console.error("Error parsing message:", error);
}
});
// Error handling
client.on("error", (error) => {
console.error("MQTT connection error:", error);
});
// Cleanup on process termination
process.on("SIGINT", () => {
client.end(() => {
console.log("Disconnected from MQTT broker");
process.exit();
});
});
also participating the Sensor Data Network 2.0 project ()