Funny behavior after server/lib update

Hi there,

Hardware: Arduino UNO via USB
Server version: 0.37.3
Lib version: 0.5.3

I have observed some really funny behavior since updating my server and lib to the latest release (see above).

  1. BLYNK_CONNECTED() fires about every second even though the connection is stable and not interrupted.
  2. Blynk.syncAll(); syncs buttons just fine, however vertical steppers are no longer being synchronized.

Does any of you have a clue what’s happening here?

Cheers,
Dan

@dan.i.am please provide some debug info.

1 Like

Hi Dmitriy,

Do you mean debug info from the hardware or the Blynk server?

Cheers,
Dan

Let’s start from hardware side. However, more info you provide more chances for the issue to be solved.

1 Like

Alright, I don’t have a way to output debug information since I am using the USB connection variant, however I started a new sketch and Blynk is working as expected. So I am assuming it must be my code then.

Here is the debug sketch that is working perfectly fine:

#include <BlynkSimpleStream.h>

char auth[] = "nah";
WidgetTerminal terminal(V0);
int stepper = 0;
BLYNK_WRITE(V1)
{
  stepper = param.asInt();
  terminal.println(param.asInt());
  terminal.flush();
}

BLYNK_WRITE(V2)
{
  terminal.println(stepper);
  terminal.flush();
}

BLYNK_CONNECTED()
{
  Blynk.syncAll();
  digitalWrite(2, HIGH);
  delay(500);
  digitalWrite(2, LOW);
}

void setup() {
  // put your setup code here, to run once:
  pinMode(2, OUTPUT);
  Serial.begin(9600);
  Blynk.begin(Serial, auth);
}

void loop() {
  // put your main code here, to run repeatedly:
  Blynk.run();
}

Now, here is my actual code that executes the Blank Connected method over and over again:

// HydroCab - June 2018

#include <BlynkSimpleStream.h>
#include <TimeLib.h>
#include <WidgetRTC.h>

char auth[] = "nah";
String hostname = "nah.nah.com";
BlynkTimer timerSystem;
WidgetRTC rtc;
WidgetTerminal terminal(V13);


// Digital Control Pins
int hydroPump = 2;
int growingLight = 3;
int airPump = 4;
int fans = 5;
int supplyPump = 8;

// Analog Sensor Pins
int dhtInternal = A0;
int dhtExternal = A1;
int waterLevel = A2;

// Sensor Thresholds
int waterLow = 0;
int waterHigh = 0;
int internalTempHigh = 0;
int externalTempHigh = 0;
int internalHumHigh = 0;
int externalHumHigh = 0;

// Timers
int lampTimer = 1;
int statsTimer = 2;
int pumpTimer = 3;
long lampTimerInterval = 5000; // 5 seconds
long statsTimerInterval = 5000; // 5 seconds
long pumpTimerInterval = 500; // 0.5 seconds

// Lamp Time
int startHour = 0;
int stopHour = 0;

// Pump Timer
unsigned long onTime = 0;
unsigned long offTime = 0;
unsigned long previousMillis = 0;
unsigned long interval = onTime;
boolean pumpState = false;

void setup()
{
  // Pin Modes
  pinMode(hydroPump, OUTPUT);
  pinMode(airPump, OUTPUT);
  pinMode(growingLight, OUTPUT);
  pinMode(fans, OUTPUT);
  pinMode(supplyPump, OUTPUT);
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(dhtInternal, INPUT);
  pinMode(dhtExternal, INPUT);
  pinMode(waterLevel, INPUT);

  Serial.begin(9600);
  Blynk.begin(Serial, auth);

  // Timers
  statsTimer = timerSystem.setInterval(statsTimerInterval, statsRoutine);
  // Notify on boot
  terminalOutput("System fired up.");
}

void pumpRoutine()
{
  digitalWrite(hydroPump, pumpState);
  unsigned long currentMillis = millis();

  if ((unsigned long)(currentMillis - previousMillis) >= interval)
  {
    if (pumpState)
    {
      interval = offTime;
    }
    else
    {
      interval = onTime;
    }
    pumpState = !(pumpState);
    previousMillis = currentMillis;
    terminalOutput("Pump state changed.");
  }
}

