BLYNK
BLYNK.IO       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

Display only one sensor (of several) attached to one device

I have four ultrasonic sensors attached to one device (nodemcu). I have setup so I have four gauges displayed on the screen. What I want to do is be able to choose only ONE (at a time) to display. Sort of what device selector does if you have the sensors on separate devices. BG on this. The display if for the levels on four water tanks. I only want to monitor the one that is currently in use. Any suggestions?

Blynk Legacy or Blynk IoT ?

Pete.

Assuming your are using Blynk IoT …
And a free plan with a limited set of Widgets.

I would try this (I hope possible):
In GUI:

  • Place 1 Gauge widget assigned to Vn
  • Place 4 Button widgets. Set to Switch function.

In code:

  • When a button is pressed - let code “reset” all other buttons, so any earlier pressed is released (for visibility reasons)
  • Assign the data you want to display for this button to the Vn Pin.

I.e. you just assign the chosen data in your nodemcu to the Gauge pin.
This would be a way to “emulate” a device selector. OK, more of a data stream selector, but same principle.

I have not tested this myself. But this is the way I should try to do it.

/Jonas

2 Likes

HI Pete:
In either. Which ever is easier.
Thanks
Jeff

Thanks Jonas. I’ll give it a go and let you know how it works. If it does, it will solve my problem nicely.
Jeff

I’ve been mulling the best way to go about this. I’m wondering if I can accommodate all four buttons in one loop statement (if/then/else) or separate loop statements for each button ie loop button1()?
Also thinking of putting an LED above the button for each tank so I can tell which tank is active.
Jeff

Are you using virtual pins for your button widgets?
If so, then what exactly do you mean by…

Do you mean a physical LED, or an LED widget?
If the latter, then you’ll know which is active by the status of the switch.

I think it would be better if you were MUCH clearer about exactly what it is you are trying to achieve.

Pete.

Okay Pete.
I have four tanks with ultrasonic sensors to measure the water depth. They are connected to one device. At present I have it set up to see all four tanks on the screen.We only use one tank at a time, so only want to see the active tank on the Blynk screen. So I need a way to switch between tanks.
I’ve been toying with the idea that Jonas mentioned above. I would have four virtual buttons, four virtual LEDs and one Virtual Gauge. (nine widgets).
By selecting a button, the value associated with that button would be displayed on the virtual gauge, and the virtual LED would light up to indicate that particular tank is active.

The program would have four void loop functions - one for each of the buttons & LED’s If the value was High, for the button then it would print the data (depth) for that particular tank. If the value was low it would not or revert to zero (still have to figure that part out).

End result. Press a virtual button on the screen for a particular tank, the value is displayed and a virtual LED lights.
Does that explain in more clearly?
Jeff

Well, the LEDs are redundant, because the active button widget would indicate which tank was in use. As @jonas said….

You didn’t answer my question about whether you’d be attaching your switch widgets to virtual or digital pins, but from your description I guess you’re thinking of digital pins…

This is a bad approach, and limits you to Blynk Legacy, as Blynk IoT doesn’t support physical pins yet. The logic is much simpler when using virtual pins and removes the need to constantly poll the associated GPIOs looking for a change of state.

If you did use digital pins then you certainly shouldn’t be cluttering-up your void loop in the way that you described…

Pete.

Thanks, Pete, I’ll try and work out a very basic program using virtual pins and post it for comments.
Jeff

I’ve spent a few hours today trying to come up with a sketch that will accomplish the multi-sensor single display. I’ve tried to keep this initial sketch very simple - just to see if it works. I’m only using 2 tanks (will upscale it to 4 later). Instead of cluttering things up with calculating the distance, I’ve assigned numeric values for two distances to see if the program will send the data correctly. If it works then I’ll add the rest of the program that receives the data from the transmitter.
I’ve run out of time, so I won’t be able to test it until tomorrow. If anyone sees any glaring errors let me know otherwise I’ll report on my first effort tomorrow.
Again, thanks for the help.
Jeff

