Using WiFiManager
This commit is contained in:
47
src/main.cpp
47
src/main.cpp
@@ -1,13 +1,13 @@
|
||||
#include <Arduino.h>
|
||||
#include <ESP8266WiFi.h>
|
||||
#include <PubSubClient.h>
|
||||
#include <DNSServer.h>
|
||||
#include <ESP8266WebServer.h>
|
||||
#include <WiFiManager.h>
|
||||
|
||||
#define TURN_ON LOW
|
||||
#define TURN_OFF HIGH
|
||||
|
||||
#define wifi_ssid "etxean"
|
||||
#define wifi_password "4300sf08fhln"
|
||||
|
||||
#define mqtt_server "192.168.249.5"
|
||||
#define mqtt_port 1883
|
||||
#define mqtt_user "homedevice"
|
||||
@@ -21,20 +21,31 @@
|
||||
#define will_message "false"
|
||||
|
||||
WiFiClient espClient;
|
||||
PubSubClient client(espClient);
|
||||
PubSubClient mpttClient(espClient);
|
||||
|
||||
void heartbeat();
|
||||
void longbeat();
|
||||
void setup_wifi();
|
||||
void setup_wifi(String ssid, String password);
|
||||
void reconnect();
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
randomSeed(analogRead(0));
|
||||
pinMode(LED_BUILTIN, OUTPUT);
|
||||
pinMode(LED_BUILTIN, TURN_OFF);
|
||||
|
||||
setup_wifi();
|
||||
client.setServer(mqtt_server, mqtt_port);
|
||||
//WiFiManager
|
||||
WiFiManager wifiManager;
|
||||
//wifiManager.resetSettings(); //reset saved settings
|
||||
wifiManager.autoConnect();
|
||||
|
||||
//if you get here you have connected to the WiFi
|
||||
Serial.print("WiFi connected to: ");
|
||||
Serial.println(WiFi.SSID());
|
||||
Serial.print("IP address: ");
|
||||
Serial.println(WiFi.localIP());
|
||||
|
||||
mpttClient.setServer(mqtt_server, mqtt_port);
|
||||
}
|
||||
|
||||
long lastMessage = 0;
|
||||
@@ -48,11 +59,11 @@ bool checkBound(float newValue, float prevValue, float maxDiff) {
|
||||
}
|
||||
|
||||
void loop() {
|
||||
if (!client.connected()) {
|
||||
if (!mpttClient.connected()) {
|
||||
reconnect();
|
||||
}
|
||||
client.loop();
|
||||
client.publish(will_topic, "true");
|
||||
mpttClient.loop();
|
||||
mpttClient.publish(will_topic, "true");
|
||||
|
||||
long now = millis();
|
||||
if (now - lastHeartbeat > heartbeatMillis) {
|
||||
@@ -72,19 +83,19 @@ void loop() {
|
||||
Serial.print(distance_topic);
|
||||
Serial.print(" = ");
|
||||
Serial.println(String(distance).c_str());
|
||||
client.publish(distance_topic, String(distance).c_str());
|
||||
mpttClient.publish(distance_topic, String(distance).c_str());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void setup_wifi() {
|
||||
void setup_wifi(String ssid, String password) {
|
||||
delay(10);
|
||||
// We start by connecting to a WiFi network
|
||||
Serial.println();
|
||||
Serial.print("Connecting to ");
|
||||
Serial.println(wifi_ssid);
|
||||
Serial.println(ssid);
|
||||
|
||||
WiFi.begin(wifi_ssid, wifi_password);
|
||||
WiFi.begin(ssid, password);
|
||||
|
||||
while (WiFi.status() != WL_CONNECTED) {
|
||||
delay(500);
|
||||
@@ -99,16 +110,16 @@ void setup_wifi() {
|
||||
|
||||
void reconnect() {
|
||||
// Loop until we're reconnected
|
||||
while (!client.connected()) {
|
||||
while (!mpttClient.connected()) {
|
||||
Serial.print("Attempting MQTT connection...");
|
||||
// Attempt to connect
|
||||
// If you do not want to use a username and password, change next line to
|
||||
// if (client.connect("ESP8266Client")) {
|
||||
if (client.connect(mqtt_client_id, mqtt_user, mqtt_password, will_topic, 2, false, will_message)) {
|
||||
// if (mpttClient.connect(mqtt_client_id)) {
|
||||
if (mpttClient.connect(mqtt_client_id, mqtt_user, mqtt_password, will_topic, 2, false, will_message)) {
|
||||
Serial.println("connected");
|
||||
} else {
|
||||
Serial.print("failed, rc=");
|
||||
Serial.print(client.state());
|
||||
Serial.print(mpttClient.state());
|
||||
Serial.println(" try again in 5 seconds");
|
||||
// Wait 5 seconds before retrying
|
||||
delay(5000);
|
||||
|
||||
Reference in New Issue
Block a user