Can we send values from app to hardware?

I built an 8 head dosing pump that I wish to control with Blynk and my Arduino. I tried researching this topic a few weeks back, and made a post that suggested I look into Blynk.syncVirtual, but I am unable to put together much and additional research also turned up little on the matter.

I wish to send dosing volumes in milliliters from my app to a variable stored in the Arduino. If possible, it would be better if I can add a decimal (eg. 31.6 ml). Upon receipt of this number, the Arduino can make the needed calculations and execute the instruction. I am still working on what to do when this number is received, but I hope to also begin working out the Blynk side kwirks as well.

TYIA

Considering a new approach, perhaps BLYNK_READ() is what I need? I would still need a widget that is able to adjust numbers or values though. Any feedback, leads, research tips, anything that gets me closer to this goal is most appreciated!

You could try Terminal, then convert the string to number in the sketch.

http://docs.blynk.cc/#widgets-displays-terminal

https://www.arduino.cc/en/Reference/StringToFloat

2 Likes

actually, i do not relly understand what is your problem?
can’t you already send values with slider or step? if you need floats, you can send bigger numbers, and divide them by 10 in the hardware side. or i’m missing something?

1 Like

No wanek, I think you’re spot on! I am somewhat unfamiliar with the widgets and am also still getting used to coding in general. I played around with the Step widget as you suggest, and it gives the resolution I seek. I am also looking into Gunner’s suggestion of using StringtoFloat() on the receiving side. Thanks for the tips!

Edit - Can I write code to handle Button widgets like physical buttons? Because this will ultimately control 8 different pumps, I would like to be able to adjust the output for each pump prior to the motor engaging. So for each motor, I would have a Step widget and also a Button widget. Or can this be accomplished in the app? As previously stated, I am still kinda new and building my project is my motivation to learn, but there is just simply TOO much for an old mind to learn and comprehend.

You can control physical pins with virtual buttons if that is what you are actually wanting.

This function is called whenever you send data to hardware from phone, in a project. I.e. A button press.
Place a button widget in your project, select a virtual pin, select desired button mode and add code below with corresponding virtual pin as selected in the app. V0 is just an example.

So when you press your button in the phone this function is called, it gets the parameter sent (0 button off 1 button on)
and writes this to a physical pin on your board. Hope this clears some questions. Also every widget that can send values basically works like this.

BLYNK_WRITE(V0) 
{
    int state = param.asInt();
    digitalWrite(physicalPin, state);
} 

Just to note the button different behaviour: It can be set as a switch and a pushbutton. Both are useful, but you need to know the difference. On the very beginning I messed with those.

i’m not sure what you want exactely to do.

if you have to set all the 8 dosage values first, and start the motor just after that, you can do this with 8 different sliders widget, sending values to variables or an array, then have one button widget (set to switch or pushbutton, depending on your preference) which state you will read in hardware, and if reads 1 (true), you can invoke a function to start the pump.

1 Like

I don’t need all 8 values prior to the first motor engaging, but I do need to adjust the step/slider first. So essentially, when the button/switch is ticked in the app, then the motor can execute according to the value of the step/slider. That said, I suppose I can choose to wait till I have all 8 values set, and just engage the pumps at will with the tick of a button/switch. I’m near certain the replies above give me what I seek, I just need to better understand the sequence of events and code it accordingly. Simple tests are easy for me, but the longer they get, the harder it is to see right away. I’ll get through it! Thanks all for the feedback and support!

I hope someone can assist me with the code. I spent many hours over the last 4 days trying to arrange it so it compiles, but my lack of coding skills won’t let me proceed. I have the app set up perfect with the step widgets and buttons, but the code is holding me back.


#include <SPI.h>                       //Used by Blynk
#include <Ethernet.h>                  //Used by Blynk
#include <BlynkSimpleEthernet.h>       //Used by Blynk
#include <SimpleTimer.h>               //Used to run functions at preset intervals
#define BLYNK_PRINT Serial
char auth[] = " ";                     //Paste code that app emailed you between "quotes" (no spaces)

