I’ve got some simple code that I’m using for a remote temp sensor; the gist of the code is that it reads the temp and battery, mesh publishes it and then goes to sleep (I have it set for 60 seconds right now for testing, will be more like 15-60 minutes in the final version). I got it working just fine when having it fully connect to the Particle cloud, however I noticed that it tends to take a while (5-10 seconds) and obviously this all requires that my Argon has internet connectivity. While not strictly necessary, it would be nice to just have it mesh connect since it doesn’t need internet (and I’d like to know how, for other projects where internet might be more intermittent).
Been trying to use Semi-Automatic mode to have it only connect to the mesh network when it’s running on battery but still connect to the cloud when it’s powered via USB. I definitely cuts down on the time that’s it’s powered on each cycle, but I’ve been getting weird behavior.
For one it seems to wake up at somewhat random times (it tended to alternate between the coded 60 seconds and about 30 seconds). And then at some point it’s always gotten stuck flashing green until I reset it (or it kills the battery). It’s taken anywhere from ~15 minutes to over a day, but it’s always gotten stuck flashing green.
Kind of at a loss… I’m guessing there’s something simple/obvious I’m missing? Code below:
#include "Particle.h"
#include <math.h>
#define Power_Pin A1
#define Temp_Pin A0
int Temp_Reading;
float TempC;
float TempF;
int Temp;
String Temp_String;
double Battery_V;
int Battery_Percent;
String Battery_String;
String Status = "Startup";
int USB_Status;
int CHG_Status;
bool Startup_Complete;
SYSTEM_THREAD(ENABLED);
SYSTEM_MODE(SEMI_AUTOMATIC);
void setup() {
pinMode(Power_Pin, OUTPUT);
pinMode(Temp_Pin, INPUT);
pinMode(PWR, INPUT);
pinMode(CHG, INPUT);
//Particle.variable("Status", Status);
//Particle.variable("Battery_Percent", Battery_Percent);
//Particle.variable("Temp", Temp);
if (Startup_Complete != true) {
Particle.variable("Status", Status);
Particle.variable("Battery_Percent", Battery_Percent);
Particle.variable("Temp", Temp);
Particle.connect();
waitUntil(Particle.connected);
Startup_Complete = true;
}
else {
Mesh.connect();
}
Time.zone(-5);
}
void loop() {
digitalWrite(Power_Pin, HIGH);
delay(250); //Delay to give the temp sensor time to 'warm-up'. Occassionally worked with 100, worked with 500 and 300
Temp_Reading = analogRead(Temp_Pin);
digitalWrite(Power_Pin, LOW);
TempC=((100*((float)Temp_Reading/4095)*3.3)-50);
TempF=((TempC*9/5)+32);
Temp=TempF; //Select TempF or TempC
Temp_String = String(Temp);
Battery_V = analogRead(BATT) * 0.0011224;
USB_Status = digitalRead(PWR); // PWR: 0=no USB power, 1=USB powered
CHG_Status = digitalRead(CHG); // CHG: 0=charging, 1=not charging
if (USB_Status == 0) {
Status = "Running on Battery";
Battery_Percent = 157.58*Battery_V - 525.82;
Battery_String = String(Battery_Percent);
waitUntil(Mesh.ready);
Mesh.publish("Fireplace_Temp", Temp_String);
Mesh.publish("Fireplace_Battery", Battery_String);
System.sleep(PWR,CHANGE,60); //Sleeps for 60 seconds. {} can be used if no wakeup pins are wanted
}
else if (USB_Status == 1 and CHG_Status == 0) {
Status = "Battery Charging";
Battery_Percent = 127.56*pow(Battery_V,2) - 846.99*Battery_V + 1405.8;
Battery_String = String(Battery_Percent);
}
else if (USB_Status == 1 and CHG_Status == 1) {
Status = "Battery Charged/Running on USB Power";
Battery_Percent = 100;
Battery_String = String(Battery_Percent);
}
if (USB_Status == 1 and Particle.connected() == false) {
//Not sure if the variables need to be re-defined since they were during the first startup
//Particle.variable("Status", Status);
//Particle.variable("Battery_Percent", Battery_Percent);
//Particle.variable("Temp", Temp);
Particle.connect();
//waitUntil(Particle.connected); //Not sure if this is needed either, but it seems like it would be best?
}
}