Hello,
When uploading the following sketch everything works as expected
#include "DHT.h"
#include <MKRWAN.h>
DHT dht;
LoRaModem modem(Serial);
#define SECRET_APP_EUI "XXXXXXXXXXXXXXXXX"
#define SECRET_APP_KEY "XXXXXXXXXXXXXXXXXXXXXXXX"
String appEui = SECRET_APP_EUI;
String appKey = SECRET_APP_KEY;
void setup()
{
Serial.begin(9600);
while (!Serial);
// change this to your regional band (eg. US915, AS923, ...)
if (!modem.begin(EU868)) {
Serial.println("Failed to start module");
while (1) {}
};
int connected = modem.joinOTAA(appEui, appKey);
if (!connected) {
Serial.println("Something went wrong; are you indoor? Move near a window and retry");
while (1) {}
}
modem.minPollInterval(60);
Serial.println();
Serial.println("Status\tHumidity (%)\tTemperature (C)\t(F)");
dht.setup(2); // data pin 2
}
void loop() {
delay(dht.getMinimumSamplingPeriod());
float humidity = dht.getHumidity();
float temperature = dht.getTemperature();
int16_t temp = (int16_t) (temperature * 100);
int16_t hum = (int16_t) (humidity * 100);
Serial.print(dht.getStatusString());
Serial.print("\t");
Serial.print(humidity, 1);
Serial.print("\t\t");
Serial.print(temperature, 1);
// Serial.print("\t\t");
// Serial.println(dht.toFahrenheit(temperature), 1);
byte data[4];
data[0] = (temp >> 8) & 0xFF;
data[1] = temp;
data[2] = (hum >> 8) & 0xFF;
data[3] = hum;
int err;
modem.beginPacket();
modem.write(data,sizeof(data));
err = modem.endPacket(true);
if (err > 0) {
Serial.println("Message sent correctly!");
} else {
Serial.println("Error sending message :(");
Serial.println("(you may send a limited amount of messages per minute, depending on the signal strength");
Serial.println("it may vary from 1 message every couple of seconds to 1 message every minute)");
}
delay(10000);
if (!modem.available()) {
Serial.println("No downlink message received at this time.");
return;
}
}
I then decided to add the RTC library so that I can put the arduino to sleep when I do not take any measurements. I took parts of code from this forum. However when I upload the sketch not only does it not work but it also seems impossible to upload any other sketch
The port section in IDE is greyed out and when I try to upload a sketch I get the following
"Couldn't find a Board on the selected port. Check that you have the correct port selected. If it is correct, try pressing the board's reset button after initiating the upload."
The only way to recover is by pressing the reset button twice while uploading another sketch. But then if I upload the same sketch I run into the same issue. So I assume there is something wrong with the code i am using.
Can somebody see what am I doing wrong ? Here's the code
#include <RTCZero.h>
#include "DHT.h"
#include <MKRWAN.h>
RTCZero rtc;
DHT dht;
LoRaModem modem(Serial);
#define SECRET_APP_EUI "XXXXXXXXXXXXXXXXX"
#define SECRET_APP_KEY "XXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
String appEui = SECRET_APP_EUI;
String appKey = SECRET_APP_KEY;
/* Change these values to set the current initial time */
const byte seconds = 0;
const byte minutes = 42;
const byte hours = 00;
/* Change these values to set the current initial date */
const byte day = 13;
const byte month = 06;
const byte year = 18;
bool matched = false;
void setup()
{
rtc.begin();
rtc.setTime(hours, minutes, seconds);
rtc.setDate(day, month, year);
rtc.setAlarmTime(00,50, 10);
rtc.enableAlarm(rtc.MATCH_MMSS);
rtc.attachInterrupt(alarmMatch);
rtc.standbyMode();
Serial.begin(9600);
while (!Serial);
// change this to your regional band (eg. US915, AS923, ...)
if (!modem.begin(EU868)) {
Serial.println("Failed to start module");
while (1) {}
};
int connected = modem.joinOTAA(appEui, appKey);
if (!connected) {
Serial.println("Something went wrong; are you indoor? Move near a window and retry");
while (1) {}
}
modem.minPollInterval(60);
Serial.println();
Serial.println("Status\tHumidity (%)\tTemperature (C)\t(F)");
dht.setup(2); // data pin 2
}
void loop() {
if (matched) {
matched = false;
int alarmMinutes = rtc.getMinutes();
alarmMinutes += 1;
if (alarmMinutes >= 60) {
alarmMinutes -= 60;
}
delay(dht.getMinimumSamplingPeriod());
float humidity = dht.getHumidity();
float temperature = dht.getTemperature();
int16_t temp = (int16_t) (temperature * 100);
int16_t hum = (int16_t) (humidity * 100);
Serial.print(dht.getStatusString());
Serial.print("\t");
Serial.print(humidity, 1);
Serial.print("\t\t");
Serial.print(temperature, 1);
// Serial.print("\t\t");
// Serial.println(dht.toFahrenheit(temperature), 1);
byte data[4];
data[0] = (temp >> 8) & 0xFF;
data[1] = temp;
data[2] = (hum >> 8) & 0xFF;
data[3] = hum;
int err;
modem.beginPacket();
modem.write(data,sizeof(data));
err = modem.endPacket(true);
if (err > 0) {
Serial.println("Message sent correctly!");
} else {
Serial.println("Error sending message :(");
Serial.println("(you may send a limited amount of messages per minute, depending on the signal strength");
Serial.println("it may vary from 1 message every couple of seconds to 1 message every minute)");
}
delay(10000);
if (!modem.available()) {
Serial.println("No downlink message received at this time.");
return;
}
rtc.setAlarmTime(rtc.getHours(), alarmMinutes, rtc.getSeconds());
rtc.standbyMode(); // Sleep until next alarm match
}
}
void alarmMatch() {
matched = true;
}