#define pump 2                         //Pump connected @ D2
float ml;                              //Step Widget to determine amount of ml/Output 0-80/Step 0.2
uint32_t startCount;                   //Millis reference used to calculate stopCount
uint32_t stopCount;                    //
uint32_t multiplier = 858;             //Number of milliseconds for motor to produce 1ml of liquid

SimpleTimer timer;                     //SimpleTimer instance named timer

void setup()
{
  Serial.begin(9600);
  Blynk.begin(auth);
  while (Blynk.connect() == false) {}
  timer.setInterval(5000L, checkPump); //checkPump function is tested every 5 seconds
}

void loop()
{
  Blynk.run();
  timer.run();
}

int checkPump()
{
  BLYNK_READ(V1);                         //Read Button/Switch state
  if ((V1) == 1)
  {
    ml = BLYNK_READ(V0) * multiplier;     //Read Step Widget Value x multiplier
    startCount = millis();                //Reference the Millis count
    stopCount = ml - startCount;
  }
  if (stopCount <= millis())
  {
    analogWrite(pump, 255);
    BLYNK_WRITE(V1) = 0;
  }
  }
  else
  {
    analogWrite(pump, 0);
  }
}

Edit - Once I have code to run a single pump, I can duplicate the code to run the others respectively.

Hello. You are doing reading of V1 in wrong way. Here is more details on how you should do that - http://docs.blynk.cc/#blynk-main-operations-state-syncing

Thank you Dmitriy, I researched, but couldn’t wrap my brain around the sync yet, but I’ve used Blynk.virtualWrite in a different sketch in a similar way so I tried it and it worked, though it still seems I have trouble in the remainder of the sketch. I verified the virtualWrite works because when I toggle switch in app, it returns to off a few seconds later in accordance with the timer. My problem now is that after the MC boots and runs for a short length of time, the pin goes HIGH and remains so indefinitely. I’ll keep trying new ideas, plus post this question to the Arduino forum in the likely event that the problem is not Blynk related.


#include <SPI.h>                         //Used by Blynk
#include <Ethernet.h>                    //Used by Blynk
#include <BlynkSimpleEthernet.h>         //Used by Blynk
#include <SimpleTimer.h>                 //Used to run functions at preset intervals
#define BLYNK_PRINT Serial
char auth[] = "PasteAuthCodeHere";       //Paste code that app emailed you between "quotes"

#define pump 3                           //Pump connected @ D2
float ml;                                //Step Widget to determine amount of ml/Output 0-80/Step 0.2                              //
uint32_t startCount;                     //Millis reference used to calculate stopCount
uint32_t stopCount;                      //
uint32_t multiplier = 858;               //Number of milliseconds for motor to produce 1ml of liquid

SimpleTimer timer;                       //SimpleTimer instance named timer

void setup()
{
  Serial.begin(9600);
  Blynk.begin(auth);
  while (Blynk.connect() == false) {}
  timer.setInterval(2000L, checkPump);   //checkPump function is tested every 2 seconds
  pinMode(pump, OUTPUT);
  digitalWrite(pump, LOW);
  Blynk.virtualWrite(V1, 0);
}

void loop()
{
  Blynk.run();
  timer.run();
}

void checkPump()
{
  BLYNK_READ(V1);                         //Read Button/Switch state
  if ((V1) == 1)                          //If Switch is HIGH, execute following code
  {
    BLYNK_READ(V0);                       //Read Step Widget Value x multiplier
    {
      ml = (V0) * multiplier;             //Step Widget Value times Calibration Multiplier
      startCount = millis();              //Reference the Millis count
      stopCount = ml + startCount;
    }
    if (stopCount <= millis())            //If V0 x multi + start is less than or equal to total millis
    {
      digitalWrite(pump, HIGH);
      Blynk.virtualWrite(V1, 0);          //Reset Switch back to OFF position
    }
    else
    {
      //ml = 0;
      //startCount = 0;
      //stopCount = 0;
      digitalWrite(pump, LOW);
    }
  }

}