Getting timers to work

Am I missing some sort of include, I want it to connect to wifi for 10 seconds and then sleep

error: ‘Timer’ was not declared in this scope
Timer timer(1000, turnOff);

I am using a Particle Photon

#include "application.h"
SYSTEM_MODE(MANUAL);

#define CONNECTED 10000
#define SLEEP 60

int led2 = D7;
Timer timer(10000, turnOff, true);

void setup() {

    pinMode(led2, OUTPUT);
    digitalWrite(led2, LOW);

    WiFi.setCredentials("","");
    WiFi.connect();
}

void turnOff()
{
    System.sleep(SLEEP_MODE_DEEP,SLEEP);
}

void loop() {

    if (WiFi.ready() == false) {
        digitalWrite(led2, LOW);
    }
    else
    {
        digitalWrite(led2, HIGH);
        timer.start();

    }
    delay(100);
}

** updated the code, still does not work

@burf2000, you need to declare your timer only once as a global (before setup()). Then, in loop(), start the timer in your else{} with timer.start(). :wink:

1 Like

Are you targeting a Photon?

If you are targeting a Spark Core (as I assume) you’ll not find Software Timers by default.
You’d need to import the freertos4core library into your project.

And if you are targeting a device that does support them, do as @peekay123 said.

In any case, consider not calling WiFi.setCredentials() each time anew - it’s not necessary and might even cause flash wear on your device.
Furthermore I’m not sure the WiFi module is initially activated in MANUAL mode, so you might need to call WiFi.on() too.
And while we are at it, if you want to make sure WiFi is ready before you enter loop() you can use

  WiFi.on();
  WiFi.connect();
  waitUntil(WiFi.ready); // or waitFor(WiFi.ready, 30000); // to wait max. 30sec
1 Like

I have updated the code and still does not compile, does the Particle Photon have timers

Yes, the Photon supports Software Timers, but you also need to tell your IDE that you are building for a Photon.

What IDE are you using?

How do I do that? (Thanks for your help)

What IDE are you using?

1 Like

Atom with local compiling

Are you sure about local compiling? (Particle Dev - which I assume you mean when saying Atom - does still use the cloud build farm to generate a binary)
But if it’s actually local building, you might need to update the system repo the local compiler uses fo building.

Where did you get the local compiler package from?

Anyhow, when using Particle Dev you should have a colored dot in your status bar at the bottom (what color has it and what letter has it written in it?).
You can click on the lable to the right of it or use the menu Particle - Select device … to …well, select your target device.

I am using Atom with the particle-offline-compiler package installed (0.13) I probably downloaded it 3 weeks ago

On my right menu, Particle.offline I have targeted the Photon

Are timers fairly new then?

Do you mean this one (which is a year old)?


Or have you got a link to your other one?

And what target version have you selected for your Photon? It needs to be 0.5.0 or later.

I’d suggest you first go with the cloud builds and only after having more experience with the Particle firmware structures move on to local building.

Oh wow did not see that was a year old, where can I get the updated one from?

My issue is my device may never be on the internet and so how can I deploy to them if they don’t have access to the cloud.

Ok so it wont be the firmware out of date, it be the compiler?

What do you mean with “my device”?
Do you refer to your Photon or your development machine?

If your dev machine has internet connection you can always build in the cloud, download the resulting binary (which will happen automatically with CLI and Particle Dev cloud builds) and flash that binary over USB.

I don’t think there is a newer version yet, since most man power is going into a complete rework of all Particle IDEs to unify their behaviour and provide better library support and portability.
What’s the most recent system version you can select with your offline compiler?

Also see this opening note in the repo

So the device won’t have Internet access, how do I compile in the cloud but deploy over USB? DO I use Particle Dev instead

As I already said when you asked that same question earlier

Since you never came back to that, I didn’t see any point in adding the how-to :wink:

1 Like

Sorry for not coming back, a how to will be useful, I been off work for 3 weeks with the birth of our 3rd son Charlie

I have compiled it in P-Dev, I will try and find the CLI guide

Sorted particle flash --serial firmware.bin

I guess (since I don’t use Dev anymore) that’d be also required with Dev

# in DFU mode
particle flash --usb <yourFirmware.bin>
# or (less reliable) in Listening mode
particle flash --serial <yourFirmware.bin>