Hey Peekay123, I read through all your replay and they are very usefully. So after that, I modified my code that it wakes up from deep sleep and record the time if the ball tilt switch moved/sharked. If it wakes up due to time, it should not record the time but upload the date. However, it does not work. What I really get: When I don’t connect to the ball tilt switch, it wakes up first time record the time, and second time upload the date.
I am a beginner for this.
Here are some question that I want to ask you:
- Do I need connect the Vbat in order to keep the RTC running when it is in deep sleep?
- Does the photon reset the time when it wakes up from deep sleep. (no wifi connected when it wakes up).
STARTUP(System.enableFeature(FEATURE_RETAINED_MEMORY)); // Enables backup RAM
STARTUP(WiFi.selectAntenna(ANT_EXTERNAL)); // choose external antenna
//STARTUP(WiFi.selectAntenna(ANT_AUTO)); //continually switches at high speed between antennas
SYSTEM_MODE(MANUAL); // Puts device in manual mode
#include <string.h>
//#define d_length 4
int boardLed = D7; // This is the LED that is already on your device.
//int button = A0; // This is where push button is plugged in.
retained int counter = 0;
retained int list[4];
retained int ETA_Wake;
//retained bool is_open = true;
retained bool is_startup = true;
//retained int list[d_length];
//retained char strtime[255];
char buffer[10];
char all_times[255];
int wakeUpRange = 13;
int lower_ETA;
int higher_ETA;
int wakeTime;
void setup() {
// This part is mostly the same:
pinMode(boardLed,OUTPUT); // Our on-board LED is output
// pinMode(button,INPUT); // Our photoresistor pin is input
// Now flash the D7 LED on and off three times to let us know that we're ready to go!
}
void loop() {
wakeTime = Time.now();
lower_ETA = ETA_Wake - wakeUpRange;
higher_ETA = ETA_Wake + wakeUpRange;
if (wakeTime > higher_ETA || wakeTime < lower_ETA)
{
if (counter < 3) {
/*
Particle.connect();
Particle.process();
delay(5000);
int diff = wakeTime-ETA_Wake-wakeT;
Spark.publish("still alive", all_times, diff, PRIVATE);
delay(600);
*/
list[counter] = Time.now();
counter++;
// And flash the on-board LED on and off.
digitalWrite(boardLed,HIGH);
delay(300);
digitalWrite(boardLed,LOW);
delay(300);
digitalWrite(boardLed,HIGH);
delay(300);
digitalWrite(boardLed,LOW);
}
else {
list[counter] = Time.now();
counter++;
digitalWrite(boardLed,HIGH); delay(100);
digitalWrite(boardLed,LOW); delay(100);
digitalWrite(boardLed,HIGH); delay(100);
digitalWrite(boardLed,LOW); delay(100);
digitalWrite(boardLed,HIGH); delay(100);
digitalWrite(boardLed,LOW);
Particle.connect();
Particle.process();
delay(5000);
for(int i=0; i<counter ; i++) {
itoa(list[i], buffer, 10);
//for(int j=1; j<9 ; j++) {
for(int j=1; j<9 ; j++) {
//all_times[i*9+j] = buffer[j+1];
all_times[i*9+j-1] = buffer[j+1];
}
//all_times[(i+1)*9] = ',';
all_times[(i+1)*9-1] = ',';
}
Spark.publish("Operation", all_times, 60, PRIVATE);
delay(600);
/*
if (counter%2 != 0) {
is_open = !is_open;
}
*/
counter = 0;
memset(&all_times[0], 0, sizeof(all_times));
memset(&list[0], 0, sizeof(all_times));
memset(&buffer[0],0,sizeof(buffer));
//Particle.disconnect();
//WiFi.off();
//delay(100);
}
}
else
{
if(counter == 0){
Particle.connect();
Particle.process();
delay(5000);
Spark.publish("still alive", all_times, 60, PRIVATE);
delay(600);
//Particle.disconnect();
//WiFi.off();
}
else{
Particle.connect();
Particle.process();
delay(5000);
for(int i=0; i<counter ; i++) {
itoa(list[i], buffer, 10);
//for(int j=1; j<9 ; j++) {
for(int j=1; j<9 ; j++) {
//all_times[i*9+j] = buffer[j+1];
all_times[i*9+j-1] = buffer[j+1];
}
//all_times[(i+1)*9] = ',';
all_times[(i+1)*9-1] = ',';
}
Spark.publish("Operation", all_times, 60, PRIVATE);
//delay(600);
delay(1000);
counter = 0;
memset(&all_times[0], 0, sizeof(all_times));
memset(&list[0], 0, sizeof(all_times));
memset(&buffer[0],0,sizeof(buffer));
//Particle.disconnect();
//WiFi.off();
//delay(100);
}
}
//is_startup = false;
//System.sleep(A0,RISING,30);
delay(300);
ETA_Wake = Time.now()+40;
System.sleep(SLEEP_MODE_DEEP, 40);
}