Skip to main content

IoT Home Automation | Connecting Yard Gates (Beninca and ESP8266 controller)

I continue the journey of home automation using ESP8266 and Arduino with yard gates. In the last post related on this topic, I have done the base automation of the garage doors.

Welding
From this perspective, nothing special. Except welding the pines of ESP8266 and relay and putting one of top each other I was done. I prefer to use relays shields that can be added one on top each other allowing you to mount the controller fast and without additional welding or wires.

Arduino Code
In theory, the code should be similar to the garage doors automations. The base idea was the same, using HTTP code to decide if we need to close or open a circuit controlled by the relay.
 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
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>

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

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

void setup() {
  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.hostname("YardGates1");
  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"/yardgate1";
  Serial.println(url);
  http.begin(url);
  http.setTimeout(3000);
  int httpCode = http.GET();
  Serial.println("HTTP Code for yard gates:");
  Serial.println(httpCode);
  if (httpCode == 201) {              
    Serial.println("Open Relay");
  digitalWrite(D1, HIGH);    
    delay(2000);
    Serial.println("Close Relay");
  digitalWrite(D1, LOW);    
  }
    
  delay(2000);
}

Backend Code
The system was designed in such a way to not involve too much change when a new device is added. I just added a new button on the UI and registered the new device. From there, the device can retrieves his state using his own ID.

Integration with Beninca
At yard gates, I have the same hardware system from Beninca. I had to look on the wires to see where I need to connect the relay. I identify that the relay needs to be connected on PP and PHOT. When you close this circuit, the gate command will be triggered (open or close). The pins can be found in middle right of the board. Beninca is connected to 220V, so I had to use a transformer from 220V to 5V that I connected directly to Beninca (top left of the board).

As for garage doors, we don’t have a state to know if the door is open or close, but for now this is more than enough.

Problems
The initial smoke test went super-fast and I was sure that this would be a 5-minute job, but surprise. Things were not as simple as I thought.

1. WiFi signal
The yard gates are outside where WiFI signal is not so good. Even if the laptop has a good connection, once I close the plastic case of Beninca, the ESP8266 does not have WiFi connection anymore.
To solve this problem I had to move the router closer to the window. Fortunately, this solved the problem, even if I had a repeater prepared in case of problems with WiFi signal.

220V Power Bridge
The power bridge between 220V pins and the transformer was not as good as I thought. The wires were loose and I lost the power to the ESP8266. It took me a while to understand why when the ESP8266 is connected to the laptop works great, but once I connect to Beninca I lost the connection.
Somebody asked we from where I have the transformers. Well....I have a box full with transformed from phones, Kindles and other type of devices.

Device name
You should remember that there are some routers, like the one that don’t like to have two devices connected with the same name. Both ESP8266 that I have now connected were named ES. This trigger to lose the connectivity between ESP8266 and the router.
I had to rename each of them from the code using the below command:

1
WiFi.hostname("NewESPName");

We are done. I have now the yard gates connected to my system. Just don’t forget to take your keys in the case your internet connection goes down.

Comments

Popular posts from this blog

How to audit an Azure Cosmos DB

In this post, we will talk about how we can audit an Azure Cosmos DB database. Before jumping into the problem let us define the business requirement: As an Administrator I want to be able to audit all changes that were done to specific collection inside my Azure Cosmos DB. The requirement is simple, but can be a little tricky to implement fully. First of all when you are using Azure Cosmos DB or any other storage solution there are 99% odds that you’ll have more than one system that writes data to it. This means that you have or not have control on the systems that are doing any create/update/delete operations. Solution 1: Diagnostic Logs Cosmos DB allows us activate diagnostics logs and stream the output a storage account for achieving to other systems like Event Hub or Log Analytics. This would allow us to have information related to who, when, what, response code and how the access operation to our Cosmos DB was done. Beside this there is a field that specifies what was th...

Can User Technical Profile Influence AI Architecture Decisions?

In the last few days, I ran a small experiment on Devin/Windsurf/Cascade, BMAD, and LLM-based architecture decisions. The initial concern was simple: if an AI coding assistant knows something about the user, for example, that the user is a Java developer or a .NET developer, can this information influence the architecture and technology stack that the AI will propose? This question is important because many companies are starting to use AI tools not only for coding but also for product discovery, architecture, documentation, and technical decision-making. If the AI is influenced by the developer's personal profile, the result may not be fully neutral. It may look like a business or architecture decision, but in reality, it may be partially shaped by the context of the person using the tool. The test was done using BMAD with Devin/Windsurf/Cascade. The scenario was a unified field service operations platform. The task was to let BMAD refine the product, define the architecture and s...

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...