Language syntax

Structure

setup()

The setup() function is called when an application starts. Use it to initialize variables, pin modes, start using libraries, etc. The setup function will only run once, after each powerup or device reset.

// EXAMPLE USAGE

int button = D0;
int LED = D1;
//setup initializes D0 as input and D1 as output
void setup()
{
  pinMode(button, INPUT_PULLDOWN);
  pinMode(LED, OUTPUT);
}

void loop()
{
  // ...
}

If you are using SYSTEM_THREAD(ENABLED), setup() is called before connecting to the network. Threading is enabled by default in Device OS 6.2.0 and later.

If you are using SYSTEM_MODE(SEMI_AUTOMATIC) or SYSTEM_MODE(MANUAL), setup() is also called before connecting to the network, regardless of threading.

In the specific case of not using the system thread and using SYSTEM_MODE(AUTOMATIC) setup() is only called after connecting to the cloud (breathing cyan).

For more information on startup behavior, see Startup behavior.

loop()

After creating a setup() function, which initializes and sets the initial values, the loop() function does precisely what its name suggests, and loops consecutively, allowing your program to change and respond. Use it to actively control the device. A return may be used to exit the loop() before it completely finishes.

// EXAMPLE USAGE

int button = D0;
int LED = D1;
//setup initializes D0 as input and D1 as output
void setup()
{
  pinMode(button, INPUT_PULLDOWN);
  pinMode(LED, OUTPUT);
}

//loops to check if button was pressed,
//if it was, then it turns ON the LED,
//else the LED remains OFF
void loop()
{
  if (digitalRead(button) == HIGH)
    digitalWrite(LED,HIGH);
  else
    digitalWrite(LED,LOW);
}

If you are using SYSTEM_THREAD(ENABLED), loop() is called before connecting to the network. Threading is enabled by default in Device OS 6.2.0 and later.

If you are using SYSTEM_MODE(SEMI_AUTOMATIC) or SYSTEM_MODE(MANUAL), loop() is also called before connecting to the network, and when disconnected, regardless of threading.

In the specific case of not using the system thread and using SYSTEM_MODE(AUTOMATIC) loop() is only called after actively to the cloud (breathing cyan). It stops running when reconnecting (blinking green or blinking cyan). It also stops running while downloading an OTA update.