Serial Communication Not Working with SUART pins

I am able to use serial communication using the built in rxtx pins on nodemcu and pins 5 and 6 on my arduino uno. Now i want to use the SUART ports, but my data is not printing on the nodemcu's COM port. I dont know what the problem is, please help.

Hello, it's clean up day and our crystal balls are in the dishwasher...

do yourself a favour and please read How to get the best out of this forum and provide way more information (code (using code tags), circuit, power, ...)

Hi sorry, my apologies. my code is here:

for arduino:

#include <SoftwareSerial.h>
#define SAMPLES 300   //Number of samples you want to take everytime you loop
#define ACS_Pin A1    //ACS712 data pin analong input

float High_peak,Low_peak;         //Variables to measure or calculate
float Amps_Peak_Peak, Amps_RMS;
float p = 0;
float ACS_Value;
SoftwareSerial espSerial(5,6);

String str;
String s;
void setup(){
Serial.begin(9600);
espSerial.begin(9600);
pinMode(A0,INPUT); // set pin a0 as input pin
pinMode(ACS_Pin,INPUT);
delay(2000);
}


  uint16_t get_max() {
  uint16_t max_v = 0;
  for(uint8_t i = 0; i < 100; i++) {
    uint16_t r = analogRead(A0);  // read from analog chan  nel 3 (A0)
    //Serial.println(r);
    //Serial.println(r);
    //Serial.println("---------------"+i);
    if(max_v < r) max_v = r;
    //if(isdigit(r) &&  max_v < r) max_v = r;
    delayMicroseconds(200);
   
  }
  //Serial.print ("Former Voltage"+ max_v); 
  return max_v;
  }


   






void loop()
{

        char buf[10];
       // get amplitude (maximum - or peak value)
        uint32_t v = get_max();
       //Serial.print ("Former Voltage ");
       //Serial.print (v); 
       // get actual voltage (ADC voltage reference = 1.1V)
       v = v * 1100/1023;
      // calculate the RMS value ( = peak/√2 )
      v /= sqrt(2);

      
     
      read_Amps();                               //Launch the read_Amps function
      Amps_RMS = Amps_Peak_Peak*0.3536*0.06;  
      
      p = v*Amps_RMS;
  
      //Serial.print("   ac voltage  ") ; // specify name to the corresponding value to be printed
      //Serial.print(ACS_Value);
      //Serial.print(v) ; // prints the ac value on Serial monitor
      //Serial.print(",");
      //Serial.print(Amps_RMS); 
      //Serial.print(",");
      //Serial.print(p);//Here I show the RMS value and the peak to peak value, you can print what you want and add the "A" symbol...
      //Serial.print("\t");
     

      Serial.println();

 


     
     str = String(p) + String (",")+ String(v);
     //s = str;
     Serial.print(str);
     espSerial.println(str);
     delay(100);

}





void read_Amps()            //read_Amps function calculate the difference between the high peak and low peak 
{                           //get peak to peak value
  int cnt;            //Counter
  High_peak = 0;      //We first assume that our high peak is equal to 0 and low peak is 1024, yes inverted
  Low_peak = 1024;
  
      for(cnt=0 ; cnt<SAMPLES ; cnt++)          //everytime a sample (module value) is taken it will go through test
      {
         ACS_Value = analogRead(ACS_Pin); //We read a single value from the module

        
        if(ACS_Value > High_peak)                //If that value is higher than the high peak (at first is 0)
            {
              High_peak = ACS_Value;            //The high peak will change from 0 to that value found
            }
        
        if(ACS_Value < Low_peak)                //If that value is lower than the low peak (at first is 1024)
            {
              Low_peak = ACS_Value;             //The low peak will change from 1024 to that value found
            }
      }                                        //We keep looping until we take all samples and at the end we will have the high/low peaks values
      
  Amps_Peak_Peak = High_peak - Low_peak;      //Calculate the difference
}

