SoftwareSerial for ESP8266

I want to use softwareSerial for my ESP8266 Wifi-module.
But i don´t receive any data. I only need the receive RX pin.
For RX i want to use GPIO16. Can this pin be used for Softwareserial receive?

#include <SoftwareSerial.h>
String TagData;

SoftwareSerial readerSerial(16, SW_SERIAL_UNUSED_PIN); // RX, TX

void setup() 
{
  readerSerial.begin(9600);
}

void loop() 
{            
      while(readerSerial.available() > 0) 
      {
          byte incomingData = readerSerial.read();
          TagData = TagData + String(incomingData);
      }
}

Hi dawinci

Are you using the ESP8266 board support in the Arduino IDE (from this website: GitHub - esp8266/Arduino: ESP8266 core for Arduino).

According to the documentation there, it needs a special version of SoftwareSerial, not the standard Arduino one. You can get it here: GitHub - plerup/espsoftwareserial: Implementation of the Arduino software serial for ESP8266

Regards

Ray

Hey Ray,

My Arduino Code is already compiling without any errors.
So the ESP8266 board Support for Arduino IDE is running.
And the folder "espsoftwareserial-master" is located under "C:\Users\Daniel\Desktop\arduino-1.6.5-r5\libraries".
I´m already able to Switch LED´s over Wifi.
Just the SoftwareSerial is not yet running as wanted.
Can i use every pin on the ESP8266 for SoftwareSerial?
Or does it need Interrupt capability?
I´m also not sure if "SoftwareSerial readerSerial(16, SW_SERIAL_UNUSED_PIN);" takes pin GPIO16?

Regards
Daniel

There is a private method in the ESP software serial library ...

bool SoftwareSerial::isValidGPIOpin(int pin) {
   // Some GPIO pins are reserved by the system
   return (pin >= 0 && pin <= 5) || (pin >= 12 && pin <= 15);
}

Looks like 16 cannot be used. :slightly_frowning_face:

I hooked it up to GPIO5 and it now works yay.
Thx for your support :smiley:

1 Like

Hit there,

I know it's been a while but I'm trying to do the same and having a problem with the software serial.

My ESP8266 works fine alone without serial, as well when it's running software serial without wifi but when I try to combine both, it stops the wifi communication with computer.

Did you had the same thing ?

How is your code?

Are you like sensing with arduino and then sending data to ESP8266 ?

I encounter similar issues.

Depending on what kind of ESP8266 board you are using (mine is HUZZAH), note that all most all the pin( GPIO, TX, RX, etc) are 3.3V voltage. They are not 5V compatible. So if you are trying to use 3.3V serial to communicate with a 5V serial (e.g. Arduino), it's not gonna work.

I tested using a level shifter in RX of ESP8266 to communicate with the Arduino and it works (I did not visually send the data transmission tho, I use LED to check. Received data or not will trigger different LED blinking pattern). This may be one way to go. Good luck.

hey, I can confirm that it's not working, especially when transmitting data from esp to arduino, it works in about 80% of the cases but the data gets corrupted.

@DKAndrew what kind of level shifter did you use ?

I am using esp8266 12E nodemcu,i am just trying to test softwareserial but not able to do..i am uploading file from example but not able to do...here i am attaching screenshot what i have done...thanks

i am just trying to test softwareserial but not able to do.

What does that mean ?
Have you tried a lower baud rate ?

I see that you got it working, but I have a question reggarding what Hackscribble said... Why do you guys think it's the actual reason they reserved some pins of the ESP8266?

blacktrooper2:
Hit there,

I know it's been a while but I'm trying to do the same and having a problem with the software serial.

My ESP8266 works fine alone without serial, as well when it's running software serial without wifi but when I try to combine both, it stops the wifi communication with computer.

Did you had the same thing ?

How is your code?

Are you like sensing with arduino and then sending data to ESP8266 ?

