#include <WiFi.h>
#include <MQTT.h>
const char ssid[] = "Wokwi-GUEST";
const char pass[] = "";
WiFiClient net;
MQTTClient client;
unsigned long lastMillis = 0;
void connect() {
Serial.print("checking wifi...");
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(1000);
}
Serial.print("\nconnecting...");
while (!client.connect("clientid")) {
Serial.print(".");
delay(1000);
}
Serial.println("\nconnected!");
}
void setup() {
pinMode(33, INPUT);
Serial.begin(115200);
WiFi.begin(ssid, pass);
client.begin("broker.emqx.io", net);
connect();
}
void loop() {
client.loop();
delay(10); // <- fixes some issues with WiFi stability
if (!client.connected()) {
connect();
}
// publish a message roughly every second.
if (millis() - lastMillis > 1000) {
lastMillis = millis();
int pot = analogRead(33);
Serial.println(pot);
client.publish("nusabot/pot", String(pot));
}
}