Skip to main content

IoT Home Automation | Connecting Garage doors (Beninca and ESP8266 controller)

Now, it’s the moment to talk about how I managed the ESP8266 together with Arduino. In the last post on this subject, we talk about high-level architecture and the backend components.

Welding
From the hardware perspective I realize that it is more simple to stick two relays on a board where I would also weld the ESP8266. The relays that I had can be controlled only on D1 pin. Considering this, I decided to use D3 pin on ESP8266 and redirect it to the second relay. Both relays needs to be connected to the GND and 3.3V pins of the ESP8266 board.
If you have, a development board try to play with it before doing the final welding. I was too existed related to the projects and I made the welding first. I was luck, it worked from the first time.
I just started the to learn again how to weld, so ignore my skills. As a coworker told me "If it works, it ain't stupid".

The other option would be to create a tower of two relays and ESP8266 on top of each others, but I did not liked how it would look like. Additional to this you need to be aware that if you put two relays one on top of each other’s the pin connections might not be perfect. Because of this, I lost around 4h trying to debug the code to see what I am doing wrong. The code was perfect, but because I putted two relays, one on top of the other the pin connection was not perfect.

Arduino Code
The initial code, without any security on top of it was simple to write. I was helped by another coworker that already done an automation of heating system. With his sample code, I was able to develop my own application on top of Arduino in just a few minutes.
As you can see below, in the setup function I specifies that I want to use D1 and D3 pins as outputs and I configure the WIFI mode. Be sure from the moment 0 that you have the Serial Monitor working to be able to see all the things that you write to the Serial Monitor(“Console”).
At this step my mistake was that I configured the board as “WeMos D1(Retired)” and not as “WeMos D1 R2 & mini”. The code was deployed with success on the retired board, but the Serial Monitor was not working.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>

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

#define SSID "SSID"
#define PASSWORD "1234"
#define URLROOT "http://XXX/api/iot"

void setup() {
  pinMode(D3, OUTPUT);
  pinMode(D1, 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());
    
  HTTPClient http;
  String url = URLROOT"/gate1";
  Serial.println(url);
  http.begin(url);
  http.setTimeout(3000);
  int httpCode = http.GET();
  Serial.println("HTTP Code for gate 1:");
  Serial.println(httpCode);
  if (httpCode == 201) {              
    Serial.println("Open Relay");
  digitalWrite(D1, HIGH);    
    delay(2000);
    Serial.println("Close Relay");
  digitalWrite(D1, LOW);    
  }

  HTTPClient http2;
  String url2 = URLROOT"/gate2";
  Serial.println(url2);
  http2.begin(url2);
  http2.setTimeout(3000);
  int httpCode2 = http2.GET();
  Serial.println("HTTP Code for gate 2:");
  Serial.println(httpCode2);
  if (httpCode2 == 201) {              
    Serial.println("Open Relay");
  digitalWrite(D3, HIGH);    
    delay(2000);
    Serial.println("Close Relay");
  digitalWrite(D3, LOW);    
  }
    
  delay(2000);
}

In the main loop I’m doing 3 things:

  • Connection to the WiFi
  • Checking if I have something available for each relay
  • Triggering the relay close for 2s

In my case was enough to send a command to the garage gate (close a circuit) to trigger the open or close of the doors. This is why I just had to keep the relay close for 2 seconds. I’m playing with HTTP codes, where 201 means a trigger of a relay.

The code could be rewritten to execute only one call, and based on the HTTP code (200, 201, 202) to trigger a specific relay. For now I prefer to keep them in this way (debugging purpose).
I also observed that until the setup code is executed, the relay connected on D3 has some moment when it close and open the circuit. I was afraid that this would affect the system and trigger the second door to open/close, but I was luck, the door is not trigger. Otherwise I would had to weld a 10k resistor on D3 and ground.

Integration with Beninca
First step was to identify the pins on the board of Beninca where I need to connect the relays. I was lucky that I was able to identify them pretty easy and a simple test using a wire validate what pins are for gate 1 and what are the pins for gate 2 (buttom central of the Beninca board).
Additional to this I had to feed the ESP8266 with 5V. Because Beninca works with 220V I decided to use a small 220V to 5V transformer that I had from Kindle – small and compact (bottom left of the Beninca board).
For now, I’m using a plastic glass as a box, but after a few weeks of testing I plan to do create a nice plastic box. Unfortunately, there is not enough space inside Beninca box to allow me to add the system inside it.


We are done! We have gates connect directly to the our IoT solution. Next I plan to connect the gate doors to the system. 

The final temporary "blue" box can be seen below (smile).

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…