2

In this project I will be using multiple ESP8266 WiFi modules (4) connected by software serial to an Arduino Leonardo Micro (1 UART). I plan to rotate between all 4 (starting with 2): Open port, write, read response, close port, move on....

This is a very barebones version of my code to address this one issue. Works flawlessly with a single (always open) connection, but it does not work when I close Ser1 (even if I don't start Ser2!!!) Is the xxxx.end(); command broken or something? Or am I doing something wrong? :/

Note: This code sends "AT" to the module, it responds with "OK"

When adding Ser2, LED flashes on Arduino periodically indicating it is writing to serial monitor, but nothing appears...

Code:

#include <SoftwareSerial.h>
SoftwareSerial Ser1(10, 8);
SoftwareSerial Ser2(16, 7);
SoftwareSerial Ser3(14, 4);
SoftwareSerial Ser4(15, 2);
//Set up (RX, TX) pins^
void setup() {
 pinMode(10, INPUT);
 pinMode(16, INPUT);
 pinMode(14, INPUT);
 pinMode(15, INPUT);
 pinMode(8, OUTPUT);
 pinMode(7, OUTPUT);
 pinMode(4, OUTPUT);
 pinMode(2, OUTPUT);
 Serial.begin(9600);
 //Set up Serial Monitor on host computer
}
void loop()
{
 delay(1000);
 Ser1.begin(9600); // open Ser1
 Ser1.write( 'A' );
 Ser1.write( 'T' );
 Ser1.write( '\r' );
 Ser1.write( '\n' ); // sends the line: "AT" with appropriate format
 Ser1.listen(); // make sure it's listening
 while ((Ser1.available() > 0)) { //Read entire string
 Serial.write(Ser1.read()); //Dump it to serial monitor
 }
 //Should Respond with "OK"
 Ser1.end(); //close Ser1 port
 //This breaks the code for some reason....
 Serial.println(); // Extra line in Serial Monitor for neatness
 Ser2.begin(9600); // open Ser2
 Ser2.write( 'A' );
 Ser2.write( 'T' );
 Ser2.write( '\r' );
 Ser2.write( '\n' );
 Ser2.listen();
 while ((Ser2.available() > 0)) {
 Serial.write(Ser2.read());
 }
 Ser2.end();
}
asked Jul 29, 2017 at 2:02
5
  • Why are you beginning and ending the object each time instead of just beginning once? Commented Jul 29, 2017 at 2:04
  • @Ignacio, Software serial is only capable of one connection at a time, so it must be closed before I switch to a different port. Problem is my attempt to close it breaks the code. :( Commented Jul 29, 2017 at 4:30
  • 2
    No, it is capable of only receiving on one connection at a time. Commented Jul 29, 2017 at 4:56
  • @IgnacioVazquez-Abrams Do you suggest beginning them all in setup? Then how can I switch the receiving from port to port? Commented Jul 29, 2017 at 5:23
  • 2
    Exactly how you already are. Commented Jul 29, 2017 at 5:24

1 Answer 1

2

As Ignacio mentions, you should not be stopping and starting the serial ports. Instead just switch between them with .listen():

Enables the selected software serial port to listen. Only one software serial port can listen at a time; data that arrives for other ports will be discarded. Any data already received is discarded during the call to listen() (unless the given instance is already listening).

SoftwareSerial::listen() Manual Page

So your code would be written as:

#include <SoftwareSerial.h>
SoftwareSerial Ser1(10, 8);
SoftwareSerial Ser2(16, 7);
SoftwareSerial Ser3(14, 4);
SoftwareSerial Ser4(15, 2);
//Set up (RX, TX) pins^
void setup() {
 Serial.begin(9600);
 Ser1.begin(9600); // open Ser1
 Ser2.begin(9600); // open Ser2 
}
void loop()
{
 delay(1000);
 Ser1.listen(); // Select Ser1 for receiving
 Ser1.print(F("AT\r\n")); 
 delay(1000); // Give it time to send a response or you'll get nothing!
 while ((Ser1.available() > 0)) { //Read entire string
 Serial.write(Ser1.read()); //Dump it to serial monitor
 }
 //Should Respond with "OK"
 Serial.println(); // Extra line in Serial Monitor for neatness
 Ser2.listen(); // Select Ser2 for receiving
 Ser2.print(F("AT\r\n"));
 delay(1000); // Give it time to send a response or you'll get nothing!
 while ((Ser2.available() > 0)) {
 Serial.write(Ser2.read());
 } 
}
answered Jul 29, 2017 at 10:05
1
  • I really appreciate how you fixed my problem AND optimised my code at the same time!! I think the main issue was not waiting for a response (I tried your fix lol). Amazing work!! Commented Jul 29, 2017 at 10:57

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.