Geekzone: technology news, blogs, forums
Guest
Welcome Guest.
You haven't logged in yet. If you don't have an account you can register now.


chimera

506 posts

Ultimate Geek


#192198 1-Mar-2016 15:53
Send private message

Here's some code I wrote / took snippets off the web, to build an Arduino Garage Door Opener.  There is plenty of code around, but I couldn't find anything complete that would work for me.  You need to add in the PubSubClient library (in Arduino IDE, from menu's Sketch, Include Library, Manage Library, search pubsubclient and install the latest version)

 

I've tied this into OpenHAB/Mosquitto (MQTT broker - required) for home automation, so figured I'd share the code to make it easier.  Its based on an Arduino Uno with Ethershield, a small 5V relay that's used to "trigger" the garage door switch (open/close the door), and 2 x reed switches to determine if the door is open or closed.  I'm running OpenHAB/Mosquitto on C.H.I.P (http://getchip.com)  Had to setup a NAT policy on firewall to redirect requests on 192.168.222.254 (LAN interface of firewall) on port 1883 (MQTT port) to 172.16.221.200 (wireless IP of C.H.I.P on my Wifi network) coz it didn't seem to like routing direct to the wireless IP MQTT port for some reason (maybe firewall related, investigate later as its working in this format)

 

 

 

On the Arduino:

 

- Pin 8 goes to IN on relay

 

- 5V goes to VCC on relay

 

- GND goes to GND on relay

 

 

 

For Reed switches, one cable goes to GND the other goes to:

 

- For Closed state, pin 2

 

- For Open state, pin 3

 

 

 

You will need to change the IP addresses in the code to suit your LAN, the IP for the Arduino itself and the IP for the MQTT broker (or in my case, IP of firewall which translates port TCP1883 through to TCP1883 on C.H.I.P)  You will have to ensure your OpenHAB configuration is configured to match as well (source at the bottom) otherwise alter the code to suit.

 

 

 

Here's the code (sorry, pasting has lost its indentation for easier reading)

 

 

 

// Garage Door Activation and Status (or code for any relay)

 

#include <SPI.h>
#include <Ethernet.h>
#include <PubSubClient.h>

 

// Arduino Pin8 to Relay
#define kickRelay 8 // Relay module - trigger door here
#define led 13 // Onboard LED - Activation Indicator

 

// Arduino to Reed Switch - Closed (Pin2) / Open (Pin3)
const int switchClosed = 2;
const int switchOpen = 3;

 

// Door states
enum doorStates {
DOOROPEN = 0,
DOORCLOSED = 1,
DOOROTHER = 2,
};

 

// Initial door state
doorStates doorState = DOOROTHER;

 

// IP address of the MQTT Broker
IPAddress MQTT_SERVER(192, 168, 222, 254);

 

// IP address of this Arduino
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 222, 100);

 

// Delay timer
unsigned long durationToCheck = 0;

 

// Define message buffer and publish string
char message_buff[10];
String pubString;

 

// Ethernet Initalization
EthernetClient ethClient;

 

// Publish, Subscribe client
PubSubClient mqttClient(MQTT_SERVER, 1883, callback, ethClient);

 

// Callback to Arduino from MQTT (inbound message arrives for a subscription)
void callback(char* topic, byte* payload, unsigned int length) {

// MQTT Inbound messaging
int iChar = 0;
for(iChar=0; iChar<length; iChar++) {
message_buff[iChar] = payload[iChar];
}
message_buff[iChar] = '\0';

 

// Convert buffer to string
String msgString = String(message_buff);
Serial.println("Inbound: " + String(topic) +":"+ msgString);

// Trigger Garage Door, briefly light LED as an indicator
if ( msgString == "ACTIVATE" ) {
digitalWrite(kickRelay, HIGH);
digitalWrite(led, HIGH);
delay(1000);
digitalWrite(kickRelay, LOW);
digitalWrite(led, LOW);
}

}

 


void setup() {

 

// LED pin = relay notifier
pinMode(led, OUTPUT);
digitalWrite(led, LOW);

 

// Reed switch pins are inputs / activate internal pullup resistor
pinMode(switchOpen, INPUT);
digitalWrite(switchOpen, HIGH);
pinMode(switchClosed, INPUT);
digitalWrite(switchClosed, HIGH);

 

// Relay pin is an output
pinMode(kickRelay, OUTPUT);

// Start Network
Ethernet.begin(mac, ip);
delay(1500);

 

// Start Serial
Serial.begin(9600);

// Display IP for debugging purposes
printIPAddress();

 

}

 

void loop() {

 

// If not MQTT connected, try connecting
if (!mqttClient.connected()) {

 

// Connect to MQTT broker on the openhab server, retry constantly
while (mqttClient.connect("garagedoor") != 1) {
Serial.println("Error connecting to MQTT (State:" + String(mqttClient.state()) + ")");
delay(1000);
}

// Subscribe to the activate switch on OpenHAB
mqttClient.subscribe("openhab/garage/doorswitch");

}

// Wait a little bit...
if (millis() > (durationToCheck + 10000)) {

durationToCheck = millis();
// Serial.println(String(durationToCheck) + " - " + millis());

// Publish Door Status
if (digitalRead(switchClosed) == LOW) {
doorState = DOORCLOSED;
// Serial.println("Door state is closed");
}
else if (digitalRead(switchOpen) == LOW) {
doorState = DOOROPEN;
// Serial.println("Door state is open");
}
else {
doorState = DOOROTHER;
// Serial.println("Door state is in use or partially open");
}

 

// Define and send message about door state
pubString = String(doorState);
pubString.toCharArray(message_buff, pubString.length()+1);
mqttClient.publish("openhab/garage/doorstatus", message_buff);
Serial.println("Outbound: " + String("openhab/garage/doorstatus") +":"+ message_buff);

}

mqttClient.loop();

}

 


// Print IP for debugging, can be removed if needed
void printIPAddress()
{
Serial.print("My IP address: ");

for (byte thisByte = 0; thisByte < 4; thisByte++) {
// print the value of each byte of the IP address:
Serial.print(Ethernet.localIP()[thisByte], DEC);
Serial.print(".");
}

 

Serial.println();
}

 

 

 

In your "openhab.cfg" file, under MQTT Transport section entries contain:

 

mqtt:mqttbroker.url=tcp://localhost:1883
mqtt:mqttbroker.clientId=openhab

 

 

 

Create a file "switch.map" and put it in the transform folder.  File contains:

 

0=Open
1=Closed
2=Partially Open

 

 

 

In your items file, entries are:

 

/* Doors */
Number doorStatus "Door Status [MAP(switch.map):%d]" <garagedoor> (All) {mqtt="<[mqttbroker:openhab/garage/doorstatus:state:default]"}
Switch doorButton "Garage Door" <garagedoor> (All) {mqtt=">[mqttbroker:openhab/garage/doorswitch:command:ON:ACTIVATE]"}

 

 

 

In your sitemap file, entries are:

 

Frame label="Garage Door" {
Text item=doorStatus
Switch item=doorButton mappings=[ON="GO"]
}

 

 

 

For testing the door activation, open Serial Monitor in Arduino then click the 'GO' button to open/close the Garage Door in OpenHAB interface - you should see an inbound "ACTIVE" message coming in on the Arduino serial interface.

 

For testing the door status, on the OpenHAB/Mosquitto server, run the command:

 

mosquitto_sub -d -t "openhab/garage/doorstatus"

 

You should see inbound PUBLISH messages (numbers 0,1 or 2) coming in from the Arduino which indicate the door state (open, closed, partially open)

 

 

 

Hope this helps someone out!

 

 

 

Cheers

 

 


View this topic in a long page with up to 500 replies per page Create new topic
 1 | 2
chimera

506 posts

Ultimate Geek


  #1502535 2-Mar-2016 00:32
Send private message

I should point out that, while the basic wiring direct to the relay "worked", I noticed when i went to power up the Arduino for the first time it would kick the relay.  So on a power cut/restore, the Garage door would open, that obviously isn't gonna work!  I suspect I could just put a small-ish 470ohm resistor in after the pin 8 output, however didn't have one that small to test.  So had a look on Arduino web site and according to that even small 5V relays should still be connected via a resistor (1K), diode (I used 1N4001) and transistor (2N2222) 

 

http://playground.arduino.cc/uploads/Learning/relays.pdf

 

I changed my circuit to the above and its all good now on power up (NOTE: the LED there is just for testing the relay - those outputs usually go to the Garage Door switch)

 

 

Code and OpenHAB stuff still remains the same.

 

Cheers

 

 


 
 
 

Move to New Zealand's best fibre broadband service (affiliate link). Note that to use Quic Broadband you must be comfortable with configuring your own router.
chopsuwe
25 posts

Geek


  #1507157 6-Mar-2016 22:27
Send private message

Put your code inside [ code ] blah [ / code ] tags (remove the spaces) to make is show properly.

Assuming you have the relay between an Arduino pin and ground it shouldn't trigger at power up. If it does there is something wrong in your code. Possibly you should write low to the pin right at the top of the setup function but more likely there is something else going on.

Oh and connect the relay up properly. You risk toasting the output pin by running it direct. If you can't find a 2n2222 use a BC548.

chimera

506 posts

Ultimate Geek


  #1507990 8-Mar-2016 11:19
Send private message

The relay is connected up properly.  See the image above. 




Blanch
254 posts

Ultimate Geek


  #1533445 15-Apr-2016 10:30
Send private message

Thanks for sharing your project. I had tried using a raspberry pi for this but it would trip the relay when power cycled.

 

I power cycled the Arduino (unplugged and plugged the USB) a few times and it doesn’t trip the relay? (I haven’t fitted the resistor, diode or transistor yet)

 

Things I still need to do

 

  • Figure out how I’m going to mount the open switch (roller door open position can be a bit random)
  • Figure out how long I can make my cable runs
  • Figure out how to add another relay and more reed switches to the Arduino code.

Thanks again.

 

 

 

Garage

 

 


chimera

506 posts

Ultimate Geek


  #1535191 18-Apr-2016 20:45
Send private message

Hey,

No worries glad it could help.

What type of Arduino did you use? I used an Uno rev3. Perhaps the Arduino you used fixed the power/relay trip issue on boot up.

I still have a couple of other tweaks to the code - which I'll post when done. Specifically because I'm also running an Amazon Echo in my house (so I can just say "Alexa, open the garage door" or "Alexa, close the garage door") however currently there is only a single 'trigger' to do both, close or open regardless of current state. So at present if I say "Alexa, open the garage door" but it's already open, it closes it (and visa versa) which is a bit crap coz I can't see the door from the lounge and half the time the door is already open and I end up closing it!

Cheers

MikeAqua
7769 posts

Uber Geek


  #1535470 19-Apr-2016 11:33
Send private message

Silly question ... what are the practical advantages over a standard remote control?  What are typical scenarios where a remote won't do the job?

 

 





Mike


  #1535489 19-Apr-2016 11:52
Send private message

MikeAqua:

 

Silly question ... what are the practical advantages over a standard remote control?  What are typical scenarios where a remote won't do the job?

 

 

Remote monitoring of your door (i.e. checking if it is open/closed), and the ability to remotely open/close the door.

 

This was one of the first things I automated in my home and it has been very useful over the years. From satisfying that nagging doubt that you forgot to close the garage door when you head off on holiday, to allowing your father-in-law into the garage to borrow the lawn mower.

 

I also have rules setup in openHAB to notify me if the door is left open for more than 10 mins - for the times when you come home in a rush and forget to close the door as you enter the house.

 

As with most things home automation related, it is only once devices and *things* are connected and integrated, that you are able to build the really handy rules etc. The more things you have connected, the more you can do!




MikeAqua
7769 posts

Uber Geek


  #1535531 19-Apr-2016 12:27
Send private message

Thanks, that makes sense.





Mike


Blanch
254 posts

Ultimate Geek


  #1535552 19-Apr-2016 12:52
Send private message

chimera: Hey,

No worries glad it could help.

What type of Arduino did you use? I used an Uno rev3. Perhaps the Arduino you used fixed the power/relay trip issue on boot up.

I still have a couple of other tweaks to the code - which I'll post when done. Specifically because I'm also running an Amazon Echo in my house (so I can just say "Alexa, open the garage door" or "Alexa, close the garage door") however currently there is only a single 'trigger' to do both, close or open regardless of current state. So at present if I say "Alexa, open the garage door" but it's already open, it closes it (and visa versa) which is a bit crap coz I can't see the door from the lounge and half the time the door is already open and I end up closing it!

Cheers

 

I’m using a Arduino UNO Compatible DCcduino http://www.geeker.co.nz/arduino/compatible-board/arduino-uno-compatible-dccduino-uno-with-cable.html

 

 

 

Yep Alexa is on the list of things I need, especially now SumnerBoy has one ;).

 

