Blynk.logEvent causes a crash/freeze/non responsive

Have searched for this and cannot find any info on this particular error. Maybe I am not doing something right.

My sketch works great, however, when this command is inserted, the serial monitor and blynk IoT stops working. I see the seconds in Blynk count up for a couple seconds, then everything stops.

  if ( Percent / 1000 <= 25 && millis() > 60000 ) {
        Blynk.logEvent("low_water","Please add water to Piney!")
        Serial.println("add water to tree");
  }

The if condition is not even met, yet it stops the entire thing.
But if I just comment out that one line as so…

  if ( Percent / 1000 <= 25 && millis() > 60000 ) {
  //      Blynk.logEvent("low_water","Please add water to Piney!");
          Serial.println("add water to tree");
  }

then the rest of the program and I see the rest of the data on Blynk.

I do have the event setup in Blynk.

EDIT: any instance of Blynk.logEvent(“eventname”) causes entire program to not run on Blynk. I have tried it many ways.

FYI… full code here: (please excuse sloppy code, I am beginner :slight_smile: )

/*************************************************************
  WARNING!
    It's very tricky to get it working. Please read this article:
    http://help.blynk.cc/hardware-and-libraries/arduino/esp8266-with-at-firmware

  You’ll need:
   - Blynk IoT app (download from App Store or Google Play)
   - Arduino Uno board
   - Decide how to connect to Blynk
     (USB, Ethernet, Wi-Fi, Bluetooth, ...)

  There is a bunch of great example sketches included to show you how to get
  started. Think of them as LEGO bricks  and combine them as you wish.
  For example, take the Ethernet Shield sketch and combine it with the
  Servo example, or choose a USB sketch and add a code from SendData
  example.
 *************************************************************/

// Template ID, Device Name and Auth Token are provided by the Blynk.Cloud
// See the Device Info tab, or Template settings
#define BLYNK_TEMPLATE_ID           "redacted"
#define BLYNK_DEVICE_NAME           "Quickstart Template"
#define BLYNK_AUTH_TOKEN            "redacted"


// Comment this out to disable prints and save space
#define BLYNK_PRINT Serial


#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
char auth[] = BLYNK_AUTH_TOKEN;

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "redacted";
char pass[] = "redacted";

// Hardware Serial on Mega, Leonardo, Micro...
//#define EspSerial Serial1

// or Software Serial on Uno, Nano...
#include <SoftwareSerial.h>
SoftwareSerial EspSerial(2, 3); // RX, TX

// Your ESP8266 baud rate:
#define ESP8266_BAUD 9600

ESP8266 wifi(&EspSerial);

BlynkTimer timer;

const int OUT_PIN = A2;
const int IN_PIN = A4;

//Capacitance between IN_PIN and Ground
//Stray capacitance value will vary from board to board.
//Calibrate this value using known capacitor.
const float IN_STRAY_CAP_TO_GND = 24.48;
const float IN_CAP_TO_GND  = IN_STRAY_CAP_TO_GND;
//Pullup resistance will vary depending on board.
//Calibrate this with known capacitor.
const float R_PULLUP = 34.8;  //in k ohms
const int MAX_ADC_VALUE = 1023;
float capacitance;
#include <Smoothed.h>   // Include the library
Smoothed <float> mySensor; 
Smoothed <float> mySensor2;
int full = 1100;
int empty = 400;
int freq = 100;
int momentTime = 1000;


// This function sends Arduino's up time every second to Virtual Pin (5).
// In the app, Widget's reading frequency should be set to PUSH. This means
// that you define how often to send data to Blynk App.
void myTimerEvent()
{
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V5, millis() / 1000);
}

void waterLevel()
{
    float smoothedSensorValueAvg = mySensor.get();
    //Capacitor under test between OUT_PIN and IN_PIN
    //Rising high edge on OUT_PIN
    pinMode(IN_PIN, INPUT);
    digitalWrite(OUT_PIN, HIGH);
    int val = analogRead(IN_PIN);
    digitalWrite(OUT_PIN, LOW);


    
      //Big capacitor - so use RC charging method

      //discharge the capacitor (from low capacitance test)
      pinMode(IN_PIN, OUTPUT);
      while (millis() % (1000) != 0) //Default 1000
      ;

      //Start charging the capacitor with the internal pullup
      pinMode(OUT_PIN, INPUT_PULLUP);
      unsigned long u1 = micros();
      unsigned long t;
      int digVal;

      //Charge to a fairly arbitrary level mid way between 0 and 5V
      //Best not to use analogRead() here because it's not really quick enough
      do
      {
        digVal = digitalRead(OUT_PIN);
        unsigned long u2 = micros();
        t = u2 > u1 ? u2 - u1 : u1 - u2;
      } while ((digVal < 1) && (t < 400000L));

      pinMode(OUT_PIN, INPUT);  //Stop charging
      //Now we can read the level the capacitor has charged up to
      val = analogRead(OUT_PIN);

      //Discharge capacitor for next measurement
      digitalWrite(IN_PIN, HIGH);
      int dischargeTime = (int)(t / 1000L) * 5;
      delay(dischargeTime);    //discharge slowly to start with
      pinMode(OUT_PIN, OUTPUT);  //discharge remainder quickly
      digitalWrite(OUT_PIN, LOW);
      digitalWrite(IN_PIN, LOW);

      //Calculate and print result
      capacitance = -(float)t / R_PULLUP
                              / log(1.0 - (float)val / (float)MAX_ADC_VALUE);
      capacitance = capacitance*1000;

    // Read the value from the sensor
    float currentSensorValue = capacitance;
    
    // Add the new value to both sensor value stores
    mySensor.add(currentSensorValue);
    
    // Get the smoothed values
    //float smoothedSensorValueAvg = mySensor.get();
    
    // Output the smoothed values to the serial stream. Open the Arduino IDE Serial plotter to see the effects of the smoothing methods.
    float Percent = constrain(map(smoothedSensorValueAvg,empty,full,0,100000),0,100000);

  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V6, Percent/1000);
  Blynk.virtualWrite(V7, smoothedSensorValueAvg);
  if ( Percent / 1000 <= 25 && millis() > 60000 ) {
        Blynk.logEvent("low_water","Please add water to Piney!");
  }
  
}

