Book Image

ESP8266 Internet of Things Cookbook

By : Marco Schwartz
Book Image

ESP8266 Internet of Things Cookbook

By: Marco Schwartz

Overview of this book

The ESP8266 Wi-Fi Module is a self contained System on Chip (SOC) with an integrated TCP/IP protocol stack and can give any microcontroller access to your Wi-Fi network. It is capable of either hosting an application or offloading all Wi-Fi networking functions from another application processor. This book contains practical recipes that will help you master all ESP8266 functionalities. You will start by configuring and customizing the chip in line with your requirements. Then you will focus on core topics such as on-board processing, sensors, GPIOs, programming, networking, integration with external components, and so on. We will also teach you how to leverage Arduino using the ESP8266 and you'll learn about its libraries, file system, OTA updates, and so on. The book also provide recipes on web servers, testing, connecting with the cloud, and troubleshooting techniques. Programming aspects include MicroPython and how to leverage it to get started with the ESP8266. Towards the end, we will use these concepts and create an interesting project (IOT). By the end of the book, readers will be proficient enough to use the ESP8266 board efficiently.
Table of Contents (15 chapters)
ESP8266 Internet of Things Cookbook
Credits
About the Author
About the Reviewer
www.PacktPub.com
Customer Feedback
Preface
Index

Uploading your first sketch to the ESP8266


In this recipe, we are going to look at how to upload sketches to the ESP8266. This will enable you to understand some basics of the Arduino programming language and get you to upload your first sketch to your ESP8266 board.

Getting ready

Ensure that the board definitions of the ESP8266 board are installed in your Arduino IDE as explained earlier, and then connect your ESP8266 board to the computer. Now you can proceed to do the rest of the connections.

In this project, you will require a few extra components. They are:

Start by mounting the LED onto the breadboard. Connect one end of the 220 Ω resistor to the positive leg of the LED (the positive leg of an LED is usually the taller one of the two legs). Connect the other end of the resistor on another rail of the breadboard and connect one end of the jumper wire to that rail and the other end of the jumper wire to pin 5 of the ESP8266 board. Take another jumper wire and connect one if its ends to the negative leg of the LED and connect the other end to the GND pin of the ESP8266.

How to do it…

We will configure the board to output a low signal on pin 5 for a duration of one second and then output a high signal on pin 5 for a duration of two seconds. The process will repeat forever:

// LED pin
int ledPin = 5;

void setup() {
  pinMode(ledPin, OUTPUT);
}

void loop() {
  
  // OFF
  digitalWrite(ledPin, LOW);                                
  delay(1000);             

  // ON
  digitalWrite(ledPin, HIGH); 
  delay(2000);                    
}

Refer the following steps:

  1. Copy the code and paste it in your Arduino IDE.

  2. Ensure your ESP8266 board is connected to your computer, and then proceed to select the board in Tools|Board menu. If the ESP8266 board definitions were properly installed, you should see a list of all ESP8266 boards in the menu:

  3. Choose the board type you are using. In this case it is the Adafruit HUZZAH ESP8266.

  4. Select the serial port where the ESP8266 board is connected from the Tools|Port menu and then proceed to upload the code to your board:

How it works…

This is a simple LED blinking program that we used to demonstrate how to upload code to an Adafruit ESP8266 board. The program blinks an LED connected to pin 5 of the Adafruit ESP8266 board every three seconds. This is done by turning off the LED for one second and turning on the LED for two seconds, continuously.

There's more…

The frequency at which the LED blinks can be increased or reduced by adjusting the delays in the program. For instance, the LED can be made to blink faster through reducing the second delay from two seconds to one second, by changing this statement delay(2000); to delay(1000);.

See also

Having successfully uploaded your first sketch to your ESP8266 board, you can go to the next recipe and learn how to connect your ESP8266 board to a local Wi-Fi network.