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(

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