Setting variable value in Blynk which then ESP8266 based WeMos D1 will pull

Hi guys first post here so forgive me if a lot of this is simple. Essentially, I am trying to find a elegant way for me to wirelessly set variables in the beginning of my sketch.

I have seen in blynk you can control pins, and read in data from pins. Is there a way to store a value and set it to a variable, not directly push that value to a pin.

Instead of having to update my code and recompile every time I change a parameter, I want to write into the code to “go to blynk and check the value of this variable.” That way I can just set the parameters with Blynk and hit go on the WeMos D1 board I am using.

Example:
Currently I have varaible:
Hopadd = [0, 10, 20];
Hopinfo = [“2oz Saaz”. “1oz coloumbus”, “1oz cascade”];

I would like to set Hopadd and Hopinfo in the blynk app and then
Some sort of code that says:
read: Hopadd
read Hopinfo

…then my sketch

Using WiFimanager library to connect to WiFi as well.

Any help is greatly appreciated!

@philmurp Menu widget or Terminal widget or WiFI Manager.

Looks like Terminal Widget could work.
I have a question: What would the code below look like since I am asking for more than one variable? Should I input multiple terminal widgets to multiple virtual pins, or can I put it into a loop of some sort?

I saw the code I believe you suggested to someone else:

BLYNK_WRITE(V1) // Terminal Widget
{
String stringTerminal = param.asStr(); // get data entry from Terminal
float floatTerminal = stringTerminal.toFloat();
floatTerminal = floatTerminal + 12.3; // add a float to the ‘number’ entered into Terminal
char buffer[10];
stringTerminal = dtostrf(floatTerminal,5, 2, buffer); // width and precision

if(floatTerminal > 20)
{
terminal.println(“Sum is over 20”); // some random text
terminal.flush();
}

else
{
terminal.println(stringTerminal); // show in Terminal the result of the float entered into Terminal plus 12.3
terminal.flush();
}
}

How many variables?

Can the variables have any value or just a selection of values?

Its for a brewing system, and yes every variable can be anything:

MASHTEMP: one number
MASHTIME: one number
BOILTIME: one number
HOPADD: [Array with the timing of several different hop adds, this array size can change]
HOPINFO: (Array of STRINGs corresponding to the timing of the hop adds above)

I was thinking i could just repeat the request for more information and set each one at a time, but I wonder if there is a more elegant solution such as an if statement etc

The Terminal will need to have an if statement.

I guess you might be able to get away with just one Terminal.

You could prefix the string you post to Terminal A = MASHTEMP, B = MASHTIME etc and then if, if else, else statements will use Arduino “startsWith” function.

Are you manually feeding the data into Terminal and if so can’t this be automated somewhere in the brewing system?

So these are the so called “Brewing Parameters” that change for everytime I brew. So I would like to input these variables, rather than coding them into the sketch and have to recompile the sketch everytime as I update the set variables. Maybe I am wrong and that would be easier, but I think this would work if I can get the terminal to work.

As I am reading about toFloat() function, it seems that any string after numbers are dropped off. So could I say
I input my values into terminal as such: 60MASHTIME …etc for all variable I end it with the variable it is then the code is:

for x=1 to 5

String stringTerminal = param.asStr();
if stringTerminal = string.endsWith(MASHTEMP)
float MASHTEMP=stringTerminal.toFloat()
if stringTerminal = string.endsWith(MASHTIME)
float MASHTIME=stringTerminal.toFloat()
if stringTerminal = string.endsWith(BOILTIME)
float BOILTIME=stringTerminal.toFloat()
if stringTerminal = string.endsWith(HOPADDS)
float HOPADDS=stringTerminal.toFloat()
else
const char* HOPADDSINFO=stringTerminal

The for statement ensures that a terminal response is requested 5 times until all variables have been input. Would this code allow me to be prompted 5 times, ie what line of code is the one that prompts me to then input something into the terminal widget?

If you were to use the if statements it would be == not =.

I don’t think you need a for loop as the Terminal is always waiting for your input.

From our use of the Terminal we would have something like this:

BLYNK_WRITE(V0)  // Terminal
{
  String fromTerminal = param.asStr();
  if (fromTerminal.endsWith("MASHTEMP") ){
    float MASHTEMP = fromTerminal.toFloat(); 
  }
  else if (fromTerminal.endsWith("MASHTIME") ) {
    float MASHTIME = fromTerminal.toFloat();
  }
  // more "else if" statements
  else {
    // etc
  }
}
1 Like

I see, so when the board is connected then terminal is active and thus everytime I input a value it will run these if statements. My concern is when this is at the top of a lot of other code, how do I get it to hang and wait for me to input all the necessary information before going ahead and executing every other code.

I suppose I could put a counter in all the if statements as well and then have a large if statements saying only proceed if counter = 5 etc

i use buttons to adjust my variables:

