Author Archives: R-B

Tutorial 5: Setting up an ESP8266 Web Server

In this tutorial, we will explore how to setup an ESP8266 web server to serve an webpage that can be displayed on a client’s browser. The client can be any other computer, smartphone, or tablet connected to the same WiFi network. The webpage will also provide an user interface to allow you to toggle an I/O pin of the ESP8266 hardware.

ESP8266 Webserver setup tutorial

ESP8266 Webserver setup tutorial

Hardware Setup

There is nothing much to do in the hardware setup of this experiment. In your EasyESP-1 board, all you need to do is to connect the LD1 pin to D1 pin using a jumper cable. This will connect the LD1 LED on board to the D1 I/O pin of the ESP8266.

Setup

Hardware Setup

Software

As mentioned above, this tutorial illustrates how to create a standalone local web server using EasyESP-1. The web server will respond to a client’s request with a web page that will allow the user to toggle the LED connected to the D1 I/O pin. The first step to do that is to create a server object, which is done with the following statement.

WiFiServer server(80);

The server will respond to clients on port 80. You need to provide the credentials (network SSID and password) to connect to the chosen WiFi network. You can initiate the connection using WiFi.begin(ssid, password) function. Once connected to the network, you can find out the local IP address assigned to your ESP8266 using WiFi.localIP(). The complete code for this tutorial is posted below. The content of the webpage is defined in the string variable HTMLpage.

#include 
#include 
#include 
#include 
 
ESP8266WebServer Webserver(80);
 
// Replace with your network credentials
const char* ssid = "your network ssid";
const char* password = "password";
 
String HTMLpage = "";
int LED = D1;
 
