Book Image

Arduino Home Automation Projects

By : Marco Schwartz
Book Image

Arduino Home Automation Projects

By: Marco Schwartz

Overview of this book

Table of Contents (14 chapters)
Arduino Home Automation Projects
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Testing the relays and Wi-Fi connection


Now that the hardware configuration is complete, we can start writing some code to test our project. We'll first write a simple sketch to test a given relay. Go over to your Arduino IDE and you can start writing some code. The most important parts of the code will be detailed below, and you can find the complete code on the GitHub repository of the project at https://github.com/openhomeautomation/arduino-home-automation/tree/master/chapter2.

The first step is to declare which pin the relay you want to test is connected to:

const int relay_pin = 6;

We use a const int variable here, which is similar to #define but better, since we are sure the constant is of the right type. In the setup() function of the sketch, we need to specify that this pin is a digital output with the pinMode() function:

pinMode(relay_pin,OUTPUT);

Then, inside the loop() function, we are basically going to activate the relay, wait for 5 seconds, switch it off again, wait for 5 seconds...