Hi All,
I'm baffled here and thought I'd get out to the community and ask. I should prefix this post and say that I think the hardware on my project is fine because if I upload a blank sketch and do serial passthrough on the hardware serial from the terminal, it works as intended (remembering to 'cross the streams' when using this method).
I'm connecting an ESP-01 Wifi module to an Arduino Uno. Pins 2,3 are software serial to the device. Plenty of power is supplied to the device using a 3.3v LM3940 regulator and CH_PD is pulled high. When i transmit the first command to the ESP, it returns the response as would be expected. However, when i repeat the same command immediately after (by duplicating the same code_, nothing happens...ever. Doesn't matter what command, whether i've coded it into flash, or used the terminal relaying the data between hardware and software ports.
The code is below. I've simplified it removing all the unrelated bits. I've placed all the code in the setup area as i'm not concerned with looping in this demonstration. In my main program i send a command in the setup section and let the serial reflector/relay in the loop work to pass the data back to the terminal via the serial port. In this case, the first command in the setup section works, as does the reflector back to the terminal, and then that's it. No other commands sent from the program or terminal work. Yes, there are buffer overruns in the code below, but the same behaviour occurs in the main program where there isn't any that I'm aware of.
#include <SoftwareSerial.h>
SoftwareSerial device(2,3); // Rx, Tx
void setup() {
Serial.begin(9600); // Serial port for connection to host
device.begin(9600); // Serial port for connection to serial device
while (!Serial); // wait for serial ports to connect. Needed for native USB port only
// THIS CODE SNIPPET DOES WORK
device.println("AT+GMR");
Serial.println("AT+GMR command FIRST attempt, sent to ESP, waiting 3s...");
delay(3000); // THIS DELAY CAUSES BUFFER OVERRUN, BUT NOT THE ISSUE
if(device.available()){
Serial.println("Message received from ESP, it follows:");
Serial.println("--------------------------------------");
while(device.available())
{
Serial.write(device.read());
}
Serial.println("\r\n--------------------------------------");
Serial.println("Read of ESP AT+GMR No.1 completed.\r\n\r\n");
} else {
Serial.println("\r\nERROR! No response received!\r\n\r\n");
}
// THIS DUPLICATE CODE SNIPPET DOES NOT WORK BECAUSE IT'S NOT THE FIRST TIME
device.println("AT+GMR");
Serial.println("AT+GMR command SECOND attempt, sent to ESP, waiting 3s...");
delay(3000); // THIS DELAY CAUSES BUFFER OVERRUN, BUT NOT THE ISSUE
if(device.available()){
Serial.println("Message received from ESP, it follows:");
Serial.println("--------------------------------------");
while(device.available())
{
Serial.write(device.read());
}
Serial.println("\r\n--------------------------------------");
Serial.println("Read of ESP AT+GMR No.2 completed.\r\n\r\n");
} else {
Serial.println("\r\nERROR! No response received!\r\n\r\n");
}
delay(5000);
Serial.println("Exiting Setup");
}
void loop() {
}
The response I get in the terminal using the above code is below. It just doesn't make sense.
AT+GMR command FIRST attempt, sent to ESP, waiting 3s...
Message received from ESP, it follows:
AT+GMR
AT version:1.1.0.0(May 11 2016 18:09:56)
SDK version:
Read of ESP AT+GMR No.1 completed.
AT+GMR command SECOND attempt, sent to ESP, waiting 3s...
ERROR! No response received!
Exiting Setup
I've even done the serial reflector and this doesn't do anything on the terminal. I find, serial communication only works on the first use in the setup section
/**
* SerialReflector
*/
#include <SoftwareSerial.h>
SoftwareSerial device(2,3); // Rx, Tx
void setup() {
Serial.begin(9600); // Serial port for connection to host
device.begin(9600); // Serial port for connection to serial device
}
void loop() {
if(device.available())
{
Serial.write(device.read());
}
if(Serial.available())
{
device.write(Serial.read());
}
}
If anyone can provide some advice, I would greatly appreciate it!
Thanks!
Rob