Button press(BLYNK_WRITE) causes Arduino Crash

Hey,

I‘m having some problems with a button in the Blynk App. I‘m using an Arduino Mega2560 + W5100 + the Blynk App on iOS + the latest Version of the Blynk library.

My project consists off 5DHT22, 5 Soil Moisture Sensors and some fans. I had no problem reading the Data from the sensors and displaying them in the app. My latest addition is a button, which i wanted to use to turn the fans on and off.
So i used Blynk_Write(V40) in my code for that and it only works once but then my Arduino disconnects from the App everytime.

Does anyone know what might cause this behaviour?

I‘ve searched the forum for an hour or so now and couldn‘t really find something related, but my best guess would be that i‘m sending too much data at once maybe? I have 2000ms timers(using the BlynkTimer and setInterval()) each for sending the temperature, humidity and soil moisture and in my App i have a refresh interval of 5 seconds for each value, 15 values so far in total.

If it helps, i can upload my code in a few hours or so when i‘m back home!

Thanks in advance :slight_smile:

//Libraries you need to make this Sketch Work
#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>
#include <DHT.h>

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

//Insert your Auth-Token from the Blynk App here
char auth[] = "YourAuthToken";

//Ethernet-Shield and SD-Card-Slot on Shield
#define W5100_CS  10
#define SDCARD_CS 4


//Temperaure/Moisture Sensors
#define dhtLeftTop 7
#define dhtRightTop 6
#define dhtCenter 5
#define dhtLeftBottom 4
#define dhtRightBottom 3

//Define your DHT-Type
#define DHTTYPE DHT22

//Soil Moisture Sensors
#define SoilLeftFront A0
#define SoilLeftBack A1
#define SoilCenterBack A2
#define SoilRightBack A3
#define SoilRightFront A4

//Airflow
#define airflow 10
#define filter 11

//Create DHT-Objects
DHT dht1(dhtLeftTop, DHTTYPE);
DHT dht2(dhtRightTop, DHTTYPE);
DHT dht3(dhtCenter, DHTTYPE);
DHT dht4(dhtLeftBottom, DHTTYPE);
DHT dht5(dhtRightBottom, DHTTYPE);

//Use BlynkTimer to set the Interval in which the Sensor Data is sent to the Blynk App
BlynkTimer timer;

//Slows down Fan RPM -> Lower Noise Output
void silentMode(){
  
  //Sets Fan RPM(0 - 255)
  analogWrite(airflow, 125);
  analogWrite(filter, 125);
}

//Sends Temperature Information to Blynk
void readSensorTemperature(){
  
  //Read Temperature
  float t1 = dht1.readTemperature();
  float t2 = dht2.readTemperature();
  float t3 = dht3.readTemperature();
  float t4 = dht4.readTemperature();
  float t5 = dht5.readTemperature();

  //Send Temperature to BLYNK
  Blynk.virtualWrite(V1, t1);
  Blynk.virtualWrite(V2, t2);
  Blynk.virtualWrite(V3, t3);
  Blynk.virtualWrite(V4, t4);
  Blynk.virtualWrite(V5, t5);

  //Calculate and send Average Temperature to Blynk (only if all Sensors have aquired valid Data)
  if (isnan(t1) || isnan(t2) || isnan(t3) || isnan(t4) || isnan(t5)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  float averageTemperature = (t1 + t2 + t3 + t4 + t5) / 5;
  Blynk.virtualWrite(V20, averageTemperature);
}

//Sends Humidity Information to Blynk
void readSensorHumidity(){

  //Read Humidity
  float h1 = dht1.readHumidity();
  float h2 = dht2.readHumidity();
  float h3 = dht3.readHumidity();
  float h4 = dht4.readHumidity();
  float h5 = dht5.readHumidity();

  //Send Humidity to BLYNK
  Blynk.virtualWrite(V6, h1);
  Blynk.virtualWrite(V7, h2);
  Blynk.virtualWrite(V8, h3);
  Blynk.virtualWrite(V9, h4);
  Blynk.virtualWrite(V10, h5);

  //Calculate and send Average Humidity to Blynk (only if all Sensors have aquired valid Data)
  if (isnan(h1) || isnan(h2) || isnan(h3) || isnan(h4) || isnan(h5)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  float averageHumidity = (h1 + h2 + h3 + h4 + h5) / 5;
  Blynk.virtualWrite(V21, averageHumidity);
}

//Sends Soil Moisture Information to Blynk
void readSoilMoisture(){

  //Read Soil Moisture
  int m1 = analogRead(SoilLeftFront);
  int m2 = analogRead(SoilLeftBack);
  int m3 = analogRead(SoilCenterBack);
  int m4 = analogRead(SoilRightBack);
  int m5 = analogRead(SoilRightFront);

  //Send Soil Moisture to Blynk
  Blynk.virtualWrite(V11, m1);
  Blynk.virtualWrite(V12, m2);
  Blynk.virtualWrite(V13, m3);
  Blynk.virtualWrite(V14, m4);
  Blynk.virtualWrite(V15, m5);
}

void setup(){

  // Debug console
  Serial.begin(9600);

  //Deactivate SD-CARD
  pinMode(SDCARD_CS, OUTPUT);
  digitalWrite(SDCARD_CS, HIGH); 

  //Connect to Blynk
  Blynk.begin(auth);

  //Start Temp/Hum Sensors
  dht1.begin();
  dht2.begin();
  dht3.begin();
  dht4.begin();
  dht5.begin();

  //Timer for Temperature Readings
  timer.setInterval(2000L, readSensorHumidity);

  //Timer for Humidity Readings
  timer.setInterval(2000L, readSensorTemperature);

  //Timer for Soil Moisture Readings
  timer.setInterval(2000L, readSoilMoisture)
  
//Fan-Mode-Switch
BLYNK_WRITE(V40)
{
  int fanmode = param.asInt();
  if(fanmode == 0)
  {
    if(averageTemperature > 26.5)
    {
      analogWrite(airflow, 240);
      analogWrite(filter, 240);
    }
    else
    {
      analogWrite(airflow, 150);
      analogWrite(filter, 150);
    }
  }
  else
  {
    silentMode();
  }
}

void loop(){

  //Starts Blynk
  Blynk.run();

  //Starts Timer
  timer.run();
}

Code formatting is backticks, not commas or apostrophes… I fixed it this time :wink:

Blynk%20-%20FTFC

As for your issue… Perhaps someone else will dig through your code for you, but I don’t wish to right now :innocent: So I suggest you start by placing some Serial.print() commands in various functions to track where your code is as it runs… then perhaps you can determine exactly where it stops working.

Also, all your timer controlled functions are running at the exact same time every 2 seconds… it is recommended to space them out so they do NOT run concurrently.

1 Like

That’s your problem.
Digital pin 10 is needed by the Ethernet shield, if you write other stuff to it you disable the Ethernet.

Pete.

2 Likes

Hmmm… I think this pin is also used by the Ethernet adapter… Google it.

Opps… late by a keystroke :stuck_out_tongue:

2 Likes

Thank you! I posted my answer and then saw my mistake but couldn’t edit it, because my account was temporarily put on hold :frowning:

My Timers are not set up properly yet, because i wanted too see how much traffic the arduino can handle at once and i didn’t understand how to properly stagger them yet. The example you provided perfectly explains it, thank you for that! :slight_smile:

That totally makes sense! :sweat_smile: I randomly picked a pwm port just for testing purposes and must have overseen this! Thank you guys for the quick replies! :smile: