#include <WiFi.h>
#include <MQTT.h>
WiFiClient net;
MQTTClient client;
const char ssid[] = "Wokwi-GUEST";
const char pass[] = "";
const int pinMerah = 27;
const int pinHijau = 26;
const int pinBiru = 25;
const int pinLed = 33;
void setup() {
Serial.begin(9600);
pinMode(pinMerah, OUTPUT);
pinMode(pinHijau, OUTPUT);
pinMode(pinBiru, OUTPUT);
pinMode(pinLed,OUTPUT);
digitalWrite(pinLed, HIGH);
delay(2000);
digitalWrite(pinLed, LOW);
WiFi.begin(ssid, pass);
client.begin("agusarii.cloud.shiftr.io", net);
client.onMessage(subscribe);
rgb(1,0,0); //merah
while(WiFi.status() != WL_CONNECTED){
delay(500);
}
rgb(0,1,0); //hijau
while(!client.connect("gatauya1010101010")){
delay(500);
}
rgb(0,0,1); //biru
client.subscribe("Hadeh01/#",1);
}
void loop() {
client.loop();
delay(20);
}
void subscribe(String &topic, String &data){
if(topic == "Hadeh01/led" && data == "nyala"){
digitalWrite(pinLed,HIGH);
} else{
digitalWrite(pinLed,LOW);
}
}
void rgb(bool red, bool green, bool blue){
digitalWrite(pinMerah, red);
digitalWrite(pinHijau, green);
digitalWrite(pinBiru, blue);
}