Hi Gang
I'm sending MQTT data periodically to a broker. I'm using the WiFiNina and ArduinoMqttClient Libraries to publish data and the RTCZero Library to put the ARDUINO NANO 33 IoT BOARD periodically into standby to conserve power.
If I simply use the RTCZero Library my Sketch works.
//TEST RTCZERO BY JWTFPV V1
#include <RTCZero.h>
bool staAla = false;
byte minAla = 0;
RTCZero rtcZer;
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
rtcZer.begin();
rtcZer.setDate(1, 1, 20);
rtcZer.setTime(0, 0, 0);
rtcZer.setAlarmMinutes(minAla);
rtcZer.enableAlarm(rtcZer.MATCH_MMSS);
rtcZer.attachInterrupt(isrAla);
delay(10000);
}
void loop() {
if (staAla) {
staAla = false;
minAla += 1;
minAla %= 60;
rtcZer.setAlarmMinutes(minAla);
digitalWrite(LED_BUILTIN, HIGH);
delay(1000);
digitalWrite(LED_BUILTIN, LOW);
delay(1000);
rtcZer.standbyMode();
}
}
void isrAla() {
staAla = true;
}
If I combine the ArduinoMqttClient, RTCZero & WiFiNina Libraries my Sketch doesn't work.
//TEST RTCZERO BY JWTFPV V2
#include <ArduinoMqttClient.h>
#include <RTCZero.h>
#include <SPI.h>
#include <WiFiNINA.h>
const char mqttBroker[] = "xxxxxx";
const char mqttPassword[] = "xxxxxx";
const char mqttUser[] = "xxxxxx";
const char topic0[] = "xxxxxx";
const char wifiPassword[] = "xxxxxx";
const char wifiSSID[] = "xxxxxx";
const int mqttPort = xxxxxx;
bool staAla = false;
byte minAla = 0;
WiFiSSLClient wifiClient;
MqttClient mqttClient(wifiClient);
RTCZero rtcZer;
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
rtcZer.begin();
rtcZer.setDate(1, 1, 20);
rtcZer.setTime(0, 0, 0);
rtcZer.setAlarmMinutes(minAla);
rtcZer.enableAlarm(rtcZer.MATCH_MMSS);
rtcZer.attachInterrupt(isrAla);
delay(10000);
}
void loop() {
if (staAla) {
digitalWrite(LED_BUILTIN, HIGH);
staAla = false;
minAla += 1;
minAla %= 60;
rtcZer.setAlarmMinutes(minAla);
while (WiFi.begin(wifiSSID, wifiPassword) != WL_CONNECTED) delay(10000);
mqttClient.setUsernamePassword(mqttUser, mqttPassword);
if (!mqttClient.connect(mqttBroker, mqttPort)) while (1);
mqttClient.poll();
mqttClient.beginMessage(topic0);
mqttClient.print(1);
mqttClient.endMessage();
WiFi.end();
digitalWrite(LED_BUILTIN, LOW);
rtcZer.standbyMode();
}
}
void isrAla() {
staAla = true;
}
If I use a simple 'BlinkWithoutDelay' approach along with putting the critical WiFi.begin and mqttClient.connect in void setup() my Sketch does work.
//TEST RTCZERO BY JWTFPV V3
#include <ArduinoMqttClient.h>
#include <SPI.h>
#include <WiFiNINA.h>
const char mqttBroker[] = "xxxxxx";
const char mqttPassword[] = "xxxxxx";
const char mqttUser[] = "xxxxxx";
const char topic0[] = "xxxxxx";
const char wifiPassword[] = "xxxxxx";
const char wifiSSID[] = "xxxxxx";
const int mqttPort = xxxxxx;
const unsigned long milPer = 1 * 60 * 1000;
unsigned long milOld = 0;
WiFiSSLClient wifiClient;
MqttClient mqttClient(wifiClient);
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
while (WiFi.begin(wifiSSID, wifiPassword) != WL_CONNECTED) delay(10000);
mqttClient.setUsernamePassword(mqttUser, mqttPassword);
if (!mqttClient.connect(mqttBroker, mqttPort)) while (1);
}
void loop() {
unsigned long milNew = millis();
if (milPer <= milNew - milOld) {
digitalWrite(LED_BUILTIN, HIGH);
milOld = milNew;
mqttClient.beginMessage(topic0);
mqttClient.print(1);
mqttClient.endMessage();
digitalWrite(LED_BUILTIN, LOW);
}
mqttClient.poll();
}
The problem with this last approach is twofold.
-
I'm unable to put the board into standby thus saving power.
-
If I lose internet I will have to perform a manual reset.
I'm wondering if anyone could shed some light as to where I might be going wrong?
Cheers
Jase