Skip to content

Commit

Permalink
Some Wi-Fi improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
ezcGman committed Jun 14, 2021
1 parent 74d2c50 commit 7eedfa3
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions sketch/soil-moisture-sensor.ino
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@
#include <DateTime.h>


const char* ssid = "YourSSID";
const char* password = "YourWiFiPassword";
const char* wifiSsid = "YourSSID";
const char* wifiPassword = "YourWiFiPassword";

const char* mqttServer = "YourMQTTServer";
const int mqttPort = 1883;
const char* mqttUser = "YourMQTTUser";
const char* mqttPassword = "YourMQTTPassword";

// Used for Wi-Fi and MQTT
const byte maxConnTries = 15;

// https://github.com/esp8266/Arduino/blob/master/variants/nodemcu/pins_arduino.h
const byte analogMoisturePin = A0;
const byte enableSensorPin = 4;
Expand All @@ -29,9 +32,16 @@ const int waterValue = 412;
// Yes, that should be in the alerting systems business logic, but we use this boolean flag in the alerting. So we set this flag a little earlier.
const int plantDryThreshold = 550;

const byte maxConnTries = 15;

WiFiClient wifiClient;
/*
* To speed up Wi-Fi conenction, you can hardcode these values and use the
* alternate WiFi.config() and WiFi.begin() calls in connectWifi()
const byte wifiChannel = 7;
IPAddress ipEsp(192, 168, 178, 100);
IPAddress ipRouter(192, 168, 178, 1);
IPAddress subnet(255, 255, 255, 0);
uint8_t macRouter[6] = { 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF };
*/
PubSubClient mqttClient(wifiClient);


Expand All @@ -40,11 +50,15 @@ void connectWifi() {
int wifiConnTries = 0;

WiFi.mode(WIFI_STA);
WiFi.setSleepMode(WIFI_LIGHT_SLEEP);
WiFi.begin(ssid, password);
/*
* Use these two calls for faster Wi-Fi connection.
WiFi.config(ipEsp, ipRouter, ipRouter, subnet);
WiFi.begin(wifiSsid, wifiPassword, wifiChannel, macRouter);
*/
WiFi.begin(wifiSsid, wifiPassword);
while (WiFi.status() != WL_CONNECTED && wifiConnTries < maxConnTries) {
wifiConnTries++;
Serial.printf("Connecting to Wi-Fi %s ...\n", ssid);
Serial.printf("Connecting to Wi-Fi %s ...\n", wifiSsid);
delay(1000);
}

Expand Down

0 comments on commit 7eedfa3

Please sign in to comment.