#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)
char msg[MSG_BUFFER_SIZE];
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 = 8;
int tanggalLahir = 9;
int bulanLahir = 9;
int tahunLahirLastTwoDigits = 4;
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);
}
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");
digitalWrite(ledPin, HIGH);
} 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();
// print the temperature in the Serial Monitor:
// Serial.print("Temperature: ");
// Serial.print(tempC); // print the temperature in Celsius
// Serial.println("°C");
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);
}
}