//gauge is V0
//button 1 is V2
//button 3 is v3
 
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
 
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "--your-ssid--";
char pass[] = "--your-wifi-password--";
 
int tank1 =23;// arbitrary number as place holder for data sent from transmitter - just for testing now
int tank2 =55;// arbitrary number as place holder for data sent from transmitter - just for testing now
 
void setup() {
  WiFi.begin(ssid, pass);
}
 
void loop() {
  Blynk.run();
}
 
BLYNK_WRITE(V2) { // Button for Tank1 on V2
 
  if (param.asInt() == 1) {  // If button state is 1 (HIGH) then...
   Blynk.virtualWrite(V0, tank1);  // To Display Widget
  } else {  // If button state is 0 (LOW) then...
       Blynk.virtualWrite(V0, "N/A");  // Reset Display 
      }
}
 
BLYNK_WRITE(V3) { // Button for Tank2 on V3
 
  if (param.asInt() == 1) {  // If button state is 1 (HIGH) then...
   Blynk.virtualWrite(V1, tank2);  // To Display Widget
  } else {  // If button state is 0 (LOW) then...
       Blynk.virtualWrite(V1, "N/A");  // Reset Display 
      }
}

Your sketch is not gonna work.

first, you didn’t define the pins you’re using, there’s no sensors calibration, etc…

second, your void setup isn’t complete.

third, you will need a timer.

Well, first of all you aren’t turning off the other virtual button widgets.

Secondly, this:

will send the value of tank1 to the gauge widget just once, when the button is pressed. Is this really what you want?

int active_tank; // used to tell another part of the code which tank to read

