Moin zusammen,
ich hab mir folgende Dinge organisiert und wollte den folgenden Code hochladen, aber bekomme dann eine Fehlermeldung, die ich nicht verstehe
#include <ESP8266WiFi.h>
#include <DHT.h>
// -------------------- USER SETTINGS --------------------
const char* ssid = "privat"; // <-- replace with your WiFi SSID
const char* password = "privat"; // <-- replace with your WiFi password
// ThingSpeak settings
const char* server = "api.thingspeak.com";
const char* writeAPI = "privat"; // <-- replace with your ThingSpeak WRITE API key
// Map your fields in the channel: Field1 = Temperature (°C), Field2 = Humidity (%RH)
// Sensor settings
#define DHTPIN 2 // GPIO0 (NodeMCU pin label: D3)
#define DHTTYPE DHT22
// ------------------------------------------------------
DHT dht(DHTPIN, DHTTYPE);
WiFiClient client;
// Upload every ≥15 s. Use 20 s for safety.
const unsigned long UPLOAD_INTERVAL_MS = 20000;
unsigned long lastUpload = 0;
void connectWiFi() {
Serial.print("Connecting to WiFi: ");
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
// Wait for connection
uint8_t tries = 0;
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print('.');
if (++tries >= 60) { // ~30 seconds timeout
Serial.println("\nWiFi connect timeout. Restarting...");
ESP.restart();
}
}
Serial.println("\nWiFi connected!");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
void setup() {
Serial.begin(115200);
delay(50);
Serial.println();
Serial.println("DHT11 + NodeMCU → ThingSpeak");
dht.begin();
connectWiFi();
}
bool postToThingSpeak(float temperatureC, float humidity) {
// Build URL-encoded POST body. API key can go in header (X-THINGSPEAKAPIKEY)
// or as field "api_key" in the body. We’ll send it in the header and keep
// the body to fields only.
String postBody = "field1=" + String(temperatureC, 1) + "&field2=" + String(humidity, 1);
if (!client.connect(server, 80)) {
Serial.println("Connection to ThingSpeak failed.");
return false;
}
// HTTP request
client.print(String("POST /update HTTP/1.1\r\n") +
"Host: " + String(server) + "\r\n" +
"Connection: close\r\n" +
"X-THINGSPEAKAPIKEY: " + String(writeAPI) + "\r\n" +
"Content-Type: application/x-www-form-urlencoded\r\n" +
"Content-Length: " + String(postBody.length()) + "\r\n\r\n" +
postBody + "\r\n");
// Read response (optional but useful for debugging)
unsigned long start = millis();
while (client.connected() && millis() - start < 3000) {
while (client.available()) {
String line = client.readStringUntil('\n');
// Uncomment to see entire response:
// Serial.println(line);
// ThingSpeak returns an integer line with the entry number if successful
if (line.length() == 1 && line[0] == '\r') {
// blank line before body
String body = client.readString();
Serial.print("ThingSpeak response (entry ID or 0): ");
Serial.println(body);
break;
}
}
}
client.stop();
return true;
}
void loop() {
// Respect ThingSpeak & DHT11 timing
if (millis() - lastUpload < UPLOAD_INTERVAL_MS) {
delay(50);
return;
}
// Read sensor
float h = dht.readHumidity();
float t = dht.readTemperature(); // Celsius
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT11. Retrying next cycle...");
lastUpload = millis(); // still wait until next interval
return;
}
Serial.print("Temperature: ");
Serial.print(t, 1);
Serial.print(" °C, Humidity: ");
Serial.print(h, 1);
Serial.println(" %RH");
// Upload to ThingSpeak
if (postToThingSpeak(t, h)) {
Serial.println("Update sent to ThingSpeak.");
} else {
Serial.println("Update failed.");
}
lastUpload = millis();
}
Arduino: 1.8.19 (Windows Store 1.8.57.0) (Windows 10), Board: "Generic ESP8285 Module, 80 MHz, Flash, Legacy (new can return nullptr), All SSL ciphers (most compatible), dtr (aka nodemcu), 26 MHz, 1MB (FS:64KB OTA:~470KB), 2, v2 Lower Memory, Disabled, None, Only Sketch, 115200"
Executable segment sizes:
IROM : 246484 - code in flash (default or ICACHE_FLASH_ATTR)
IRAM : 27616 / 32768 - code in IRAM (ICACHE_RAM_ATTR, ISRs...)
DATA : 1268 ) - initialized variables (global, static) in RAM/HEAP
RODATA : 1292 ) / 81920 - constants (global, static) in RAM/HEAP
BSS : 25232 ) - zeroed variables (global, static) in RAM/HEAP
Der Sketch verwendet 276660 Bytes (28%) des Programmspeicherplatzes. Das Maximum sind 958448 Bytes.
Globale Variablen verwenden 27792 Bytes (33%) des dynamischen Speichers, 54128 Bytes fĂĽr lokale Variablen verbleiben. Das Maximum sind 81920 Bytes.
esptool.py v2.8
Serial port COM8
Connecting........_____....._____....._____....._____....._____....._____.....____Traceback (most recent call last):
File "C:\Users\Natalie\Documents\ArduinoData\packages\esp8266\hardware\esp8266\2.7.4/tools/upload.py", line 65, in <module>
esptool.main(cmdline)
File "C:/Users/Natalie/Documents/ArduinoData/packages/esp8266/hardware/esp8266/2.7.4/tools/esptool\esptool.py", line 2890, in main
esp.connect(args.before)
File "C:/Users/Natalie/Documents/ArduinoData/packages/esp8266/hardware/esp8266/2.7.4/tools/esptool\esptool.py", line 483, in connect
raise FatalError('Failed to connect to %s: %s' % (self.CHIP_NAME, last_error))
esptool.FatalError: Failed to connect to ESP8266: Timed out waiting for packet header
esptool.FatalError: Failed to connect to ESP8266: Timed out waiting for packet header
Kann mir da jemand helfen?

