The adafruitio_07_digital_out example uses digital pin 5 by default on all boards, and that can be modified by changing the LED_PIN define.

Note: If you are using the WICED Feather, you will need to change the LED_PIN define to PC5 instead of the default setting of 5.

/************************ Example Starts Here *******************************/

// digital pin 5
#define LED_PIN 5

The next chunk of code sets up an Adafruit IO Feed instance for a feed called digital.

// set up the 'digital' feed
AdafruitIO_Feed *digital = io.feed("digital");

In the setup function, we set the LED_PIN as a digital output, and connect to Adafruit IO. We also attach a function called handleMessage to the digital feed that will be called whenever your device receives messages for that feed.

The code will wait until you have a valid connection to Adafruit IO before continuing with the sketch. If you have any issues connecting, check config.h for any typos in your username or key.

void setup() {

  // set led pin as a digital output
  pinMode(LED_PIN, OUTPUT);

  // start the serial connection
  Serial.begin(115200);

  // wait for serial monitor to open
  while(! Serial);

  // connect to io.adafruit.com
  Serial.print("Connecting to Adafruit IO");
  io.connect();

  // set up a message handler for the 'digital' feed.
  // the handleMessage function (defined below)
  // will be called whenever a message is
  // received from adafruit io.
  digital->onMessage(handleMessage);

  // wait for a connection
  while(io.status() < AIO_CONNECTED) {
    Serial.print(".");
    delay(500);
  }

  // we are connected
  Serial.println();
  Serial.println(io.statusText());

  digital->get();
}

Next, we have the main loop() function. The first line of the loop function calls io.run(); this line will need to be present at the top of your loop in every sketch. It helps keep your device connected to Adafruit IO, and processes any incoming data.

void loop() {
  // io.run(); is required for all sketches.
  // it should always be present at the top of your loop
  // function. it keeps the client connected to
  // io.adafruit.com, and processes any incoming data.
  io.run();
}

The final chunk of code is the handleMessage function. This is the function that is called whenever the digital feed gets a message.

We use the data->toPinLevel() function to convert the incoming data to either LOW or HIGH, and set the state of the LED_PIN to that value.

// this function is called whenever an 'digital' feed message
// is received from Adafruit IO. it was attached to
// the 'digital' feed in the setup() function above.
void handleMessage(AdafruitIO_Data *data) {

  Serial.print("received <- ");

  if(data->toPinLevel() == HIGH)
    Serial.println("HIGH");
  else
    Serial.println("LOW");

  // write the current state to the led
  digitalWrite(LED_PIN, data->toPinLevel());

}

Upload the sketch to your board, and open the Arduino Serial Monitor. Your board should now connect to Adafruit IO.

    Connecting to Adafruit IO....

Adafruit IO connected.
  

Toggle the button on your Adafruit IO dashboard, and you should see the following in the Arduino Serial Monitor.

received <- HIGH
received <- LOW
received <- HIGH
received <- LOW

You can now toggle the button on your Adafruit IO dashboard, and you should see your LED turn on and off.

This guide was first published on Apr 30, 2015. It was last updated on Apr 30, 2015.

This page (Arduino Code) was last updated on Feb 15, 2017.

Text editor powered by tinymce.