Wiki

VirginSoil-NeoPixel-Callbacks

The VirginSoil-NeoPixel-Callbacks comes with the IOTAppStory library as explained here.

This example code is to demonstrate a possible solution to providing feedback about the state of the application to the end user. In the previous VirginSoil-Full example we used the serial monitor to view printed messages.

In this example we will use a single neopixel to show colors depending on the state of your app. The nice thing about neopixels is that they keep their state(color) in between commands.

As quoted from Adafruit Industries wiki page
NeoPixel is Adafruit's brand of individually-addressable red-green-blue (RGB) LED. They are based on the WS2812 LED and WS2811 driver...

We will use the Adafruit_NeoPixel library in this example. But feel free to use any library you want because this example is more about the idea than the actual code itself.

 

Methods used

 

As we already talked about the defines, include & initializing the IOTAppStory library in the VirginSoil-Basic example and the IAS methods in the VirginSoil-Full example we will skip that here.

#define FEEDBACKLED 4

#include <Adafruit_NeoPixel.h>

Adafruit_NeoPixel pixels = Adafruit_NeoPixel(1, FEEDBACKLED, NEO_GRB + NEO_KHZ800);

FEEDBACKLED Define which pin on the esp is connected to the NeoPixel you want to use for feedback. (D2 for the Wemos Neo Pixel shield)
Include the Adafruit_NeoPixel library
Initialize the library

 

uint32_t none   = pixels.Color(0, 0, 0);

uint32_t cyan   = pixels.Color(0, 255, 255);              // wait / booting
uint32_t white  = pixels.Color(255, 255, 255);            // configuration mode

uint32_t blue   = pixels.Color(0, 0, 255);                // If mode button is released, I will enter in firmware update mode.
uint32_t purple = pixels.Color(128, 0, 128);              // If mode button is released, I will enter in configuration mode.

uint32_t yellow = pixels.Color(255, 255, 100);            // firmware update mode: calling home / checking for updates
uint32_t orange = pixels.Color(255, 140, 0);              // firmware update mode: downloading update
uint32_t red    = pixels.Color(255, 0, 0);                // firmware update mode: update error, check your logs

uint32_t green  = pixels.Color(0, 128, 0);                // loop / running

These are the declared colors we are going to use in the callbacks.

 

// ======================== SETUP ======================== 
void setup() {
  ...

  IAS.onModeButtonShortPress([]() {
    Serial.println(F(" If mode button is released, I will enter in firmware update mode."));
    Serial.println(F("*-------------------------------------------------------------------------*"));
    
    pixels.setPixelColor(0, blue);                        // send blue / release for updates to the neopixel
    pixels.show();
  });

  IAS.onModeButtonLongPress([]() {
    Serial.println(F(" If mode button is released, I will enter in configuration mode."));
    Serial.println(F("*-------------------------------------------------------------------------*"));
    
    pixels.setPixelColor(0, purple);                      // send purple / release for config to the neopixel
    pixels.show();
    delay(100);
  });

  IAS.onConfigMode([]() {
    pixels.setPixelColor(0, white);                       // send white /entered config mode to the neopixel
    pixels.show();
    delay(100);
  });

  IAS.onFirmwareUpdateCheck([]() {
    pixels.setPixelColor(0, yellow);                      // send yellow / update check to the neopixel
    pixels.show();
    delay(100);
  });

  IAS.onFirmwareUpdateDownload([]() {
    pixels.setPixelColor(0, orange);                      // send orange / update download to the neopixel
    pixels.show();
    delay(100);
  });

  IAS.onFirmwareUpdateError([]() {
    pixels.setPixelColor(0, red);                         // send red / update error to the neopixel
    pixels.show();
    delay(100);
  });

  ...
}

Find out more about callbacks here.

 

// ======================== LOOP ======================== 
void loop() {
  IAS.loop();


  //-------- Your Sketch starts from here ---------------

  if(digitalRead(MODEBUTTON) == HIGH) {

    if(millis() - printEntry > 5000) {
      printEntry = millis();
  
      // send black / off to the neopixel
      pixels.setPixelColor(0, none);
      pixels.show();
      delay(400);
    }else{
      // send green / running to the neopixel
      pixels.setPixelColor(0, green);
      pixels.show();
      delay(100);
    }
  }

}

If the button is not being pressed set the neopixel to green and blink every 5 seconds.

Wemos-VirginSoil-OLED-Callbacks
VirginSoil-Full

Post by

Onno Dirkzwager