esptool.FatalError: Failed to connect to ESP8266: Timed out waiting for packet h

I've been trying to upload codes on my nodeMCU ESP8266...I am on my macOS. I keep getting the titled error always. I tried pressing the reset button and the flash button simultaneously...It works sometimes...does not work sometimes. I have my project review coming up very soon. I need a permanent solution to this. Waiting for a solution.

This is the code I've been using:

#include <OneWire.h>
#include <DallasTemperature.h>

#define D3 0 // fan assigning the ESP8266 pin to arduino pin
#define ONE_WIRE_BUS 12 // thermometer

int fanPin = 0;
int dutyCycle = 0;

// Setup a oneWire instance to communicate with OneWire device
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);

void setup() {
  Serial.begin(115200);

  pinMode(fanPin, OUTPUT); // sets the pins as outputs:

  sensors.begin(); // Start up the library

  analogWriteRange(100); // to have a range 1 - 100 for the fan
  analogWriteFreq(10000);
}

float readSensorTemp() {
  sensors.requestTemperatures(); // Send the command to get temperatures
  Serial.print("Temperature: ");  
  Serial.println(sensors.getTempCByIndex(0));
  return sensors.getTempCByIndex(0);
}

void controlFanSpeed (int fanSpeedPercent) {
  Serial.print("Fan Speed: ");
  Serial.print(fanSpeedPercent);
  Serial.println("%");  
  analogWrite(fanPin, fanSpeedPercent); // set the fan speed
}

void loop() {
  float sensorTemp = readSensorTemp(); // Request sensor value

  // Map (change) the sensor reading of <=5 to >=60 to a value between 10 and 100
  int fanSpeedPercent = map(sensorTemp, 5, 60, 10, 100);

  controlFanSpeed (fanSpeedPercent); // Update fan speed
  delay(1000);
}

Do you believe that the uploading problem is in some way caused by your code? Does the upload problem never occurr if you upload the "blink" sketch, for example?

What happens if you make this change?

float readSensorTemp() {
  sensors.requestTemperatures(); // Send the command to get temperatures
  float tempC = sensors.getTempCByIndex(0);
  Serial.print("Temperature: ");  
  Serial.println(tempC);
  return tempC;
}

Which core are you using ? what IDE version ?

I tried pressing the reset button and the flash button simultaneously

make sure you release the reset button first.
Failed to connect actually suggests that it can not find the nodeMCU at all, or that the port the nodeMCU is connected to is otherwise occupied. Make sure the Serial monitor window is closed (and any 3rd party Serial monitors like putty)
Close the IDE, do the reset & flash button thing and re-open it.

Oh and eh... do you have any hardware connected to pin 0 ? The 'flash' button pulls pin 0 'LOW' when you press it so that when you reset the unit, it goes into 'flash-mode' , if you have something connected to GPIO 0, it can not influence the pins state at startup if you want both modes available to you. Sorry about the whole other story, cause this is probably what is wrong. check this reference

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.