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);
}