user38107: I am sorry for posting this question for the nth time but I am still not able to find a solution for this error.
I am trying to upload the WiFiScan(Arduino IDE example program)program code to ESP8266 using Arduino Mega 2560. I followed the procedure given here. And I tried uploading it with "Generic ESP8266 Module" and "Adafruit HUZZAH ESP8266" selected as the Board. But I am still getting the following error.
trying to connect
  espcomm_send_command: sending command header
  espcomm_send_command: sending command payload
warning: espcomm_sync failed
error: espcomm_open failed
error: espcomm_upload_mem failed

Edited to add:

Connections-
ESP8266 - Arduino Mega2560
Tx - Tx1(Pin 18)
Rx - Rx1(Pin 19)
Gnd - Gnd
Vcc - 3.3V (Directly from Arduino's 3.3V pin)
CH_PD - 3.3V (Directly from Arduino's 3.3V pin)
GPIO0 - Push button K2 (refer photo)
RST - Push button K1
While pressing the K2 button(Flash button - GPIO0), I clicked the K1 button(Reset button - RST) and then released K2. According to the document, this should bring the ESP8266 to flash mode. Now when I upload the code, it throws the above error.

user38107: After googling for several hours, I was able to figure out the reason. Thanks to this link. Transferring data to ESP8266 with Arduino acting as USB to Serial converter did not work. Using a USB to TTL converter, and following this link you can easily transfer data to ESP8266.

38107: <p>I am sorry for posting this question for the nth time but I am still not able to find a solution for this error.</p>

<p>I am trying to upload the WiFiScan(Arduino IDE example program)program code to ESP8266 using Arduino Mega 2560. I followed the procedure given <a href="https://diyhacking.com/esp8266-tutorial/" rel="nofollow noreferrer">here</a>. And I tried uploading it with "Generic ESP8266 Module" and "Adafruit HUZZAH ESP8266" selected as the Board. But I am still getting the following error.</p>

<pre><code>trying to connect
  espcomm_send_command: sending command header
  espcomm_send_command: sending command payload
warning: espcomm_sync failed
error: espcomm_open failed
error: espcomm_upload_mem failed
</code></pre>

<p>Edited to add:
<a href="https://i.stack.imgur.com/PT0k9.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/PT0k9.jpg" alt="enter image description here"></a></p>

<p><strong>Connections-</strong></p>

<p><strong>ESP8266 - Arduino Mega2560</strong></p>

<p>Tx - Tx1(Pin 18)</p>

<p>Rx - Rx1(Pin 19)</p>

<p>Gnd - Gnd</p>

<p>Vcc - 3.3V (Directly from Arduino's 3.3V pin)</p>

<p>CH_PD - 3.3V (Directly from Arduino's 3.3V pin)</p>

<p>GPIO0 - Push button K2 (refer photo)</p>

<p>RST - Push button K1</p>

<p>While pressing the K2 button(Flash button - GPIO0), I clicked the K1 button(Reset button - RST) and then released K2. According to the document, this should bring the ESP8266 to flash mode. Now when I upload the code, it throws the above error.</p>
 38107: <p>After googling for several hours, I was able to figure out the reason. Thanks to this <a href="https://github.com/esp8266/Arduino/issues/1207" rel="nofollow noreferrer">link</a>. Transferring data to ESP8266 with Arduino acting as USB to Serial converter did not work. Using a USB to TTL converter, and following this <a href="https://diyhacking.com/esp8266-tutorial/" rel="nofollow noreferrer">link</a> you can easily transfer data to ESP8266.</p>
