Last active
June 10, 2021 00:16
-
-
Save tassioauad/3937ad83a64d188deac08f71e648d1d6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <WiFi.h> | |
#include <PubSubClient.h> | |
const char* ssid = "MEUWIFI"; | |
const char* password = "SENHA"; | |
const char* mqtt_server = "IP_MQTT_SERVER"; | |
WiFiClient espClient; | |
PubSubClient client(espClient); | |
String payloadString; | |
void callback(char* topic, byte* payload, unsigned int length) { | |
Serial.print("Topico "); | |
Serial.print(topic); | |
Serial.print(": "); | |
for (int i = 0; i < length; i++) { | |
payloadString += (char) payload[i]; | |
} | |
Serial.println(payloadString); | |
} | |
void setup() { | |
Serial.begin(9600); | |
Serial.print("Conectando ao WiFi... "); | |
WiFi.begin(ssid, password); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
Serial.print("."); | |
} | |
Serial.println("WiFi conectado!"); | |
Serial.println("Endereço IP: "); | |
Serial.println(WiFi.localIP()); | |
client.setServer(mqtt_server, 1883); | |
client.setCallback(callback); | |
client.connect("1"); | |
while (!client.connected()) { | |
Serial.print("Falha:"); | |
Serial.print(client.state()); | |
Serial.println(" nova tentativa em 5 segundos"); | |
delay(5000); | |
client.connect("1"); | |
} | |
} | |
void loop() { | |
client.publish("iot", "Alo?! Aqui eh o NodeMCU..."); | |
delay(5000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment