Hello,
I've been working on a device that will be battery powered and I only really need data once a day, and I don't want to use an interrupt. So deep sleep would be perfect for this project. But, I can't seem to find info or get my code to work correctly. The deep sleep in the ArduinoLowPower.h library seems to be the library that I need to make this all work. Although, I'm slightly confused on the correct methodology for reestablishing the connection and sending updates to the cloud.
Overall, this code seems to be function very strangely. I change the status of variables and they are changed back without any input (like the code is constantly running from the top, not just inside the loop.
You can see in the code that I've commented out all the sleep functions or power saving functions.
/*
Sketch generated by the Arduino IoT Cloud Thing "Untitled 2"
https://create.arduino.cc/cloud/things/1aa74123-c776-4208-9547-d56ebabcf173
Arduino IoT Cloud Variables description
The following variables are automatically generated and updated when changes are made to the Thing
float angle;
float angle1;
float angle2;
float batVolt;
float salinity;
float salinity1;
float salinity2;
bool pushValues;
bool sleep;
Variables which are marked as READ/WRITE in the Cloud Thing will also have functions
which are called when their values are changed from the Dashboard.
These functions are generated with the Thing and added at the end of this sketch.
*/
#include "thingProperties.h"
#include <MKRIMU.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <ArduinoLowPower.h>
#include <ArduinoECCX08.h>
#define ONE_WIRE_BUS 2 // DS18B20 pin
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature DS18B20(&oneWire);
float heading, roll, pitch;
unsigned long currentTime;
unsigned long previousTime;
const long interval = (1000*60*5); //5 minutes
unsigned long sleepTime = (10*60*1000);
int firstIt;
float slope = -0.00519;
float intersect = 1.467432;
void setup() {
pinMode(LED_BUILTIN, OUTPUT); // set LED pin to output
// Initialize serial and wait for port to open:
Serial.begin(9600);
// This delay gives the chance to wait for a Serial Monitor without blocking if none is found
delay(1500);
//LowPower.begin();
DS18B20.begin(); // start DS18B20
IMU.begin();
// Defined in thingProperties.h
initProperties();
// Connect to Arduino IoT Cloud
ArduinoCloud.begin(ArduinoIoTPreferredConnection);
delay(1000);
firstIt = 0;
}
void loop() {
//digitalWrite(NINA_RESETN, LOW); // activate nina
digitalWrite(LED_BUILTIN, HIGH);
sleep = false;
Serial.println("-------");
Serial.print("Sleep Status: ");
Serial.println(sleep);
delay(3000);
// Connect to Arduino IoT Cloud
//ArduinoCloud.begin(ArduinoIoTPreferredConnection);
delay(500);
ArduinoCloud.update();
batVolt=4.108887*(float)analogRead(ADC_BATTERY)/1000.0; // read battery voltage
Serial.print("Battery: ");
Serial.println(batVolt);
Serial.print("first iteration1: ");
Serial.println(firstIt);
//Serial.println("hello world");
if (IMU.eulerAnglesAvailable()) {
delay(500);
IMU.readEulerAngles(heading, roll, pitch);
delay(500);
angle = pitch;
salinity = slope*angle + intersect;
}
//send data to the arduino iot cloud
ArduinoCloud.update();
currentTime = WiFi.getTime();
delay(500);
if (firstIt = 0)
{
Serial.print("first iteration1: ");
Serial.println(firstIt);
//Serial.println(previousTime);
previousTime = currentTime;
//Serial.println(previousTime);
firstIt = 1;
Serial.print("first iteration2: ");
Serial.println(firstIt);
}
//Serial.print("Current time: ");
//Serial.println(currentTime);
//Serial.print("Previous time: ");
//Serial.println(previousTime);
digitalWrite(LED_BUILTIN, LOW);
delay(500);
digitalWrite(LED_BUILTIN, HIGH);
delay(500);
digitalWrite(LED_BUILTIN, LOW);
delay(500);
digitalWrite(LED_BUILTIN, HIGH);
if (currentTime - previousTime >= 120)
{
//Serial.print("Current time: ");
//Serial.println(currentTime);
//Serial.print("Previous time: ");
//Serial.println(previousTime);
previousTime = currentTime;
sleep = true;
Serial.print("Sleep Status: ");
Serial.println(sleep);
//WiFi.end();
digitalWrite(LED_BUILTIN, LOW); // set LED low again
ArduinoCloud.update();
//delay(3000);
// Send board to sleep mode
//digitalWrite(NINA_RESETN, HIGH); // reset nina
//ECCX08.begin();
//LowPower.deepSleep(60000);
}
}
void setLine()
{
if (pushValues == true)
{
slope = (salinity1 - salinity2)/(angle1 - angle2);
intersect = (salinity1 - slope*angle1);
pushValues = false;
}
}
/*
Since Salinity1 is READ_WRITE variable, onSalinity1Change() is
executed every time a new value is received from IoT Cloud.
*/
void onSalinity1Change() {
// Add your code here to act upon Salinity2 change
}
/*
Since Salinity2 is READ_WRITE variable, onSalinity2Change() is
executed every time a new value is received from IoT Cloud.
*/
void onSalinity2Change() {
// Add your code here to act upon Salinity2 change
}
/*
Since Angle1 is READ_WRITE variable, onAngle1Change() is
executed every time a new value is received from IoT Cloud.
*/
void onAngle1Change() {
// Add your code here to act upon Angle1 change
}
/*
Since Angle2 is READ_WRITE variable, onAngle2Change() is
executed every time a new value is received from IoT Cloud.
*/
void onAngle2Change() {
// Add your code here to act upon Angle2 change
}
/*
Since PushValues is READ_WRITE variable, onPushValuesChange() is
executed every time a new value is received from IoT Cloud.
*/
void onPushValuesChange() {
// Add your code here to act upon PushValues change
setLine();
}