[Solved] ESP-01 and Uno/Mega2560

I have a simple sketch to test an ESP-01 unit. The sketch work fine on a UNO 3 board but does not work on Mega 2560.

Suggestions how to make it work on a Mega 2560?

Output:

20:13:37.807 -> *****************************************************
20:13:37.847 -> ********** Program Start : Connect Arduino WiFi
20:13:37.927 -> Initiate AT commands with ESP8266 
20:13:37.967 -> AT command:AT
20:13:42.967 ->    Result:FAILED

Code:

#include <SoftwareSerial.h>

#define     RX    19    // Uno: 3
#define     TX    18    // Uno: 2
SoftwareSerial AT(RX,TX); 

String ssid     = "foobar";
String password = "xxxxx";
String apiKeyIn = "foo/bar/uno.php";

const unsigned int writeInterval = 25000;

String host = "192.168.2.22";
String port = "80";

boolean AT_cmd_result = false; 

void setup() 
{
  bool status;

  Serial.begin(9600);
  Serial.println(F("*****************************************************"));
  Serial.println(F("********** Program Start : Connect Arduino WiFi"));

  pinMode(RX, INPUT);
  pinMode(TX, OUTPUT);
  AT.begin(9600);

  Serial.println(F("Initiate AT commands with ESP8266 "));
  status = sendATcmd("AT", 5, "OK");
  while (!status) ;
  
  status = sendATcmd("AT+CWMODE=1", 5, "OK");
  while (!status) ;
  
  Serial.print(F("Connecting to WiFi:"));
  Serial.println(ssid);
  status = sendATcmd("AT+CWJAP=\"" + ssid + "\",\"" + password + "\"", 20, "OK");
  if (status) {
    status = sendATcmd("AT+CIPMUX=1", 10, "OK");
  }
  while (!status) ;
}

void loop() 
{
  static int  success = 0, failed = 0;
  bool        status;
  char        buffer[30];
  
  String url = "GET /";
  url += apiKeyIn;
  url += "?v1=";
  url += random(10, 100);
  url += "&v2=";
  url += random(10, 100);
  Serial.println(F("\n\n*****************************************************"));
  Serial.println(F("********** Open TCP connection "));
  
  status = sendATcmd("AT+CIPSTART=0,\"TCP\",\"" + host + "\"," + port, 20, "OK");
  
  if (status) {
    status = sendATcmd("AT+CIPSEND=0," + String(url.length() + 4), 10, ">");
  }

  if (status) {
    Serial.print(F("********** requesting URL: "));
    Serial.println(url);
    sendATcmd(url, 10, "OK");
    delay(2000);
    status = sendATcmd("AT+CIPCLOSE=0", 10, "OK");
  }
  
  Serial.println(F("********** Close TCP Connection "));
  Serial.println(F("*****************************************************"));

  if (status) {
    ++success;
  } else {
    ++failed;
  }
  sprintf(buffer, "OK: %d, Failed: %d", success, failed);
  Serial.println(buffer);
  
  delay(writeInterval);
}

bool sendATcmd(String AT_cmd, int AT_cmd_maxTime, char readReplay[]) 
{
  int   trycnt = 0;
  bool  status = false;
  
  Serial.print(F("AT command:"));
  Serial.println(AT_cmd);

  while (trycnt < AT_cmd_maxTime) {
    AT.println(AT_cmd);
    if (AT.find(readReplay)) {
      AT_cmd_result = true;
      break;
    }
  
    ++trycnt;
  }
  
  Serial.print(F("   Result:"));
  if (true == AT_cmd_result) {
    Serial.println(F("DONE"));
    status = true;
  } else {
    Serial.println(F("FAILED"));
  }
  
  AT_cmd_result = false;

  return status;
}

The Mega has 4 hardware serial ports negating the need to use SoftwareSerial.

.

and SoftwareSerial does work only on some pins of Mega

I checked this page and based on that I changed it to use pin 14 and 15 (instead of 18 and 19), but it still does not work on Mega 2560.

The Mega has 4 hardware serial ports negating the need to use SoftwareSerial.

What does that mean?

thehardwareman:
What does that mean?

I should have written The Mega has 4 hardware serial ports. SoftwareSerial is not needed.

Thanks. I found out about that so by modifying the code it worked. Did not know about Serial1, 2 and 3 until I found an example yesterday evening.