programming a Mega +WiFi R3 Atmega2560+NodeMCU ESP8266

Hello All,

We recently brought Mega with Wifi board available in https://robu.in/product/wemos-mega-wifi-r3-atmega2560nodemcu-esp8266-32mb-memory-usb-ttl-ch340g-compatible-for-arduino-mega/

My goal is to send data read by arduino mega to local server through ESP8266, I modified the code available in forum done by solarsevand settings done as per theri instructions https://forum.arduino.cc/index.php?topic=464400.15

Im attaching modified code

String ssid="xxxx";                     // key element: Wifi network SSID
String password ="zzz";               // key element: Wifi network password




String str;
boolean DEBUG=true;



void showResponse(int waitTime){
    long t=millis();
    char c;
    while (t+waitTime>millis()){
      if (Serial3.available()){
        c=Serial3.read();
        if (DEBUG) Serial.print(c);
      }
    }                   
}


void setup() {                
  DEBUG=true;                           // enable debug serial

  Serial.begin(9600);                 // key element: enable serial to see program action in serial monitor
  Serial3.begin(115200);                // key element: enable serial3; printing to Serial3 sends information directly to ESP8266
  
  //Serial3.println("AT+RST");          // if needed, Enable this AT command line to reset the module;
  //showResponse(1000);

  //Serial3.println("AT+UART_CUR=9600,8,1,0,0");    // if needed, Enable this AT command line to set esp8266 serial speed to 9600 bps
  //showResponse(1000);
  
 
  Serial3.println("AT+CWMODE=1"); 
  
  showResponse(5000);
  if (DEBUG) Serial.println("AT+CWMODE=1");


  Serial3.println("AT+CWJAP=\""+ssid+"\",\""+password+"\"");  // key element: use this AT command to join network using SSID and password
  showResponse(1000);
  if (DEBUG) Serial.println("AT+CWJAP=\""+ssid+"\",\""+password+"\"");
  if (DEBUG)  Serial.println("Setup completed");
}



void loop() {

int sensorValue = analogRead(A0);
  // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
  float voltage = sensorValue * (5.0 / 1023.0);
  // print out the value you read:
  



// start TCP connection
  String cmd = "AT+CIPSTART=\"TCP\",\"";             //key element: set up this AT command to start connection
  cmd += "172.18.20.27";                          // api.thingspeak.com
  cmd += "\",80";                                    // port number
  Serial3.println(cmd);                              //Key element: print AT command to ESP8266, which is on Serial3
  if (DEBUG) Serial.println(cmd);
  if(Serial3.find("Error")){
    if (DEBUG) Serial.println("AT+CIPSTART error");
    return false;
  }


  String getStr = "GET /--/--/--/----.php";   
  getStr +="?c=";
  getStr += String(voltage);
  getStr +="&h=";
  getStr += String(sensorValue);
  getStr +=" HTTP/1.1\r\n";
  getStr +="Host: 172.18.20.27\r\n";
  getStr +="Connection: close\r\n\r\n";

  
  cmd = "AT+CIPSEND=";                        //key element: setup for sending string length
  cmd += String(getStr.length());
  Serial3.println(cmd);                       //key element: send AT string length command to ESP8266 on Serial3
  if (DEBUG)  Serial.println(cmd);  
  delay(100);

// Write sensor values to thingspeak
//  if(Serial3.find(">")){                    //this if statement didn't work for me; manually sending precceding AT commands in serial monitor yielded the ">", but this if statement didn't read it
    Serial3.print(getStr);                    //key element: send GET command (which includes data) to ESP8266 on Serial3
    delay(1000);
    if (DEBUG)  Serial.print(getStr);
  
// thingspeak needs 15+ sec delay between updates,     
  delay(1000);  
}

with the above code im not able to connect to my local server. Im attaching serial monitor output, Please let me know where im going wrong

if you have AT 1.7 (SDK 3) or AT 2.1 (IDF) you can use my WiFiEspAT library

you can't print the cmd right after CIPSEND. you have to wait for prompt >

I flashed the firmware in esp8266 but in serial monitor following error is displayed

ets Jan 8 2013,rst cause:4, boot mode:(1,6)

reva23:
I flashed the firmware in esp8266 but in serial monitor following error is displayed

ets Jan 8 2013,rst cause:4, boot mode:(1,6)

switch 7 off. you have still the flashing mode on. (the 1 in boot mode tells me that)

Sir, that means the following switch configuration has to be made for flashing the firmware to esp8266

