In this project, I developed a device that measures the the temperature and humidity using a NodeMCU. I am currently using this device for measuring temperatures in the province of Sinop in Türkiye. Below, you can see the real-time measurements.
I put the device in a box that I 3D printed. In the box, the microcontroller and the sensors are at seperate sections.
The data comes from the microcontroller follows this route reaching here.:
• NodeMCU → Home Router → Server Computer→ Internet
NodeMCU sends the data via local network. Afterwards, the server computer calls a backend software and sends the data securely to https://espapi.uygardirik.com.tr/.

NodeMCU ESP-12E DHT11-22 Scheme
Above, I prepared a scheme for you to understand better.
These are the all materials I used:
• NodeMCU ESP-12E
• DHT22
• DHT11
• Jumper Cables
The code I used is shown below:
//Uygar Dirik - 27.11.2026
#include
#include
#include
#include
#include
const char* ssid = "";
const char* password = "";
const char* mqttHost = "uygardirik.com.tr";
const int mqttPort = 1234;
const char* mqttUser = "esp_sensor";
const char* mqttPass = "";
const char* clientId = "esp8266sinop_01";
DHT dht11(D2, DHT11);
DHT dht22(D3, DHT22);
WiFiClient wifiClient;
PubSubClient client(wifiClient);
ESP8266WebServer server(80);
unsigned long lastPublish = 0;
unsigned long lastMqttTry = 0;
int dht11Errors = 0;
int dht22Errors = 0;
int wifiReconnects = 0;
int mqttReconnects = 0;
void connectWifi() {
Serial.println("Connecting to WiFi");
WiFi.disconnect();
WiFi.mode(WIFI_STA);
WiFi.persistent(false);
WiFi.setAutoReconnect(true);
WiFi.setSleepMode(WIFI_NONE_SLEEP);
WiFi.setPhyMode(WIFI_PHY_MODE_11G);
WiFi.begin(ssid, password);
unsigned long start = millis();
while (WiFi.status() != WL_CONNECTED && millis() - start < 20000) {
delay(500);
Serial.print(".");
}
if (WiFi.status() == WL_CONNECTED) {
Serial.println();
Serial.println("WiFi connected");
Serial.println("IP: " + WiFi.localIP().toString());
Serial.println("RSSI: " + String(WiFi.RSSI()) + " dBm");
} else {
Serial.println();
Serial.println("WiFi failed");
}
}
void connectMqtt() {
Serial.print("Connecting to MQTT... ");
if (client.connect(clientId, mqttUser, mqttPass, "esp8266sinop/status", 0, true, "offline")) {
Serial.println("connected");
client.publish("esp8266sinop/status", "online", true);
} else {
Serial.println("failed, rc=" + String(client.state()));
}
}
void sendValue(const char* topic, float value) {
if (isnan(value)) {
Serial.println(String(topic) + " is NaN, skipped");
return;
}
bool ok = client.publish(topic, String(value, 1).c_str());
Serial.println(String(topic) + " = " + String(value, 1) + (ok ? " sent" : " failed"));
}
void sendText(const char* topic, String text) {
bool ok = client.publish(topic, text.c_str());
Serial.println(String(topic) + (ok ? " sent" : " failed"));
}
String getUptime() {
unsigned long seconds = millis() / 1000;
unsigned long days = seconds / 86400;
unsigned long hours = (seconds % 86400) / 3600;
unsigned long minutes = (seconds % 3600) / 60;
unsigned long secs = seconds % 60;
String text = "";
if (days > 0) text += String(days) + "d ";
text += String(hours) + "h " + String(minutes) + "m " + String(secs) + "s";
return text;
}
String getReport() {
String text = "NodeMCU ESP8266 report\n";
text += "Uptime: " + getUptime() + "\n";
text += "WiFi RSSI: " + String(WiFi.RSSI()) + " dBm\n";
text += "WiFi reconnects: " + String(wifiReconnects) + "\n";
text += "MQTT reconnects: " + String(mqttReconnects) + "\n";
text += "Free memory: " + String(ESP.getFreeHeap()) + " bytes\n";
text += "Errors:\n";
if (dht11Errors == 0 && dht22Errors == 0) {
text += "- None\n";
}
if (dht11Errors > 0) {
text += "- DHT11 returned NaN " + String(dht11Errors) + " times\n";
}
if (dht22Errors > 0) {
text += "- DHT22 returned NaN " + String(dht22Errors) + " times\n";
}
return text;
}
void setup() {
Serial.begin(115200);
delay(300);
Serial.println("Starting");
dht11.begin();
dht22.begin();
connectWifi();
server.on("/", []() {
server.send(200, "text/plain", "ESP8266 running");
});
ElegantOTA.begin(&server);
server.begin();
client.setServer(mqttHost, mqttPort);
client.setBufferSize(768);
client.setKeepAlive(30);
}
void loop() {
if (WiFi.status() != WL_CONNECTED) {
wifiReconnects++;
connectWifi();
return;
}
if (!client.connected()) {
if (millis() - lastMqttTry > 5000) {
lastMqttTry = millis();
mqttReconnects++;
connectMqtt();
}
return;
}
client.loop();
if (millis() - lastPublish >= 20000) {
lastPublish = millis();
float dht11Temp = dht11.readTemperature();
float dht11Hum = dht11.readHumidity();
float dht22Temp = dht22.readTemperature();
float dht22Hum = dht22.readHumidity();
if (isnan(dht11Temp) || isnan(dht11Hum)) dht11Errors++;
if (isnan(dht22Temp) || isnan(dht22Hum)) dht22Errors++;
Serial.println("Publishing");
sendValue("esp8266sinop/dht11temp", dht11Temp);
sendValue("esp8266sinop/dht11hum", dht11Hum);
sendValue("esp8266sinop/dht22temp", dht22Temp);
sendValue("esp8266sinop/dht22hum", dht22Hum);
sendText("esp8266sinop/log", getReport());
}
server.handleClient();
ElegantOTA.loop();
}