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

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