hi, i also get this trouble, someone help me please...
my nodeMcu work fine connect to my database before i use software serial...
but when i try get data from arduino Mega with softwareSerial to nodemcu... softwareSerial work fine but can't send data to my database
when i combine both, i receive

<html>
<head><title>400 Bad Request</title></head>
<body bgcolor="white">
<center><h1>400 Bad Request</h1></center>
<hr><center>nginx/1.10.0 (Ubuntu)</center>
</body>
</html>

1. my arduino mega code

/*
Measuring AC Current Using ACS712
*/
int mVperAmp = 100; // use 185 for 5A module 100 for 20A Module and 66 for 30A Module
int limit = 50;
int CT1 = 600.0 ; 
String ALLIN ;
const int sensorIn1R = A1; const int sensorIn1S = A2; const int sensorIn1T = A3; //RAK 1
const int sensorIn2R = A4; const int sensorIn2S = A5; const int sensorIn2T = A6; //RAK 2
const int sensorIn3R = A7; const int sensorIn3S = A8; const int sensorIn3T = A9; //RAK 3
const int sensorIn4R = A10; const int sensorIn4S = A11; const int sensorIn4T = A12; //RAK 4
const int sensorIn5R = A13; const int sensorIn5S = A14; const int sensorIn5T = A15; //RAK 5

// RAK 1
double Voltage1R = 0; double VRMS1R = 0; double AmpsRMS1R = 0; double calibrasi1R = 0; String SR1 ;
double Voltage1S = 0; double VRMS1S = 0; double AmpsRMS1S = 0; double calibrasi1S = 0; String SS1 ;
double Voltage1T = 0; double VRMS1T = 0; double AmpsRMS1T = 0; double calibrasi1T = 0; String ST1 ;
// RAK 2
double Voltage2R = 0; double VRMS2R = 0; double AmpsRMS2R = 0; double calibrasi2R = 0;
double Voltage2S = 0; double VRMS2S = 0; double AmpsRMS2S = 0; double calibrasi2S = 0;
double Voltage2T = 0; double VRMS2T = 0; double AmpsRMS2T = 0; double calibrasi2T = 0;


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

void loop(){

  
  /*==================RAK 1===================*/
 Voltage1R = getVPP1R();
 VRMS1R = ((Voltage1R/2.0) *0.707106781); 
 AmpsRMS1R = ((VRMS1R * 1000)/mVperAmp)-0.110;
 if (AmpsRMS1R<0.00) AmpsRMS1R = 0.00 ;
 calibrasi1R = (CT1*AmpsRMS1R/5.0);

 Voltage1S = getVPP1S();
 VRMS1S = ((Voltage1S/2.0) *0.707106781); 
 AmpsRMS1S = ((VRMS1S * 1000)/mVperAmp)-0.110;
 if (AmpsRMS1S<0.00) AmpsRMS1S = 0.00 ;
 calibrasi1S = (CT1*AmpsRMS1S/5.0);

 Voltage1T = getVPP1T();
 VRMS1T = ((Voltage1T/2.0) *0.707106781); 
 AmpsRMS1T = ((VRMS1T * 1000)/mVperAmp)-0.110;
 if (AmpsRMS1T<0.00) AmpsRMS1T = 0.00 ;
 calibrasi1T = (CT1*AmpsRMS1T/5.0);

  /*==================RAK 1===================*/
 SR1 = (String) calibrasi1R;
 SS1 = (String) calibrasi1S;
 ST1 = (String) calibrasi1T;

 ALLIN += SR1;
 ALLIN += ",";
 ALLIN += SS1;
 ALLIN += "," ; 
 ALLIN += ST1 ; 
 
Serial.println(ALLIN);
Serial1.println(ALLIN);

 ALLIN ="";
}

  /*==================RAK 1===================*/