void setup(void){
  HTMLpage += "

ESP8266 Webserver Demo (Toggle LED)

LED  

";
 
  pinMode(LED, OUTPUT);
  digitalWrite(LED, LOW);
  Serial.begin(115200);
  WiFi.begin(ssid, password);
  Serial.println("");
 
  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connected to ");
  Serial.println(ssid);
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
 
  if (MDNS.begin("esp8266", WiFi.localIP())) {
    Serial.println("MDNS responder started");
  }
 
  Webserver.on("/", [](){
    Webserver.send(200, "text/html", HTMLpage);
  });
  Webserver.on("/ledON", [](){
    Webserver.send(200, "text/html", HTMLpage+"

LED is ON

");
    digitalWrite(LED, HIGH);
    delay(1000);
  });
  Webserver.on("/ledOFF", [](){
    Webserver.send(200, "text/html", HTMLpage+"

LED is OFF

");
    digitalWrite(LED, LOW);
    delay(1000); 
  });
 
  Webserver.begin();
  Serial.println("HTTP Webserver started");
}
 
void loop(void){
  Webserver.handleClient();
}

Download Webserver Tutorial code

Output

After uploading the web server code posted above (make sure you typed in your network credentials), open a Serial Monitor window for the IP address assigned to your EasyESP-1 board. If the connection to the selected WiFi network is successful, you will see the following output on the Serial Monitor window. The IP address displayed here is the IP address of the ESP8266 web server.

Serial Monitor terminal

Serial Monitor terminal output

Now open a browser on any computer or tablet connected to the same WiFi network and type that IP address in the URL field and hit Go. You will see the web page served by the EasyESP-1 as follows.

WebServer1

Web server home page

Now if you press ON button, the browser will send that info to the server, and the ESP8266 will turn ON the LED connected to D1 pin. Similarly, clicking on OFF button will turn the LED OFF.

WebServer2 WebServer3

Toggling LED via web server

Toggling LED via web server

Buy EasyESP-1 board

More tutorials

EASYESP-1: A RAPID PROTOTYPING AND DEVELOPMENT BOARD FOR ESP8266
TUTORIAL 1: SETTING UP THE ARDUINO IDE FOR EASYESP-1
TUTORIAL 2: EASYESP-1 “HELLO WORLD” EXAMPLE
TUTORIAL 3: CONNECTING AN OLED DISPLAY TO ESP8266
TUTORIAL 4: WORKING WITH ESP8266 WIFI SCAN CLASS

Tutorial 4: Working with ESP8266 WiFi Scan Class

Working with ESP8266 WiFi functionality using Arduino IDE has been made surprisingly simple by the availability of the versatile ESP8266WiFi library that allows user to configure the ESP8266 as a WiFi network scanner, a WiFi station (connected to a WiFi network), a soft access point (creating it’s own WiFi network), etc. In this tutorial, we will explore the features of the Scan Class of the ESP8266WiFi library. The Scan Class allows you to scan and list the available WiFi networks in the range. In order to try this feature, open an example code from File->Examples->ESP8266WiFi->WiFiScan.

In the example code,

int n = WiFi.scanNetworks();

returns the number of WiFi networks found within the range. You can also print out the names and Received Signal Strength Indication (RSSI) values in dBm using WiFi.SSID() and WiFi.RSSI() functions.

WiFi Scanner example

WiFi Scanner example

The complete WiFi Scan example code is posted below.

/*
 * This sketch demonstrates how to scan WiFi networks. 
 * The API is almost the same as with the WiFi Shield library, 
 * the most obvious difference being the different file you need to include:
 */
#include "ESP8266WiFi.h"

void setup() {
 Serial.begin(115200);

 // Set WiFi to station mode and disconnect from an AP if it was previously connected
 WiFi.mode(WIFI_STA);
 WiFi.disconnect();
 delay(100);

 Serial.println("Setup done");
}

void loop() {
 Serial.println("scan start");

 // WiFi.scanNetworks will return the number of networks found
 int n = WiFi.scanNetworks();
 Serial.println("scan done");
 if (n == 0)
 Serial.println("no networks found");
 else
 {
 Serial.print(n);
 Serial.println(" networks found");
 for (int i = 0; i < n; ++i)
 {
 // Print SSID and RSSI for each network found
 Serial.print(i + 1);
 Serial.print(": ");
 Serial.print(WiFi.SSID(i));
 Serial.print(" (");
 Serial.print(WiFi.RSSI(i));
 Serial.print(")");
 Serial.println((WiFi.encryptionType(i) == ENC_TYPE_NONE)?" ":"*");
 delay(10);
 }
 }
 Serial.println("");

 // Wait a bit before scanning again
 delay(10000);
}

Upload the example code to your EasyESP-1 board and open the Serial Monitor window. You will see the ESP8266 prints out the list of available WiFi networks on the screen, as shown below.

WiFi scanner output

WiFi scanner output

Buy EasyESP-1 board

More tutorials

EASYESP-1: A RAPID PROTOTYPING AND DEVELOPMENT BOARD FOR ESP8266
TUTORIAL 1: SETTING UP THE ARDUINO IDE FOR EASYESP-1
TUTORIAL 2: EASYESP-1 “HELLO WORLD” EXAMPLE
TUTORIAL 3: CONNECTING AN OLED DISPLAY TO ESP8266

Send a note to your valentine “the IoT way”

Valentine’s day is coming soon, and some of you must be ready to send a personal note to your long-distance sweetheart. Would you like to try something different this year? How about sending a warm note via internet, or more appropriately, “the IoT way”. Becky Stern posted a new Instructable about sending a valentine note over the internet using ESP8266 devices and Adafruit IO cloud platform. This project uses a small vibrating motor to gently wave a tissue paper heart and flash an LED when it receives instructions over the internet from another device.

Sending your valentine a note via internet

Sending your valentine a note via internet

I built two versions of the ESP8266 wifi circuit, also equipped with two buttons for triggering the two commands. The devices talk over the Adafruit IO cloud data service to communicate with each other from anywhere with wifi, and I’ll show you how to activate your valentine with the API gateway service IFTTT as well, in case you only want to build one valentine circuit.

This is a pretty easy Internet of Things project! Before you dive in, you should have a workable knowledge of the Arduino software and electronics soldering. Try my beginner Arduino Class and/or Randy Sarafan’s basic Electronics Class to get up to speed!

Demo video is posted below.

Tutorial 3: Connecting an OLED display to ESP8266

Whether you want to show sensor readings, implement a navigational user interface menu, or display diagnostic information during prototyping a project, a graphic OLED display is always a cool add-on to embedded systems. In this tutorial, we will learn how to interface an I2C monochromatic OLED screen to ESP8266 (we will use EasyESP-1 board) using Arduino IDE. The OLED display used in this tutorial is SSD1306-based that can be bought for ~$5 on eBay or Aliexpress. You can also get a similar I2C OLED display with a grove connector from Elecrow for plug-and-play interfacing with the EasyESP-1 board.

Interfacing an SSD1306-based I2C OLED to EasyESP-1

Interfacing an SSD1306-based I2C OLED to EasyESP-1

Buy EasyESP-1 board

Hardware Setup

The I2C OLED breakout board chosen for this tutorial consists of a 0.96″ diagonal length display with 128×64 monochrome pixels. Despite its small size, the readability is pretty good due to its high contrast. Individual pixels are turned on or off by the on-board SSD1306 controller chip that supports both SPI and I2C interface. This tutorial uses an OLED display with I2C interface and has only four signal lines, namely VCC, GND, SCL, and SDA. For this experiment, these pins are connected to 3.3V, GND, D1, and D2 pins on the EasyESP-1 board, respectively.

SSD1306 based 0.96" OLED display module

SSD1306 based 0.96″ OLED display module

Wiring OLED pins to the EasyESP-1 board

Wiring OLED pins to the EasyESP-1 board

If you would like to use a grove OLED module, it can be connected to one of the I2C port on the EasyESP-1 board, as shown below.

Connecting a grove OLED

Connecting a grove OLED to I2C connector on the EasyESP-1 board

Software

There are several libraries available to support the SSD1036-based OLEDs for Arduino platform. For ESP8266, I would suggest to use one written by Daniel Eichhorn from squix. It supports both I2C and SPI versions of SSD1306 based 128×64 pixel OLED displays on the Arduino/ESP8266 platform. The library is very versatile and supports drawing pixels, lines, rectangles, and text of different font sizes. You can download this from the following github link.

https://github.com/squix78/esp8266-oled-ssd1306

Installation and setup of an external user library to Arduino IDE is easy. First, download the zipped library folder to your machine. Then unzip it and move the unzipped library folder to your Documents/Arduino/libraries/ location. You need to restart the Arduino IDE in order to use it in your new application.

After installing the OLED library, it’s time to try some built-in examples that come with the library. Open File->Examples->ESP8266 Oled Driver for SSD1306 display-> SSD1306SimpleDemo.

fgf

Testing OLED examples

The example programs are configured for I2C version of SSD1306 OLED by default. So, the only thing that you need to modify in the example code is to assign the correct ESP8266 I/O pins for I2C communication. Note that the EasyESP-1 I2C ports use D2 for SDA and D1 for SCL. So find the following I2C initialization statement in the code and make the changes as

// Initialize the OLED display using Wire library
SSD1306 display(0x3c, D2, D1);

Next, compile the program and upload it to the EasyESP-1 board.

Output

The SSD1306SimpleDemo example cycles through all the basic features of the OLED library, like drawing pixels, rectangle, circles, texts, WiFi logo, etc.

Drawing concentric circles

Drawing concentric circles

OLED text output

OLED text output

You can also try running other examples in Daniel’s OLED library. There is one called SSD1306ClockDemo that displays an analog and digital clock on the LED screen. For this example, you will need to install the Arduino Time library.

Clock example

Clock example

Buy EasyESP-1 board

More tutorials

TUTORIAL 2: EASYESP-1 “HELLO WORLD” EXAMPLE
TUTORIAL 1: SETTING UP THE ARDUINO IDE FOR EASYESP-1
EASYESP-1: A RAPID PROTOTYPING AND DEVELOPMENT BOARD FOR ESP8266

DIY Arduino Pong Game

Nick from educ8s.tv shows in the following video tutorial how to build a pong game using Arduino Uno, a 0.95″ SSD1331 driven SPI color OLED display, and some tact switches and jumper wires. You can play against the Arduino that controls the green paddle. Each time a player scores, the score is displayed on the screen.

DIY Pong game using Arduino

DIY Pong game using Arduino

Posted below is the complete video tutorial.

« Older Entries Recent Entries »