Can a button change a variable in a function()

thank you!

i have seen some many programs that looks much more refined & elegant than mine - so appreciate any ways to make it faster/easier to write/change/read is great…

and would these be the “nested” IF statements i have been reading about?

i will give it a go. :smile:

Continuing the discussion from Can a button change a variable in a function():

@Dave1829 I have a similar project to you and up until a few days ago two or three buttons linked to virtual pins were handled in the following way:

BLYNK_WRITE(V0)   // manual control
{
  int ManualMode = param.asInt();
  if (ManualMode == 1) {
     Blynk.virtualWrite(V1, 0);  // timer control button off
     Blynk.virtualWrite(V2, 0);  // thermostatic control button off
     ManualFunction();
  }
}

BLYNK_WRITE(V1)   // timer control
{
  int TimerMode = param.asInt();
  if (TimerMode == 1) {
     Blynk.virtualWrite(V0, 0);  // manual control button off
     Blynk.virtualWrite(V2, 0);  // thermostatic control button off
     TimerFunction();
  }
}

BLYNK_WRITE(V2)   // thermostatic control
{
  int ThermoMode = param.asInt();
  if (ThermoMode == 1) {
     Blynk.virtualWrite(V0, 0);  // manual control button off
     Blynk.virtualWrite(V1, 0);  // timer control button off
     ThermoFunction();
  }
}

So as you press one button it turns off the other buttons in the app and it works fine. Also up until yesterday I had my system start up in a known state of off but decided I wanted to use syncAll instead.

syncAll works well but in circumstances like ours (several buttons controlling the same device) you have to include code to check all 3 buttons in the sketch above to determine if the device should be on or off after syncing. My different modes have various variables that need to be reinstated when syncing as they are shown in the virtual LCD. So far I have added a couple of Value Displays to hold these variables as they are reinstated when syncing e.g. in timer mode the number of minutes remaining before the device is turned off.

@Pavlo I am struggling to recover the Value Display variable to my sketch variable on sync. With Value Display on V3 and set with a reading frequency of 5 seconds in the app I have tried the following but it isn’t passing the data to the variable:

BLYNK_WRITE(V3)   // Value Display holding CountdownTime variable
{
    CountdownTime = param.asInt();
}

Could you please indicate the syntax I require for reading the Value Display contents into a variable.

1 Like

thank you Costas, this should be OK for the time being :smile:

I have not yet considered syncing - but i probably will have to once i get something running and want to take it further…

are you doing HVAC system? do you have a link to a description?

Never quite sure what HVAC means when I see it Dave::grinning:

Our system is ‘the same’ as Google’s Nest and British Gas’ Hive but without their multi million (billion) dollar budget :grinning:

Central heating control via the internet (Blynk) using off the shelf RF controlled boiler switches. Our Wemos with DS18B20 and RF transmitter send the required RF signals for on and off based on room temperature, time of the day or specified amount of time etc.

Ideally it is a basic burglar alarm too as we have PIR attached to the Wemos but at the moment the WiFi signal is triggering the PIR.

eek, i never knew it could be misconstrued!?

i am in AU and we don’t have boilers, our temperatures are enough to boil things however…

i am using large inline fans to circulate cooler/warmer air around & through our house, and this improves the moisture issue too (we live on a cliff, which is a watercourse at times)…

i have DHT22’s reading the temps and various dampers opening and closing depending on which air is available at the right temps… (i am going to move to using BME280’s as a few of my DHT22’s are not coping with our extremes it seems…)

this Blynk interface is a dream to use and beats the crap out of the clunky HTML web pages i was trying to write to control my HVAC with!

1 Like

We are in the Eastern Med so most people don’t have Central heating here either and hot water is generally from solar panels. But as a Brit I am used to bleak winters from my days back in the UK :grinning:

@Pavlo I guess because Value Display is not a Controller widget (it is a display widget) that is why the following doesn’t work:

BLYNK_WRITE(V3)   // Value Display holding CountdownTime variable
{
    CountdownTime = param.asInt();
}

