Created
January 8, 2021 14:59
-
-
Save yeffrimic/2ed5be3bbdddfcbb8d56bef87630469d 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 <WiFiManager.h> // https://github.com/tzapu/WiFiManager | |
#include <PubSubClient.h> // https://github.com/knolleary/pubsubclient/ | |
#define BUILTIN_LED 2 | |
#define CONNECTION_TOPIC "/F-litter/connection/status" | |
#define MQTT_RECONNECTIO_MS_TIME 5000 | |
WiFiClient espClient; | |
PubSubClient mqttClient(espClient); | |
long lastReconnectAttempt = 0; | |
const char * mqttServer = "broker.mqttdashboard.com"; | |
String clientId = "F-litterCat-"; | |
void callback(char *topic, byte *payload, unsigned int payloadLenght) | |
{ | |
Serial.println("recibiendo informacion desde el topico:"); | |
Serial.println(topic); | |
} | |
boolean reconnectMqtt() | |
{ | |
clientId += String(random(0xffff), HEX); | |
// Attempt to connect | |
if (mqttClient.connect(clientId.c_str(), CONNECTION_TOPIC, 2, false, "0")) | |
{ | |
// Once connected, publish an announcement... | |
mqttClient.publish(CONNECTION_TOPIC, "1"); | |
// ... and resubscribe | |
mqttClient.subscribe("inTopic"); | |
} | |
return mqttClient.connected(); | |
} | |
void setup() | |
{ | |
// put your setup code here, to run once: | |
WiFi.mode(WIFI_STA); // explicitly set mode, esp defaults to STA+AP | |
// define the pin on board as output | |
pinMode(BUILTIN_LED, OUTPUT); | |
Serial.begin(115200); | |
//WiFiManager, Local intialization. Once its business is done, there is no need to keep it around | |
WiFiManager wm; | |
// Automatically connect using saved credentials, | |
// if connection fails, it starts an access point with the specified name ( "AutoConnectAP"), | |
// if empty will auto generate SSID, if password is blank it will be anonymous AP (wm.autoConnect()) | |
// then goes into a blocking loop awaiting configuration and will return success result | |
bool res; | |
res = wm.autoConnect("F-litterCat AP", "IOT12345"); // password protected ap | |
if (!res) | |
{ | |
Serial.println("Failed to connect"); | |
// ESP.restart(); | |
while (1) | |
{ | |
digitalWrite(BUILTIN_LED, HIGH); | |
delay(100); | |
digitalWrite(BUILTIN_LED, LOW); | |
delay(100); | |
} | |
} | |
else | |
{ | |
//if you get here you have connected to the WiFi | |
Serial.println("connected...yeey :)"); | |
} | |
mqttClient.setServer(mqttServer, 1883); | |
mqttClient.setCallback(callback); | |
} | |
void loop() | |
{ | |
// put your main code here, to run repeatedly: | |
if (!mqttClient.connected()) | |
{ | |
long now = millis(); | |
if (now - lastReconnectAttempt > MQTT_RECONNECTIO_MS_TIME) | |
{ | |
lastReconnectAttempt = now; | |
// Attempt to reconnect | |
if (reconnectMqtt()) | |
{ | |
lastReconnectAttempt = 0; | |
} | |
} | |
} | |
else | |
{ | |
// Client connected | |
mqttClient.loop(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment