Skip to content

Commit

Permalink
Merge branch 'master' into conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
anubisg1 authored Nov 15, 2021
2 parents fa4b5ff + 6134ab9 commit 9bb6aee
Show file tree
Hide file tree
Showing 4 changed files with 319 additions and 316 deletions.
207 changes: 105 additions & 102 deletions esp8266-hunter-sprinkler/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,103 +1,106 @@
#include <ESP8266mDNS.h>
#include "LittleFS.h"
#include <ArduinoJson.h>

#include <wifi.h>
#include <mqtt.h>
#include <web_server.h>

AsyncWebServer server = AsyncWebServer(80);
bool wifiDisconnected = false;
bool enableMQTT = true;
MQTT MqttSettings;
MQTT from_wifi_manager;

void setup() {
Serial.begin(SERIAL_SPEED);
// WiFi.mode(WIFI_STA);
Serial.println("mounting FS...");
if (LittleFS.begin()) {
Serial.println("mounted file system");
if (LittleFS.exists("/config.json")) {
//file exists, reading and loading
Serial.println("reading config file");
File configFile = LittleFS.open("/config.json", "r");
if (configFile) {
Serial.println("opened config file");
DynamicJsonDocument doc(1024);
DeserializationError error = deserializeJson(doc, configFile);
serializeJson(doc, Serial);
if (error) {
Serial.print(F("\ndeserializeJson() failed: "));
Serial.println(error.c_str());
Serial.println("failed to load json config");
} else {
Serial.println("\njson successfully parsed");
MqttSettings.server = doc["mqtt_server"].as<String>();
MqttSettings.port = doc["mqtt_port"];
MqttSettings.username = doc["mqtt_username"].as<String>();
MqttSettings.password = doc["mqtt_password"].as<String>();
}
}
}
} else {
Serial.println("failed to mount FS");
}
// this is here just to make sure that if wifimanager is
// skipped when config is saved, MQTT still works, otherwise empty data would override
// what we learned from config file saved on disk
from_wifi_manager = setup_wifi();
if (from_wifi_manager.server != "" ){
MqttSettings = from_wifi_manager;
}
setup_WebServer();
if (shouldSaveConfig) {
Serial.println("saving config");
DynamicJsonDocument doc(1024);
doc["mqtt_server"] = MqttSettings.server;
doc["mqtt_port"] = MqttSettings.port;
doc["mqtt_username"] = MqttSettings.username;
doc["mqtt_password"] = MqttSettings.password;
File configFile = LittleFS.open("/config.json", "w");
if (!configFile) {
Serial.println("failed to open config file for writing");
}
serializeJson(doc, Serial);
serializeJsonPretty(doc, configFile);
configFile.close();
//end save
}
if (enableMQTT) {
client.setServer(MqttSettings.server.c_str(), MqttSettings.port);
client.setCallback(mqtt_callback);
}
Serial.print("\nIP address: ");
Serial.println(WiFi.localIP());
Serial.printf("%s:%d\n", MqttSettings.server.c_str(), MqttSettings.port);
Serial.printf("Ready! Open http://%s.local in your browser\n", HOSTNAME);
}

