Programming ESP by Atmega 2560 serial

Hi,

is it possible to upload sketch to esp d1 through RX TX that connected to Atmega 2560 RX1 TX1? if so how that can be achieved?

best regards Sayed

it is was me... Connect the Board's RESET pin to GND before to attempt to program the ESP + jumper wires from RX->RX1 and TX-> TX1

sherzaad:
it is was me… Connect the Board’s RESET pin to GND before to attempt to program the ESP + jumper wires from RX->RX1 and TX-> TX1

@sherzaad ,he doesn’t ask about Arduino Mega (no need to hold the ATmega in reset. any sketch without Serial.begin() is OK)

@Sayed, maybe you want this?: Site Maintenance

I think we need a detailed explanation of what you're trying to accomplish. Usually when someone asks about this sort of thing they are just trying to use their Arduino board as a USB to TTL serial adapter to program the ESP from the computer. However, you say "esp d1", which makes me think of the WeMos D1 boards that already have a USB to TTL serial adapter on board.

thanks all, actually i need to use WeMos D1 as WiFi for atmega 2560, i have connect rx to tx1 and tx to rx1 of mega, i have solder the WeMos D1 under atmega so it is not possible for me to program it by USB, so i need to use atmega 2560 to program the WeMos D1 through rx tx pins

Juraj: @sherzaad ,he doesn't ask about Arduino Mega (no need to hold the ATmega in reset. any sketch without Serial.begin() is OK)

@Sayed, maybe you want this?: https://forum.arduino.cc/index.php?topic=519181.0

i believed it is what i looks for but little hard to understand, i am trying to understanding.

i have solder the WeMos D1 under atmega so it is not possible for me to program it by USB

That sounds a little silly, i always keep my boards detachable from the circuit using male & female header pins

Juraj: @sherzaad ,he doesn't ask about Arduino Mega (no need to hold the ATmega in reset. any sketch without Serial.begin() is OK)

@Sayed, maybe you want this?: https://forum.arduino.cc/index.php?topic=519181.0

If you could help me dear in this, if i connect the pins that described, then what configuration i should select in arduino ide after uploading the SlaveForEspFirmata.ino to Atmega 2560 and where to put the actual sketch that need to upload to esp thanks again, Best regards, Sayed

Sayed: If you could help me dear in this, if i connect the pins that described, then what configuration i should select in arduino ide after uploading the SlaveForEspFirmata.ino to Atmega 2560 and where to put the actual sketch that need to upload to esp thanks again, Best regards, Sayed

the EspProxy sketch is an enhancement of the SerialPassthrough example. you can start with that. modify the SerialPassthrough sketch to use at least 38400 baud. upload it to Mega and then open the sketch for Wemos, select Wemos and upload to it

where i can find the CH_EN pin in the Wemos D1 mini, i figure out that D3 is for GPIO0 but CH_EN not clear for me

Sayed: where i can find the CH_EN pin in the Wemos D1 mini, i figure out that D3 is for GPIO0 but CH_EN not clear for me

use RST pin (over a level shifter)

i had upload the bellow code to atmega 2560 and connecting the esp pins through level shiftier,
then i set the sketch setting to normal esp programming setting and upload the sketch of esp
but it fails giving this error

warning: espcomm_sync failed
error: espcomm_open failed
error: espcomm_upload_mem failed
error: espcomm_upload_mem failed

atmega sketch

// Sketch to bridge Serial to esp8266 on Serial2.
// Supports esp8266 flashing.
// For the reset of esp to flashing mode CH_EN and GPIO0 of esp must be connected
// to Atmega pins for pulling them LOW (use diodes to prevent accidental 5V output to esp)

#define FLASHING // uncomment for use with esptool and Flash Download Tool, if flashing detection always fails

#if !defined(HAVE_HWSERIAL2)
#include <NeoSWSerial.h>
NeoSWSerial Serial2;
#ifdef FLASHING
#define BAUD 19200
#else
#define BAUD 38400
#endif
#else
#define BAUD 115200
#endif

#define ESP_ENABLE_PIN 45
#define ESP_GPIO0_PIN 44

void resetESP(boolean toBootloader) {
if (toBootloader) {
pinMode(ESP_GPIO0_PIN, OUTPUT);
digitalWrite(ESP_GPIO0_PIN, LOW);
}
digitalWrite(ESP_ENABLE_PIN, LOW);
delay(5);
pinMode(ESP_ENABLE_PIN, INPUT); // let it to pull-up resistor
if (toBootloader) {
delay(100);
pinMode(ESP_GPIO0_PIN, INPUT); // let it to pull-up resistor
}
}

