#include <DHT.h>
#include <WiFi.h>
#include <PubSubClient.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <max6675.h>
const char* ssid = "Wokwi-GUEST";
const char* password = "";
#define tb_server "thingsboard.cloud"
const int tb_port = 1883;
#define tb_token "ayzs6zficcy6iw1sxc3h"
WiFiClient espClient;
PubSubClient client(espClient);
#define DHT_PIN 15
#define DHT_TYPE DHT22
#define ONE_WIRE_BUS 13
DHT dht(DHT_PIN, DHT_TYPE);
int thermoDO = 19;
int thermoCS = 23;
int thermoCLK = 5;
MAX6675 thermocouple(thermoCLK, thermoCS, thermoDO);
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
DeviceAddress sensorT1 = {0x28, 0xFF, 0x2C, 0xE3, 0x8A, 0x15, 0x04, 0x3D};
DeviceAddress sensorT2 = {0x28, 0xFF, 0x7B, 0xB2, 0x8A, 0x16, 0x04, 0x05};
DeviceAddress sensorT3 = {0x28, 0xFF, 0x4F, 0xE2, 0x8A, 0x16, 0x04, 0xE5};
DeviceAddress sensorT4 = {0x28, 0xFF, 0x6A, 0xE3, 0x8A, 0x15, 0x04, 0x4E};
DeviceAddress sensorT5 = {0x28, 0xFF, 0x8E, 0xE4, 0x8A, 0x15, 0x04, 0x4F};
const int sampleWindow = 50; // Sample window width in mS (50 mS = 20Hz)
unsigned int sample;
#define SENSOR_PIN 32
const int airflowSensorPin = 33; // Assuming the airflow sensor is connected to pin 34
int airflowValue = 32;
void setup()
{
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Koneksi WiFi...");
}
Serial.println("Terhubung ke WiFi");
dht.begin();
sensors.begin();
client.setServer(tb_server, tb_port);
client.setCallback(callback);
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
// Baca suhu dan kelembaban dari sensor DHT22
float temperatureDHT = dht.readTemperature();
float humidity = dht.readHumidity();
if (!isnan(temperatureDHT) && !isnan(humidity)) {
// Kirim data DHT22 ke ThingsBoard
String telemetryDataDHT = "{\"temperature_DHT\":" + String(temperatureDHT) + ",\"humidity\":" + String(humidity) + "}";
char attributesDHT[telemetryDataDHT.length() + 1];
telemetryDataDHT.toCharArray(attributesDHT, telemetryDataDHT.length() + 1);
client.publish("v1/devices/me/telemetry", attributesDHT);
Serial.println("Data DHT22 terkirim: " + telemetryDataDHT);
}
// Baca suhu dari sensor DS18B20
float temperatureT1 = getTemperature(sensorT1);
float temperatureT2 = getTemperature(sensorT2);
float temperatureT3 = getTemperature(sensorT3);
float temperatureT4 = getTemperature(sensorT4);
float temperatureT5 = getTemperature(sensorT5);
// Ganti nilai -127 dengan 0
if (temperatureT1 == -127) {
temperatureT1 = 0;
}
if (temperatureT2 == -127) {
temperatureT2 = 0;
}
if (temperatureT3 == -127) {
temperatureT3 = 0;
}
if (temperatureT4 == -127) {
temperatureT4 = 0;
}
if (temperatureT5 == -127) {
temperatureT5 = 0;
}
// Kirim data DS18B20 ke ThingsBoard
String telemetryDataDS18B20 = "{\"temperature_T1\":" + String(temperatureT1) + ",\"temperature_T2\":" + String(temperatureT2) +
",\"temperature_T3\":" + String(temperatureT3) + ",\"temperature_T4\":" + String(temperatureT4) +
",\"temperature_T5\":" + String(temperatureT5) + "}";
char attributesDS18B20[telemetryDataDS18B20.length() + 1];
telemetryDataDS18B20.toCharArray(attributesDS18B20, telemetryDataDS18B20.length() + 1);
client.publish("v1/devices/me/telemetry", attributesDS18B20);
Serial.println("Data DS18B20 terkirim: " + telemetryDataDS18B20);
float temperatureMAX6675 = thermocouple.readCelsius();
if (temperatureMAX6675 != NAN) {
String telemetryDataMAX6675 = "{\"temperature_MAX6675\":" + String(temperatureMAX6675) + "}";
char attributesMAX6675[telemetryDataMAX6675.length() + 1];
telemetryDataMAX6675.toCharArray(attributesMAX6675, telemetryDataMAX6675.length() + 1);
client.publish("v1/devices/me/telemetry", attributesMAX6675);
Serial.println("Data MAX6675 terkirim: " + telemetryDataMAX6675);
}
unsigned long startMillis = millis(); // Start of sample window
float peakToPeak = 0; // peak-to-peak level
unsigned int signalMax = 0; // minimum value
unsigned int signalMin = 4096; // maximum value
while (millis() - startMillis < sampleWindow) {
sample = analogRead(SENSOR_PIN); // get reading from microphone
if (sample < 4096) { // toss out spurious readings
if (sample > signalMax) {
signalMax = sample; // save just the max levels
} else if (sample < signalMin) {
signalMin = sample; // save just the min levels
}
}
}
peakToPeak = signalMax - signalMin; // max - min = peak-peak amplitude
float db = map(peakToPeak, 200, 2000, 47, 115); // calibrate for deciBels
Serial.println(db);
// Publish sound sensor data to ThingsBoard
String telemetryDataSound = "{\"sound_db\":" + String(db) + "}";
char attributesSound[telemetryDataSound.length() + 1];
telemetryDataSound.toCharArray(attributesSound, telemetryDataSound.length() + 1);
client.publish("v1/devices/me/telemetry", attributesSound);
Serial.println("Sound data sent: " + telemetryDataSound);
airflowValue = analogRead(airflowSensorPin);
String telemetryDataAirflow = "{\"airflow_value\":" + String(airflowValue) + "}";
char attributesAirflow[telemetryDataAirflow.length() + 1];
telemetryDataAirflow.toCharArray(attributesAirflow, telemetryDataAirflow.length() + 1);
client.publish("v1/devices/me/telemetry", attributesAirflow);
Serial.println("Airflow data sent: " + telemetryDataAirflow);
delay(1000);
}
float getTemperature(DeviceAddress address) {
sensors.requestTemperaturesByAddress(address);
return sensors.getTempC(address);
}
void callback(char* topic, byte* payload, unsigned int length) {
// Tambahkan kode callback jika diperlukan
}
void reconnect() {
while (!client.connected()) {
Serial.print("Menghubungkan ke ThingsBoard...");
if (client.connect("ESP32_Client", tb_token, "")) {
Serial.println("Berhasil");
} else {
Serial.print("Gagal, status=");
Serial.print(client.state());
Serial.println(" Coba lagi dalam 5 detik");
delay(5000);
}
}
}