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();
}