#include <WiFi.h>
#include <MQTT.h>
#include <NusabotSimpleTimer.h>
const char ssid[] = "Wokwi-GUEST";
const char pass[] = "password WiFi";
const int pinRed = 2;
const int pinGreen = 4;
const int pinBlue = 16;
const int pinFlameSensor= 23; // Pin analog untuk flame sensor
WiFiClient net;
MQTTClient client;
NusabotSimpleTimer timer;
String chipId = String(ESP.getEfuseMac());
char clientId[6];
void setup() {
chipId.toCharArray(clientId, sizeof(clientId));
pinMode(pinRed, OUTPUT);
pinMode(pinGreen, OUTPUT);
pinMode(pinBlue, OUTPUT);
pinMode(pinFlameSensor, INPUT);
WiFi.begin(ssid, pass);
client.begin("broker.emqx.io", net);
}
void loop() {
if (!client.connected()) {
connect();
}
int flameValue = digitalRead(pinFlameSensor);
if (flameValue == 1) {
client.publish("kelasiot/flame", "Api Terdeteksi");
} else {
client.publish("kelasiot/flame", "Aman");
}
delay (10);
client.loop();
}
void rgb(bool red, bool green, bool blue) {
digitalWrite(pinRed, red);
digitalWrite(pinGreen, green);
digitalWrite(pinBlue, blue);
}
void connect() {
rgb(1, 0, 0);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
rgb(0, 1, 0);
while (!client.connect(clientId)) {
delay(500);
}
rgb(0, 0, 1);
}