• Tidak ada hasil yang ditemukan

Microprocessor System and IoT Temperature Monitoring Program Using MQTT

N/A
N/A
Muhammad Harsandy

Academic year: 2024

Membagikan "Microprocessor System and IoT Temperature Monitoring Program Using MQTT"

Copied!
6
0
0

Teks penuh

(1)

UAS Sistem Mikroprosessor Dan IoT

Nama : Muhammad Harsandy BS NIM : 32222023

Kelas : 2A Telekomunikasi

Buatlah program monitoring suhu dengan sistem IoT (MQTT) menggunakan sensor LM35 dan ESP32, pada laman wokwi.com dengan algoritma berikut ini:

- suhu <19,035 publish pesan dingin

- suhu 19,035 – 21,15 publish pesan hangat

- suhu >21,15 publish pesan panas

x = (dua digit terakhir NIM + Tanggal Lahir + Bulan Lahir + 2 Digit Terakhir tahun lahir) * 0.015.

maka,

x = (23+09+11+4) x 0.015 = 0,705

Berikut kodingannya:

// https://wokwi.com/projects/382880709954964481

#include <WiFi.h>

#include "PubSubClient.h"

const char* ssid = "Wokwi-GUEST";

const char* password = "";

const char* mqttServer = "broker.emqx.io";

int port = 1883;

char clientId[50];

unsigned long lastMsg = 0;

#define MSG_BUFFER_SIZE (50)

(2)

int value = 0;

WiFiClient espClient;

PubSubClient client(espClient);

const int ledPin = 2;

#define ADC_VREF_mV 5000.0 // in millivolt

#define ADC_RESOLUTION 4096.0

#define PIN_LM35 34

// Function to calculate x based on the algorithm float calculateX() {

// Replace with your NIM, Tanggal Lahir, Bulan Lahir, and 2 Digit Tahun Lahir

int nimLastTwoDigits = 23;

int tanggalLahir = 9;

int bulanLahir = 11;

int tahunLahirLastTwoDigits = 04;

float x = (nimLastTwoDigits + tanggalLahir + bulanLahir + tahunLahirLastTwoDigits) * 0.015;

return x;

}

void setup() {

Serial.begin(9600);

randomSeed(analogRead(0));

delay(10);

Serial.println();

Serial.print("Connecting to ");

Serial.println(ssid);

wifiConnect();

Serial.println("");

Serial.println("WiFi connected");

Serial.println("IP address: ");

Serial.println(WiFi.localIP());

client.setServer(mqttServer, port);

client.setCallback(callback);

pinMode(ledPin, OUTPUT);

}

(3)

void wifiConnect() { WiFi.mode(WIFI_STA);

WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) { delay(500);

Serial.print(".");

} }

void mqttReconnect() {

while (!client.connected()) {

Serial.print("Attempting MQTT connection...");

long r = random(1000);

sprintf(clientId, "clientId-%ld", r);

if (client.connect(clientId)) { Serial.print(clientId);

Serial.println(" connected");

client.subscribe("inTopic2A");

} else {

Serial.print("failed, rc=");

Serial.print(client.state());

Serial.println(" try again in 5 seconds");

delay(5000);

} } }

void callback(char* topic, byte* message, unsigned int length) { Serial.print("Message arrived on topic: ");

Serial.print(topic);

Serial.print(". Message: ");

String stMessage;

for (int i = 0; i < length; i++) { Serial.print((char)message[i]);

stMessage += (char)message[i];

}

Serial.println();

if (String(topic) == "inTopic2A") { Serial.print("Changing output to ");

if (stMessage == "1") { Serial.println("on");

(4)

} else if (stMessage == "0") { Serial.println("off");

digitalWrite(ledPin, LOW);

} } }

void loop() {

// get the ADC value from the temperature sensor int adcVal = analogRead(PIN_LM35);

// convert the ADC value to voltage in millivolt

float milliVolt = adcVal * (ADC_VREF_mV / ADC_RESOLUTION);

// convert the voltage to the temperature in Celsius float tempC = milliVolt / 10;

float x = calculateX();

if (!client.connected()) { mqttReconnect();

}

client.loop();

unsigned long now = millis();

if (now - lastMsg > 2000) { lastMsg = now;

//++value;

// print the temperature in the Serial Monitor:

Serial.print("Temperature: ");

Serial.print(tempC); // print the temperature in Celsius Serial.println("°C");

// Check temperature range and publish corresponding message if (tempC < 27 * x) {

snprintf(msg, MSG_BUFFER_SIZE, "Dingin");

} else if (tempC >= 27 * x && tempC <= 30 * x) { snprintf(msg, MSG_BUFFER_SIZE, "Hangat");

} else {

snprintf(msg, MSG_BUFFER_SIZE, "Panas");

}

Serial.print("Publish message: ");

Serial.println(msg);

client.publish("outTopic2A", msg);

} }

(5)

Kondisi saat suhu <19,035 publish pesan dingin

Kondisi saat suhu 19,035 – 21,15 publish pesan hangat

(6)

Kondisi saat suhu >21,15 publish pesan panas

Referensi

Dokumen terkait

Parameters such as surrounding humidity, lighting quality and temperature was controlled using NodeMCU microcontroller unit with the help of an internet connection.. Sprinkler system