#include <WiFi.h>
#include <MQTT.h>
const char ssid[]= "Wokwi-GUEST";
const char pass[]= "";
const int pinMerah = 27;
const int pinHijau = 33;
const int pinBiru = 25;
const int pinCyan = 26;
WiFiClient net;
MQTTClient client;
void setup() {
pinMode(27, OUTPUT);
pinMode(33, OUTPUT);
pinMode(25, OUTPUT);
pinMode(pinCyan, OUTPUT);
WiFi.begin(ssid, pass);
client.begin ("broker.emqx.io", net);
client.onMessage(subscribe);
while (WiFi.status() != WL_CONNECTED){
rgb(1,0,0);
delay (500);
}
rgb(0,1,0);
while(!client.connect("ppserialnumberxxxx")){
delay(500);
}
rgb(0,0,1);
client.subscribe ("rumah1/#");
// put your setup code here, to run once:
}
void loop() {
client.loop();
// put your main code here, to run repeatedly:
delay(10); // this speeds up the simulation
}
void subscribe (String &topic, String &data){
if (topic == "rumah1/makan"){
if(data == "nyala"){
digitalWrite(pinCyan, 1);
}else{
digitalWrite(pinCyan,0);
}
}
}
void rgb (bool red, bool green, bool blue){
digitalWrite(pinMerah, red);
digitalWrite(pinHijau, green);
digitalWrite(pinBiru, blue);
}