As Blynk knows the content of Value Display when syncing is there anyway for me to have access to the content?

I’m not sure I understood your question, sorry. Please explain more

I think he means to access the value displayed on the display widget :slight_smile:

Exactly @Lichtsignaal

Sync has the information but I want to pass the contents of the display widget to a variable in my sketch i.e. when the variable has been lost because the Wemos has crashed etc and it syncs with the Blynk server.

What you are asking now, I think, is the secret of the chef :wink:

Since you probably dictate what will be on the value widget, I think it’s easier to keep track of that instead of getting a stored value from “somewhere”, wether it be the server or the app.

Ow btw, @Costas you should definitely join the chat room on Slack :wink:

I could use EEPROM but that would blow it in a few days with the write limits. With sync this requirement will be needed by MANY users. Why bother syncing if you don’t have all the info?

Not a chat type of guy, busy coding :smile:

Hm… This is good point. I think I just missed that part during implementation of sync on server side. Don’t see any reasons why it shouldn’t be implemented.

I created corresponding ticket - https://github.com/blynkkk/blynk-server/issues/109

Thanks @Dmytro look forward to making use of the feature.

Touchwood our PIR implementation seems to function pretty much as expected now. Requires further testing though.

So Nest / Hive with basic intruder alarm.

Firmware updates to the Central Heating (ch) system are available by pressing a Blynk button, opening a web browser at http://ch.local/update and flashng the latest bin via OTA. Currently the system requires users to receive the bin file by email but we have started coding automatic firmware updates which will be sent from one of our servers. OTA seems a bit flakey on our Ubuntu test system but flies with Windows (much faster and more convenient than a USB cable). Anyone else using OTA with Ubuntu (14.04)?

Need to see if we can get Serial Monitor equivalent with OTA for debugging purposes.

All integrated with @tzapulica’s WiFiManager so we can access any router with any Blynk token without changing the sketch. WiFiManager moved on to version 10 a few hours ago and it just gets better and better.

@Costas thanks for the kind words. i need to do some ota examples as well somepoint soon, do you have your sketch public anywhere?

also funny you should mention OTA debugging, if you have a spare esp8266 that you can dedicate to being a debugging terminal here you go https://github.com/tzapu/WebSocketSerialMonitor

thanks!

and i’ve tried, but its not responding to the button presses in the app - do i need to make the ‘cancelAll, roofOn and houseOn’ separate functions? or have i just made a basic error?

int setMaxTemp;
int setHouseOn;
int setRoofOn;
int setCancelAll;
int setAuto;

BLYNK_WRITE(V30)
{
  BLYNK_LOG("Got new setMaxTemp value: %i", setMaxTemp = param.asInt());
ventLogic();
}

BLYNK_WRITE(V21)   // CANCEL mode
{
  int setCancelAll = param.asInt();
  if (setCancelAll == 1)
  {
    Blynk.virtualWrite(V22, 0);  // AUTO button off
    Blynk.virtualWrite(V31, 0);  // HOUSE button off
    Blynk.virtualWrite(V23, 0);  // ROOF button off
    ventLogic();
  }
}
BLYNK_WRITE(V23)   // ROOF mode
{
  int setRoofOn = param.asInt();
  if (setRoofOn == 1)
  {
    Blynk.virtualWrite(V22, 0);  // AUTO button off
    Blynk.virtualWrite(V31, 0);  // HOUSE button off
    Blynk.virtualWrite(V21, 0);  // CANCEL button off
ventLogic();
  }
}

BLYNK_WRITE(V22)   // AUTO mode
{
  int setAuto = param.asInt();
  if (setAuto == 1)
  {
    Blynk.virtualWrite(V31, 0);  // HOUSE button off
    Blynk.virtualWrite(V21, 0);  // CANCEL button off
    Blynk.virtualWrite(V23, 0);  // ROOF button off
   ventLogic();
  }
}
BLYNK_WRITE(V31)   // HOUSE mode
{
  int setHouseOn = param.asInt();
  if (setHouseOn == 1)
  {
    Blynk.virtualWrite(V21, 0);  // CANCEL button off
    Blynk.virtualWrite(V22, 0);  // AUTO button off
    Blynk.virtualWrite(V23, 0);  // ROOF button off
   ventLogic();
  }
}

