I have a working Particle (Photon) application which is running fine.
Everything is in the setup() function and there is no need of loop().
It wakes up every 15 seconds, queries a sensor, posts the data to a website and then goes back to sleep.
This is very efficient from a power consumption perspective (actually, would be better if I could wake up less frequently (like every5 minutes), but I find that the wifi takes two-or-three wake cycles to actually connect when it first powers up, and 15 seconds is about as long as I can safely wait without recreating that problem.
However, even with these frequent updates, I canāt refresh the device via the cloud (which is semi-desirable in my application).
What are the minimum requirements to be able to flash a device that is asleep most of the time via the cloud?
Iāve included my current code since it is short for completeness.
#include "Adafruit_DHT.h"
#include "application.h"
#include "HttpClient.h"
#define SERVER "www.delong.com"
#define PORT 80
#define CGI_PATH "/cgi-bin/post_temp.cgi"
#define MY_ID "Photon_Test1"
// DHT22 is installed next to Photon on a breadboard such that Photon 567G = DHT 1234. A 10kΩ resistor is also
// placed across 56/12. Hence, to power the DHT, we need to hold 5 high.
#define DHTPOWER 5
#define DHTPIN 6
#define DHTTYPE DHT22
#define SLEEP_DURATION 15 // Sleep for 15 seconds between polling intervals For debugging
http_header_t headers[] = {
{ "Accept", "*/*" },
{ NULL, NULL },
};
DHT probe(DHTPIN, DHTTYPE);
HttpClient http;
uint32_t next_time;
http_request_t request;
http_response_t response;
void setup() {
Serial.begin(115200);
float tC;
float hum;
char Query[1024];
char tbuf[10];
char hbuf[10];
Serial.println();
Serial.println("Application>\tGathering Data");
// Gather the data
pinMode(DHTPOWER, OUTPUT);
digitalWrite(DHTPOWER, HIGH);
probe.begin();
delay(1000); // Let probe stabilize after powerup.
hum = probe.getHumidity();
tC = probe.getTempCelcius();
Serial.print("Application>\t\tHumidity: ");
Serial.println(hum);
Serial.print("Application>\t\tTemperature: ");
Serial.println(tC);
// Report the data
Serial.println("Application>\tPerparing Query");
request.hostname=SERVER;
request.port=PORT;
String(tC).toCharArray(tbuf, 6); // Ugly, but apparently only workable way on Particle
String(hum).toCharArray(hbuf, 6); // Ugly, but apparently only workable way on Particle
sprintf(Query, "%s?my_id=%s&sensor_unit_1=dht_2&temp_1=%s&hum_1=%s", CGI_PATH, MY_ID, tbuf, hbuf);
Serial.print("Applicatin>\t\tQuery String: ");
Serial.println(Query);
request.path = Query;
// Send the request
Serial.println("Application>\tSending Query");
http.get(request, response, headers);
Serial.print("Application>\tResponse status: ");
Serial.println(response.status);
Serial.print("Application>\tHTTP Response Body: ");
Serial.println(response.body);
// Sleep until next report period
Serial.println("Sleeping");
delay(500); // Let the Serial buffer drain before power down.
System.sleep(SLEEP_MODE_DEEP, SLEEP_DURATION);
Serial.println("This shouldn't happen");
}
void loop() {
}