1=2=3=4=7=OFF and 5=6=ON

reva23:
Sir, that means the following switch configuration has to be made for flashing the firmware to esp8266

1=2=3=4=7=OFF and 5=6=ON

for flashing 7 has to be ON. for running OFF. it is the io 0 boot configuration pin of the esp8266

I followed your instruction
Firmware Flashing: 5=6=7=ON
ESP8266 connect to serial monitor: 5=6=ON and 7=OFF

but the putty prints following error:

ets Jan 8 2013,rst cause:4, boot mode:(3,7)

I have not loaded any sketch in esp8266. Loaded the firmware and connected to serial monitor to check the response for AT commands

bootloader.png

bootloader.png

try to add to add --flash_mode dout before --flash_size when flashing AT firmware

I set Flash mode to DIO but during flashing time it automatically changes to QIO.

Still same error. attaching the serial error output screenshot.

reva23:
I set Flash mode to DIO but during flashing time it automatically changes to QIO.

Still same error. attaching the serial error output screenshot.

what AT firmware version do you upload? and copy here the exact esptool command line please

I hope attached screenshot will serve the purpose

you should use esptool.
if you want to use the FDT, then uncheck SpiAutoSet and select DIO.
and it is simpler to download the AT firmware here https://www.espressif.com/en/support/download/at?keys=&field_type_tid[]=14
download 1.7.4 (it is from SDK version 3.0.4)

I downloaded "ESP8266 NonOS AT Bin V1.7.4"version firmware and uploaded using FDT as per the settings suggested. But still Serial monitor response is not proper. Im attaching the screenshot Pls have a look

reva23:
I downloaded "ESP8266 NonOS AT Bin V1.7.4"version firmware and uploaded using FDT as per the settings suggested. But still Serial monitor response is not proper. Im attaching the screenshot Pls have a look

this is the correct output. the garbage is bootlog at different baud rate. ignore it.
"ready" is from AT firmware.

now set the newline characters in the bottom right corner of Serial Monitor to "Both CR&LF" and send AT

Thanks for your patience,

Now esp8266 is responding to the AT commands and im attaching the serial monitor screenshot.

String ssid="xxxx";                     // key element: Wifi network SSID
String password ="xxxx";               // key element: Wifi network password
          



boolean DEBUG=true;

//======================================================================== showResponse 
// solarsev doesn't know if this is a key element in this form (vs delay(1000))...

void showResponse(int waitTime){
    long t=millis();
    char c;
    while (t+waitTime>millis()){
      if (Serial3.available()){
        c=Serial3.read();
        if (DEBUG) Serial.print(c);
      }
    }                 
}

//================================================================================ setup
void setup() {                
  DEBUG=true;                           // enable debug serial

  Serial.begin(9600);                 // key element: enable serial to see program action in serial monitor
  Serial3.begin(115200);                // key element: enable serial3; printing to Serial3 sends information directly to ESP8266
//  showResponse(100);
  Serial.println("Next");
  Serial3.println("AT");
  Serial3.println("AT+RST");          // if needed, Enable this AT command line to reset the module;
 showResponse(1000);


// set esp8266 as client
  Serial3.println("AT+CWMODE=1");
  showResponse(5000);
//  
//  // key element: use this AT command to put ESP8266 into client mode
//  showResponse(1000);
if (DEBUG) Serial.println("AT+CWMODE=1");  
// 
//
//// connect to wifi
  Serial3.println("AT+CWJAP=\""+ssid+"\",\""+password+"\"");  // key element: use this AT command to join network using SSID and password
  
if (DEBUG) Serial.println("AT+CWJAP=\""+ssid+"\",\""+password+"\"");
if (DEBUG)  Serial.println("Setup completed");
}