void setup() {
pinMode(LED_BUILTIN, OUTPUT);

Serial.begin(BAUD);

#ifdef FLASHING
digitalWrite(LED_BUILTIN, HIGH);
Serial2.begin(BAUD * 2); //double speed is necessary for stub upload, but it disturbs frequency test of the ‘download tool’
resetESP(true); // reset to bootloader. no need to push the B/L button while connecting to USB
#else
digitalWrite(LED_BUILTIN, LOW);
Serial2.begin(BAUD);
resetESP(false);
#endif
}

void loop() {
while (Serial.available()) {
detectFlashing();
Serial2.write(Serial.read());
}
while (Serial2.available()) {
Serial.write(Serial2.read());
}
}

#ifdef FLASHING
void detectFlashing() {
// empty
}
#else
const byte syncFrame = {0xC0, 0x00, 0x08, 0x24, 0x00, 0xDD, 0x00, 0x00, 0x00, 0x07, 0x07, 0x12, 0x20};
const byte checkSumPos = 5; // esptool.py and FDT do not send the checksum for sync frame, IDE does
byte syncFrameIndex = 0;
boolean detectSyncFrame = true;

void detectFlashing() {
if (!detectSyncFrame)
return;
byte b = Serial.peek();
if (!(b == syncFrame[syncFrameIndex] || (syncFrameIndex == checkSumPos && b == 0x00))) {
syncFrameIndex = 0;
} else {
syncFrameIndex++;
if (syncFrameIndex == sizeof(syncFrame)) {
detectSyncFrame = false;
resetESP(true); // reset to bootloader
digitalWrite(LED_BUILTIN, HIGH);
Serial2.write(syncFrame, sizeof(syncFrame) - 1); // last byte was not read, only peek
}
}
}
#endif

try to reset the Mega before Upload to esp8266. and look at Wemos LEDs if it resets while setup() of the EspProxy sketch runs.

make some tests if the reset works. put some blinking sketch into Wemos and a sketch reseting the Wemos on command from Serial Monitor

i had checked it is not blinking except if reset manually by reset button,
i test this

void setup() {
// put your setup code here, to run once:
pinMode(45, OUTPUT);
digitalWrite(45, LOW);
}

void loop() {
// put your main code here, to run repeatedly:
digitalWrite(45, HIGH);
delay(5000);
digitalWrite(45, LOW);

}

light blinking in esp

Juraj:
try to reset the Mega before Upload to esp8266. and look at Wemos LEDs if it resets while setup() of the EspProxy sketch runs.

make some tests if the reset works. put some blinking sketch into Wemos and a sketch reseting the Wemos on command from Serial Monitor

Sayed: i had checked it is not blinking except if reset manually by reset button, i test this light blinking in esp

make the control of reset and io 0 work. I can't help you with it from here. You can try it without level shifting. The code in Mega doesn't set the pins HIGH. For me the reset worked only with level conversion. Maybe because the pull-up on Wemos is very 'weak' and can't 'hold' both pins. You can try to set the pin to INPUT_PULLUP after reset. The 5 V pull-up shouldn't be a problem for the esp8266 pin. But make sure, you don't set the Mega pins wired to esp8266 HIGH.

Hi,
i notice that when i switch between pins the esp LED blink when i reset atmega 2560
i mean this part the pin numbers
#define ESP_ENABLE_PIN 44
#define ESP_GPIO0_PIN 45
where 44 pin connected to U3 on esp and 45 to RST
uploading setting in the attached photo

the sequence of pin actions in setup() of EspProxy is a reset of esp8266 with io 0 low at boot.
pulling RST low for same milliseconds and then releasing it to let the pull-up resistor pull it HIGH should reset the esp8266.

test it

to get the esp8266 to flashing mode the io 0 must be low at boot. io 0 has a pull-up resistor on Wemos. we pull io 0 LOW while the esp8266 is in reset so after we let the chip boot in goes into flashing mode. the we can let the io 0 go HIGH by the pull-up resistor

test if you can pull-down the io 0 with ATmega. for example with a sketch that lights the LED if io 0 is LOW

thanks very much,
i have the following test,
i change the pins mode to output and pull low then high as your guidance,
and not success initially, but after that i notice that some time the esp led flashing
so i try to hold on atmega reset button then when sketch of esp start to upload i release it
it program successfully,
any way get automaticly?
the following code that i change

