Digital pin needs to be HIGH on startup

I’ve got a light at home that on startup should go on, so that when I switch the light on with the physical light switch it actually goes on.
I managed to do this with “digitalWrite(2, HIGH)” , however the button in the app doesn’t show it’s on. I have to cycle it on and off first before it has synced if that makes sense.
I have spend hours trying to sort this but still haven’t managed to find a solution.
Please help! :slight_smile:

You need to run syncVirtual() in setup after connecting.

http://docs.blynk.cc/#blynk-firmware-virtual-pins-control-blynksyncvirtualvpin

1 Like

Thanks for that.
I managed to have the digital pin sync up with the button in the app, but I need it the other way around.
This is my code so far:

char auth[] = "xxxxxxx";
char ssid[] = "xxxxxx";
char pass[] = "xxxxxx";
bool isFirstConnect = true;

pinMode(1, OUTPUT)
digitalWrite(1, HIGH)


BLYNK_CONNECTED() 
{
  if (isFirstConnect) {
    
    Blynk.syncAll();
    isFirstConnect = false;
  } 
}

void setup()
{
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
}

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

Please take it easy on me. I’m quite the noob. :slight_smile:

Firstly edit your post with correct code formatting.

```cpp
CODE
```

Done. Thanks!

1 Like

This must be inside setup()… this is basic C++/Arduino code otherwise it won’t compile.

Also not sure what your question is?
Sync just will just update your hardware to the same state as your widget…

I need the widget to sync to the hardware. So that if I switch on my light with the wall switch (so power goes to the ESP8266) the light comes on and the widget state changes to “ON”.

  1. Hardware boots up.
  2. Checks if light is on via syncVirtual
  3. Update widget state to current light state

Correct?

Edit… your question in your last post makes your project sound more complex…

So basically when ever you turn on your physical AC light switch, it will power up the ESP, and thus power up the relay which you want to start “ON” (or active so the light is on)… also correct?

Yes indeed.

hmm your system doesnt sounds very efficient…

you should read over this thread and about the voltage tester module the OP got. He created a system to read if there is voltage in the system, sync the relay and widgets, and also control it… without the ESP ever losing power so its super fast.

I know. The problem is that I live in a rented house and there is no neutral at the switch. So the only solution for me was to build it into the light fixture itself and leave it switched on. This works for me but is not so handy for guests.
Thanks for your efforts!

No neutral? just a live in and live out and earth?

Yup. There is obviously a neutral at the light fixture just not at the switch.

Hah that sounds rather dangerous… I’d hate to become the earth when touching the switch :smile:

2 Likes

No Neutral in the light switch is the standard way in most homes in the UK.
Neutral goes direct to the light fitting while the wall switch(es) interrupt the Line supply. There is also usually a constant live Line (unstitched) available at the light switch.

The best solution is usually to locate the microprocessor near the light switch and sense a change of state in the power coming from the light switches. You then process the change of state of the switch to mean that you toggle the light. If the light is already on then you turn it off - regardless of whether flipping the switch from one position to the other has actually turned the Line supply on or off. All you’re interested in is that it’s changed, so you flip the state of the light.
How you go about interfacing the mains voltage supply from the switch to the Arduino so that you know the power state has changes isn’t something that I’m going to get into.
Current sensing the supply from the switches using no-contact induction sensors wont work, as the power from the light switches isn’t actually going to the lights - if it did then you wouldn’t be able to control the state if the light with the microprocessor.

If you do find a way of safely detecting the state of the supply from the switches then the approach I’ve just described will only work if there is an un-switched Line supply available at the light fitting, as this is what you’ll be feeding the lightbulb with via your relay, once you sense what the light switch (and the Blynk app) is telling you to do in terms of switching the light on or off.

Pete.

Which is why in my case the two lighting had to replace a physical switch at one of the 2 way circuit so it could work with existing mains wiring

Cheers

Kev