Skip to main content

IoT Home Automation | Stabilize the garage doors solution after power break (resistors and capacitors)

A few weeks’ ago, I added the proximity sensors to the garage doors. Everything was perfect until the first power break when one of the garage doors automatically open. Initially we did not know what was the cause, so after a few weeks same thing happen 2 or 3 times.

Root Cause
Because of this instability, I had to shut down the ESP8266. You do not want the garage door open when you are not home and you have a dog that might eat even the car itself (smile).
It seems that this is a common problem when you use ESP8266 connected to a power source that is not stable. The problem is hard to replicate without a power break. When a power break occurs and the power is back there is a fluctuation in the electrical current that is hard to replicate.
This happens on all digital ports except D1. On D1 it seems that the version of ESP8266 board that I have has a resistor that does his job.
Additional to this the number of consumers is pretty high with the additional proximity sensors attached to the ESP8266. Extra consumer affect in combination with electrical fluctuations can affect for short periods of time the ESP8266 behavior. In my case even a 0.1s pulse of energy on D2 triggers the garage doors.

Solution
To solve this problem I decided to do two things. One was to add a resistor on D2 to cut any energy fluctuations  that might appear when the power comes back. The second was to add capacitors to 3.3V and 5.0V ports
Resistor
I added a 10k resistor between D2 and Ground. In theory, this should cut short spikes that might come when the power is back. The resistor was added to the board where I have the two relays.


Capacitors
On 3.3V and 5.0V outputs of ESP8266 I added 3 capacitors on each between ground and each output. This should cut all electrical spikes when the energy consumptions is high. For example, when power is back and all sensors are starting.
I added 1 X 1uf, 1 X 10uf and 1 X100uf on 3.3V and another set on 5.0 pins.

Modules
This was a good opportunity to rewire the circuits. I’ve done things a little more modular, a board with ESP8266 and another board with relays. Additional to this, for all the wiring between modules and sensors I added reusable ports and some extra wires that will be later use to connect the central unit of alarm system.


Source Code
There were only small updates to the source code. I'm using now D1 and D2 for relays and D3 and D4 for proximity sensors of the gate doors. D5 to D8 are reserved for the alarm system that will be later on added.
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>

#ifdef ESP8266
extern "C" {
#include "user_interface.h"
}
#endif

#define SSID "XXX"
#define PASSWORD "YYY"
#define URLROOT "ZZZ"

void setup() {
  pinMode(D1, OUTPUT);
  pinMode(D2, OUTPUT);
    
  // This is required to read ADC values reliably
  wifi_set_sleep_type(NONE_SLEEP_T);
  
  Serial.begin(57600); 
  
  // Delay is required only for debugging
  delay(2000);
  Serial.println("Setup complete");
  
  WiFi.mode(WIFI_STA);
}
void loop() {
  int retries = 0;
  if (WiFi.status() != WL_CONNECTED) {
    Serial.println("Not connected to the WiFi.");
    WiFi.begin(SSID, PASSWORD);
    Serial.println("after wifi begin");
    
    while ( retries < 30 ) {
      Serial.println("loop");
      if (WiFi.status() == WL_CONNECTED) {
        break;
      }
      delay(1000);
      retries++;
    }
    Serial.println("Exiting loop() for another wifi try.");
    return;
  }
  else {
    Serial.println("Connected to WIFI!");
  }    
  
  Serial.println(WiFi.localIP());
  
  // Read gate state (1-Open | 2-Close from sensor)
  int gate1State = 1 + digitalRead(D3);
  int gate2State = 1 + digitalRead(D4);
  Serial.println("Gate state: 1-Open | 2-Close");
  Serial.print("Gate 1 state:");
  Serial.println(gate1State);
  Serial.print("Gate 2 state:");
  Serial.println(gate2State);
  HTTPClient http;
  String url = URLROOT"?Id=1&Status=";
  url = url + gate1State;
  Serial.println(url);
  http.begin(url);
  http.setTimeout(3000);
  int httpCode = http.GET();
  Serial.println("HTTP Code for gate 1:");
  Serial.println(httpCode);
  actionOnGate(httpCode,1,gate1State);

  HTTPClient http2;
  String url2 = URLROOT"?Id=2&Status=";
  url2 = url2 + gate2State;
  Serial.println(url2);
  http2.begin(url2);
  http2.setTimeout(3000);
  int httpCode2 = http2.GET();
  Serial.println("HTTP Code for gate 2:");
  Serial.println(httpCode2);
  actionOnGate(httpCode2,2,gate2State);
    
  delay(2000);
}

