https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json
a) DHT sensor library
DHT sensor libraryb) Adafruit Unified Sensor
Adafruit Unified Sensorc) PubSubClient
PubSubClientPo wybraniu płytki ustaw:
| Parametr | Wartość |
|---|---|
| USB CDC On Boot | Enabled ← WAŻNE! |
| CPU Frequency | 160MHz |
| Flash Size | 4MB |
| Flash Mode | QIO |
| Upload Speed | 921600 |
| Core Debug Level | None (wyłącza debug ESP-IDF) |
Tools → Port → Wybierz port z ESP32-C3 (np. /dev/cu.usbmodem1101 na Mac, COM3 na Windows)
DHT11 (moduł PCB z 3 pinami):
┌─────────────┐
│ DHT11 │
│ ┌───────┐ │
│ │ │ │ │ │
└──┼──┼─┼─┼──┘
│ │ │ │
VCC│ │GND
DATA
│
↓
ESP32-C3:
┌──────────────┐
│ VCC → 3.3V │
│ DATA→ GPIO10 │
│ GND → GND │
└──────────────┘
Uwaga: Jeśli masz czysty czujnik DHT11 (4 piny), potrzebujesz rezystora pull-up 10kΩ między DATA a VCC. Moduły PCB mają go już wbudowany.
ESP32-C3 ma natywny USB, ale czasami wymaga manualnego wejścia w tryb bootloadera:
Sposób 1 - Automatyczny (zazwyczaj działa):
Sposób 2 - Manualny (jeśli automatyczny nie działa):
Sposób 3 - Przez Serial Monitor (gdy już jest wgrany kod):
=== ESP32-C3 DHT11 MQTT ===
DHT11 zainicjalizowany
✓ DHT11 OK: 23.00°C, 45.00%
✓ WiFi połączone!
✓ MQTT połączono
const char* ssid = "NAZWA_TWOJEJ_SIECI"; // ← Twoja sieć WiFi (2.4GHz!)
const char* password = "HASLO_DO_SIECI"; // ← Hasło WiFi
const char* mqtt_server = "test.mosquitto.org"; // ← Adres brokera MQTT
const int mqtt_port = 1884; // ← Port MQTT
#include <WiFi.h>
#include <PubSubClient.h>
#include <DHT.h>
// Konfiguracja WiFi
const char* ssid = "NAZWA_TWOJEJ_SIECI";
const char* password = "HASLO_DO_SIECI";
// Konfiguracja MQTT
const char* mqtt_server = "test.mosquitto.org";
const int mqtt_port = 1884;
const char* mqtt_user = "rw";
const char* mqtt_password = "readwrite";
// Topiki MQTT
const char* temp_topic = "dom/salon/temperatura";
const char* hum_topic = "dom/salon/wilgotnosc";
// Konfiguracja DHT11
#define DHTPIN 10
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
WiFiClient espClient;
PubSubClient client(espClient);
unsigned long lastMsg = 0;
const long interval = 10000;
void setup() {
Serial.begin(115200);
delay(1000);
Serial.println("\n\n=== ESP32-C3 DHT11 MQTT ===");
// Inicjalizacja DHT11
Serial.println("=== TEST DHT11 ===");
Serial.print("Pin: GPIO");
Serial.println(DHTPIN);
dht.begin();
Serial.println("DHT11 zainicjalizowany");
// Czekaj 2 sekundy na stabilizację czujnika
Serial.println("Czekam 2s na stabilizację DHT11...");
delay(2000);
// Test odczytu DHT11 PRZED WiFi
Serial.println("Test odczytu DHT11:");
float h = dht.readHumidity();
float t = dht.readTemperature();
if (isnan(h) || isnan(t)) {
Serial.println("✗ DHT11 nie odpowiada!");
} else {
Serial.print("✓ DHT11 OK: ");
Serial.print(t);
Serial.print("°C, ");
Serial.print(h);
Serial.println("%");
}
// Połączenie z WiFi
setup_wifi();
// Konfiguracja MQTT
client.setServer(mqtt_server, mqtt_port);
client.setKeepAlive(60);
client.setCallback(callback);
Serial.println("Setup zakończony");
}
void setup_wifi() {
delay(10);
Serial.println("\n--- Konfiguracja WiFi ---");
Serial.print("SSID: ");
Serial.println(ssid);
Serial.print("Hasło długość: ");
Serial.println(strlen(password));
WiFi.mode(WIFI_STA);
WiFi.disconnect();
delay(100);
Serial.println("Rozpoczynam łączenie...");
WiFi.begin(ssid, password);
int attempts = 0;
while (WiFi.status() != WL_CONNECTED && attempts < 30) {
delay(500);
Serial.print(".");
attempts++;
if (attempts % 10 == 0) {
Serial.print("\nStatus WiFi: ");
Serial.println(WiFi.status());
}
}
Serial.println();
if (WiFi.status() == WL_CONNECTED) {
Serial.println("✓ WiFi połączone!");
Serial.print("Adres IP: ");
Serial.println(WiFi.localIP());
Serial.print("RSSI: ");
Serial.print(WiFi.RSSI());
Serial.println(" dBm");
} else {
Serial.println("✗ Błąd połączenia WiFi!");
Serial.print("Status: ");
Serial.println(WiFi.status());
Serial.println("Sprawdź:");
Serial.println("- Czy SSID jest poprawne");
Serial.println("- Czy hasło jest poprawne");
Serial.println("- Czy sieć to 2.4GHz (nie 5GHz)");
}
}
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Wiadomość [");
Serial.print(topic);
Serial.print("]: ");
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
Serial.println();
}
void reconnect() {
while (!client.connected()) {
Serial.print("Łączenie z MQTT...");
String clientId = "ESP32C3-";
clientId += String(random(0xffff), HEX);
if (client.connect(clientId.c_str(), mqtt_user, mqtt_password)) {
Serial.println(" ✓ połączono");
} else {
Serial.print(" ✗ błąd, rc=");
Serial.print(client.state());
Serial.println(" | ponowna próba za 5s");
delay(5000);
}
}
}
void loop() {
// Sprawdź połączenie WiFi
if (WiFi.status() != WL_CONNECTED) {
Serial.println("WiFi rozłączone! Ponowne łączenie...");
setup_wifi();
return;
}
if (!client.connected()) {
reconnect();
}
client.loop();
unsigned long now = millis();
if (now - lastMsg > interval) {
lastMsg = now;
Serial.println("\n--- Odczyt czujnika ---");
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
if (isnan(humidity) || isnan(temperature)) {
Serial.println("✗ Błąd odczytu DHT11 (czujnik niepodłączony?)");
return;
}
Serial.print("Temperatura: ");
Serial.print(temperature);
Serial.print("°C | Wilgotność: ");
Serial.print(humidity);
Serial.println("%");
char tempString[8];
char humString[8];
dtostrf(temperature, 1, 2, tempString);
dtostrf(humidity, 1, 2, humString);
bool temp_sent = client.publish(temp_topic, tempString);
bool hum_sent = client.publish(hum_topic, humString);
Serial.print("Temperatura: ");
Serial.println(temp_sent ? "✓ wysłana" : "✗ błąd");
Serial.print("Wilgotność: ");
Serial.println(hum_sent ? "✓ wysłana" : "✗ błąd");
}
delay(10);
}
test.mosquitto.org:1884rw, Password: readwritedom/salon/temperatura i dom/salon/wilgotnoscmosquitto_sub -h test.mosquitto.org -p 1884 -u rw -P readwrite -t "dom/salon/#" -v
Rozwiązanie: Ustaw Core Debug Level → None w Tools
Rozwiązanie: Zainstaluj ESP32 wersję 3.0.7 (nie 3.3.x)
Rozwiązanie:
Rozwiązanie:
Rozwiązanie:
client.loop() jest wywoływany częstoclient.setKeepAlive(60) w setupRozwiązanie:
Zadanie 1: Zmień interwał odczytu z 10s na 5s
Zadanie 2: Dodaj kontrolkę LED na GPIO8, która miga gdy dane są wysyłane
Zadanie 3: Zmodyfikuj kod tak, aby wysyłał dane tylko gdy temperatura lub wilgotność zmieni się o więcej niż 0.5°C / 2%
Zadanie 4: Dodaj punkt rosy (Dew Point) jako dodatkowy topik MQTT
Powodzenia z projektem IoT! 🚀