void loop() {
if (enableMQTT) {
if (!client.connected()) {
int retries = 0;
while (!client.connected()) {
if(retries < 50) {
mqtt_connect(MqttSettings.username.c_str(),MqttSettings.password.c_str());
}
else {
retries++;
// Wait 5 seconds before retrying
delay(5000);
}
if(retries >= 50) {
ESP.restart();
}
}
}
}
//wifiDisconnected = checkWifiConnection(wifiDisconnected);
client.loop();
MDNS.update();
//timer.run();
/**
* First version: July 2020 - Eloi Codina Torras
*/
#include <ESP8266mDNS.h>
#include "LittleFS.h"
#include <ArduinoJson.h>

#include <wifi.h>
#include <mqtt.h>
#include <web_server.h>

AsyncWebServer server = AsyncWebServer(80);
bool wifiDisconnected = false;
bool enableMQTT = true;
MQTT MqttSettings;
MQTT from_wifi_manager;

void setup() {
Serial.begin(SERIAL_SPEED);
// WiFi.mode(WIFI_STA);
Serial.println("mounting FS...");
if (LittleFS.begin()) {
Serial.println("mounted file system");
if (LittleFS.exists("/config.json")) {
//file exists, reading and loading
Serial.println("reading config file");
File configFile = LittleFS.open("/config.json", "r");
if (configFile) {
Serial.println("opened config file");
DynamicJsonDocument doc(1024);
DeserializationError error = deserializeJson(doc, configFile);
serializeJson(doc, Serial);
if (error) {
Serial.print(F("\ndeserializeJson() failed: "));
Serial.println(error.c_str());
Serial.println("failed to load json config");
} else {
Serial.println("\njson successfully parsed");
MqttSettings.server = doc["mqtt_server"].as<String>();
MqttSettings.port = doc["mqtt_port"];
MqttSettings.username = doc["mqtt_username"].as<String>();
MqttSettings.password = doc["mqtt_password"].as<String>();
}
}
}
} else {
Serial.println("failed to mount FS");
}
// this is here just to make sure that if wifimanager is
// skipped when config is saved, MQTT still works, otherwise empty data would override
// what we learned from config file saved on disk
from_wifi_manager = setup_wifi();
if (from_wifi_manager.server != "" ){
MqttSettings = from_wifi_manager;
}
setup_WebServer();
if (shouldSaveConfig) {
Serial.println("saving config");
DynamicJsonDocument doc(1024);
doc["mqtt_server"] = MqttSettings.server;
doc["mqtt_port"] = MqttSettings.port;
doc["mqtt_username"] = MqttSettings.username;
doc["mqtt_password"] = MqttSettings.password;
File configFile = LittleFS.open("/config.json", "w");
if (!configFile) {
Serial.println("failed to open config file for writing");
}
serializeJson(doc, Serial);
serializeJsonPretty(doc, configFile);
configFile.close();
//end save
}
if (enableMQTT) {
client.setServer(MqttSettings.server.c_str(), MqttSettings.port);
client.setCallback(mqtt_callback);
}
Serial.print("\nIP address: ");
Serial.println(WiFi.localIP());
Serial.printf("%s:%d\n", MqttSettings.server.c_str(), MqttSettings.port);
Serial.printf("Ready! Open http://%s.local in your browser\n", HOSTNAME);
}

void loop() {
if (enableMQTT) {
if (!client.connected()) {
int retries = 0;
while (!client.connected()) {
if(retries < 50) {
mqtt_connect(MqttSettings.username.c_str(),MqttSettings.password.c_str());
}
else {
retries++;
// Wait 5 seconds before retrying
delay(5000);
}
if(retries >= 50) {
ESP.restart();
}
}
}
}
//wifiDisconnected = checkWifiConnection(wifiDisconnected);
client.loop();
MDNS.update();
//timer.run();
}
120 changes: 60 additions & 60 deletions esp8266-hunter-sprinkler/src/web_server.cpp
Original file line number Diff line number Diff line change
@@ -1,61 +1,61 @@
/**
* This file includes sets up the web server to serve teh API and webpage
* to esily control the Hunter Irrigation System.
*
* First version: July 2020 - Eloi Codina Torras
*/
#include <ESPAsyncTCP.h>
#include <ESP8266mDNS.h>

#include <global_config.h>
#include <web_server.h>
#include <web_server_api.h>
#include <ota.h>

/**
* Configure the paths and start the server.
*/
void setup_WebServer() {
if (!MDNS.begin(HOSTNAME)) {
Serial.println("Error setting up MDNS responder!");
while (1) {
delay(1000);
}
}
Serial.println("mDNS responder started");
setup_APIRoutes();
server.begin();
MDNS.addService("http", "tcp", 80);
}

/**
* Set up all the API routes
*/
void setup_APIRoutes() {
server.on("/api/start/zone", HTTP_GET, startZoneRequest);

server.on("/api/start/program", HTTP_GET, startProgramRequest);

server.on("/api/stop/zone", HTTP_GET, stopZoneRequest);

server.on("/api/start", HTTP_GET, [](AsyncWebServerRequest *request){
request->send_P(400, "text/plain", "What should I start?");
});

server.on("/api", HTTP_GET, [](AsyncWebServerRequest *request){
request->send_P(403, "text/plain", "This is the API root");
});

server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) {request->redirect("/update");});

server.on("/update", HTTP_GET, [](AsyncWebServerRequest *request){handleUpdate(request);});

server.on("/doUpdate", HTTP_POST,
[](AsyncWebServerRequest *request) {},
[](AsyncWebServerRequest *request, const String& filename, size_t index, uint8_t *data,
size_t len, bool final) {handleDoUpdate(request, filename, index, data, len, final);}
);

server.onNotFound([](AsyncWebServerRequest *request){request->send(404);});

/**
* This file includes sets up the web server to serve teh API and webpage
* to esily control the Hunter Irrigation System.
*
* First version: July 2020 - Eloi Codina Torras
*/
#include <ESPAsyncTCP.h>
#include <ESP8266mDNS.h>

#include <global_config.h>
#include <web_server.h>
#include <web_server_api.h>
#include <ota.h>

/**
* Configure the paths and start the server.
*/
void setup_WebServer() {
if (!MDNS.begin(HOSTNAME)) {
Serial.println("Error setting up MDNS responder!");
while (1) {
delay(1000);
}
}
Serial.println("mDNS responder started");
setup_APIRoutes();
server.begin();
MDNS.addService("http", "tcp", 80);
}

/**
* Set up all the API routes
*/
void setup_APIRoutes() {
server.on("/api/start/zone", HTTP_GET, startZoneRequest);

server.on("/api/start/program", HTTP_GET, startProgramRequest);

server.on("/api/stop/zone", HTTP_GET, stopZoneRequest);

server.on("/api/start", HTTP_GET, [](AsyncWebServerRequest *request){
request->send_P(400, "text/plain", "What should I start?");
});

server.on("/api", HTTP_GET, [](AsyncWebServerRequest *request){
request->send_P(403, "text/plain", "This is the API root");
});

server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) {request->redirect("/update");});

server.on("/update", HTTP_GET, [](AsyncWebServerRequest *request){handleUpdate(request);});

server.on("/doUpdate", HTTP_POST,
[](AsyncWebServerRequest *request) {},
[](AsyncWebServerRequest *request, const String& filename, size_t index, uint8_t *data,
size_t len, bool final) {handleDoUpdate(request, filename, index, data, len, final);}
);

server.onNotFound([](AsyncWebServerRequest *request){request->send(404);});

}
Loading

0 comments on commit 9bb6aee

Please sign in to comment.