How can I correctly set initial state of widgets?

I’d like to set the initial state and value of some widgets such as led on/off, display, properties but its not working for me. Is there a Blynk.isRunning(project_name) type of function? What is the best practice for doing this?

I’m open for suggestions on what I’m doing wrong or a different way to approach it.

Hardware and libraries:
Nexus 6 with Android 7.0 installed
Sparkfun RedBoard (Uno clone)
Adafruit BlueFruit LE Shield – This could once again be an issue with BLE?
Big Easy Driver (stepper motor driver – not used in this sketch, but is connected)
Blynk library v0.3.10

Here are the things I tried. The complete sketch is at the end of the post

My first thought is to put them in setup()

void setup()
{
  // other setup stuff

  Blynk.virtualWrite(V0, 101);
  Blynk.setProperty(V0, "label", "What Animal?");
  Blynk.setProperty(V0, "color", "#99FFFF");
}

Doesn’t work.
So I tried waiting for connections

void setup()
{
  // other setup stuff

  while (!Blynk.connected() && !ble.isConnected())
  {  delay(2000);  }
  
  Blynk.virtualWrite(V0, 101);
  Blynk.setProperty(V0, "label", "What Animal?");
  Blynk.setProperty(V0, "color", "#99FFFF");
}

Still no joy.

So then I try moving the settings into a function and calling it from setup

void setup()
{
  // other setup stuff

  while (!Blynk.connected() && !ble.isConnected())
  { delay(2000); }
  
  startvalues();
}

Still doesn’t work, but if I start it after a delay.

SimpleTimer timer;
int delay_start = timer.setTimeout(100, startValues);

void setup()
{
  // other setup stuff

  while (!Blynk.connected() && !ble.isConnected())
  { delay(2000); }
  
  timer.restartTimer(delay_start);
}

it somewhat works. The colors and labels are set, but only the setProperty lines.
The only way I can get the value itself to be set is if the dashboard is running.

Any ideas?

Here is the complete sketch.

/////////////////////////////////////
//
// V2   labeledValue, freq = push
// BLE module

// #define BLYNK_DEBUG
#define BLYNK_PRINT Serial
#define BLYNK_USE_DIRECT_CONNECT // If trouble connecting trying commenting out

#include <SimpleTimer.h>
#include <SPI.h>
#include <SoftwareSerial.h>

// BLE
#include <BlynkSimpleSerialBLE.h>
// Adafruit Bluefruit LE Shield which using SPI
#include <Adafruit_BLE.h>
#include <Adafruit_BluefruitLE_SPI.h>

#define BLUEFRUIT_SPI_CS 8
#define BLUEFRUIT_SPI_IRQ 7
#define BLUEFRUIT_SPI_RST 4
#define BLUEFRUIT_VERBOSE_MODE false

Adafruit_BluefruitLE_SPI ble(BLUEFRUIT_SPI_CS, BLUEFRUIT_SPI_IRQ, BLUEFRUIT_SPI_RST);

char auth[] = "xxxxxxxxxxxxxxxxxxxxxxx";

int cats = 0;
void startValues()
{
  Serial.println("Enter startValues()");
  Blynk.virtualWrite(V0, 171);
  Blynk.setProperty(V0, "label", "What Animal?");
  Blynk.setProperty(V0, "color", "#0099FF");
  Serial.println("Exit startValues()");
}

SimpleTimer timer;
int delay_start = timer.setTimeout(100, startValues);

void setup()
{
  Serial.begin(9600);
  Blynk.begin(auth, ble);

  ble.begin(BLUEFRUIT_VERBOSE_MODE);
  ble.factoryReset(); // Optional but advisable
  ble.setMode(BLUEFRUIT_MODE_DATA);

  while (!Blynk.connected() && !ble.isConnected())
  {
    delay(2000);
  }
  Serial.println("All connected");

  timer.restartTimer(delay_start);

  // startValues();

  // Blynk.setProperty(V0, "label", "What Animal?");
  // Blynk.setProperty(V0, "color", "#99FFFF");
  // Blynk.virtualWrite(V0, 101);
}

void loop()
{
  Blynk.run();
  timer.run();
}

Hello. BLYNK_CONNECTED may fit your need.

BLYNK_CONNECTED {
    ///do init stuff
}

That simplifies it some, thanks @Dmitriy