BLYNK_WRITE(V2) { // Button for Tank1 on V2
  if (param.asInt()
  {
   Blynk.virtualWrite(V3, 0);  // Turn off the button for tank 2
   Blynk.virtualWrite(V4 0);  // Turn off the button for tank 3
   Blynk.virtualWrite(V4, 0);  // Turn off the button for tank 4
   active_tank=1; //We want tank 1 to be read, and the results sent to V0 
  }
  else
  {  // Button for tank 1 was turned off
  Blynk.virtualWrite(V1, 1);  // Turn the button back on, because we always want to monitor 1 tank 
  }
}

This code will simulate radio buttons using the 4 widgets buttons set to switch mode, connected to V2, V3, V4 & V5
With radio buttons, one button is always selected. If you wanted to be able to monitor none of the tanks then you can change the else statement to allow a press of a button to turn it off.

I’m assuming you’ll use a timer to read the later level in your selected tank, maybe once every few seconds? if so, then the active_tank variable can be used by your timed function to read the appropriate tank and send the result to V0.

Pete.

Thanks Pete for the constructive help. I don’t know what I’d do if wasn’t for folks like you to help. I have a working program that does all that. If I can get the program above to simply display a preset value on the gauge when I press a button, and another preset value when I press the 2nd button, I’ll consider this step a success. I’ll then implement the “working” program into my existing program.
I’ll see if I can implement your suggestions into my program and give it a trial this afternoon.
Thanks agian.
Jeff

I would use a segmented switch, process input from it in sketch and then send the correct value to the gauge. This would minimize the coding for enabling/disabling buttons.

1 Like

A segmented switch would be great, but I haven’t been able to find one in the widgets. Can you tell me where it is?
Jeff

Hi Pete:
I’m sorry if I’m not really clear. I don’t want to constantly monitor tank one. I just want to monitor whichever tank is on. The reason is we have four tanks. We worry that if we leave them all connected to the cottages if there is a break in the line we could lose ALL our water. So we leave one on for a week, then I go up turn it off and turn on a different tank for a week. I continue this until we’ve circulated through the four tanks.
So for the week I just want to keep an eye on the tank that’s connected.
I managed to get my program working, and it appears that when the button (which is in the switch mode) is turned on the value for that tank is displayed. If another switch is selected that value is diplayed. It is not necessary to turn off the previous button. If I turn the displayed button off no value is shown, which is okay.
As I’ve mentioned, now that I have the sketch working I’ll replace the arbitrary numbers I put in to test, with the data coming from the tank.
There were a few small errors in your code (missing bracket and V4 used twice) which I fixed, but I’m not sure where it would fit in my code to improve it. It appears to turn off the switches, which doesn’t seem necessary. I bow to your expertise and would always like to improve my sketch, so if you can tell me exactly how to integrate it in the below sketch, I’d be most gratefull. Pavel (above) suggested a segmented switch, which would simplify the display, but I can’t find one in the widget selection. Here’s the “working” sketch:

 
//button1 is V2
//button2 is V3
//button3 is V4
//button4 is V5
//gauge is V0
 
#define BLYNK_PRINT Serial  
#include <SPI.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
 
// Your WiFi credentials.
#define BLYNK_PRINT Serial  
#include <SPI.h>
char auth[] = "xxxxxxxxxxxxxxxxxxx"; //Enter the Auth code which was send by Blink
 
char ssid[] = xxxxxxxxxxx";  //Enter your WIFI Name
char pass[] = "xxxxxxxxxxxxx;  //Enter your WIFI Password
 
int tank1 =23;// arbitrary number as place holder for data sent from transmitter - just for testing now
int tank2 =55;// arbitrary number as place holder for data sent from transmitter - just for testing now
int tank3 = 75;// arbitrary number as place holder for data sent from transmitter - just for testing now
int tank4 = 17;// arbitrary number as place holder for data sent from transmitter - just for testing now
 
void setup() {
Blynk.begin(auth, ssid, pass);
}
 
void loop() {
  Blynk.run();
}
 
BLYNK_WRITE(V2) { // Button for Tank1 on V2
 
  if (param.asInt() == 1) {  // If button state is 1 (HIGH) then...
   Blynk.virtualWrite(V0, tank1);  // To Display Widget
  } else {  // If button state is 0 (LOW) then...
       Blynk.virtualWrite(V0, "N/A");  // Reset Display 
      }
}
 
BLYNK_WRITE(V3) { // Button for Tank2 on V3
 
  if (param.asInt() == 1) {  // If button state is 1 (HIGH) then...
   Blynk.virtualWrite(V0, tank2);  // To Display Widget
  } else {  // If button state is 0 (LOW) then...
       Blynk.virtualWrite(V0, "N/A");  // Reset Display 
      }
}
BLYNK_WRITE(V4) { // Button for Tank3 on V4
 
  if (param.asInt() == 1) {  // If button state is 1 (HIGH) then...
   Blynk.virtualWrite(V0, tank3);  // To Display Widget
  } else {  // If button state is 0 (LOW) then...
       Blynk.virtualWrite(V0, "N/A");  // Reset Display 
      }
}
BLYNK_WRITE(V5) { // Button for Tank4 on V5
 
  if (param.asInt() == 1) {  // If button state is 1 (HIGH) then...
   Blynk.virtualWrite(V0, tank4);  // To Display Widget
  } else {  // If button state is 0 (LOW) then...
       Blynk.virtualWrite(V0, "N/A");  // Reset Display 
      }
}

You can find the segmented switch in the interface widgets section

found it, Pavel, way down the list! I’ll see if I can get it to work in my sketch.

I never thought that you did. I’m not sure that makes you assume that.

That’s the whole point of the code snippet, an is what was suggested by @jonas, which you appeared to agree with.
If that’s not what you want then I can’t see how your user interface is going to work.

It was meant as an example of how your BLYNK_WRITE functions should be structured, so it would replace one of those BLYNK_WRITEs and be modified for each of your other virtual switches.

I agree that the segmented switch is a simpler solution, but as you’d already seemed to like the solution suggested by @jonas, and you seem to have difficulty understanding why virtual LEDs are not needed, I thought it best not to throw that idea 8nto the mix, as it would simply confuse the issue.

Pete.