When the unit is not in deep sleep, I would like to turn of all unnecessary activities on a MKR1000 board, such as the serial console, while leaving the RTC and WiFi running. I see the ArduinoLowPower.h library in the repository. It seems to have only LowPower.deepSleep() available.
What else is possible? I see some mention of an AVR library, but how would I add that to a sketch if I can't get to it via the Manage Libraries menu? Or is there a better way?
The way it is now, the system wakes up, reads some sensors, reports the readings over Wifi, and then goes to sleep again. It is the interval when the system is not sleeping that I wish to minimize the activities of the board.
Is the serial console the only activity that you want to disable? It seems unlikely to be using very much power. In order to help you optimize your power usage, it is necessary to know what your activities are. Since you haven't posted your sketch, we don't know what those are. That make it something we can only guess unless you provide your code.
Waking up, and going immediately back to sleep uses very little power.
But if you are waking up AND checking your sensors AND using WiFi every time, that is obviously not efficient.
However, as you haven’t posted any code we don’t know if you are doing that or not.
Before deciding to use a power saving library, you need to first work out how much power you are using, and where. Once you know that only then can you decide the base way to minimise consumption.
For example there’s little point reducing you Arduino’s consumption by a few mA, if the WiFi is burning through 50mA or more by staying associated with your AP.
Ok. Here is the current sketch. I appreciate tips on power-saving. There are probably a lot of design mistakes and misunderstandings contained in it:
/*
MKR1000
*/
#include <SPI.h> // for built-in WiFi shield
#include <WiFi101.h> // WiFi support
#include <RTCZero.h> // Real-time Clock
#include <PubSubClient.h> // MQTT client
#include <OneWire.h> // for DS18B20
#include <DallasTemperature.h> // also for DS18B20 https://www.milesburton.com/Dallas_Temperature_Control_Library
#include <ArduinoLowPower.h> // for deepSleep() function
#define ONE_WIRE_BUS 0 // data wire is on pin 0 on this Arduino
OneWire oneWire(ONE_WIRE_BUS); // for any OneWire devices, not just Maxim/Dallas
DallasTemperature sensors(&oneWire); // pass reference to Dallas Temperature IC
RTCZero rtc;
#include "arduino_secrets.h"
// these connection data come from arduino_secrets.h
char ssid[] = SECRET_SSID; // network SSID (name)
char pass[] = SECRET_PASS; // network password
int status = WL_IDLE_STATUS;
void mqttpubcallback(char* topic, byte* payload, unsigned int length) {
// handle message delivered
}
void setup() {
// WiFi.maxLowPowerMode();
WiFi.hostname("mkr1000");
Serial.begin(115200);
// low power mode??
for (int ledPin = 0; ledPin <= 21; ledPin++) {
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
}
// check if the WiFi module works
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present");
// don't continue if the hardware is missing or broken:
while (true);
}
// attempt to connect to WiFi network:
while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network
status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection:
delay(10000);
}
rtc.begin();
unsigned long epoch;
int numberOfTries = 0, maxTries = 6;
do {
epoch = WiFi.getTime();
numberOfTries++;
}
while ((epoch == 0) && (numberOfTries < maxTries));
if (numberOfTries == maxTries) {
Serial.print("NTP unreachable!!");
while (1);
}
else {
Serial.print("Epoch received: ");
Serial.println(epoch);
rtc.setEpoch(epoch);
Serial.println();
}
sensors.begin(); // start Dallas Temperature IC Control Library
DeviceAddress deviceAddress;
int devicecount = sensors.getDeviceCount();
for (int i = 0; i < devicecount; i++) {
if (sensors.getAddress(deviceAddress, i)) {
Serial.print("Device ");
Serial.println(i);
sensors.setResolution(deviceAddress, 12); // set temperature resolution (9 through 12)
}
}
}
void dummy() {
// This function will be called once on device wakeup, avoid calling delay() and long running functions
}
IPAddress mqttserver(192, 168, 1, 140);
WiFiClient mqtt;
PubSubClient mqttclient(mqttserver, 1883, mqttpubcallback, mqtt);
String topic = "temperatures";
void loop() {
status = WiFi.status();
if (status != WL_CONNECTED) {
Serial.print("Attempting to reconnect to SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network.
status = WiFi.begin(ssid, pass);
delay(2000);
}
Serial.print("WiFI status: ");
Serial.print(status);
Serial.print("\t");
// get Epoch to send along with temperatures
unsigned long epoch = rtc.getEpoch();
Serial.print(epoch);
Serial.print("~");
// get temperature readings of each device on the bus
sensors.requestTemperatures();
float t0 = sensors.getTempCByIndex(0);
float t1 = sensors.getTempCByIndex(1);
Serial.print(" Temperature is: ");
Serial.print(t0);
Serial.print(" ");
Serial.print(t1);
Serial.print("\n");
// send temperatures if possible
if (mqttclient.connect("arduinoClient", MQTT_ACCT, MQTT_PASS)) {
String temperatures = String(epoch) + "\t3\t" + String(t0);
mqttclient.publish(topic.c_str(), String(temperatures).c_str());
temperatures = String(epoch) + "\t4\t" + String(t1);
mqttclient.publish(topic.c_str(), String(temperatures).c_str());
mqttclient.disconnect();
}
LowPower.deepSleep(280000);
// ride the time up to the 5-minute mark
while(rtc.getMinutes() % 5) {
LowPower.deepSleep(5000);
}
}
The serial activity in the sketch will be commented out eventually.