#include <WiFi.h>
#include <MQTT.h>
WiFiClient net;
MQTTClient client;
const char ssid[] = "Wokwi-GUEST";
const char pass[] = "";
const int pinRed = 2;
const int pinGreen = 4;
const int pinBlue = 16;
void setup() {
pinMode(pinRed, OUTPUT);
pinMode(pinGreen, OUTPUT);
pinMode(pinBlue, OUTPUT);
WiFi.begin(ssid, pass);
client.begin("broker.emqx.io", net);
connect();
}
void loop() {
if (!client.connected()){
connect();
}
delay(10); // this speeds up the simulation
}
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-unik")){
delay(500);
}
rgb(0,0,1);
}