/*==================1R===================*/
float getVPP1R()
{
  float result1R;
  float readValue1R;            
  float maxValue1R = 0;          
  float minValue1R = 1024;          
   uint32_t start_time1R = millis();
   while((millis()-start_time1R) < limit) 
   {
       readValue1R = analogRead(sensorIn1R);
       
       if (readValue1R > maxValue1R) 
       {maxValue1R = readValue1R;}
       
       if (readValue1R < minValue1R) 
       {minValue1R = readValue1R;}
   }
   result1R = ((maxValue1R - minValue1R) * 5.0)/1024.0;
   return result1R;
 }
/*==================1S===================*/
 float getVPP1S()
{
  float result1S;
  float readValue1S;             
  float maxValue1S = 0;          
  float minValue1S = 1024;          
   uint32_t start_time1S = millis();
   while((millis()-start_time1S) < limit)
   {
       readValue1S = analogRead(sensorIn1S);
       
       if (readValue1S > maxValue1S) 
       {maxValue1S = readValue1S;}
       
       if (readValue1S < minValue1S) 
       {minValue1S = readValue1S;}
   }
   result1S = ((maxValue1S - minValue1S) * 5.0)/1024.0;
   return result1S;
 }
 /*==================1T===================*/
 float getVPP1T()
{
  float result1T;
  float readValue1T;             
  float maxValue1T = 0;          
  float minValue1T = 1024;          
   uint32_t start_time1T = millis();
   while((millis()-start_time1T) < limit)
   {
       readValue1T = analogRead(sensorIn1T);
       
       if (readValue1T > maxValue1T) 
       {maxValue1T = readValue1T;}
       
       if (readValue1T < minValue1T) 
       {minValue1T = readValue1T;}
   }
   result1T = ((maxValue1T - minValue1T) * 5.0)/1024.0;
   return result1T;
 }

2. my nodeMcu code

#include<SoftwareSerial.h>
#include "StringSplitter.h"
#include <ESP8266WiFi.h>

SoftwareSerial SUART( 4, 5); //SRX  = DPin-2; STX = DPin-3

String ir1 , is1, it1 ;
String data="", tanggal="", aa, bb, cc, dd, ee, ff, gg, hh, ii, jj, kk, ll, mm, nn, oo, pp, qq, rr, ss ;
const char* ssid     = " wifi2";
const char* password = " merdeka ";
const char* host = "microdpl.000webhostapp.com";



void setup() {
  
  WiFi.begin(ssid, password); 
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");  
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
  Serial.print("Netmask: ");
  Serial.println(WiFi.subnetMask());
  Serial.print("Gateway: ");
  Serial.println(WiFi.gatewayIP());

    
  Serial.begin(115200);
  SUART.begin(115200); 

}

void loop() {
serial1 ();
delay (1000);
Serial.print("connecting to ");
  Serial.println(host);

  WiFiClient client;
  const int httpPort = 80;
  if (!client.connect(host, httpPort)) {
    Serial.println("connection failed");
    return;
  }

  String url = "/api/gardu1/insert.php?idmesin=100&vab=" + String (ir1) +"&vbc="+String (is1)+"&vca="+String (it1);
  Serial.print("Requesting URL: ");
  Serial.println(url);
  
  client.print(String("GET ") + url + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" + 
               "Connection: close\r\n\r\n");
  delay(2000);
  
  while(client.available()){
   String line = client.readStringUntil('\r');
   Serial.print(line);
  }
  
  Serial.println();
  Serial.println("closing connection");
  delay(10000);
      data = "";
      
  ir1="";
  is1="";
  it1="";
  data="";


 Serial.println("test");
}
  void serial1 (){
    if(SUART.available()){
      data= SUART.readStringUntil('\n');
      StringSplitter *splitter = new StringSplitter (data, ',', 3);
      int itemCount = splitter->getItemCount();

      ir1 = splitter->getItemAtIndex(0);
      is1 = splitter->getItemAtIndex(1);
      it1 = splitter->getItemAtIndex(2);
      
      /*
      Serial.print(ir1);
      Serial.println(" = ");
      Serial.print(is1);
      Serial.println(" = ");
      Serial.print(it1);
      */
      
      SUART.flush ();
    }
}