//void loop(){}
// ====================================================================== loop
void loop() {

  int sensorValue = analogRead(A0);
  // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
  float voltage = sensorValue * (5.0 / 1023.0);
  // print out the value you read:
  //str = String (voltage) + String(",")+ String (sensorValue)+ String(",")+ String ("1");
  



// start TCP connection
  String cmd = "AT+CIPSTART\"TCP\",\"";             //key element: set up this AT command to start connection
//  String cmd = "30";
  cmd += "172.18.20.27";                          // api.thingspeak.com
  cmd += "\",80";                                    // port number
  Serial3.println(cmd);                              //Key element: print AT command to ESP8266, which is on Serial3
  showResponse(1000);
  if (DEBUG) Serial.println(cmd);
  if(Serial3.find("Error")){
    if (DEBUG) Serial.println("AT+CIPSTART error");
    return false;
  }
//
// prepare GET string for data upload to Thingspeak   //key element: string must match Thingspeak specified format
  String getStr = "GET /xxx.php";   
  //getStr += apiKey;
  getStr +="?c=";
  getStr += String(voltage);
  getStr +="&h=";
  getStr += String(sensorValue);
  getStr +=" HTTP/1.1\r\n";
  getStr +="Host: 172.18.20.27\r\n";
  getStr +="Connection: close\r\n\r\n";

  // send data length to Thingspeak
  cmd = "AT+CIPSEND=";                        //key element: setup for sending string length
  cmd += String(getStr.length());
  Serial3.println(cmd);                       //key element: send AT string length command to ESP8266 on Serial3
  if (DEBUG)  Serial.println(cmd);  
  delay(100);

// Write sensor values to thingspeak
//  if(Serial3.find(">")){                    //this if statement didn't work for me; manually sending precceding AT commands in serial monitor yielded the ">", but this if statement didn't read it
    Serial3.print(getStr);                    //key element: send GET command (which includes data) to ESP8266 on Serial3
    delay(1000);
    if (DEBUG)  Serial.print(getStr);
  
// thingspeak needs 15+ sec delay between updates,     
  delay(1000);  
}

I loaded the above code in special mode i.e 1=2=3=4=ON and remaining in OFF condition and set RXD/TXD switch to 3 and selected board as Ärduino Mega.

Serial output shows the esp8266 is connected wifi but it is not sending data to local server.

I recommend you to use my WiFiEspAT library

I already installed this library.

I tried Your “check firmware.ino” sketch but the serial monitor prints “Communication with WiFi module failed!”

DIP switch status are 1=2=3=4=ON and rest are OFF and in arduino IDE, I selected Arduino Mega.

Im attaching modified code:

/*
  This sketch checks the version of the AT firmware in attached esp8266.

  created in Jul 2019 for WiFiEspAT library
  by Juraj Andrassy https://github.com/jandrassy

*/

#include <WiFiEspAT.h>

// Emulate Serial1 on pins 6/7 if not present
#if defined(ARDUINO_ARCH_AVR) && !defined(HAVE_HWSERIAL3)
//#include <SoftwareSerial.h>
//SoftwareSerial Serial1(6, 7); // RX, TX
#define AT_BAUD_RATE 115200
#else
#define AT_BAUD_RATE 115200
#endif

void setup() {

  Serial.begin(115200);
  while (!Serial);

  Serial3.begin(AT_BAUD_RATE);
  WiFi.init(Serial3);

  if (WiFi.status() == WL_NO_MODULE) {
    Serial.println("Communication with WiFi module failed!");
    // don't continue
    while (true);
  }

  char ver[10];
  int major = 0;
  int minor = 0;
  if (WiFi.firmwareVersion(ver)) {
    Serial.print("AT firmware version ");
    Serial.println(ver);
    char* tok = strtok(ver, ".");
    major = atoi(tok);
    tok = strtok(NULL, ".");
    minor = atoi(tok);
    if (major == 2 && minor == 0) {
      Serial.println("AT firmware version 2.0 doesn't support passive receive mode and can't be used with the WiFiEspAt library");
    } else if (major < 1 || (major == 1 && minor < 7)) {
      Serial.println("WiWiEspAT library requires at least version 1.7.0 of AT firmware (but not 2.0)");
    } else {
      Serial.println("AT firmware is OK for the WiFiEspAT library.");
#ifdef WIFIESPAT1
      if (major > 1) {
        Serial.println("For AT firmware version 2 comment out #define WIFIESPAT1 in EspAtDrvTypes.h");
      }
#else
      if (major == 1) {
        Serial.println("For AT firmware version 1 add #define WIFIESPAT1 in EspAtDrvTypes.h");
      }
#endif
    }
  } else {
    Serial.println("Error getting AT firmware version");
  }


}

void loop() {
}

reva23:
I already installed this library.

I tried Your “check firmware.ino” sketch but the serial monitor prints “Communication with WiFi module failed!”

DIP switch status are 1=2=3=4=ON and rest are OFF and in arduino IDE, I selected Arduino Mega.

your sketch with AT commands works a the WiFiEspAT example doesn’t? there is no difference in this

please turn on debug log.

I doubt whether its working with my sketch because serial monitor doesn't print OK as response for AT commands.