Before adding the Arduino I had to stand in front of the garage door and say “wife/ kids, open the garage door” (my wife took my key fob after breaking her one).

 

Thanks again guys for all you help.


chimera

506 posts

Ultimate Geek


  #1535597 19-Apr-2016 13:22
Send private message

Blanch:

 

I’m using a Arduino UNO Compatible DCcduino http://www.geeker.co.nz/arduino/compatible-board/arduino-uno-compatible-dccduino-uno-with-cable.html

 

Yep Alexa is on the list of things I need, especially now SumnerBoy has one ;).

 

Before adding the Arduino I had to stand in front of the garage door and say “wife/ kids, open the garage door” (my wife took my key fob after breaking her one).

 

Thanks again guys for all you help.

 

 

Hahaaaa! I tried that, the usual response was "open it yourself"

 

I have an IP camera at the back of the garage too so can view (albeit, not integrated into OpenHAB yet) the garage state if ever needed.

 

Thanks for the link.  The integration I'm doing with my HRV system at the moment (getting HRV temperatures to OpenHAB) is working with the Arduino Uno that I usually use for my garage door (but isn't working with an ESP8266 which I really wanted to use) so have since decided (because I have an Ethernet port near the HRV control panel) to just use another Arduino with Ethernet and run it that way. Geek Studio aren't too far away from me either so very handy.  

 

