Hi Gabriel,
Thank you so much for the help.
I have tried to call sodaq_wdt_reset()
frequently and eliminated delay(x)
from my code but still nothing is uploaded and it is saving to SD card, and without WDT it uploads, I get so confused!.
Here is my program sketch, it is for recording water level using ultrasonic sound sensor
/////// NEED to fix sleep code so that it wakes up every minute to check time.
#include "Sodaq_wdt.h"
#include <SPI.h>
#include <SD.h>
#include <RTCZero.h>
#include "GPRSbee.h"
//Cellular Network
#define APN "internet"
#define APN_USERNAME ""
#define APN_PASSWORD ""
//Internet Time Service Defines
#define TIME_URL "time.sodaq.net"
#define TIME_ZONE 3
#define TIME_ZONE_SEC (TIME_ZONEВ * 3600)
//SpeakThings constants
#define URL "api.thingspeak.com/update"
#define WRITE_API_KEY "xxxxxxxxxxxxxxxxxx" //this is my channel's key
RTCZero rtc;
File dataFile;
bool hz_flag;
int arraysize = 9; //quantity of values to find the median (sample size). Needs to be an odd number
int rangevalue[] = {0, 0, 0, 0, 0, 0, 0, 0, 0}; //declare an array to store the samples. not necessary to zero the array values here, it just makes the code clearer
void setup()
{
delay(15000); // wait 15seconds before starting up
Serial.begin(57600);
SerialUSB.print("Initializing SD card...");
if (!SD.begin(SS_2)) {
SerialUSB.println("initialization failed!");
return;
}
SerialUSB.println("initialization done.");
rtc.begin(); // start the clock
// Set the alarm at the 12 second mark
rtc.setAlarmMinutes(12);
// Match only seconds (Periodic alarm every minute)
rtc.enableAlarm(RTCZero::MATCH_MMSS);
// Attach ISR
rtc.attachInterrupt(RTC_ISR);
//Open Serial1 for the GPRSbee
Serial1.begin(57600);
//Switch on the VCC for the Bee socket
digitalWrite(BEE_VCC, HIGH);
gprsbee.initAutonomoSIM800(Serial1, BEEDTR, BEE_VCC, BEECTS);
gprsbee.setDiag(SerialUSB);
// Set sleep mode
SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk;
pinMode(LED_BUILTIN, OUTPUT);
//Sync time
syncRTCwithServer();
//Adjust for time change
int hourtime = rtc.getHours();
int TanzaniaTime = hourtime + 3;
rtc.setHours(TanzaniaTime);
sodaq_wdt_enable(WDT_PERIOD_8X);
}
void loop()
{
sodaq_wdt_reset();
digitalWrite(LED_BUILTIN, HIGH);
delay(50);
digitalWrite(LED_BUILTIN, LOW);
delay(50);
digitalWrite(LED_BUILTIN, HIGH);
delay(50);
digitalWrite(LED_BUILTIN, LOW);
sodaq_wdt_reset();
if (hz_flag) {
takedata();
}
// Disable USB
USB->DEVICE.CTRLA.reg &= ~USB_CTRLA_ENABLE;
//Enter sleep mode
__WFI();
// Sleeping
// Enable USB and wait for resume if attached
USB->DEVICE.CTRLB.bit.UPRSM = 0x01u;
// Awake
}
void RTC_ISR()
{
hz_flag = true;
}
void takedata()
{
digitalWrite(LED_BUILTIN, HIGH);
String url;
for(int i = 0; i < arraysize; i++)
{ //array pointers go from 0 to 4
rangevalue[i] = analogRead(A2);
delay(100);
sodaq_wdt_reset();
}
int midpoint = arraysize/2;
int depthvalue = 400 - rangevalue[midpoint]; // enter in the total possible channel height here
// int depthvalue = rangevalue[midpoint]; // raw distance
sodaq_wdt_reset();
dataFile = SD.open("datalog.txt", FILE_WRITE);
// Print date...
dataFile.print(rtc.getMonth());
dataFile.print("/");
dataFile.print(rtc.getDay());
dataFile.print("/");
dataFile.print(rtc.getYear());
dataFile.print(", ");
// ...and time
dataFile.print(rtc.getHours());
dataFile.print(":");
dataFile.print(rtc.getMinutes());
dataFile.print(":");
dataFile.print(rtc.getSeconds());
dataFile.print(", ");
dataFile.println(depthvalue);
//SerialUSB.print(depthvalue);
sodaq_wdt_reset();
delay(100);
dataFile.flush();
sodaq_wdt_reset();
OneSecondDelay();
sodaq_wdt_reset();
gprsbee.on();
TwoSecondDelay();
url += String("api.thingspeak.com/update");
url += String("?key=");
url += String(WRITE_API_KEY);
url += String("&field1=");
url += String(depthvalue);
sodaq_wdt_reset();
char buffer[512];
memset(buffer, '\0', sizeof(buffer));
OneSecondDelay();
bool retval = gprsbee.doHTTPGET(APN, APN_USERNAME, APN_PASSWORD, url.c_str(), buffer, sizeof(buffer));
SerialUSB.println(retval);
SerialUSB.println("GET result: ");
sodaq_wdt_reset();
//Lets be safe in case result length > 247
for (int i=0; i<strlen(buffer); i++) {
SerialUSB.print(buffer[i]);
}
TwoSecondDelay();
sodaq_wdt_reset();
gprsbee.off();
sodaq_wdt_reset();
hz_flag = false;
sodaq_wdt_reset();
digitalWrite(LED_BUILTIN, LOW);
}
void HalfSecondDelay()
{
sodaq_wdt_reset();
delay(500);
sodaq_wdt_reset();
}
void OneSecondDelay()
{
sodaq_wdt_reset();
delay(500);
sodaq_wdt_reset();
delay(500);
sodaq_wdt_reset();
}
void TwoSecondDelay()
{
sodaq_wdt_reset();
delay(500);
sodaq_wdt_reset();
delay(500);
sodaq_wdt_reset();
delay(500);
sodaq_wdt_reset();
delay(500);
sodaq_wdt_reset();
}
void syncRTCwithServer()
{
char buffer[20];
if (gprsbee.doHTTPGET(APN, APN_USERNAME, APN_PASSWORD, TIME_URL, buffer, sizeof(buffer)))
{
sodaq_wdt_reset();
SerialUSB.println("HTTP GET: " + String(buffer));
//Convert the time stamp to unsigned long
char *ptr;
uint32_t newTs = strtoul(buffer, &ptr, 0);
rtc.setEpoch(newTs);
}
}
I would appreciate any help please, I have stuck here for a while.
Thanks.