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.

VE7JRO
2,51519 gold badges27 silver badges29 bronze badges
asked Sep 19, 2019 at 19:50
3
  • 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. Commented Sep 19, 2019 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 Commented Sep 20, 2019 at 4:17
  • you can use SoftwareSerial on any pair of pins. that is why it is called 'software' Serial Commented Sep 20, 2019 at 8:43

1 Answer 1

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.

answered Sep 20, 2019 at 6:05
1
  • 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. Commented Sep 20, 2019 at 11:51

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.