Cheers.


olivernz
472 posts

Ultimate Geek

ID Verified
Trusted
Lifetime subscriber

  #1535608 19-Apr-2016 13:33
Send private message

@Chimera: Using a lot of ESP8266es for stuff. Still have to connect the Heat Pump via IR. The ESP won't do that because it is technically not capable (or at least I'm told it can't generate the IR signals quick enough). I have working code for my Fujitsu heat pump for a Arduino and I'll probably just use the ESP as WiFi bridge for that scenario. Annoying but can't be helped at the moment.


  #1535624 19-Apr-2016 13:54
Send private message

I am just starting out with some ESP8266s as well. Check out https://github.com/marvinroger/homie-esp8266 for quite a tidy little framework, especially useful if you are using MQTT for inter-device comms. Supports OTA updates and looks after all the WIFI and MQTT broker connection/reconnection logic.


chimera

506 posts

Ultimate Geek


  #1535627 19-Apr-2016 13:59
Send private message

olivernz:

 

@Chimera: Using a lot of ESP8266es for stuff. Still have to connect the Heat Pump via IR. The ESP won't do that because it is technically not capable (or at least I'm told it can't generate the IR signals quick enough). I have working code for my Fujitsu heat pump for a Arduino and I'll probably just use the ESP as WiFi bridge for that scenario. Annoying but can't be helped at the moment.

 

 

