[SOLVED]Make Photon run code without being necessarily connected to the Particle cloud

How can I make my Photon run code (setup, start) without being necessarily connected to the Particle cloud? Sometimes I am loosing connection to the cloud, and my circuit just stops executing.

#include <math.h>

int voltage_pin = A2;        //Pin assignment that will receive the signal from the voltage shield
int relay1 = D7;            // Attach the relay 1 on D1 pin from Photon
int relay2 = D6;            // Attach the relay 2 on D2 pin from Photon
int pushbutton1 = D3;       // Attach the pushbutton 1 on D3 pin from Photon
int pushbutton2 = D4;       // Attach the pushbutton 2 on D4 pin from Photon

volatile int state_relay1 = HIGH;       // Initialize the relay 1 as HIGH or ON
volatile int state_relay2 = HIGH;       // Initialize the relay 2 as HIGH or ON


// Setup of the Photon pins and serial port communication
void setup() {
    pinMode(voltage_pin, INPUT);     // Input Pin for the voltage sensor
    Serial.begin(9600);
      
    pinMode(relay1, OUTPUT);                                  
    pinMode(pushbutton1, INPUT_PULLUP);                      
    attachInterrupt(pushbutton1, toggle_relay1, FALLING);   
  
    pinMode(relay2, OUTPUT);                                 
    pinMode(pushbutton2, INPUT_PULLUP);                     
    attachInterrupt(pushbutton2, toggle_relay2, FALLING);
}

// Main function that will continuously call for helper functions to do its logic
void loop() {

    
    double voltage = get_voltage(); 
   
    digitalWrite(relay1, state_relay1);  
    digitalWrite(relay2, state_relay2);  

    Serial.println("The voltage is:");
    Serial.println(voltage);
    Serial.println("######################################");
    delay(500);
}
1 Like

Never mind guys, just found this code in another thread and it did the job:

SYSTEM_MODE(SEMI_AUTOMATIC)
SYSTEM_THREAD(ENABLED)

const uint32_t msRetryDelay = 5*60000; // retry every 5min
const uint32_t msRetryTime  =   30000; // stop trying after 30sec

bool   retryRunning = false;
Timer retryTimer(msRetryDelay, retryConnect);  // timer to retry connecting
Timer stopTimer(msRetryTime, stopConnect);     // timer to stop a long running try

void setup()
{
  Serial.begin(115200);
  pinMode(D7, OUTPUT);
  Particle.connect();
  if (!waitFor(Particle.connected, msRetryTime))
    WiFi.off();                // no luck, no need for WiFi
}

void loop()
{
  // do your stuff
  digitalWrite(D7, !digitalRead(D7));
  
  if (!retryRunning && !Particle.connected())
  { // if we have not already scheduled a retry and are not connected
    Serial.println("schedule");
    stopTimer.start();         // set timeout for auto-retry by system
    retryRunning = true;
    retryTimer.start();        // schedula a retry
  }
  delay(500);
}

void retryConnect()
{
  if (!Particle.connected())   // if not connected to cloud
  {
    Serial.println("reconnect");
    stopTimer.start();         // set of the timout time
    WiFi.on();
    Particle.connect();        // start a reconnectino attempt
  }
  else                         // if already connected
  {
    Serial.println("connected");
    retryTimer.stop();         // no further attempts required
    retryRunning = false;
  }
}

void stopConnect()
{
    Serial.println("stopped");

    if (!Particle.connected()) // if after retryTime no connection
      WiFi.off();              // stop trying and swith off WiFi
    stopTimer.stop();
}
4 Likes

Glad you were able to figure out your issue!

1 Like