void setup()
{
  // Debug console
  Serial.begin(9600);

  

  // Set ESP8266 baud rate
  EspSerial.begin(ESP8266_BAUD);
  delay(10);
 Serial.println("baud rate set complete!");

  Blynk.begin(auth, wifi, ssid, pass);
  // You can also specify server:
  //Blynk.begin(auth, wifi, ssid, pass, "blynk.cloud", 80);
  //Blynk.begin(auth, wifi, ssid, pass, IPAddress(192,168,1,100), 8080);
 Serial.println("connected to Blynk complete!");

    // Setup a function to be called every second
  timer.setInterval(1000L, myTimerEvent);
  timer.setInterval(2000L, waterLevel);
 Serial.println("timer interval set complete!");



 //Serial.println("initializing pinmodes 1...");
 pinMode(OUT_PIN, OUTPUT);
  //digitalWrite(OUT_PIN, LOW);  //This is the default state for outputs
//Serial.println("initializing pinmodes 2...");
  pinMode(IN_PIN, OUTPUT);
  //digitalWrite(IN_PIN, LOW);
 //Serial.println("initializing smoothed...");
  mySensor.begin(SMOOTHED_AVERAGE, 25);  //Default 50
 //Serial.println("initializing sensor clear...");
  mySensor.clear();
 Serial.println("Setup loop complete!");

}

void loop()
{
  Blynk.run();
  timer.run(); // Initiates BlynkTimer

  // You can inject your own code or combine it with other sketches.
  // Check other examples on how to communicate with Blynk. Remember
  // to avoid delay() function!
}

Hey there,

I’d recommend you to try a simple sketch to send the event just to make sure it’s working.

Some information about the Blynk library version you are using, how your Event is configured and what you see in your serial monitor from boot-up through to crash would be useful.

Pete.

Library version installed:

Serial monitor when Blynk.logEvent is used (never connects to Blynk):

    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v1.0.1 on Arduino Uno

[624] Connecting to FBI Surveillance Van 3
[3811] AT version:1.1.0.0(May 11 2016 18:09:56)
SDK version:1.5.4(baaeaebb)
compile time:May 20 2016 15:08:19
OK
[7108] +CIFSR:STAIP,"192.168.1.132"
+CIFSR:STAMAC,"e8:db:84:96:54:4d"
[7116] Connected to WiFi
[17482] Ready (ping: 39ms).
[48551] Ready (ping: 39ms).
[79346] Ready (ping: 40ms).
[110147] Ready (ping: 40ms).
[140952] Ready (ping: 39ms).

Serial monitor when Blynk.logEvent is removed from code:

    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v1.0.1 on Arduino Uno

[624] Connecting to FBI Surveillance Van 3
[3811] AT version:1.1.0.0(May 11 2016 18:09:56)
SDK version:1.5.4(baaeaebb)
compile time:May 20 2016 15:08:19
OK
[4889] Failed to enable MUX
[8102] +CIFSR:STAIP,"192.168.1.132"
+CIFSR:STAMAC,"e8:db:84:96:54:4d"
[8112] Connected to WiFi
[18476] Ready (ping: 38ms).
connected to Blynk complete!
timer interval set complete!
Setup loop complete!

I have had success as @John93 suggested by putting it into a simple sketch. So I came back and put it into the myTimerEvent() function and it worked! However, I really need it in the waterLevel() function, and it will not work there, but other Blynk commands do. I will keep working on trying to figure this out. Thanks for the help. :slight_smile:

in my opinion it’s better to use an ultrasonic sensor to measure the water level, I’d use an HCSR-04 to measure the water level and turn it into a percentage.

I’d try simplifying your waterLevel() function, initially by taking out the smoothing code and find-out what it is about that function that causes the problem.
Also, you shouldn’t have pinMode statements in your functions like this, they belong in void setup.

If your problems persist then I’d try using different pins.

Pete.