this is how i do it for “target” temp for my HVAC:

BLYNK_WRITE(V14)  // target temp down
{
  tempDown = param.asInt();
  if (tempDown == 1) {
    tempDownFunc();
  }
}

BLYNK_WRITE(V15) // target temp up
{
  tempUp = param.asInt();
  if (tempUp == 1) {
    tempUpFunc();
  }
}
void tempDownFunc()
{
  targetTemp = targetTemp - 1;
  if (debug == 1) {
    Serial.println(F("System = tempDown button pressed"));
    Serial.println(targetTemp);
    Serial.println(F("------------"));
  }
  Blynk.virtualWrite(V1, targetTemp);
  ventLogic();
}
void tempUpFunc()
{
  targetTemp = targetTemp + 1;
  if (debug == 1) {
    Serial.println(F("System = tempUp button pressed"));
    Serial.println(targetTemp);
    Serial.println(F("------------"));
  }
  Blynk.virtualWrite(V1, targetTemp);
  ventLogic();
}

HTH :slight_smile:

(disclaimer; i am not a coder - i just hack at it til it works!)

Yes all Blynk widgets are active once you are connected.
Blynk sketches are different to regular Arduino sketches.

The Blynk libraries check all your Widgets on an ongoing basis to see if you have pushed a button or sent a command through Terminal etc.

So with SimpleTimer all your other stuff has to check the status from the Widgets and then execute your regular code.

@Dave1829 we have VERY similar code to yours for our buttons (and sliders). Not sure why I didn’t suggest similar when the OP asked how it might be done. Maybe because I have been working on free apps i.e. 2000 energy units or less. Menu and Terminal Widgets cost a lot less than a dozen buttons and take up less space.

IIRC - it was YOU who actually taught it to me :wink:

not sure how it will help OP exactly with his ‘hopinfo’ parameter?

Thanks for the help guys, using Blynk is really nice and saves a lot of hardware. That code is nice, I will keep that in mind for future use or if I cant properly debug any issues I get with terminal.

The HOPADDS and HOPADDSINFO variables are the only two that have me worried since they are arrays.

I am wondering if for HOPADDS, the tofloat() function will work properly for an array when I input something like:
[10 , 60 , 90]HOPADDS
is it going to properly convert that to a float array of [10 , 60 , 90] ?

This is a really dumb question but these if statements etc go into the

void loop
Blynk.run(

If statements…

)

or are the if statements in the setup area

NOTHING goes in the loop other than Blynk.run(); and timer.run();
If you put anything in the loop your sketch will run very badly with Blynk.

The if statements are in BLYNK_WRITE(Vsomenumber) functions as shown in the examples provided by Dave and I.

Best place for these functions, BEFORE setup();

ok makes sense still trying to figure out how Blynk integrates with the rest of the sketch. My prior to void(setup) code look as follows: I am getting all sorts of errors when verifying due to variables not being declared; any ideas on how I can fix this?

//////////////////////USER INPUTS from Blynk Terminal
BLYNK_WRITE(V0) // Terminal
{
String fromTerminal = param.asStr();
if (fromTerminal.endsWith(“MASHTEMP”) ){
float MASHTEMP = fromTerminal.toFloat();
}
else if (fromTerminal.endsWith(“MASHTIME”) ) {
const int MASHTIME = fromTerminal.toFloat();
}
else if (fromTerminal.endsWith(“BOILTIME”) ) {
const int BOILTIME = fromTerminal.toFloat();
}
else if (fromTerminal.endsWith(“CHILLTIME”) ) {
const int CHILLTIME = fromTerminal.toFloat();
}
else if (fromTerminal.endsWith(“HOPADDS”) ) {
const int HOPADDS = fromTerminal.toFloat();
}

else {
const char* HAPADDSINFO = fromTerminal;
}
}

////////Setting up variables to be declared by Blynk app
const int MASHTEMP; // Desired Mash Temp(deg F)
const int MASHTIME; //Desired Mash Time(milli sec.)
const int BOILTIME; //Desired Boil Time(milli sec.)
const int CHILLTIME; //Desired time to pump all wort through the wort chiller (milli sec.)
//HOP ADDITIONS, HOPADDS and HOPADDSINFO must be the same array length
int HOPADDS []; //Hop times
const char* HOPADDSINFO []//Hop adds contents
float HopAddtime; //Creating variable for for loop below
int h; //Declare the for loop variable

Edit your last post, highlight the sketch and press the </> button but perhaps add back in the include statements first.

Surely the variables are not const?

Within each run all are constant but then between brew days they will be changed. So they need to be set one time at the beginning and then they are constants throughout the rest of the code

Ok, I see.

The only compilation error I get is with const char* HAPADDSINFO = fromTerminal;
All the rest is fine but this is with a blank sketch as I don’t know what else you have in your sketch.