Requesting the state of a PB widget from code on the controller

At this stage it will require a redesign of my PCB and circuitry. Also the power saving mode is a requirement of my design

Im using a JDY-08 BLE module. It uses the TI CC2541 chip so any other module thats based on this chip would function in a similar model to the one i have. Thanks @JustBertC JustBertC

Looks like I don’t have one of those modules so I can’t test :confused:

Can you control a LED widget with your button?

Yes i can. Well if you have any BLE module. Do you have an HC05?

I have an HC05/6.

But if you can control an LED we may have a solution?..

With the WidgetLED defined in code you can use your button to control the LED (widget) and then in your code, try calling led.getValue();

Something like this:

WidgetLED myLed(V1);
bool ledState;

checkLed(){
  ledState = myLed.getValue();
}

I’ll rephrase that to say if you can control an LED in your app (LED widget) maybe using a virtual pin? Then you can use the getValue function to read the state of it and this the state of your button.

2 Likes

Not applicable with BT/BLE… but BLYNK_CONNECTED (and possibly Blynk.connected()) should function in same capacity as the App IS the Server link.

As discused very recently here - [SOLVED] Can ESP32 BT mode use "BLYNK_APP_CONNECTED ()"?

i am not sure if i understand you correctly. Are you suggesting that i use an LED widget and a Push Button combination such that the toggling of the Push Button widget changes the state of the LED widget? The problem will be the same in that case. When the hardware is asleep and the PB is pressed the hardware does not receive the change of state and the LED widget will not change. As long as the hardware is required to process the control of both the PB and the LED widget we still have a problem. If there was a way for PB widget to change the state of the LED widget without needing the hardware to process the command then this is definitely something that can work.

Hello @Gunner. Thanks for the response. I read through the documents and the link you shared. i understand that this function will be called whenever the app is disconnected from the hardware. But how will this help me determine the state of the push button unless you are saying that if i run the Blynk.syncVirtual(vPin) inside this function that it will actually sync the vpin eventhough i am using BT comms?

Set the push button so that it sends either 0 or 255 and assign it to a virtual pin. Then put the LED on the same virtual pin.

Pete.

2 Likes

Thanks for clarifying i will give that a try!

1 Like

As @PeteKnight said. Then you can “check” the state of the LED widget using getValue as I mentioned previously.

1 Like

I seem to be doing something wrong here. i am not getting the value of the LED. It is always reading zero. I cant find the getValue() command in the blynk docs. Am i using it correctly if i wrote:

 ledState=myLed.getValue();
 Serial.printlnf("ledState: %i", ledState);

I keep getting zero.
Here is the full program:


#include <BlynkSimpleSerialBLE.h>
char auth[] = "auth token";           //Auth token
int SDOption;


SYSTEM_MODE(MANUAL);                                        //Switches off the wifi module on photon on start up

WidgetLED myLed(V14);
int ledState;


BlynkTimer timer1;
int Powersave=0;
void AllFunctions(void);

void setup()
{
  // Debug console
    Serial.begin(9600);
    Serial1.begin(9600);                                    //Initialise serial communication on the TX and RX pins
    pinMode(D7, OUTPUT);
    pinMode(A7, INPUT_PULLDOWN);
    Blynk.begin(Serial1,auth);
    Serial.println("Photon Setup completed");
    Blynk.notify("Hardware Online");
    delay(1000);
    timer1.setInterval(3000L, AllFunctions);  
    
}



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

void AllFunctions(){
    
    ledState=myLed.getValue();
    Serial.printlnf("ledState: %i", ledState);
    
    if(ledState==255){
        
        Serial.println("The SD card will be on because the LED is ON");
        
    }
    
    
    if(digitalRead(A7)==LOW){
        
        Powersave=1;
        Serial.println("A7 is low");
        
        if(Powersave==1){
        
        Serial.println("Powersave mode activated");
        System.sleep(A7,RISING,2);
        
        }
    }
    else
    Powersave=0;
    
}


BLYNK_WRITE(V14){                       
    
    SDOption = param.asInt();       
    
        if(SDOption==255){
            
        Serial.println("PB is ON");
            
        }        
        else{
        
        Serial.printlnf("PB is OFF");
        
        }

}

BLYNK_WRITE(V11){
        int pinData = param.asInt(); 
    
        if(pinData==HIGH){
        
            digitalWrite(D7, HIGH);
            Serial.println("LED is ON");
            Blynk.syncVirtual(V14);  
            Serial.printlnf("Virtual pin V14: %d", SDOption);
            
        }
        else{
        
        digitalWrite(D7, LOW);
        Serial.println("LED is OFF");
        
        }
}
 

It’s one of those little undocumented features that you only find if you dig through the library code, of course now you know about it we’ll have to send the boys around… :no_mouth:

So maybe it’s another one of those commands that don’t work with Bluetooth. :frowning_face:

Pete.

How are setting up the app?

Button for V11 and led for V14?

I have two PBs. The PB assigned to V14 is the PB state that i am trying to monitor even when the hardware goes offline. V14 is also assigned to the LED widget. I am using the PB associated to V11 to request the state of the V14 and display it on the serial monitor

What about this?:

#include <BlynkSimpleSerialBLE.h>
char auth[] = "auth token";           //Auth token
int SDOption;

SYSTEM_MODE(MANUAL);                                        //Switches off the wifi module on photon on start up

WidgetLED myLed(V14);
int ledState;

BlynkTimer timer1;
int Powersave = 0;
void AllFunctions(void);

void setup()
{
  // Debug console
  Serial.begin(9600);
  Serial1.begin(9600);                                    //Initialise serial communication on the TX and RX pins
  pinMode(D7, OUTPUT);
  pinMode(A7, INPUT_PULLDOWN);
  Blynk.begin(Serial1, auth);
  Serial.println("Photon Setup completed");
  Blynk.notify("Hardware Online");
  delay(1000);
  timer1.setInterval(3000L, AllFunctions);
}



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

void AllFunctions() {
  ledState = myLed.getValue();
  Serial.printlnf("ledState: %i", ledState);
  if (ledState < 0) {
    Serial.println("The SD card will be on because the LED is ON");
  }
  if (digitalRead(A7) == LOW) {
    Powersave = 1;
    Serial.println("A7 is low");
    if (Powersave == 1) {
      Serial.println("Powersave mode activated");
      System.sleep(A7, RISING, 2);
    }
  } else {
    Powersave = 0;
  }
}


BLYNK_WRITE(V14) {
  SDOption = param.asInt();
  if (SDOption < 0) {
    Serial.println("PB is ON");
  } else {
    Serial.printlnf("PB is OFF");
  }
}

BLYNK_WRITE(V11) {
  int pinData = param.asInt();

  if (pinData == HIGH) {
    digitalWrite(D7, HIGH);
    Serial.println("LED is ON");
    //Blynk.syncVirtual(V14);
    Serial.printlnf("Virtual pin V14: %d", SDOption);
  } else {
    digitalWrite(D7, LOW);
    Serial.println("LED is OFF");
  }
}

Perhaps I am the only one, but I do not understand the exact nature of your desired task… if the device is offline/sleeping, then your code is not running (at lease not with a Blynk connection) thus you can not monitor anything.

Ah okay i think i understand your confusion. I am sorry i also forgot to mention that the PB assigned to V11 was used to switch on an LED on my hardware, as a means of verifying that i have a connection between my app and hardware, and i just used this for a double purpose of also indicating the state of the LED widget. You will see i defined D7 as an output which is the pin associated with the hardware LED. Is this what you question was referring to?

What i mean is that i would like my hardware to be able to determine the state of the PB after it returns from sleep so that it does not miss the change in state completely

Normaly that is handled with a Blynk.syncVirtual(vPin) command… but that is not an option with BT/BLE… you are simply trying to do things that were not meant to be done with that connection type.