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(

Azure AD and AWS Cognito side-by-side

In the last few weeks, I was involved in multiple opportunities on Microsoft Azure and Amazon, where we had to analyse AWS Cognito, Azure AD and other solutions that are available on the market. I decided to consolidate in one post all features and differences that I identified for both of them that we should need to take into account. Take into account that Azure AD is an identity and access management services well integrated with Microsoft stack. In comparison, AWS Cognito is just a user sign-up, sign-in and access control and nothing more. The focus is not on the main features, is more on small things that can make a difference when you want to decide where we want to store and manage our users.  This information might be useful in the future when we need to decide where we want to keep and manage our users.  Feature Azure AD (B2C, B2C) AWS Cognito Access token lifetime Default 1h – the value is configurable 1h – cannot be modified

What to do when you hit the throughput limits of Azure Storage (Blobs)

In this post we will talk about how we can detect when we hit a throughput limit of Azure Storage and what we can do in that moment. Context If we take a look on Scalability Targets of Azure Storage ( https://azure.microsoft.com/en-us/documentation/articles/storage-scalability-targets/ ) we will observe that the limits are prety high. But, based on our business logic we can end up at this limits. If you create a system that is hitted by a high number of device, you can hit easily the total number of requests rate that can be done on a Storage Account. This limits on Azure is 20.000 IOPS (entities or messages per second) where (and this is very important) the size of the request is 1KB. Normally, if you make a load tests where 20.000 clients will hit different blobs storages from the same Azure Storage Account, this limits can be reached. How we can detect this problem? From client, we can detect that this limits was reached based on the HTTP error code that is returned by HTTP