#include <WiFi.h>
#include <PubSubClient.h>
#include <math.h> // For the log() function
#include <DHTesp.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
// Wi-Fi Credentials
const char* ssid = "Wokwi-GUEST";
const char* password = "";
//ADC Info
const float BETA = 3950; // Beta coefficient(from datasheet)
const int analogPin = 34; // ESP32 ADC pin (use GPIO 34 or other ADC-capable pins)
// MQTT Broker Settings
const char* mqtt_server = "mqtt.eclipseprojects.io";
const char* mqtt_topic = "Son Goku";
WiFiClient espClient;
PubSubClient client(espClient);
const int DHT_PIN = 15; // GPIO pin connected to DHT11 DATA pin
//Weather
String URL = "https://api.openweathermap.org/data/2.5/weather?";
String ApiKey = "d51ad11f5d48098e4e0d86f9f1d87bfb";
String lat = "-26.8521";
String lon = "26.6667";
// Function to connect to Wi-Fi
void connectWiFi() {
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("WiFi Connected");
}
// Function to connect to the MQTT broker
void connectMQTT() {
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Create a unique client ID
String clientId = "ESP32Client-" + String(random(0xffff), HEX);
if (client.connect(clientId.c_str())) {
Serial.println("Connected to MQTT broker");
} else {
Serial.print("Failed to connect, state ");
Serial.println(client.state());
delay(5000);
}
}
}
DHTesp dht;
void setup() {
Serial.begin(115200);
connectWiFi();
client.setServer(mqtt_server, 1883); // Set MQTT broker address and port
connectMQTT();
dht.setup(DHT_PIN, DHTesp::DHT11);
}
void loop() {
if (!client.connected()) {
connectMQTT(); // Reconnect if the connection is lost
}
client.loop(); // Keep the connection alive
// Read the analog value (0-4095 range for ESP32)
int analogValue = analogRead(analogPin);
// Temperature calculation (adjusted for ESP32 ADC range)
float celsius = 1 / (log(1 / (4095.0 / analogValue - 1)) / BETA
+ 1.0 / 298.15) - 273.15;
// Output temperature to the serial monitor
Serial.print("NTC Temperature: ");
Serial.print(celsius);
Serial.println(" °C");
TempAndHumidity data = dht.getTempAndHumidity();
Serial.println("DHT11 Temperature: " + String(data.temperature, 2) + "°C");
Serial.println("DHT11 Humidity: " + String(data.humidity, 2) + "%");
Serial.println("---");
delay(5000);
String payload = String(celsius) + "°C" ;
String temp = String(data.temperature, 2) + "°C" ;
client.publish(mqtt_topic, payload.c_str());
client.publish(mqtt_topic, temp.c_str());
Serial.println("Published: " + payload);
Serial.println("Published: " + temp);
delay(5000);
HTTPClient http;
http.begin(URL + "lat=" + lat + "&lon=" + lon + "&units=metric&appid=" + ApiKey);
int httpCode = http.GET();
if (httpCode > 0){
String JSON_Data = http.getString();
DynamicJsonDocument doc(2048);
deserializeJson(doc, JSON_Data);
JsonObject obj = doc.as<JsonObject>();
const char* description = obj["weather"][0]["description"].as<const char*>();
const float temp = obj["main"]["temp"].as<float>();
const float humidity = obj["main"]["humidity"].as<float>();
Serial.println(temp);
Serial.println(humidity);
Serial.println(description);
delay(5000); // Delay for 5 seconds before sending the next message
}
http.end();
}