Breathing white light using SEMI-AUTOMATIC mode and Sleep

Hello, I know this question has been asked before but I am still struggling to trouble shoot/debug my code!

Basically I am trying to build a device (running wheel), with two main criteria: 1) if it doesn’t get used for 30 seconds, the photon goes to sleep, otherwise, 2) I’d like to connect to WiFi and the cloud to purge the data out to Blynk (a cell phone app).

When I upload the code with the SYSTEM_MODE(SEMI_AUTOMATIC), the status LED goes in to white breathing mode and I don’t believe it runs the loop() at all (because Pin D7 LED doesn’t go high/low when the wheel cycles).

Any help would be highly appreciated, thanks!

Here is my code:

#include <blynk.h>
#include "Particle.h"
#include "application.h"
#define Halleffect D1
SYSTEM_THREAD(ENABLED);
SYSTEM_MODE(SEMI_AUTOMATIC);


char auth[] = ""; //Blynk token

int WheelCount = 0;
int previous_state=1;
int current_state= 1;

int T1= 0;
int T_lapse_sleep= 0;
int T_lapse_data= 0;


BlynkTimer timer;

void setup() {
    Serial.begin(9600);
    timer.setInterval(10000L, WriteWheelData);
    pinMode(Halleffect,INPUT);
    pinMode(D7, OUTPUT);
    Blynk.begin(auth);
}

void loop() {
    
    current_state = digitalRead(Halleffect);
    Serial.println(current_state);
    //if a cycle is read
    if (current_state == 0){
        if (previous_state != current_state){
            WheelCount += 1;
            Serial.print("Wheel count is: ");
            Serial.println(WheelCount);
            digitalWrite (D7, HIGH);
            delay (100);
            digitalWrite (D7, LOW);
            T1= millis();
            previous_state= current_state ;
        }
    }
    //a cycle isn't read
    else{ 
        previous_state= current_state;
        T_lapse_sleep = millis() - T1;
        if (T_lapse_sleep > 30000){
            Serial.print("Sleep");
            System.sleep(Halleffect,FALLING);
        }
    }
    
    
    T_lapse_data = millis() - T1;
    if (T_lapse_data > 10000){
        if (!Particle.connected()){
            WiFi.on();
            WiFi.connect();
            Particle.connect();
        }
        Particle.publish ("Wheel count number is: ",String(WheelCount));
        timer.run();
        Blynk.run();
    }

}

void WriteWheelData()
{
  Blynk.virtualWrite(V1, WheelCount);  
  WheelCount = 0;
}

To have any Blynk features work, you need to connect to the internet first via WiFi.connect().
As it seems Blynk relys on the presence of a connection, you should always check WiFi.ready() before executing any of the Blynk functions.

Also in order to have your Particle.publish() and Blynk.run() work after the connection attempts, you need to allow for the connection to be established first.

All variables used in connection with millis() should be declared unsigned int or uint32_t.

To actually check whether loop() is being executed or not, you may want to have some unconditional D7 blink code, otherwise other factors might lead you to wrong conclusions.

Thank you for all the hints, I moved all of the blynk stuff including Blynk.begin(), until after I have established connections. That did it!!

Thanks again

1 Like