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