#include #include const char* ssid = "ANLI"; const char* password = "Actua1455"; const char* mqtt_server = "broker.emqx.io"; const char* mqtt_Client = "FabAcademy_Colombia"; const char* mqtt_Topic_Pub = "FabAcademy/week11"; WiFiClient espClient; PubSubClient client(espClient); void setup() { // put your setup code here, to run once: Serial.begin(115200); setup_wifi(); client.setServer(mqtt_server, 1883); } void setup_wifi() { delay(10); // We start by connecting to a WiFi network Serial.println(); Serial.print("Connecting to "); Serial.println(ssid); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.println("WiFi connected"); Serial.println("IP address: "); Serial.println(WiFi.localIP()); } void reconnect() { // Loop until we're reconnected while (!client.connected()) { Serial.print("Attempting MQTT connection..."); // Attempt to connect if (client.connect(mqtt_Client)) { Serial.println("connected"); } else { Serial.print("failed, rc="); Serial.print(client.state()); Serial.println(" try again in 5 seconds"); // Wait 5 seconds before retrying delay(5000); } } } void loop() { // put your main code here, to run repeatedly: if (!client.connected()) { reconnect(); } client.loop(); if (Serial.available()) { int angle = Serial.parseInt(); Serial.println(angle); if (angle >= 0 && angle <= 180) { char buf[4]; itoa(angle, buf, 10); client.publish(mqtt_Topic_Pub, buf); } Serial.read(); // clear newline } }