I was originally trying to use an ESP8266 with TTL logic level converter to interpret temperature data from the HRV system (it just sends 5V TTL serial signals at 1200 baud between roof unit and control panel) - but with the ESP using 3.3V, I needed the logic level converter.  However I couldn't get any serial data to show up at all, figured it was probably my wiring.  Its all outlined in this thread http://www.geekzone.co.nz/forums.asp?forumid=73&topicid=192361 - I can code fine, however I'm not electronically savvy, am still learning that side.  I got it working with an Uno, but it still has me confused.  See the thread for more, perhaps you maybe able to answer it wink

 

 


russelo
328 posts

Ultimate Geek


  #1535655 19-Apr-2016 14:59
Send private message

SumnerBoy:

 

I am just starting out with some ESP8266s as well. Check out https://github.com/marvinroger/homie-esp8266 for quite a tidy little framework, especially useful if you are using MQTT for inter-device comms. Supports OTA updates and looks after all the WIFI and MQTT broker connection/reconnection logic.

 

 

 

 

Thanks for pointing out Homie. Perfect client devices for my openHab which is driven mostly by MQTT messages.

 

What ESP8266 are you using for homie?  

 

Care to share  a link from Aliexpress?

 

 


  #1535659 19-Apr-2016 15:02
Send private message

