Need help on this snippe

The code below controls the frequency of my server requests to blynk.
timeNow = millis() /1000
Somehow I am still making to many requests because I keep losing server connection every 10 to 30 seconds or so.
What am I missing?

void blynkUpdate1 () {

  if ((timeNow - timeBlynkLast) > 2 && blynkCalled == 0) {
  
    Blynk.virtualWrite(9, minutesCounter);
    Blynk.virtualWrite(2, heater1);
    Blynk.virtualWrite(4, heater2);
    Blynk.virtualWrite(5, minutes);
    blynkCalled = 1;
   
  }

}




void blynkUpdate2 () {

  if ((timeNow - timeBlynkLast) > 4 && blynkCalled == 1) {
  
    Blynk.virtualWrite(6, brewStage);
    Blynk.virtualWrite(7, temperature);
    Blynk.virtualWrite(8, temperatureDHT);
    blynkCalled = 2;
   
  }

}

void blynkUpdate3 () {

  if ((timeNow - timeBlynkLast) > 6 && blynkCalled == 2) {


    lcd.clear();
    lcd.print(0, 0, LCD1); // use: (position X: 0-15, position Y: 0-1, "Message you want to print")
    lcd.print(0, 1, LCD2); // use: (position X: 0-15, position Y: 0-1, "Message you want to print")
    timeBlynkLast = timeNow;
    blynkCalled = 0;
  }
}

Snippets are not going to help anyone to help you…

I think you are overthinking this stuff. I’d highly recommend using the SimpleTimer library to handle all your millis() stuff. If you do it manual you will get lots of headaches.

What are you trying to accomplish here? Can you make a little schematic of what your software is supposed to do? That way we can probably advise you better if we have a total idea of what it is going on :slight_smile: (I definitely are getting the hang of your program, but I like to see more :wink: )

point taken, here is the full loop :slight_smile:
it is a bit long

