Last active
July 15, 2020 00:02
Star
You must be signed in to star a gist
WifiManager
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> | |
#include <Adafruit_Sensor.h> | |
#include <DHT.h> | |
#include <DHT_U.h> | |
#define DHTTYPE DHT11 | |
#define DHTPIN 2 | |
DHT_Unified dht(DHTPIN, DHTTYPE); | |
const char* ssid = "OLIVEIRA"; | |
const char* password = "20099002"; | |
const char* mqtt_server = "51.210.110.0"; | |
WiFiClient espClient; | |
PubSubClient client(espClient); | |
long lastMsg = 0; | |
char msg[50]; | |
int value = 0; | |
void callback(char* topic, byte* payload, unsigned int length) { | |
Serial.print("Topico "); | |
Serial.print(topic); | |
Serial.print(": "); | |
for (int i = 0; i < length; i++) { | |
Serial.print((char)payload[i]); | |
} | |
Serial.println(); | |
} | |
void setup() { | |
Serial.begin(9600); | |
dht.begin(); | |
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() { | |
sensors_event_t event; | |
dht.temperature().getEvent(&event); | |
client.publish("temp", String(event.temperature).c_str()); | |
dht.humidity().getEvent(&event); | |
client.publish("umid", String(event.relative_humidity).c_str()); | |
delay(5000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment