Wemos Relay Reset issue

Hello everyone. First I have read the getting started and docs page and searched the forum. and there was one person who had the issue but it was fixed with raspbian.

I have a simple standalone program running on a wemos D1 hooked up to a relay shield and two hall effect sensors. The app is a button (to open the relay briefly) and two displays (for high and low of the hall effect sensors {opened and closed door}). The program works fine enough for me but anytime the app connects to the wemos the relay is triggered without push the button. How do I stop that action? I tried programming inside arduino but the blynk library is pretty confusing to me.

Here is the code


void setup()
{
  // Debug console
  Serial.begin(9600);

  Blynk.begin(auth, ssid, pass);
}

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

Anything else I con provide just let me know! I think this is a simple fix but Iā€™m scratching my head.

EDIT: I think I got it figured out. The button in the Blynk app was set to push 1-0, since I want a default off state I switched it to 0-1 and it seems to function prefect right now. Will update if it continues to have ā€œfalseā€ opens.

Here is my new Code that seems to be just what I want. I was able to figure out how to get BLYNK_WRITE and virtual pins to work. Thanks to the great community and docs!

BLYNK_CONNECTED(){
  Blynk.syncVirtual(V1);
}

BLYNK_WRITE(V1){
  pinMode(D1, OUTPUT);
  int buttonState = param.asInt();
    if(param.asInt()){
    // button pressed    
    Blynk.setProperty(V1, "onLabel", "ON");
    digitalWrite(D1, HIGH);
    delay(500);
    digitalWrite(D1, LOW);
    Blynk.setProperty(V1, "offLabel", "OFF");
  } 
  Serial.print(buttonState);
}

void setup(){
  // Debug console
  Serial.begin(115200);
  Blynk.begin(auth, ssid, pass);
}

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

no timers ?!? whatā€™s that for strange magic :smiley: apparently there are iot projects that dont require timersā€¦

The only remark I can make is that you set the pinmode in the setup not somewhere else. You only need to set it once. What you did will also work so no biggie, its just common practice.

Another thing that strikes me as odd is that you set the relay HIGH for a short time, usually relays turn on when you set them LOW. If it works, donā€™t fret about it, its just unexpected.

o and this:

  int buttonState = param.asInt();
    if(param.asInt()){

is a bit silly. Why do you assign param to a variable and then not use the variable?

no timers ?!? whatā€™s that for strange magic :smiley: apparently there are iot projects that dont require timersā€¦
I have started to use timers in most of my code but this one seemed simple enough to not need it

The only remark I can make is that you set the pinmode in the setup not somewhere else. You only need to set it once. What you did will also work so no biggie, its just common practice.
Normally I set it under my defines and variables at the top of the code but other Blynk examples Iā€™ve seen have them in BLYNK functions which I thought strange but followed suit

Another thing that strikes me as odd is that you set the relay HIGH for a short time, usually relays turn on when you set them LOW. If it works, donā€™t fret about it, its just unexpected.
I want my relay to close its contacts and send a short high closed connection. So that is why I chose this connection method.

o and this:
int buttonState = param.asInt();
if(param.asInt()){

is a bit silly. Why do you assign param to a variable and then not use the variable?
To be honest, I have no idea what param.asInt does within Blynk. Another one of those cases where I seen it in others code examples similar. I thought I was using the variable (my virtual button in the app) is that not the case?

How would you execute this code for this example?

that one is easy. ā€˜paramā€™ is some construct of which I donā€™t know its origin. So in order to use the value inside the construct you need to convert it to something you can use options are given here:

together with a better explanation.

1 Like