Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
ESP8266 Home Automation Projects

You're reading from   ESP8266 Home Automation Projects Leverage the power of this tiny WiFi chip to build exciting smart home projects

Arrow left icon
Product type Paperback
Published in Nov 2017
Publisher Packt
ISBN-13 9781787282629
Length 196 pages
Edition 1st Edition
Tools
Arrow right icon
Authors (2):
Arrow left icon
Constantin Tambrea Constantin Tambrea
Author Profile Icon Constantin Tambrea
Constantin Tambrea
Catalin Batrinu Catalin Batrinu
Author Profile Icon Catalin Batrinu
Catalin Batrinu
Arrow right icon
View More author details
Toc

It is time for your first program

To begin this, let's evaluate the basic input and output of the Witty ESP8266 module.

The definition of pins is Light Dependent Resistor (LDR) on Witty and is attached to A0 (the analog input), the push button is connected to GPIO 4, and the LEDs are connected to GPIO 12, GPIO 13, and GPIO 15:

Delete everything that is in your Arduino IDE and replace it with the following code:

#define LDR     A0 
#define BUTTON  4 
#define RED     15 
#define GREEN   12 
#define BLUE    13 

The setup section will run only once after the module is reset or powered. The serial UART is started with 115200 bps, so the messages can be seen in the Serial Monitor window, where you also need to set the same speed in the lower-right corner of the window; otherwise, weird characters will be seen.

All pins are defined as INPUT or OUTPUT depending on their usage. The button and LDR are configured as input pins and all LED connected pins are set as output:

void setup()  
{ 
Serial.begin(115200); 
 
pinMode(LDR, INPUT); 
pinMode(BUTTON, INPUT); 
pinMode(RED, OUTPUT); 
pinMode(GREEN, OUTPUT); 
pinMode(BLUE, OUTPUT); 
} 

The loop() function is continuously running after the setup() and, in it:

  1. The analogRead function reads the value of the ambient light provided as 0-1 V by the LDR.
  2. The digitalRead function reads the value of GPIO 4, that can be either 0 V when the button is pressed or VCC 3.3 V if the button is not pressed.
  3. Show the data to the Serial Monitor with the Serial.print function. Serial.println just adds a new line.
  4. Write a random value between 0 and 1023 to GPIO 15 and GPIO 12 that will control the red and green LED color intensity. This is Pulse Width Modulation (PWM).
  5. Turn on the blue LED connected to GPIO 13.
  6. Wait 1000 ms (one second).
  7. Turn off the blue LED and continue from step 1:
void loop() 
{ 
Serial.print("LDR: "); 
Serial.println(analogRead(LDR)); 
Serial.print("BUTTON: "); 
Serial.println(digitalRead(BUTTON)); 
 
analogWrite(RED,   random(0,1023)); 
analogWrite(GREEN, random(0,1023)); 
digitalWrite(BLUE, HIGH); 
delay(1000); 
digitalWrite(BLUE, LOW); 
} 

In order to compile and flush the binary into the ESP8266 chip you need to press the Upload button.

Seeing the result

In the Serial Monitor output, as shown in the following image, we can see the values for the ambient light and the status of the button, where 0 means pressed and 1 means not pressed:

If you don't have a Witty module, you will need some extra parts such as resistors, LED, push buttons, and LDR sensors, according to the following schematics:

Let's review now the functions that allow you to control GPIO pins and the function that will print values in the Serial Monitor:

  • analogRead(pin): This reads the value on the A0 pin
  • digitalRead(pin): This reads the value for a specified pin, either LOW or HIGH
  • digitalWrite(pin, value): This writes a LOW or HIGH value to a digital pin
  • Serial.println (val): This prints data to a serial port as human-readable ASCII characters ending with \r and a new line character \n
Using analogWrite(val), where val can be in the 0 to 1023 interval, a PWM digital output pin will have a voltage between 0 and 3.3V in 1023 steps.
You have been reading a chapter from
ESP8266 Home Automation Projects
Published in: Nov 2017
Publisher: Packt
ISBN-13: 9781787282629
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 $19.99/month. Cancel anytime
Banner background image