0

I am trying to do Arduino to ESP8266 communication, but I am not able to send data to esp8266. However I can receive data from esp8266.

Here if my Arduino code:

#include <SoftwareSerial.h>
SoftwareSerial ESPserial(0, 1); // RX | TX

void setup()
{
  Serial.begin(115200);     // communication with the host computer
  // Start the software serial for communication with the ESP8266
  ESPserial.begin(9600);

  Serial.println("Enter 1 to turn led high & 2 to turn led low.");
}
// the loop function runs over and over again forever
void loop() {
  // listen for communication from the ESP8266 and then write it to the serial monitor
  if ( ESPserial.available() )   {
    Serial.write( ESPserial.read() );
  }

  // listen for user input and send it to the ESP8266
  if ( Serial.available() )       {
    ESPserial.write( Serial.read() );
  }

}

Here is esp8266 code:

void setup()
{
  Serial.begin(9600);     // communication with the host computer
  // Start the software serial for communication with the ESP8266
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(LED_BUILTIN, OUTPUT);
}
// the loop function runs over and over again forever
void loop() {
  Serial.println("ESP -- Hello");
  delay(1000);
  // listen for user input and send it to the ESP8266
  if ( Serial.available() )  {

    int ledState = Serial.parseInt();
    if (ledState == 1) {
      digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
      Serial.println("ESP Led On\n");
    }
    if (ledState == 2) {
      digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
      Serial.println("ESP Led Off\n");
    }
  }
}

Once I upload both sketches, then I can see it terminal " ESP -- Hello " keep printing comming from esp8266 but when I enter 1 or 2, its not sent to esp8266 due to which I never see "ESP led on/off" messages , so correct me guys what mistake I am making.

  • Don't ever use SoftwareSerial in pins 0 and 1! And never use pins 0 and 1 for anything if you are using Serial for Arduino to PC communication. – Majenko Sep 19 at 19:52
  • @Majenko ok thanks then how can i solve the issue of this communication on UNO i have just these one RX and TX pair it 0,1 pins so should i try the same code on mega2560 – user889030 Sep 20 at 4:17
  • you can use SoftwareSerial on any pair of pins. that is why it is called 'software' Serial – Juraj Sep 20 at 8:43
0

using SoftSerial on the hardware serial isn't working on Arduino properly as sometime data receive and sometime not with garbage added so Using digital pins other 2, & 3 other than hardware serial resolved the issue.

  • 3
    Perhaps you should clarify that using SoftSerial on the hardware serial isn't working, choose any other digital pins than the hardware serial pins. – MatsK Sep 20 at 11:51
  • @MatsK thanks i updated the answer – user889030 Sep 23 at 10:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.