Relays, weather station. Ok code- keeps disconnecting

Do you mean a Wemos D1 Mini, or one of those strange Arduino shields that Wemos made?

Pete.

D1 Mini

What bugs me is the 3.3v usage, while almost all my modules are 5v.
Atm I’m using a usb cable that feeds wemos, while 5v feed is taken from the same cable, I stripped a 5v feed upstream of the usb socket. is that ok?

I really need more info to be able to comment correctly.

The D1 Mini has a 5v pin which is connected directly to the 5v rail from the USB socket. This can be used either as a 5v input if you don’t want to have a USB cable plugged-in to the board, or as a 5v output if you are using the USB power.
It’s worth noting that the 5v supply then goes through a 5v —> 3.3v regulator and the output from this is connected to the 3.3v pin. This 3.3v pin is really meant to be an input pin so that you can power the board from a 3.3v supply, but it is also able to be used as a logical HIGH level if you what to attach a switch between it and one of the GPIO pins. It’s not intended to be used to power other 3.3v devices and using it in this way will probably result in the on-board voltage regulator getting burnt-out as it will be overloaded.
I realise you’re not using this 3.3v pin in this way, it’s just a cautionary note for anyone else reading this.

Power supply problems are a common cause of reliability issues with MCUs and are difficult to diagnose. This is where it gets difficult to recommend the best solution for you, as I don’t now what other devices you’re trying to power, and what sort of supply you’re using for your USB power.
If you’re using a relay board then I’d certainly recommend a separate power supply for that, but you should make sure that you connect thye ground of your D1 Mini to the ground on the supply for the relay board.

The D1 Mini uses 3.3v logic levels for the digital GPIO pins, although they are able to tolerate 5v. However, if you’re doing this then make sure that you’re using a good quality 5v supply, as the cheaper one may be delivering 5.5 or even 6v, which isn’t good. You can always use a simple voltage divider to lower the logic level voltage, or use a cheap level shifter board. If you’re using the analogue input pin on the D1 Mini then you’ll almost certainly nee to go down the voltage divider/level shifter route.

I must admit, when I first changed from Arduino to Wemos D1 Mini, the 3.3v thing bugged me, but now it very rarely proves to be a problem. The only occasion when I’ve needed to use a level shifter was when I was connecting an RFID reader to a D1 Mini.

Pete.

I thought I’d answer your coding questions separately from your power supply question.

I’m not a fan of using the “D” pin references in you’re code when using ESP/MCU boards. It works, but it doesn’t help with code portability and makes it difficult to diagnose when inappropriate pins ave been selected.

I do it this way instead…

const int gate = 12;  // The number of the gate pin (Pin D6 on Wemos D1 Mini)

You’ve selected good GPIO pins (12 (D6), 13 (D7) and 5 (D1)), so presumably you’ve read this:

When you’re struggling to diagnose what’s happening with the program flow then it’s a good idea to add lots of Serial.print statements to your code and see what appears in the serial monitor; like this:

void emergencybtn()

{
  //REDBUTTON EMERGENCY
  previousMillis = millis();
  
   // read the state of the pushbutton value:
   buttonState = digitalRead(buttonPin);
   
   //The button is pushed
   while (buttonState == HIGH) {
      
      currentValue = millis() - previousMillis;
      Serial.print("The button is pressed, currentValue = ");
      Serial.println(currentValue);
     
     if (currentValue > interval)  //If the button has been pressed for over 7 secs, open it
     {
      // save the last time relays has been turned on
      previousMillis = millis();   
      Serial.println("Opening the door....")
      digitalWrite(door, HIGH);      //opendoor
      delay(4000);     //give time to get in
      digitalWrite(door, LOW);    //close it
      Serial.println("The door is closed again")
      }
   
   // read the state of the pushbutton value:
   buttonState = digitalRead(buttonPin);
   
   }
}

I’d also change the serial baud rate from 9600 to 74880 as this is the default for the D1 Mini and it allows you to see the D1 Mini’s system messages in the same serial monitor as your debug messages.

This will almost certainly lead to Blynk disconnection problems at some point. As I’ve said before, a countdown timer is the best way to do this stuff, but as you don’t seem to be happy to go down that route, then use a second millis() comparison to determine how long the gate has been open for, in the same way that you check how long the button has been pressed.

You may also need to throw some Blynk.run(); commands into your emergency button routine to keep Blynk fed with processor time.

One final observation, you’re checking to see if your emergency button is pressed every 2 seconds:

This means that it could be 2 seconds before the 7 second test begins, requiring a button press of between 7 and 9 seconds in total to release your door. If that’s a problem then reduce this to a few hundred milliseconds.

Pete.

Thanks for the reply.

So… concerning my power supply doubts, I have 2 relays, one pushbutton and 2 dht18b20.

I believe I can feed these devices directly from the 5v passthrough of the D1 mini as the current drawshould be less than 0.2A.
I will try later today diagnosing my coils as I finally have some time off from work