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

Windows Docker Containers can make WIN32 API calls, use COM and ASP.NET WebForms

After the last post , I received two interesting questions related to Docker and Windows. People were interested if we do Win32 API calls from a Docker container and if there is support for COM. WIN32 Support To test calls to WIN32 API, let’s try to populate SYSTEM_INFO class. [StructLayout(LayoutKind.Sequential)] public struct SYSTEM_INFO { public uint dwOemId; public uint dwPageSize; public uint lpMinimumApplicationAddress; public uint lpMaximumApplicationAddress; public uint dwActiveProcessorMask; public uint dwNumberOfProcessors; public uint dwProcessorType; public uint dwAllocationGranularity; public uint dwProcessorLevel; public uint dwProcessorRevision; } ... [DllImport("kernel32")] static extern void GetSystemInfo(ref SYSTEM_INFO pSI); ... SYSTEM_INFO pSI = new SYSTEM_INFO(

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.51 EF 6.0.2 VS2013 It see

Navigating Cloud Strategy after Azure Central US Region Outage

 Looking back, July 19, 2024, was challenging for customers using Microsoft Azure or Windows machines. Two major outages affected customers using CrowdStrike Falcon or Microsoft Azure computation resources in the Central US. These two outages affected many people and put many businesses on pause for a few hours or even days. The overlap of these two issues was a nightmare for travellers. In addition to blue screens in the airport terminals, they could not get additional information from the airport website, airline personnel, or the support line because they were affected by the outage in the Central US region or the CrowdStrike outage.   But what happened in reality? A faulty CrowdStrike update affected Windows computers globally, from airports and healthcare to small businesses, affecting over 8.5m computers. Even if the Falson Sensor software defect was identified and a fix deployed shortly after, the recovery took longer. In parallel with CrowdStrike, Microsoft provided a too