Nodemcu code:

#include <SoftwareSerial.h>

SoftwareSerial espSerial(2,1);

#include <SafeString.h>
#include <String.h>
String p;
int v= 0;
String first_token;
String second_token;
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
}
void loop() { // run over and over
if (Serial.available()) {
  p = Serial.readStringUntil('\n'); 
  if(p.length() > 0){
    int index = p.indexOf(',');
    first_token = p.substring(0,index);
    second_token =  p.substring(index+1,p.length());
    if(first_token.toFloat()> 5 &&  first_token.toFloat()< 13 && second_token.toFloat()>60 && second_token.toFloat() < 123){
      Serial.print(first_token);
      Serial.print(",");
      Serial.print(second_token);
      Serial.println();
      Serial.flush();
    }
  delay(200);
  }
  
}

  

}

I should be getting this:

But I'm getting a blank on my nodemcu serial monitor.

I've not looked at the code yet - how are the units connected? (one is a 5V device and the other a 3.3V one)

I just connect them directly, since I don't need to use them for long.

so maybe you killed the Rx pin on your ESP... You should at least provide a voltage divider with 2 resistor.

try this code to see if things are broken

Arduino:

#include <SoftwareSerial.h>
SoftwareSerial espSerial(5,6);

void setup() {
  Serial.begin(115200);
  espSerial.begin(9600);
}

void loop() {
  if (Serial.available())  espSerial.write(Serial.read());
}

Nodemcu

#include <SoftwareSerial.h>
SoftwareSerial espSerial(2,1);

void setup() {
  Serial.begin(115200);
  espSerial.begin(9600);
}

void loop() {
  if (espSerial.available()) Serial.write(espSerial.read());
}

open two separate terminal window at 115200 bauds connected to the two MCUs and what you type on the Arduino should appear on the NodeMCU's terminal

to your code, I would suggest to study Serial Input Basics to handle this

Like I said, I was using the Usb UART pins before, and they were working fine. Now i just want to transfer the data using a different set of pins on the nodemcu, but they aren't working at all. I can achieve serial communication with the rx tx pins, but not anything else.

did the code provided above led to anything? how have you wired pins 5/6 to 2/1 ? did you connect the GND?
➜ as mentioned post a schematic of your circuit

I uploaded both your sketches onto both my devices, and I'm not seeing anything, I don't know the result I'm supposed to get.

➜ take a pen, a ruler and a piece of paper and draw the circuit then post a picture of the drawing... the picture is hard to read.

➜ did you do this?

I'm supposed to use both your sketches at 115200? Because I've heard that this will work only at 9600, because i'm trying to make the data sent to nodemcu without a usb.

Inkedschematic_forum

i sent you that pic to ensure my nodemcu pins were connected correctly. I used different coloured wires and everything. Sorry.

this is the Serial terminal with your computer, it will work fine at 115200 bauds - could go way higher
the SoftwareSerial connection is at 9600 bauds (in between the 2 boards)

--

disconnect anything that is not related to the Serial connection for the time being. Keep only the USB cable, the GND connection, and the virtual Rx to Tx and Tx to Rx (ideally add resistor divider to protect the board)

So I use my original code on arduino uno, then use your sketch for nodemcu?

No use both my codes.

disconnect anything that is not related to the Serial connection for the time being. Keep only the USB cable, the GND connection, and the virtual Rx to Tx and Tx to Rx (ideally add resistor divider to protect the board)

Done. Both monitors are blank.

type something in the Arduino monitor
if all is wired correctly and the Rx pin on the ESP not dead, then you should see the message appear in the terminal window of the NodeMCU

pins 5/6 needs to be connected to pin D2/D1 according to the code (ideally with the resistors voltage divider if it's not too late)

I don't see anything, but how come my rxtx pins were working then?

i tweaked the code to print, but im getting this on my uno but nothing on my nodemcu.

what did you tweak?

-1 means nothing to read