void setup() {
  // put your setup code here, to run once:
blynk.begin()
}

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

void ventLogic()
{
  Serial.println(F("Starting the ventilation logic section. "));

  uint32_t start = micros(); //start timer to complete logic section

  Serial.print(F("_________________________SetMaxTemp (max. inlet temp) = "));
  Serial.print(setMaxTemp);
  Serial.println(F("'C"));


//if CANCEL is selected:

if (setCancelAll == 1)
{
  //do stuff
}

//if ROOF ON is selected:

else if (setRoofOn == 1)
{
  //do stuff
}

//if HOUSE ON is selected:

else if (setHouseOn == 1)
{
  //do stuff
}

else if (setAuto == 1)
{
  //roof vent air has lowest dew point scenario:---------

  if ((roofTemp <= setMaxTemp) && (mHdp > mRdp) && (mBdp > mRdp))
  {
    //do stuff
  }

  //house vent air has lowest dew point scenario:

  else if ((houseTemp <= setMaxTemp) && (mRdp > mHdp) && (mBdp > mHdp))
  {
    //do stuff
  }

  //basement room air has lowest dew point scenario:

  else if ((mRdp > mBdp) && (mHdp > mBdp))
  {
    //do stuff
  }
}

  uint32_t stop = micros();

  Serial.print(F("Just finished the ventilation logic section, which took "));
  Serial.print(stop - start);
  Serial.println(F(" micro seconds."));
  Serial.println(F("------------"));

}

@tzapulica the OTA we use is just a quick mod to the standard WebUpdater example (in ESP8266HTTPUpdateServer folder). We remove part of the standard sketch as we are already connected to the router and Blynk etc. Something like this:

// OTA sketch extract, just add your Blynk stuff

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <ESP8266HTTPUpdateServer.h>

const char* host = "ch";  // short host name for Central Heating, becomes http://ch.local/update

// Blynk stuff

ESP8266WebServer httpServer(80);
ESP8266HTTPUpdateServer httpUpdater;

void setup(void){

// regular setup stuff

}

BLYNK_WRITE(V4)   // bring up the firmware update facility from virtual pin 4
{

  intfwUpdate = param.asInt();
  if (fwUpdate == 1) {
    byte keeplooping; // 
    int timebuttonpressed = millis() / 60000;  // time when V4 button was pressed
    //WiFi.mode(WIFI_AP_STA);  // not required
    MDNS.begin(host);
    httpUpdater.setup(&httpServer);
    httpServer.begin();
    MDNS.addService("http", "tcp", 80);
    Serial.printf("Server ready, now open http://%s.local/update in your browser\n", host);

    do
      {
        httpServer.handleClient();
        int timepassed = millis() / 60000;
        if (timepassed >= (timebuttonpressed + 5))  // 5 minutes before timeout
        {
          Blynk.connect();   // reconnect to Blynk
          delay(3000);       // allow time to reconnect to Blynk
          lcd.print(0, 0, "F/W unchanged   ");
          lcd.print(0, 1, "Upgrade later   ");
          delay(1000);
          Serial.println("Timeout, NOT resetting");
          break;
        }
      } while (keeplooping; == 0);   // just to keep looping until timeout or firmware updated
    }
}
void loop(void){

// Blynk stuff

}

I will take a look at your OTA Serial Monitor asap.

@Dave1829 the basic error might be defining the global variables and then redefining them as local variables.

The Vpin can be int, float and String (I believe) but you need them as global variables to use in the subsequent functions so for example int setCancelAll = param.asInt(); becomes setCancelAll = param.asInt();

1 Like

ahh, damn, yes, thanks, that’s probably it - i have been guilty of redefining global variables locally in the past :pensive: