Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Arduino Android Blueprints

You're reading from  Arduino Android Blueprints

Product type Book
Published in Dec 2014
Publisher Packt
ISBN-13 9781784390389
Pages 250 pages
Edition 1st Edition
Languages

Chapter 4. Wi-Fi Smart Power Plug

In this chapter, we will build an open source version of a very commonly connected object—a Wi-Fi power plug. Indeed, these kind of plugs can be bought from many stores, and usually come with their own iOS or Android app.

In this chapter, we will build such a power plug from scratch, based on Arduino. We will connect a relay module, a current sensor, and a Wi-Fi module to an Arduino board to make our own Wi-Fi power plug. The power plug will be able to switch any device on and off, and will continuously measure the power consumption of the device.

We will build an Android app to switch on and off the power plug remotely via Wi-Fi. We will also be able to get the power output on request and display it on a screen.

The following topics will be the major takeaways from this chapter:

  • Connecting a relay module, a current sensor, and a Wi-Fi module to Arduino

  • Controlling the project by sending commands via Wi-Fi

  • Building an Android application to control the project...

Hardware and software requirements


First, let's see the required hardware components for this chapter.

We need an Arduino Uno board. To control the lamp remotely (the lamp was used as an example in the chapter, but of course any 110V or 230V device can be used here), you will also need a relay module. We used a 5V relay module from Polulu, but you can use any 5V relay module that you want.

To measure the instant power consumption of the device connected to the plug, you will also need a current sensor. For this part, we will choose a breakout board based on the ACS712 chip. The following is a picture of the board I used:

You will also need a board that includes the CC3000 Wi-Fi chip, which we will use to receive commands via the Android device. For this project, we will choose a CC3000 breakout board from Adafruit. Of course, you can also use a shield from the same brand for this project; the code will be exactly the same.

To make the different connections, you will also need a breadboard and...

Writing the Arduino sketch


Now that we are sure that the connections of the relay, the current sensor, and the power cables are correct, we will write an Arduino sketch to accept connections coming via Wi-Fi from the Android device.

The following is the complete sketch for this part:

// Import required libraries
#include <Adafruit_CC3000.h>
#include <SPI.h>
#include <aREST.h>

// Relay state
const int relay_pin = 8;

// Define measurement variables
float amplitude_current;
float effective_value;
float effective_voltage = 230.; // Set voltage to 230V (Europe) or 110V (US)
float zero_sensor;

// These are the pins for the CC3000 chip if you are using a breakout board
#define ADAFRUIT_CC3000_IRQ   3
#define ADAFRUIT_CC3000_VBAT  5
#define ADAFRUIT_CC3000_CS    10

// Create CC3000 instance
Adafruit_CC3000 cc3000 = Adafruit_CC3000(ADAFRUIT_CC3000_CS, ADAFRUIT_CC3000_IRQ, ADAFRUIT_CC3000_VBAT,
                                         SPI_CLOCK_DIV2);
// Create aREST instance
aREST...

Summary


We created a DIY version of a smart power switch, based on Arduino, and controlled by an Android application via Wi-Fi. We connected all the required components to the Arduino board, wrote an Arduino sketch to accept commands via Wi-Fi, and finally, created an Android application to control the switch remotely.

In the next chapter, we will use another Arduino board, called the Arduino Yún, where we will be able to plug an USB camera. As this board will have Wi-Fi as well, we will use the project to create a remote Wi-Fi security camera.

lock icon The rest of the chapter is locked
You have been reading a chapter from
Arduino Android Blueprints
Published in: Dec 2014 Publisher: Packt ISBN-13: 9781784390389
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}