void lampRoutine()
{
  int h = hour();
  if ((h >= startHour) && (h < stopHour))
  {
    if (digitalRead(growingLight) == LOW)
    {
      digitalWrite(growingLight, HIGH);
      terminalOutput("Starting growing light.");
    }
  }
  else
  {
    if (digitalRead(growingLight) == HIGH)
    {
      digitalWrite(growingLight, LOW);
      terminalOutput("Stopping growing light.");
    }
  }
}

void statsRoutine()
{
  // replace random figures with actual sensor values
  // Internal Humidity
  Blynk.virtualWrite(V0, random(0, 100));
  // External Humidity
  Blynk.virtualWrite(V1, random(0, 100));
  // Internal Temperature
  Blynk.virtualWrite(V2, random(0, 100));
  // External Temperature
  Blynk.virtualWrite(V3, random(0, 100));
  // Water Level
  Blynk.virtualWrite(V4, random(0, 100));
}

String getDateTime()
{
  String currentTime = String(hour()) + ":" + minute() + ":" + second();
  String currentDate = String(day()) + "/" + month() + "/" + year();
  return String(currentDate + " - " + currentTime);
}

void terminalOutput(String value)
{
  terminal.println("");
  terminal.print(String(getDateTime() + ": "));
  terminal.print(value);
  terminal.println("");
  terminal.flush();
}

// Hydro Pump Control
BLYNK_WRITE(V6)
{
  int pinValue = param.asInt();

  if (pinValue == 1)
  {
    pumpTimer = timerSystem.setInterval(pumpTimerInterval, pumpRoutine);
  }
  else
  {
    timerSystem.disable(pumpTimer);
    pumpState = false;
    previousMillis = 0;
    digitalWrite(hydroPump, LOW);
  }
}

// Air Pump Control
BLYNK_WRITE(V7)
{
  int pinValue = param.asInt();

  if (pinValue == 1)
  {
    digitalWrite(airPump, HIGH);
    terminalOutput("Air pump started.");
  }
  else
  {
    digitalWrite(airPump, LOW);
    terminalOutput("Air pump stopped.");
  }
}

// Lamp Control
BLYNK_WRITE(V8)
{
  int pinValue = param.asInt();

  if (pinValue == 1)
  {
    lampTimer = timerSystem.setInterval(lampTimerInterval, lampRoutine);
  }
  else
  {
    timerSystem.disable(lampTimer);
    digitalWrite(growingLight, LOW);
  }
}

// Fan Control
BLYNK_WRITE(V9)
{
  int pinValue = param.asInt();

  if (pinValue == 1)
  {
    digitalWrite(fans, HIGH);
    terminalOutput("Fans started.");
  }
  else
  {
    digitalWrite(fans, LOW);
    terminalOutput("Fans stopped.");
  }
}

// Start Stop Time Control
BLYNK_WRITE(V10)
{
  TimeInputParam t(param);

  if (t.hasStartTime())
  {
    startHour = t.getStartHour();
  }

  if (t.hasStopTime())
  {
    stopHour = t.getStopHour();
  }
}

// Supply Pump Control
BLYNK_WRITE(V11)
{
  int pinValue = param.asInt();

  if (pinValue == 1)
  {
    digitalWrite(supplyPump, HIGH);
    terminalOutput("Supply pump started.");
  }
  else
  {
    digitalWrite(supplyPump, LOW);
    terminalOutput("Supply pump stopped.");
  }
}

// Pump On Time Stepper
BLYNK_WRITE(V14)
{
  onTime = param.asInt() * 60000;
  terminalOutput(String(onTime / 60000));
}

// Pump Off Time Stepper
BLYNK_WRITE(V15)
{
  offTime = param.asInt() * 60000;
  terminalOutput(String(offTime / 60000));
}

BLYNK_CONNECTED()
{
  rtc.begin();
  Blynk.syncAll();
  terminalOutput("Blynk connected.");
}

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

Can you see anything obviously wrong with my code?

Cheers,
Dan

Okay…found it…it’s stupid… brace yourselves.

terminalOutput() gets called quite a few times during the initial boot of the Arduino. It flooded the server and the server said nope. Arduino reconnected and the same thing happened over and over …

Cheers,
Dan

1 Like