I keep getting the same message error "esptool.FatalError: Failed to connect to ESP8266: Timed out waiting for packet header"

So i tried multiple times tries to upload but it keep giving the same error, the nodemcu esp8266 cant even connect. Anyway this is my code, sorry for the bad english.

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
#include <ArduinoJson.h>
const char* s = "Riardu";
const char* p = "arduino6";
String sn = "http://random.skom.id/iot/api/iot?id_perangkat=1";
String so = "http://random.skom.id/iot/api/iot/notif?id_perangkat=1";
double lt = 0;
double td = 3000;
const int pin = 2;
const int pinsensor = 4;
int vibrationSensorState = 0;
void setup() {
  pinMode(pin,OUTPUT);
  pinMode(pinsensor,INPUT);
  digitalWrite(pin,LOW);
  pinMode(LED_BUILTIN, OUTPUT); 
  Serial.begin(115200); 
  WiFi.begin(s, p);
  Serial.println("Connecting");
  while(WiFi.status() != WL_CONNECTED) {
    digitalWrite(LED_BUILTIN, HIGH);
    delay(500);
    digitalWrite(LED_BUILTIN, LOW);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connected to WiFi network with IP Address: ");
  Serial.println(WiFi.localIP());
  Serial.println("Timer set to 5 seconds , it will take 5 seconds before publishing the first reading.");
}

void loop() {
  vibrationSensorState = digitalRead(pinsensor);
//  Serial.println(digitalRead(pinsensor));
//  if(vibrationSensorState==HIGH){
//    Serial.println("Ada getaran cuy");
//  }else{
//    Serial.println("tidak ada getaran");
//  }
//  delay(1000);
  // Send an HTTP POST request depending on td
  if ((millis() - lt) > td) {
    //Check WiFi connection status
    if(WiFi.status()== WL_CONNECTED){
      digitalWrite(LED_BUILTIN, HIGH);
      WiFiClient client;
      HTTPClient http;
      String serverPath = sn;
      // Your Domain name with URL path or IP address with path
      http.begin(client, serverPath.c_str());
      // Send HTTP GET request
      int httpResponseCode = http.GET();
      if (httpResponseCode==200) {
        Serial.print("HTTP Response code: ");
        Serial.println(httpResponseCode);
        String payload = http.getString();
        int str_len = payload.length() + 1; 
        char json[str_len];
        payload.toCharArray(json, str_len);
        Serial.println(json);
        StaticJsonBuffer<200> jsonBuffer;
        JsonObject& root = jsonBuffer.parseObject(json);
        if(!root.success()) {
          Serial.println("parseObject() failed");
//          return false;
        }
        bool xstatus = root["status"];
        if(xstatus){
              int on0 = root["data"]["status"];
              if(on0 == 1){
                digitalWrite(pin,HIGH);
                Serial.println("true");
              }else{
                 digitalWrite(pin,LOW);
                 Serial.println("false");
              }
              int on1 = root["data"]["on1"];
              if(on1 ==1){
                 Serial.println("sensor getar dikirim");
                 if(vibrationSensorState==HIGH){
                    Serial.println("SIAP KIRIM ADA GETARAN");
                    HTTPClient http1;
                    String serverPath1 = so;
                    // Your Domain name with URL path or IP address with path
                    http1.begin(client, serverPath1.c_str());
                    // Send HTTP GET request
                    int httpResponseCode2 = http1.GET();
                    if (httpResponseCode==200) {
                       String response = http1.getString();
                       Serial.println(response);
                    }else{
                       Serial.println(httpResponseCode);
                    }
                }else{
                  Serial.println("TIDAK DIKIRIM TIDAK ADA GETARAN");
                }
              }else{
                Serial.println("sensor getar tidak di kirim");
              }
             
        }else{
            Serial.println("tidak di temukan perangkat");
        }
       
      }
      else {
        Serial.print("Error code: ");
        Serial.println(httpResponseCode);
        digitalWrite(LED_BUILTIN, HIGH);
//        digitalWrite(pin,LOW);
        delay(1000);
        digitalWrite(LED_BUILTIN, LOW);
      }
      // Free resources
      http.end();
    }
    else {
      Serial.println("WiFi Disconnected");
      digitalWrite(LED_BUILTIN, HIGH);
      delay(500);
      digitalWrite(LED_BUILTIN, LOW);
    }
    lt = millis();
  }
}

What type of computer are you using? Windows, mac , ...
What type of IDE?
What port are you using?
Can you add the complete error output in your post?
Did you use a 'full' usb cable (not a usb cable that can only charge...).

Does this mean you don't here the "USB-device-detected" sound?
If you open the device-manager and then unfold the section
connections (COM & LPT)

Do you see an entry similar to "Silicon Labs CP210x" or "USB Serial-CH340" ?
image

Click into the log window that shows the messages while compiling and uploading
press Ctrl-A, Ctrl-C
change focus to browser

insert a code-section by clicking the </> button
then paste clipboard-content into the bluemarked line
best regards Stefan

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