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 Internet of Things Cookbook

You're reading from   ESP8266 Internet of Things Cookbook Over 50 recipes to help you master ESP8266 functionality

Arrow left icon
Product type Paperback
Published in Apr 2017
Publisher Packt
ISBN-13 9781787288102
Length 268 pages
Edition 1st Edition
Tools
Concepts
Arrow right icon
Author (1):
Arrow left icon
Marco Schwartz Marco Schwartz
Author Profile Icon Marco Schwartz
Marco Schwartz
Arrow right icon
View More author details
Toc

Table of Contents (9) Chapters Close

Preface 1. Configuring the ESP8266 2. Your First ESP8266 Projects FREE CHAPTER 3. More ESP8266 Functions 4. Using MicroPython on the ESP8266 5. Cloud Data Monitoring 6. Interacting with Web Services 7. Machine to Machine Interactions 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:
    How to do it…
  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 to do it…

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.

lock icon The rest of the chapter is locked
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