void actionOnGate(int httpCode, int gateNumber, int gateState)
{
  // 201 Move
  if (httpCode == 201) {    
  Serial.println("201 - Gate move");  
    triggerGate(gateNumber);   
  }
  // 202 Close
  if ((httpCode == 202) && (gateState == 1) )
  {
    Serial.println("202 && gate Open - Gate move");  
  triggerGate(gateNumber);  
  }
  
  // 203 Open
  if ((httpCode == 203) && (gateState == 2) )
  {
  Serial.println("203 && gate Close - Gate move");  
  triggerGate(gateNumber);  
  } 
}

void triggerGate(int gateNumber){
  Serial.println("Open Relay"); 
  digitalWrite(D(gateNumber), HIGH);    
    delay(2000);
    Serial.println("Close Relay");
  digitalWrite(D(gateNumber), LOW);    
}

uint8_t D(uint8_t index) {
  switch (index) {
    case 1: return D1;
    case 2: return D2;
  }
}             

Next step
For the current solution there is 4 weeks trials before I'll make the box for it. Next week I hope to have some time to play around the alarm.

Comments

Popular posts from this blog

How to check in AngularJS if a service was register or not

There are cases when you need to check in a service or a controller was register in AngularJS.
For example a valid use case is when you have the same implementation running on multiple application. In this case, you may want to intercept the HTTP provider and add a custom step there. This step don’t needs to run on all the application, only in the one where the service exist and register.
A solution for this case would be to have a flag in the configuration that specify this. In the core you would have an IF that would check the value of this flag.
Another solution is to check if a specific service was register in AngularJS or not. If the service was register that you would execute your own logic.
To check if a service was register or not in AngularJS container you need to call the ‘has’ method of ‘inhector’. It will return TRUE if the service was register.
if ($injector.has('httpInterceptorService')) { $httpProvider.interceptors.push('httpInterceptorService&#…

ADO.NET provider with invariant name 'System.Data.SqlClient' could not be loaded

Today blog post will be started with the following error when running DB tests on the CI machine:
threw exception: System.InvalidOperationException: The Entity Framework provider type 'System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer' registered in the application config file for the ADO.NET provider with invariant name 'System.Data.SqlClient' could not be loaded. Make sure that the assembly-qualified name is used and that the assembly is available to the running application. See http://go.microsoft.com/fwlink/?LinkId=260882 for more information. at System.Data.Entity.Infrastructure.DependencyResolution.ProviderServicesFactory.GetInstance(String providerTypeName, String providerInvariantName) This error happened only on the Continuous Integration machine. On the devs machines, everything has fine. The classic problem – on my machine it’s working. The CI has the following configuration:

TeamCity.NET 4.51EF 6.0.2VS2013
It seems that there …

Fundamental Books of a Software Engineer (version 2018)

More then six years ago I wrote a blog post about fundamental books that any software engineer (developer) should read. Now it is an excellent time to update this list with new entries.

There are 5 different categories of books, that represent the recommended path. For example, you start with Coding books, after that, you read books about Programming, Design and so on.
There are some books about C++ that I recommend not because you shall know C++, only because the concepts that you can learn from it.

Coding

Writing solid codeCode completeProgramming Pearls, more programming pearls(recommended)[NEW] Introduction to Algorithms

Programming

Refactoring (M. Fowler)Pragmatic ProgrammerClean code[NEW] Software Engineering: A Practitioner's Approach[NEW] The Mythical Man-Month[NEW] The Art of Computer Programming

Design

Applying UML and Patterns (GRASP patterns)C++ coding standards (Sutter, Alexandrescu)The C++ programming language (Stroustrup, Part IV)Object-oriented programming (Peter Coad)P…