#include <WiFi.h> //Import two new library
#include <PubSubClient.h>
#define WIFI_SSID "Wokwi-GUEST" //WIFI SSID NAME
#define WIFI_PW "" //WIFI PASSWORD
#define MQTT_BROKER "test.mosquitto.org" //MQTT BROKER IP
#define MQTT_PORT 1883 //MQTT BROKER PORT
#define MQTT_TOPIC "IDP_ESP8266_TOPIC" //TOPIC NAME, NAME IT YOURSELF
WiFiClient espClient; // ESP8266 Wifi Client
PubSubClient client(espClient); // MQTT Client using the Wifi-Client
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived in topic: "); // Print out the topic
Serial.println(topic);
Serial.print("Message:"); //Print Received message
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
Serial.println();
Serial.println("-----------------------");
}
void setup() {
pinMode(LED_BUILTIN, OUTPUT); // Set up pin IO modes
digitalWrite(LED_BUILTIN, HIGH); // Turn off the lights
Serial.begin(115200);
WiFi.begin(WIFI_SSID, WIFI_PW); // Start Wi-Fi connection
while (WiFi.status() != WL_CONNECTED) { // Waiting for connection success
delay(250);
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
Serial.println("Connecting to WiFi..");
}
Serial.println("Connected to the WiFi network");
delay(100);
digitalWrite(LED_BUILTIN, LOW); //Turn on the light to indicate Wi-Fi success
Serial.println("-------------------------");
Serial.println("Hello from ESP8266");
Serial.print("Connected to hotspot: ");
Serial.println(WIFI_SSID);
Serial.print("IP address is: ");
Serial.println(WiFi.localIP());
Serial.println("-------------------------");
client.setSocketTimeout(60);
client.setKeepAlive(5);
client.setServer(MQTT_BROKER, MQTT_PORT); //Start MQTT connection
client.setCallback(callback);
while (!client.connected()) {
Serial.println("Connecting to MQTT...");
if (client.connect("ESP8266_Receive", "", "" )) { //Sign in to the broker
Serial.println("Connected to broker");
} else {
Serial.print("failed with state ");
Serial.print(client.state());
delay(2000);
}
}
client.subscribe(MQTT_TOPIC);
}
void loop() {
if (!client.connected()) {
Serial.print("!");
delay(250);
} else {
client.loop();
}
delay(10);
}