Need help with Spark.sleep()

Hi,

I am currently working on my first spark core project - a data logger for a fridge. The data logger will determine temp, humidity, and if the door has been opened. I plan to power the core with a 3.7v 1C lipo and want to use Spark.sleep() to ensure the data logger can run for as long as possible. I am really struggling to implement spark.sleep() and haven’t been able to find any examples that have helped. I find that if I put the spark.sleep(60) statement at the end of my code within the main loop the wifi module just remains off and doesn’t come back on, I need to factory reset to get the firmware on when this happens.

If anyone can shed some light on what I am doing wrong it would be much appreciated.

#include "Adafruit_DHT/Adafruit_DHT.h"
// This #include statement was automatically added by the Spark IDE.
#include "HttpClient/HttpClient.h"
#include "application.h"
HttpClient http;
#define VARIABLE_ID "temp"
#define VARIABLE_ID2 "humidity"
#define VARIABLE_ID3 "voltage"
#define TOKEN "token"
 
#define DHTPIN 4 // what pin we're connected to
// Using DHT22
#define DHTTYPE DHT22
 
DHT dht(DHTPIN, DHTTYPE);
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a
// very slow sensor)
// Read temperature as Celsius
// Headers currently need to be set at init, useful for API keys etc.
http_header_t headers[] = {
{ "Content-Type", "application/json" },
{ "X-Auth-Token" , TOKEN },
{ NULL, NULL } // NOTE: Always terminate headers will NULL
};
 
http_request_t request;
http_response_t response;
 
void setup() {
request.hostname = "things.ubidots.com";
request.port = 80;
Serial.begin(9600);
dht.begin();
}
 
void loop() {
   
float humidity = dht.getHumidity(); // Initiates humidity variable
float temp = dht.getTempCelcius(); // Initisates temp variable
const float voltsPerBit = 3.3 / 4095;  // Calculate volts per bit of ADC reading
const float ratioV =0.7971; 
// writes the humidity to Ubidots
request.path = "/api/v1.6/variables/"VARIABLE_ID"/values";
request.body = "{\"value\":" + String(temp) + "}";
http.post(request, response, headers);
// Serial.println(response.body);
// writes the humidity to Ubidots
request.path = "/api/v1.6/variables/"VARIABLE_ID2"/values";
request.body = "{\"value\":" + String(humidity) + "}";
http.post(request, response, headers);
// Serial.println(response.body);

float rawVolts = analogRead(A0) * voltsPerBit;  //Calculate voltage at A0 input
float batteryVolts = rawVolts / ratioV;
request.path = "/api/v1.6/variables/"VARIABLE_ID3"/values";
request.body = "{\"value\":" + String(batteryVolts) + "}";
http.post(request, response, headers);
// Serial.println(response.body);

Serial.print("Temperature: ");
Serial.println(temp);
Serial.print("Humidity: ");
Serial.println(humidity);
Serial.print("LiPo Voltage: ");
Serial.println(batteryVolts);
Serial.print("rawVolts: ");
Serial.println(rawVolts);

Spark.sleep(30);
delay(200);
}

Have you tried the sleep function without libraries? Try an ‘empty’ example in which you blink the LED, after which it sleeps for a while. This way we can ensure that there is no defect.
Also, if you’re using the DHT22 I’d recommend using the Piettetech library rather than the Adafruit one. There have been some problems in the past, regarding the dht, and this library seems to be the most reliable one to date.

1 Like

@tidlick1, Spark.sleep(30) will not stop the execution of code. Instead, it sets a flag for the background task (run after each loop) to turn off the CC3000 module. So loop() will continue running and the http.post() calls will run without a connection until after 30 seconds when wifi should come back on. Ideally, you would test WiFi.ready() to see when the wifi connection has been re-established before running the http code.

Perhaps you should consider controlling wifi yourself with SYSTEM_MODE(), WiFi.on() and WiFi.off() to have more control. Or use deep sleep and sample/send only at each sleep interval. :smile:

2 Likes