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
Post a Comment