void loop() {

  Blynk.run();
  blynkUpdate1 ();
  blynkUpdate2 ();
  blynkUpdate3 ();

  temperatureFunction ();
  //TEMPERATURE SETTINGS
  temperature = message2;
  temperatureDHT = message;







  timeNow = millis() / 1000; // the number of milliseconds that have passed since boot
  seconds = timeNow - timeLast;//the number of seconds that have passed since the last time 60 seconds was reached.
  if (seconds >=60) {
    timeLast = timeNow;
    minutes = minutes + 1;
    minutesCounter = minutesCounter + 1;
  }
  //if one minute has passed, start counting milliseconds from zero again and add one minute to the clock.
  if (minutes >= 60) {
    minutes = 0;
    hours = hours + 1;
  }
  // if one hour has passed, start counting minutes from zero and add one hour to the clock
  if (hours == 24) {
    hours = 0;
    days = days + 1;
  }
  //if 24 hours have passed , add one day
  if (hours == (24 - startingHour) && correctedToday == 0) {
    delay(dailyErrorFast * 1000);
    seconds = seconds + dailyErrorBehind;
    correctedToday = 1;
  }
  //every time 24 hours have passed since the initial starting time and it has not been reset this day before, add milliseconds or delay the progran with some milliseconds.
  //Change these varialbes according to the error of your board.
  // The only way to find out how far off your boards internal clock is, is by uploading this sketch at exactly the same time as the real time, letting it run for a few days
  // and then determine how many seconds slow/fast your boards internal clock is on a daily average. (24 hours).
  if (hours == 24 - startingHour + 2) {
    correctedToday = 0;
  }


  // read the value of the switch
  // digitalRead() checks to see if there is voltage
  // on the pin or not
  switch1 = digitalRead(D8);
  if (switch1 == HIGH) {
    switchState = HIGH;
  }


  //Serial.print("Minutes: " );
  //Serial.println(minutesCounter );
  //Serial.print("Brewstage : " );
  //Serial.println(brewStage );
  //Serial.print("switchstate: " );
  //Serial.println(switchState );
  //Serial.print("switch1: " );
  //Serial.println(switch1 );
  //Serial.print("Temperature: " );
  //Serial.println(temperature );


  //BELOW THE VARIOUS BREW STAGES AND WHAT THE PROGRAM DOES during
  // 0 is standby, 1 is heating strike water, 2 is waiting for mash, 3 is mashing, 4 is extracting mash bag and waiting for boil,
  //5 is heating towards boil, 6 is boil, 7 is Ice cooling, 8 is pitching, 9 is primary ferment, 10 is secondary ferment, 11 is bottle

  switch (brewStage) {

    case 0:
      //What to do when Brewstage equals 0
      display.setTextSize(1);
      display.setTextColor(WHITE);
      display.setCursor(0, 0);
      display.println("  BREWERY ");
      display.println("   READY  ");
      display.setTextColor(BLACK, WHITE); // 'inverted' text
      display.println("   Hold   ");
      display.println("   Button ");
      display.println("  To START");
      display.display();
      delay(5);
      display.clearDisplay();

      LCD1 = "Hi there Brewer";
      LCD2 = "Brewery ready!";

      if (brewStage == 0 && switchState == HIGH) {
        brewStage = 1;
        terminal.println("PREPARING STRIKE WATER");

        terminal.flush();

        buzzer = HIGH;
        buzzer = LOW;
      }
      break;

    case 1:
      //What to do when Brewstage equals 1
      digitalWrite(heater1, HIGH);
      digitalWrite(heater2, HIGH);
      display.setTextSize(1);
      display.setTextColor(WHITE);
      display.setCursor(0, 0);
      display.println(" HEATING");
      display.println();
      display.setTextColor(BLACK, WHITE); // 'inverted' text
      display.println(minutesCounter);
      display.println(temperature);
      display.print(hours);
      display.print(":");
      display.print(minutes);
      display.print(":");
      display.println(seconds);
      display.display();
      delay(5);
      display.clearDisplay();

      LCD1 = "Heating the";
      LCD2 = "Strike water";

      digitalWrite(heater1, HIGH);
      digitalWrite(heater2, HIGH);

      if (brewStage == 1 && temperature > mashTemp) {

        digitalWrite(heater1, LOW);
        digitalWrite(heater2, LOW);
        switchState = 0;
        brewStage = 2;
        terminal.println("ADD MASH NOW");

        terminal.flush();
        buzzer = HIGH;
        buzzer = LOW;
      }
      break;

    case 2:
      //What to do when Brewstage equals 2
      display.setTextSize(1);
      display.setTextColor(WHITE);
      display.setCursor(0, 0);
      display.println("  STRIKE ");
      display.println("   READY  ");
      display.println();
      display.setTextColor(BLACK, WHITE); // 'inverted' text
      display.println("   Hold  ");
      display.println("   Button ");
      display.println("  To MASH ");
      display.display();
      delay(5);
      display.clearDisplay();

      LCD1 = "Strike ready";
      LCD2 = "Press the button";

      if ( temperature < mashTemp - 2) {
        digitalWrite(heater1, HIGH);
      }

      else if ( temperature > mashTemp + 2 ) {
        digitalWrite(heater1, LOW);
      }

      if (brewStage == 2 && switchState == HIGH) {
        brewStage = 3;
        terminal.println("THE MASH STARTS NOW");

        terminal.flush();
        minutesCounter = 0;
        buzzer = HIGH;
        buzzer = LOW;
      }
      break;
    case 3:
      //What to do when Brewstage equals 3


      if ( temperature < mashTemp - 1) {
        digitalWrite(heater1, HIGH);
      }

      else if ( temperature > mashTemp + 1 ) {
        digitalWrite(heater1, LOW);
      }

      display.setTextSize(1);
      display.setTextColor(WHITE);
      display.setCursor(0, 0);
      display.println(" MASHING");
      display.println();
      display.setTextColor(BLACK, WHITE); // 'inverted' text
      display.println(minutesCounter);
      display.println(temperature);
      display.print(hours);
      display.print(":");
      display.print(minutes);
      display.print(":");
      display.println(seconds);
      display.display();
      delay(5);
      display.clearDisplay();

      LCD1 = "Mashing grains";
      LCD2 = "Just be patient";

      if (minutesCounter > mashTime) {
        brewStage = 4;
        terminal.println("YOU NEED TO REMOVE THE MASH BAG");

        terminal.flush();
        minutesCounter = 0;
        buzzer = HIGH;
        buzzer = LOW;
        switchState = 0;

      }
      break;
    case 4:
      //What to do when Brewstage equals 4
      display.setTextSize(1);
      display.setTextColor(WHITE);
      display.setCursor(0, 0);
      display.println("REMOVE BAG");
      display.println(" MASH DONE");
      display.setTextColor(BLACK, WHITE); // 'inverted' text
      display.println("   Press  ");
      display.println("   Button ");
      display.println("  To Boil ");
      display.display();
      delay(5);
      display.clearDisplay();

      LCD1 = "Remove Bag";
      LCD2 = "press button";


      if (switchState == HIGH) {

        brewStage = 5;
        switchState = 0;
        terminal.println("HEATING BOIL WATER");

        terminal.flush();
        buzzer = HIGH;
        buzzer = LOW;
        switchState = 0;
      }
      break;
    case 5:
      //What to do when Brewstage equals 5
      digitalWrite(heater1, HIGH);
      digitalWrite(heater2, HIGH);

      display.setTextSize(1);
      display.setTextColor(WHITE);
      display.setCursor(0, 0);
      display.println("  HEATING");
      display.println();
      display.setTextColor(BLACK, WHITE); // 'inverted' text
      display.println(minutesCounter);
      display.println(temperature);
      display.print(hours);
      display.print(":");
      display.print(minutes);
      display.print(":");
      display.println(seconds);
      display.display();
      delay(5);
      display.clearDisplay();

      LCD1 = "HEATING ";
      LCD2 = "Boil water";

      if (temperature >= (boilTemp - 2)) {

        brewStage = 6;
        terminal.println("THE BOIL HAS STARTED");

        terminal.flush();
        minutesCounter = 0;
        buzzer = HIGH;
        buzzer = LOW;
      }
      break;
    case 6:
      //What to do when Brewstage equals 6
      digitalWrite(heater1, HIGH);
      digitalWrite(heater2, HIGH);

      display.setTextSize(1);
      display.setTextColor(WHITE);
      display.setCursor(0, 0);
      display.println(" BOILING");
      display.println();
      display.setTextColor(BLACK, WHITE); // 'inverted' text
      display.println(minutesCounter);
      display.println(temperature);
      display.print(hours);
      display.print(":");
      display.print(minutes);
      display.print(":");
      display.println(seconds);
      display.display();
      delay(5);
      display.clearDisplay();

      LCD1 = "Boiling";
      LCD2 = "The Wort";

      if (brewStage == 6 && minutesCounter > boilTime) {
        digitalWrite(heater1, LOW);
        digitalWrite(heater2, LOW);
        brewStage = 7;
        terminal.println("BOIL IS FINISHED, START COOLING!");
        terminal.flush();
        buzzer = HIGH;
        buzzer = LOW;
        minutesCounter = 0;
      }
      break;
    case 7:
      //What to do when Brewstage equals 7
      display.setTextSize(1);
      display.setTextColor(WHITE);
      display.setCursor(0, 0);
      display.println("  ADD ICE ");
      display.println();
      display.setTextColor(BLACK, WHITE); // 'inverted' text
      display.println(minutesCounter);
      display.println(temperature);
      display.print ("Add ");
      display.print(iceNeeded);
      display.println(" L ice");
      display.print(hours);
      display.print(":");
      display.print(minutes);
      display.print(":");
      display.println(seconds);
      display.display();
      delay(5);
      display.clearDisplay();

      LCD1 = "Boil finished";
      LCD2 = "ADD ICE";
      break;
    case 8:
      //What to do when Brewstage equals 8
      break;
    case 9:
      //What to do when Brewstage equals 9
      break;
    case 10:
      //What to do when Brewstage equals 10
      break;
    case 11:
      //What to do when Brewstage equals 11
      break;
    default:
      // if nothing else matches, do the default
      // default is optional
      break;
  }




}// end void loop