void resetESP(boolean toBootloader) {
if (toBootloader) {
pinMode(ESP_GPIO0_PIN, OUTPUT);
digitalWrite(ESP_GPIO0_PIN, LOW);
}
pinMode(ESP_ENABLE_PIN, OUTPUT);
digitalWrite(ESP_ENABLE_PIN, LOW);
delay(5);
digitalWrite(ESP_ENABLE_PIN, HIGH);
if (toBootloader) {
delay(100);
digitalWrite(ESP_GPIO0_PIN, HIGH);
}
}

and full sketch is

// Sketch to bridge Serial to esp8266 on Serial2.
// Supports esp8266 flashing.
// For the reset of esp to flashing mode CH_EN and GPIO0 of esp must be connected
// to Atmega pins for pulling them LOW (use diodes to prevent accidental 5V output to esp)

#define FLASHING // uncomment for use with esptool and Flash Download Tool, if flashing detection always fails

#if !defined(HAVE_HWSERIAL2)
#include <NeoSWSerial.h>
NeoSWSerial Serial2;
#ifdef FLASHING
#define BAUD 19200
#else
#define BAUD 38400
#endif
#else
#define BAUD 115200
#endif

#define ESP_ENABLE_PIN 45
#define ESP_GPIO0_PIN 44

void resetESP(boolean toBootloader) {
if (toBootloader) {
pinMode(ESP_GPIO0_PIN, OUTPUT);
digitalWrite(ESP_GPIO0_PIN, LOW);
}
pinMode(ESP_ENABLE_PIN, OUTPUT);
digitalWrite(ESP_ENABLE_PIN, LOW);
delay(5);
digitalWrite(ESP_ENABLE_PIN, HIGH);
if (toBootloader) {
delay(100);
digitalWrite(ESP_GPIO0_PIN, HIGH);
}
}

void setup() {
pinMode(LED_BUILTIN, OUTPUT);

Serial.begin(BAUD);

#ifdef FLASHING
digitalWrite(LED_BUILTIN, HIGH);
Serial2.begin(BAUD * 2); //double speed is necessary for stub upload, but it disturbs frequency test of the ‘download tool’
resetESP(true); // reset to bootloader. no need to push the B/L button while connecting to USB
#else
digitalWrite(LED_BUILTIN, LOW);
Serial2.begin(BAUD);
resetESP(false);
#endif
}

void loop() {
while (Serial.available()) {
detectFlashing();
Serial2.write(Serial.read());
}
while (Serial2.available()) {
Serial.write(Serial2.read());
}
}

#ifdef FLASHING
void detectFlashing() {
// empty
}
#else
const byte syncFrame = {0xC0, 0x00, 0x08, 0x24, 0x00, 0xDD, 0x00, 0x00, 0x00, 0x07, 0x07, 0x12, 0x20};
const byte checkSumPos = 5; // esptool.py and FDT do not send the checksum for sync frame, IDE does
byte syncFrameIndex = 0;
boolean detectSyncFrame = true;

void detectFlashing() {
if (!detectSyncFrame)
return;
byte b = Serial.peek();
if (!(b == syncFrame[syncFrameIndex] || (syncFrameIndex == checkSumPos && b == 0x00))) {
syncFrameIndex = 0;
} else {
syncFrameIndex++;
if (syncFrameIndex == sizeof(syncFrame)) {
detectSyncFrame = false;
resetESP(true); // reset to bootloader
digitalWrite(LED_BUILTIN, HIGH);
Serial2.write(syncFrame, sizeof(syncFrame) - 1); // last byte was not read, only peek
}
}
}
#endif

Juraj:
the sequence of pin actions in setup() of EspProxy is a reset of esp8266 with io 0 low at boot.
pulling RST low for same milliseconds and then releasing it to let the pull-up resistor pull it HIGH should reset the esp8266.

test it

to get the esp8266 to flashing mode the io 0 must be low at boot. io 0 has a pull-up resistor on Wemos. we pull io 0 LOW while the esp8266 is in reset so after we let the chip boot in goes into flashing mode. the we can let the io 0 go HIGH by the pull-up resistor

test if you can pull-down the io 0 with ATmega. for example with a sketch that lights the LED if io 0 is LOW

try to use the automatic version by commenting out the #define FLASHING

use </> for code not Quote. it is hard to read

i have try but not success
when i open serial i could see command on Arduino ide i can see some characters

Juraj:
try to use the automatic version by commenting out the #define FLASHING

use </> for code not Quote. it is hard to read