-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSoilMonitor3.ino
164 lines (135 loc) · 4.02 KB
/
SoilMonitor3.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="SoilMonitor3.ino" company="DTV-Online">
// Copyright(c) 2020 Dr. Peter Trimmel. All rights reserved.
// </copyright>
// <license>
// Licensed under the MIT license. See the LICENSE file in the project root for more information.
// </license>
// <created>02-Apr-20 18:15:49</created>
// <author>peter</author>
// <summary>
// Global values, Setup() and Loop() functions. Note that this file is merged with all other '.ino' files.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
#include <BluetoothSerial.h>
#include <WiFi.h>
#include <aWOT.h>
#include <Commander.h>
#include <ping.h>
#include <ESP32Ping.h>
#include <ArduinoLog.h>
#include <jled.h>
#include <neotimer.h>
#include <ArduinoJson.hpp>
#include <ArduinoJson.h>
#include <FS.h>
#include <SPIFFS.h>
#include <esp32-hal-log.h>
#include <esp_log.h>
#include <esp_wifi.h>
#include <nvs_flash.h>
#include "src/Sensors.h"
#include "src/Settings.h"
#include "src/ApInfo.h"
#include "src/StaInfo.h"
#include "src/ErrInfo.h"
#include "src/ServerInfo.h"
#include "src/SystemInfo.h"
#include "src/WiFiManager.h"
#include "src/MimeTypes.h"
// Set the software version for the SystemInfoClass.
char* SystemInfo::SOFTWARE_VERSION = "V1.0.2 2020-04-04";
// Message lines to be printed on the serial line during startup.
auto HEADER = "SoilMonitor2";
auto COPYRIGHT = "Copyright (c) 2020 - Dr. Peter Trimmel";
// On board LED.
JLed led = JLed(LED_BUILTIN).Blink(500, 500).Forever();
// Setup update timer (1sec) and reboot timer (5 sec).
Neotimer updateTimer = Neotimer(1000);
Neotimer rebootTimer = Neotimer(5000);
// Bluetooth support (Serial).
BluetoothSerial SerialBT;
// The command processing (Serial)
Commander cmd;
// Setup soil sensors using defaults.
Sensors sensors;
// The global application settings (WiFi, sensors).
Settings settings(&sensors);
// The WiFi manager (using the settings).
WiFiManager manager(&settings);
// Create Webserver at the default port.
WiFiServer server(ServerInfo::PORT);
Application app;
// System infos.
SystemInfo sysInfo;
// Error infos.
ErrInfo error;
/// <summary>
/// The setup() function is called when a sketch starts. It initializes variables, pin modes,
/// starts using libraries, etc. The setup() function will only run once, after each powerup
/// or reset of the Arduino board.
/// </summary>
void setup()
{
// Initialize non volatile storage and settings.
esp_err_t err = nvs_flash_init();
if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND) {
// NVS partition was truncated and needs to be erased
// Retry nvs_flash_init
ESP_ERROR_CHECK(nvs_flash_erase());
err = nvs_flash_init();
}
// Initialize serial and wait 1 second for port to open.
Serial.begin(115200);
delay(1000);
// Print application startup info.
Serial.println(HEADER);
Serial.println(COPYRIGHT);
Serial.println();
// Mount the SPIFFS.
if (!SPIFFS.begin())
{
Log.fatal("An Error has occurred while mounting SPIFFS" CR);
return;
}
// Initialize system info and settings.
sysInfo.init();
settings.init(sysInfo);
// Initializing Bluetooth serial and wait 1 second.
SerialBT.begin(settings.CmdSettings.LocalName);
delay(1000);
// Initialize logging (Serial).
initLogging();
// Try to connect WiFi, and start the sensors.
manager.connect();
sensors.SoilSensors.begin();
sensors.TempSensors.begin();
// Initialize the commander and the web server.
initCommander();
initServer();
// Start timer.
updateTimer.start();
}
/// <summary>
/// This function is called consecutively, until powered down or reset (reboot).
/// </summary>
void loop()
{
WiFiClient client = server.available();
led.Update();
cmd.update();
if (client.connected())
{
app.process(&client);
}
if (updateTimer.repeat())
{
sensors.SoilSensors.update();
sensors.TempSensors.update();
sysInfo.update();
}
if (rebootTimer.done())
{
ESP.restart();
}
}