publish-mqtt-minimal.ino
Sober publishing for LPWA networks: every ten minutes the MKR NB 1500 sends battery voltage and station id to a TCP relay. The deliberately minimal pattern shows the strict essence of a publish; for a real broker, plug PubSubClient into NBClient.
Hardware
An Arduino MKR NB 1500 (SAMD21 microcontroller, u-blox LTE-M/NB-IoT modem, external antenna, LiPo power on J6 or USB). IoT SIM with data IP, reachable relay server or MQTT broker over TCP, LTE antenna.
Wiring
No external wiring; the battery reading uses the board internal VBAT input.
How it works
attachGPRS obtains an IP address; NBClient opens a TCP socket. The line-based format keeps the server relay simple. The ten-minute cadence spares both battery and radio cell.
Running it
Install the Arduino SAMD Boards package and the MKRNB library, select the board, upload. Plan for an activated IoT SIM (LTE-M or NB1) and good reception: attaching to the network can take several minutes.
Variants
Switch to real MQTT (PubSubClient + NBClient), add BME280 sensors on I2C, or compress the payload to binary to save those few data bytes.
publish-mqtt-minimal.ino
// Publication MQTT "maison" (TCP brut) depuis le MKR NB 1500.
// Format minimal : topic\n valeur\n vers un petit relais TCP.
// Pour un vrai broker, remplacez par la lib PubSubClient (TCP ok).
#include <MKRNB.h>
const char PIN_SIM[] = "0000";
const char APN[] = "iot.1nce.net";
const char SERVEUR[] = "192.0.2.10"; // votre relais / broker
const int PORT = 1883;
NB nb;
NBClient client;
const long PERIODE = 600000; // 10 min (menage le reseau LPWA)
unsigned long dernier = 0;
void setup() {
Serial.begin(9600);
if (nb.begin(PIN_SIM) != NB_READY) while (true);
while (nb.getStatus() != NETWORK_JOINED) delay(2000);
nb.attachGPRS(APN);
}
void loop() {
if (millis() - dernier < PERIODE) return;
dernier = millis();
float vbat = 3.3 * analogRead(VBAT) / 1023.0 * 1.5;
if (client.connect(SERVEUR, PORT)) {
client.print("station/01/batterie\n");
client.println(vbat, 2);
client.stop();
Serial.println(F("publie"));
} else {
Serial.println(F("connexion KO, reessai au prochain cycle"));
}
}