#include <WiFi.h>
#include <PubSubClient.h>
#include <DHT.h>
// WiFi credentials
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// MQTT broker info
const char* mqtt_server = "test.mosquitto.org";
const int mqtt_port = 1883;
// MQTT topics for NTC sensor
const char* ntc_temp_topic = "esp32/ntc/temperature";
// MQTT topics for DHT22 sensor
const char* dht_temp_topic = "esp32/dht22/temperature";
const char* dht_hum_topic = "esp32/dht22/humidity";
// Analog temperature sensor pin (NTC)
const int ntc_tempPin = 34; // Use GPIO34 (ADC1_CH6) for analog input
// NTC Thermistor parameters (adjust these based on your specific NTC)
const float ntc_nominalResistance = 10000.0; // Resistance at 25°C (10k Ohm typical)
const float ntc_nominalTemperature = 25.0 + 273.15; // Temperature at nominal resistance (in Kelvin)
const float ntc_bCoefficient = 3950.0; // Beta value of the thermistor (check datasheet)
const float ntc_seriesResistance = 10000.0; // Resistance of the series resistor in the voltage divider
// DHT setup
#define DHTPIN 4
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
// WiFi and MQTT clients
WiFiClient espClient;
PubSubClient client(espClient);
// Connect to WiFi
void setup_wifi() {
Serial.print("Connecting to WiFi");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println(" connected");
}
// Reconnect to MQTT broker
void reconnect_mqtt() {
while (!client.connected()) {
Serial.print("Connecting to MQTT...");
if (client.connect("ESP32Client_MultiSensor")) { // More descriptive client ID
Serial.println("connected");
} else {
Serial.print(" failed, rc=");
Serial.print(client.state());
delay(2000);
}
}
}
// Read analog value and convert to temperature (for NTC thermistor)
float readTemperatureNTC() {
int analogValue = analogRead(ntc_tempPin);
// Convert analog reading to resistance of the thermistor
// Assuming a simple voltage divider circuit: Vout = Vin * (R_thermistor / (R_thermistor + R_series))
float voltage = analogValue * (3.3 / 4095.0);
float thermistorResistance = ntc_seriesResistance / ((3.3 / voltage) - 1);
// Apply the Steinhart-Hart equation (simplified using Beta coefficient)
float steinhart;
steinhart = thermistorResistance / ntc_nominalResistance; // (R/Ro)
steinhart = log(steinhart); // ln(R/Ro)
steinhart /= ntc_bCoefficient; // 1/B * ln(R/Ro)
steinhart += 1.0 / ntc_nominalTemperature; // 1/T0 + 1/B * ln(R/Ro)
steinhart = 1.0 / steinhart; // T in Kelvin
steinhart -= 273.15; // Convert to Celsius
return steinhart;
}
void setup() {
Serial.begin(115200);
dht.begin();
setup_wifi();
client.setServer(mqtt_server, mqtt_port);
}
void loop() {
if (!client.connected()) {
reconnect_mqtt();
}
client.loop();
// Read and publish NTC temperature
float ntcTemperature = readTemperatureNTC();
Serial.print("NTC Temperature: ");
Serial.print(ntcTemperature);
Serial.println(" °C");
client.publish(ntc_temp_topic, String(ntcTemperature).c_str());
// Read and publish DHT22 data
float dhtTemperature = dht.readTemperature();
float humidity = dht.readHumidity();
if (isnan(dhtTemperature) || isnan(humidity)) {
Serial.println("Failed to read from DHT22 sensor!");
} else {
Serial.print("DHT Temperature: ");
Serial.print(dhtTemperature);
Serial.print(" °C | Humidity: ");
Serial.print(humidity);
Serial.println(" %");
client.publish(dht_temp_topic, String(dhtTemperature).c_str());
client.publish(dht_hum_topic, String(humidity).c_str());
}
delay(2000); // Reduced delay to accommodate both sensors
}