Serial Communication – ESP8266 Tutorial

The basic and most communication that is needed for most of the devices and program debugging serial communication. Serial communication is utilized to make ESP8266 communicate with PC and serial communication devices.

ESP8266 Serial communication is like the same way as on a regular Arduino. The hardware FIFO (128 bytes for TX and RX) Serial has extra256-byte TX and RX buffers. Some transmit and receive are interrupt-driven. Write and read functions only block the sketch executing when the respective FIFO/buffers are full.


≡ Required Component :


Here is a list of the hardware we suggest to Serial Communication ESP8266 Tutorial.

  1. NodeMcu8266
  2. Breadboard
  3. Connecting wire
  4. Breadboard Power Supply Module

≡ Example 1: Serial Data Transmission


Program to send “Hello World” message to serial

void setup()
{
Serial.begin(115200);
}
void loop()
{
    Serial.println("Hello World");
    delay(500);
}

Upload above program and opened a serial monitor to see transmitted information.

Serial Communication ESP8266


≡ Example 2: Remapping Serial to use GPIO15 (TX) and GPIO13 (RX)


Remapping serials can utilize to make the interface of two serial devices. But only one serial device attached to ESP8266 at a time. This is useful for interfacing RF-ID reader, GPS, and GSM devices.

void setup()
{
  Serial.begin(115200);
  Serial.swap(); //Remap RX TX to GPIO13(Rx) and GPIO15(Tx)
}
void loop()
{ 
   Serial.println("Hello World");
    delay(500);
}

≡ Example 3: Using Only TX


In the uploading program, you will see the blue LED flashes due to data is getting sent on GPIO2(TX).

void setup()
{
  Serial1.begin(115200);
}
void loop()
{ 
    Serial1.println("Hello World");
    delay(500);
}

≡ Example 4: Using Only RX:


If Serial1 is not used and Serial is not ended than the  TX for UART0 can map to GPIO2 instead by calling Serial.set_tx(2) after Serial.begin:

void setup()
{
  Serial.begin(115200); 
  Serial.set_tx(2);   //Remap TX pin to GPIO2
}
void loop()
{ 
    Serial.println("Hello World");
    delay(500);
}

≡ Example 5: Serial Commands begin()


Setting the data rate in bits per second (baud) for serial data transmission. For communication with the computer and use one of these rates: 300, 600, 1200, 2400, 4800, 9600, 14400, 19200, 28800, 38400, 57600, or 115200. 


≡ Example 6: Serial Commands print()


Print data to the serial port like human-readable ASCII text. This command can return many forms. Numbers printed using an ASCII character for all digit. Floats similarly printed as ASCII digits, defaulting to two decimal places. Bytes sent as a single character.

Serial.print(78); 
   Serial.print(1.23456); 
   Serial.print('N'); 
   Serial.print("Hello world.");

You can give flash-memory based strings to Serial.print() by wrapping them with F().

Serial.print(F(“Hello World”));

≡ Example 7: Serial Commands  println()


Prints data to the serial port as human-readable ASCII text following by a carriage return character (ASCII 13, or ‘\r’) and a newline character (ASCII 10, or ‘\n’).


≡ Example 8: Serial Commands available()


Getting the number of bytes (characters) available for reading from the serial port. This is data that’s already got and stored in the serial receive buffer.

void setup()
{
  Serial.begin(115200); 
}
void loop()
{   
    int dataSize = Serial.available();   
    Serial.print("Current Data count in buffer:");
    Serial.println(dataSize);
    delay(500);
}

≡ Example 9: Serial Commands  find()


Serial.find() reads data from the serial buffer until the targeting string of given length is found. The function returns true if the target string found, false if it times out.

void setup()
{
  Serial.begin(115200); 
  Serial.println("");
  Serial.println("Type few words");
  pinMode(LED,OUTPUT);
  pinMode(LED,HIGH); //LED in off
}
void loop()
{  
  if(Serial.available())
  {
    Serial.println(Serial.available());
     if(Serial.find("on"))
    {
     digitalWrite(LED,LOW); //LED on
      Serial.println("ON");
    }
    if(Serial.find("off"))
    {
      digitalWrite(LED,HIGH); //LED off
     Serial.println("OFF");
    }   
  }
}

Open the serial monitor and send ‘on’ to turn on led and ‘off’ to make it off.


≡ Example 9: Serial Commands  read()


void setup()
{
  Serial.begin(115200); 
  Serial.println("");
  Serial.println("Waiting For Data:");
}
void loop()
{  
  if(Serial.available())
  {
    Serial.println(char(Serial.read()));
  }
}

≡ Example 9: Serial Commands  Software Serial


Software serial uses a timer when you are using software serial. The timer is also utilized for the Wi-Fi communication section if you don’t give sufficient time to Wi-Fi routines it will make the problem of stack error. It is better to do a software serial only when you want two serial ports and also avoid the usage of software serial for data reception.

#include <SoftwareSerial.h>
SoftwareSerial swSerial(14, 12, false, 128);
void setup() {
  swSerial.begin(115200);    //Initialize software serial
  swSerial.println("Software serial test started");
  swSerial.println("Hello World");
}
void loop() {

}

Visit to get more tutorials on IoT project

INTERNET OF THINGS

 563 total views,  7 views today