When I launch the function getWeather() from setup() the client.readStringUntil('\r') works, but when launched by RTCZero alarm trigger it seems to crash.
#include <SPI.h>
#include <SD.h>
#include <WiFiNINA.h>
#include <ArduinoJson.h>
#include <RTCZero.h>
char ssid[] = "xxxxxxx"; // WiFi SSID
char pass[] = "xxxxxxx"; // Wifi Key
WiFiClient client;
RTCZero rtc; // Real Time Clock
const int GMT = 2; // Change this to adapt it to your time zone
int status = WL_IDLE_STATUS;
String location = "offida,IT"; // OpenWeatherMap location
String apiKey = "1111111111"; // OpenWeatherMap test Key
char server[] = "api.openweathermap.org"; // OpenWeatherMap Server
void setup() {
Serial.begin(9600);
while (!Serial) {
;
}
Serial.println(" *** Infosphere Microcontroller Testing Board ***");
// Connect to Wifi network
while (status != WL_CONNECTED) {
Serial.print("Connecting to ");
Serial.print(ssid);
Serial.print("...");
status = WiFi.begin(ssid, pass);
// wait 1 seconds for connection:
delay(1000);
}
Serial.println(" Connected.");
rtc.begin();
// Set Time via NTP
Serial.println("Retriving NTP Time...");
unsigned long epoch;
int numberOfTries = 0, maxTries = 10;
do {
epoch = WiFi.getTime();
numberOfTries++;
delay(1000);
}
while ((epoch == 0) && (numberOfTries < maxTries));
if (numberOfTries == maxTries) {
Serial.println(" NTP unreachable");
while (1);
}
else {
rtc.setEpoch(epoch);
}
/*****************************************************
*** Scheduler ***
******************************************************/
Serial.println("Waiting schedule time for starting jobs...");
rtc.setAlarmTime(13 - GMT, 19, 0); // Set time here
rtc.enableAlarm(rtc.MATCH_HHMMSS);
rtc.attachInterrupt(midnightJobs);
Serial.println(getWeather());
}
void loop()
{ }
//
int getWeather() {
int rain_prob_index = 0;
Serial.println("\nConnecting to OpenWeatherMap API...");
if (client.connect(server, 80)) {
Serial.println("Connected. Retriving weather forecast for the next 24 hours...");
client.print("GET /data/2.5/forecast?");
client.print("q=" + location);
client.print("&appid=" + apiKey);
client.print("&cnt=8");
client.println("&units=metric");
client.println("Host: api.openweathermap.org");
client.println("Connection: close");
client.println();
} else {
Serial.println("Unable to connect to OpenWeatherMap");
}
// read OpenWeatherMap response
String line = "";
while (client.connected()) {
line = client.readStringUntil('\r');
client.flush();
}
client.stop();
Serial.println(line);
return rain_prob_index;
}
/*****************************************************
*** Midnight Jobs ***
******************************************************/
void midnightJobs()
{
Serial.println(getWeather());
}
Can't figure out why the ISR is generating such amount of I/O... Do you thing that could be due to a bug in RTCZero library or something related to the MKR1010 interrupt mechanism itself?
Celofan:
Can't figure out why the ISR is generating such amount of I/O... Do you thing that could be due to a bug in RTCZero library or something related to the MKR1010 interrupt mechanism itself?
It's because the ISR calls getWeather() and that function does Serial and network I/O, neither of which should be done inside an ISR. If your ISR triggers something that doesn't need sub-microsecond timing, just set a flag and have loop() handle it.