But Blynk.virtualWrite(Vx, num) is still not getting set.
Everything else though seems to work with BLYNK_CONNECTED

/////////////////////////////////////
//
// V2   labeledValue, freq = push
// BLE module

// #define BLYNK_DEBUG
#define BLYNK_PRINT Serial
#define BLYNK_USE_DIRECT_CONNECT // If trouble connecting trying commenting out

#include <SPI.h>
#include <SoftwareSerial.h>

// BLE
#include <BlynkSimpleSerialBLE.h>
// Adafruit Bluefruit LE Shield which using SPI
#include <Adafruit_BLE.h>
#include <Adafruit_BluefruitLE_SPI.h>

#define BLUEFRUIT_SPI_CS 8
#define BLUEFRUIT_SPI_IRQ 7
#define BLUEFRUIT_SPI_RST 4
#define BLUEFRUIT_VERBOSE_MODE false

Adafruit_BluefruitLE_SPI ble(BLUEFRUIT_SPI_CS, BLUEFRUIT_SPI_IRQ, BLUEFRUIT_SPI_RST);

char auth[] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx";

BLYNK_CONNECTED()
{
  // Serial.println("BLYNK_CONNECTED()");
  Blynk.virtualWrite(V0, 171);
  Blynk.setProperty(V0, "label", "What Animal?");
  Blynk.setProperty(V0, "color", "#FF0000");
}

void setup()
{
  Serial.begin(9600);
  Blynk.begin(auth, ble);

  ble.begin(BLUEFRUIT_VERBOSE_MODE);
  ble.factoryReset(); // Optional but advisable
  ble.setMode(BLUEFRUIT_MODE_DATA);
  // Serial.println("All connected");

}

void loop()
{
  Blynk.run();
}

More details?

Ummm. Sorry if what I would like to do is not clear. What details are you looking for?

The hardware and libraries are at the top of the OP, but here it is again.

  • Nexus 6 with Android 7.0 installed
  • Sparkfun RedBoard (Uno clone)
  • Adafruit BlueFruit LE Shield – This could once again be an issue with BLE?
  • Big Easy Driver (stepper motor driver – not used in this sketch, but is connected)
  • Blynk library v0.3.10

An example sketch and snippets along with explanation are also above.

I would like to set the value of a "Value Display Wiidget when the sketch is uploaded or after it is reset. For example, I have a variable with a default value and I would like the value display widget to set the widget to the value of that variable.

I attempted to do this several different ways in setup(), but you suggested using BLYNK_CONNECTED() instead

int interval = 500; // Step interval for stepper motor
const int VALUE_WIDGET = 13; // Pin V13 for Value Widget

BLYNK_CONNECTED()
{
Blynk.virtualWrite(VALUE_WIDGET, interval);  // value is not set to 500 (interval)
Blynk.setProperty(VALUE_WIDGET, "color", "#99FF99") // color will be set
}

BLYNK_CONNECTED() seems to be working with setProperty() but not with virtualWrite(). For example, in the snippet above the value of the widget on V13 will not be set to 500.

What other details would be helpful?

screens shots of the APP settings?


int interval = 500; // Step interval for stepper motor

BLYNK_CONNECTED()
{
Blynk.virtualWrite(V13, interval);  // value is not set to 500 (interval)
Blynk.setProperty(V13, "color", "#99FF99") // color will be set
}

what happens if you do the above?

What I wrote right below that snippet in my last post –

“BLYNK_CONNECTED() seems to be working with setProperty() but not with virtualWrite(). For example, in the snippet above the value of the
widget on V13 will not be set to 500”.

In other words the widget’s color is set to #99FF99 and the widget’s value is not set to 500, the value of the variable named interval.

Stated yet another way:

  • Blynk.virtualWrite() does not work properly if called from setup() or BLYNK_CONNECTED().
  • Blynk.virtualWrite() does work properly if called after a delay.
    See my example snippets in the OP.

There are only two widgets in this example, BLE and a Labeled Value Display.

Here are the Value Display settings

1 Like

Okey. It could be related somehow with BLE. @vshymanskyy could you please advise?

Unfortunately BLE modules not very stable, so we had to put a bunch of hacks to make it work. It could be a reason.

Ok t thought it might be related to BLE. I know BLE is beta, but I do appreciate it being available. Despite the current limitations my project benefits from it, so thanks!

Thank for your detailed report, I will look into this as we move to stabilization.

1 Like