I made an autoamated brewery.
The code for that works completely.

Now I want Blynk to show me the values of my brewery on my phone.
2 temperature values and various time values.

These can be updated about once every 10 seconds so I can keep track remotely of what goes on and intervene manually if necessary

I did not use the timer library because I am allready keeping track of time in the brew process

Ok, getting there. I understand what you want and I think you are making it REALLY difficult for yourself. There is new widget called RTC (Real Time Clock) which deals with dates and times. I suggest you look into that. It will take loads of time off your hands so you can do important stuff (buy new hop, taste your beer and such, maybe even throw in a BBQ).

I haven’t used RTC widget myself, but I think it should work pretty fine.

-edit-

Please do use the timer, the way your are handling time is wonky at best … The timer is way more accurate and save you a lot of code, memory and speed :slight_smile:

you mean the simple time library right?

Yep, that is the one. On arduino.cc website are some neat examples, but I can hand you some stuff as well, if you like :slight_smile:

Allright thanks for the suggestion!
I am going to try on my own first, back in a jiffy to tell you wether the whole thing exploded or not ^^

Excellent, I’ll drink to that :wink:

Cheers!

Are you a brewer too, or just a super pro advanced mega Blynk conaisseur?

Quick question: does the update freq u set in the blynk widgets in the app have an influence on your server requests even though your arduino code only updates them in a longer interval?
I set all of them to 10 seconds now and have not had a disconnect since. Couldbe coincidence of course, as my code is still wonky :stuck_out_tongue:

Ok, well as you may have noted from reading all the other posts on here about random disconnects, there should be two things in your loop: Blynk.run() & timer.run()

Everything else gets kicked off by simpletimer…

Perhaps try this also?

1 Like

No, I’m not a brewer (yet …) but according to my wife I have to many hobbies, so for now I’ll leave it at trains. I do like beer though, lol.

I’m not sure about the update frequencies, but usually Blynk can handle a lot of requests because of it’s very small footprint. I tend to use pushdata only so the only thing which will initiate actions come from my hardware, not the app side. I just give a command and something happens.

Except for my graphs, but that is about it.

he’s pretty amazing, you should listen to him more than a code-hacker like me…

and also - this might help: i was having lots of troubles until i set my changed my update freq. in the app to PUSH…

see here: [quote=“Costas, post:20, topic:4545”]
Use PUSH for the data frequencies as Blynk knows best.
[/quote]

2 Likes

So hey all, thanks for your input!

So It has bee na few days. A bit busy but had some time for tinkering on the brewery.

What I have found out:(like @Lichtsignaal suggested and @Dave1829 just confirmed, I set the refresh rate in my android app to really slow on all widgets. I used the same code I had here above but just changed the intervals for server requests to 5 seconds, 10 seconds, 15 seconds (3, 5 and 9 before). I have not had a single disconnect since. I suspect the issue was mostly on the app side, not on my code but the longer interval prob didnt hurt either.

My wonky code is also keeping track of time very efficiently so far with only a one second deviation every few hours. (Which is fine for my purposes as a brew only takes 3 ish hours)

The simple timer library just seems like a fancy way of using the millis() function like I did in a librairy. But I will give it a go first in my next project as @Lichtsignaal said it is more efficient.

So to anyone having connection issues i recommend this based on my findings and on the input of the amazing guys above here:

1. In your app set everything to slow or push
2. Use the slowest server request interval feasible for your project in your code and seperate the urgent sensor readings and messages from the ones you only need every minute or so (like temperature or time for a brewery)

This thread can be labeled solved for me :wink:

Thanks again @Lichtsignaal @Dave1829

2 Likes

You are very welcome, looking forward to Blynk-Beer :wink:

Holy shit, that would not be a bad name for like the trashy blonde lager version

-“BLYNK BEER” -

EDIT: You think the guys at Blynk will let me steal their logo?

1 Like

For beer? Sure! :joy:

2 Likes