I have a few of these (http://www.wemos.cc/Products/) - their shop link just takes you direct to Aliexpress. They are a very small size and have some cool little shields which makes life easy for prototyping. And also got a few of these (http://www.electrodragon.com/product/wifi-iot-relay-board-based-esp8266/) which are pretty impressive for the price - case included! 

 

I have Homie running on both and they seem very reliable/solid so far (early days admittedly).


 1 | 2
View this topic in a long page with up to 500 replies per page Create new topic





News and reviews »

Logitech Introduces New G522 Gaming Headset
Posted 21-May-2025 19:01


LG Announces New Ultragear OLED Range for 2025
Posted 20-May-2025 16:35


Sandisk Raises the Bar With WD_BLACK SN8100 NVME SSD
Posted 20-May-2025 16:29


Sony Introduces the Next Evolution of Noise Cancelling with the WH-1000XM6
Posted 20-May-2025 16:22


Samsung Reveals Its 2025 Line-up of Home Appliances and AV Solutions
Posted 20-May-2025 16:11


Hisense NZ Unveils Local 2025 ULED Range
Posted 20-May-2025 16:00


Synology Launches BeeStation Plus
Posted 20-May-2025 15:55


New Suunto Run Available in Australia and New Zealand
Posted 13-May-2025 21:00


Cricut Maker 4 Review
Posted 12-May-2025 15:18


Dynabook Launches Ultra-Light Portégé Z40L-N Copilot+PC with Self-Replaceable Battery
Posted 8-May-2025 14:08


Shopify Sidekick Gets a Major Reasoning Upgrade, Plus Free Image Generation
Posted 8-May-2025 14:03


Microsoft Introduces New Surface Copilot+ PCs
Posted 8-May-2025 13:56


D-Link A/NZ launches DWR-933M 4G+ LTE Cat6 Wi-Fi 6 Mobile Hotspot
Posted 8-May-2025 13:49


Synology Expands DiskStation Lineup with DS1825+ and DS1525+
Posted 8-May-2025 13:44


JBL Releases Next Generation Flip 7 and Charge 6
Posted 8-May-2025 13:41









Geekzone Live »

Try automatic live updates from Geekzone directly in your browser, without refreshing the page, with Geekzone Live now.



Are you subscribed to our RSS feed? You can download the latest headlines and summaries from our stories directly to your computer or smartphone by using a feed reader.







GoodSync is the easiest file sync and backup for Windows and Mac