Terlebih dahulu mempersiapkan beberapa komponen yang akan digunakan yaitu:
1. NodeMCU board
2. DHT11 Temperature and Humidity Sensor
3. Mosquitto MQTT Broker, download di sini
4. Arduino IDE
5. MQTT Library for Arduino
Hubungkan DHT11 ke NodeMCU, seperti pada gambar berikut.
Wireless Adapter LAN IP-Port diteruskan ke Local IP-Port
Buka command prompt dengan akses administrator, kemudian ketik.
netsh interface portproxy add v4tov4 listenaddress=<IP PC> listenport=1883 connectaddress=127.0.0.1 connectport=1883
buat rule pada firewall untuk port 1883.
Program ESP8266
#ifdef ESP8266
#include <ESP8266WiFi.h> /* WiFi library for ESP8266 */
#else
#include <WiFi.h> /* WiFi library for ESP32 */
#endif
#include <Wire.h>
#include <PubSubClient.h>
#include "DHT.h" /* DHT11 sensor library */
#define DHTPIN 13
#define DHTTYPE DHT11 // DHT 11
DHT dht(DHTPIN, DHTTYPE);
#define wifi_ssid "**********"
#define wifi_password "**********"
#define mqtt_server "IP PC/Server"
#define humidity_topic "sensor/DHT11/humidity"
#define temperature_celsius_topic "sensor/DHT11/temperature_celsius"
#define temperature_fahrenheit_topic "sensor/DHT11/temperature_fahrenheit"
WiFiClient espClient;
PubSubClient client(espClient);
void setup() {
Serial.begin(115200);
// dht.begin();
setup_wifi();
client.setServer(mqtt_server, 1883);
}
void setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(wifi_ssid);
WiFi.begin(wifi_ssid, wifi_password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
if (client.connect("ESP8266Client")) {
Serial.println("connected");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
delay(5000);
}
}
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
// Wait a few seconds between measurements.
delay(2000);
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
float f = dht.readTemperature(true);
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
// Compute heat index in Fahrenheit (the default)
float hif = dht.computeHeatIndex(f, h);
// Compute heat index in Celsius (isFahreheit = false)
float hic = dht.computeHeatIndex(t, h, false);
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(t);
Serial.print(" *C ");
Serial.print(f);
Serial.print(" *F\t");
Serial.print("Heat index: ");
Serial.print(hic);
Serial.print(" *C ");
Serial.print(hif);
Serial.println(" *F");
Serial.print("Temperature in Celsius:");
Serial.println(String(t).c_str());
client.publish(temperature_celsius_topic, String(t).c_str(), true);
Serial.print("Temperature in Fahrenheit:");
Serial.println(String(f).c_str());
client.publish(temperature_fahrenheit_topic, String(f).c_str(), true);
Serial.print("Humidity:");
Serial.println(String(h).c_str());
client.publish(humidity_topic, String(h).c_str(), true);
}
Server MQTT
Buat server dengan mneggunakan MQTT Explorer. Install MQTT Explorer, bisa di download di sini.
Konfigurasi server seperti pada gambar berikut.
Kemudian klik connect untuk menghubungkan ke server. Berikut data dari ESP8266 yang terekam pada server.
sumber:
https://fusion-automate.super.site/blog/netsh-port-forwarding-from-local-port-to-local-port
https://arduinojson.org/v6/doc/upgrade/
0 comments:
Post a Comment