Subversion Repositories ESP8266_P1_Meter

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 raymond 1
#include <Arduino.h>
2
#include <FS.h>
3
#include <LittleFS.h>
4
#include <AsyncFsWebServer.h>   // https://github.com/cotestatnt/async-esp-fs-webserver
5
 
6
const char* hostname = "myserver";
7
#define FILESYSTEM LittleFS
8
AsyncFsWebServer server(FILESYSTEM, 80, hostname);
9
 
10
#ifndef LED_BUILTIN
11
#define LED_BUILTIN 2
12
#endif
13
 
14
// Var labels as shown in /setup webpage
15
#define LED_LABEL "The LED pin number"
16
#define BOOL_LABEL "A bool variable"
17
#define LONG_LABEL "A long variable"
18
#define FLOAT_LABEL "A float variable"
19
#define STRING_LABEL "A String variable"
20
#define DROPDOWN_TEST "A dropdown listbox"
21
#define BRIGHTNESS_LABEL "A slider control"
22
 
23
#define TB_DEVICE_NAME "Device Name"
24
#define TB_DEVICE_LAT "Device Latitude"
25
#define TB_DEVICE_LON "Device Longitude"
26
#define TB_SERVER "ThingsBoard server address"
27
#define TB_PORT "ThingsBoard server port"
28
#define TB_DEVICE_TOKEN "ThingsBoard device token"
29
#define TB_DEVICE_KEY "Provisioning device key"
30
#define TB_SECRET_KEY "Provisioning secret key"
31
 
32
// Test "options" values
33
uint8_t ledPin = LED_BUILTIN;
34
bool boolVar = true;
35
bool boolVar2 = false;
36
uint32_t longVar = 1234567890;
37
float floatVar = 15.5F;
38
String stringVar = "Test option String";
39
String dropdownSelected = "Item1";
40
AsyncFsWebServer::Slider brightness{ BRIGHTNESS_LABEL, 0.0, 100.0, 1.0, 50.0 };
41
 
42
// ThingsBoard variables
43
String tb_deviceName = "ESP Sensor";
44
double tb_deviceLatitude = 41.88505;
45
double tb_deviceLongitude = 12.50050;
46
String tb_deviceToken = "xxxxxxxxxxxxxxxxxxx";
47
String tb_device_key = "xxxxxxxxxxxxxxxxxxx";
48
String tb_secret_key = "xxxxxxxxxxxxxxxxxxx";
49
String tb_serverIP = "thingsboard.cloud";
50
uint16_t tb_port = 80;
51
 
52
/*
53
* Include the custom HTML, CSS and Javascript to be injected in /setup webpage.
54
* HTML code will be injected according to the order of options declaration.
55
* CSS and JavaScript will be appended to the end of body in order to work properly.
56
* In this manner, is also possible override the default element styles
57
* like for example background color, margins, padding etc etc
58
*/
59
#include "customElements.h"
60
#include "thingsboard.h"
61
 
62
// Callback: notify user when the configuration file is saved
63
void onConfigSaved(const char* path) {
64
  Serial.printf("\n[Config] File salvato: %s\n", path);
65
}
66
 
67
////////////////////////////////  Filesystem  /////////////////////////////////////////
68
bool startFilesystem() {
69
  if (FILESYSTEM.begin()){
70
    server.printFileList(FILESYSTEM, "/", 1);
71
    return true;
72
  }
73
  else {
74
    Serial.println("ERROR on mounting filesystem. It will be reformatted!");
75
    FILESYSTEM.format();
76
    ESP.restart();
77
  }
78
  return false;
79
}
80
 
81
////////////////////  Load application options from filesystem  ////////////////////
82
bool loadOptions() {
83
  if (FILESYSTEM.exists(server.getConfiFileName())) {
84
    // Test "options" values
85
    server.getOptionValue(LED_LABEL, ledPin);
86
    server.getOptionValue(BOOL_LABEL, boolVar);
87
    server.getOptionValue(BOOL_LABEL "2", boolVar2);
88
    server.getOptionValue(LONG_LABEL, longVar);
89
    server.getOptionValue(FLOAT_LABEL, floatVar);
90
    server.getOptionValue(STRING_LABEL, stringVar);
91
    server.getOptionValue(DROPDOWN_TEST, dropdownSelected);
92
    server.getSliderValue(brightness);
93
 
94
    // ThingsBoard variables
95
    server.getOptionValue(TB_DEVICE_NAME, tb_deviceName);
96
    server.getOptionValue(TB_DEVICE_LAT, tb_deviceLatitude);
97
    server.getOptionValue(TB_DEVICE_LON, tb_deviceLongitude);
98
    server.getOptionValue(TB_DEVICE_TOKEN, tb_deviceToken);
99
    server.getOptionValue(TB_DEVICE_KEY, tb_device_key);
100
    server.getOptionValue(TB_SECRET_KEY, tb_secret_key);
101
    server.getOptionValue(TB_SERVER, tb_serverIP);
102
    server.getOptionValue(TB_PORT, tb_port);
103
    server.closeSetupConfiguration();  // Close configuration to free resources
104
 
105
    Serial.println("\nThis are the current values stored: \n");
106
    Serial.printf("LED pin value: %d\n", ledPin);
107
    Serial.printf("Bool value 1: %s\n", boolVar ? "true" : "false");
108
    Serial.printf("Bool value 2: %s\n", boolVar2 ? "true" : "false");
109
    Serial.printf("Long value: %u\n", longVar);
110
    Serial.printf("Float value: %d.%d\n", (int)floatVar, (int)(floatVar*1000)%1000);
111
    Serial.printf("String value: %s\n", stringVar.c_str());
112
    Serial.printf("Dropdown selected: %s\n", dropdownSelected.c_str());
113
    Serial.print("Slider value: "); Serial.println(brightness.value);
114
    return true;
115
  }
116
  else {
117
      Serial.println("Failed to parse configuration file");            
118
      return false;
119
  }
120
  return true;
121
}
122
 
123
 
124
void setup() {
125
  pinMode(LED_BUILTIN, OUTPUT);
126
  Serial.begin(115200);
127
 
128
  // FILESYSTEM INIT
129
  if (startFilesystem()){
130
    // Load configuration (if not present, default will be created when webserver will start)
131
    loadOptions();      
132
  }
133
 
134
  // Try to connect to WiFi (will start AP if not connected after timeout)
135
  if (!server.startWiFi(10000)) {
136
    Serial.println("\nWiFi not connected! Starting AP mode...");
137
    server.startCaptivePortal("ESP_AP", "123456789", "/setup");
138
  }
139
 
140
  // Add custom HTTP request handlers to webserver
141
  server.on("/reload", HTTP_GET, [](AsyncWebServerRequest *request){
142
    request->send(200, "text/plain", "Options loaded");
143
    loadOptions();
144
    Serial.println("Application option loaded after web request");
145
  });
146
 
147
  // Add a new options box
148
  server.addOptionBox("My Options");
149
  server.addOption(LED_LABEL, ledPin);
150
  server.addOption(LONG_LABEL, longVar);
151
  // Float fields can be configured with min, max and step properties
152
  server.addOption(FLOAT_LABEL, floatVar, 0.0, 100.0, 0.01);
153
  server.addOption(STRING_LABEL, stringVar);
154
  server.addOption(BOOL_LABEL, boolVar);
155
  server.addOption(BOOL_LABEL "2", boolVar2);
156
  static const char* dropItem[] = {"Item1", "Item2", "Item3"};
157
  AsyncFsWebServer::DropdownList dropdownDef{ DROPDOWN_TEST, dropItem, 3, 0 };
158
  server.addDropdownList(dropdownDef);
159
  server.addSlider(brightness);  
160
 
161
  // Add a new options box with custom code injected
162
  server.addOptionBox("Custom HTML");
163
  // How many times you need (for example one in different option box)
164
  server.addHTML(custom_html, "fetch-test", /*overwrite*/ false);
165
 
166
  // Add a new options box
167
  server.addOptionBox("ThingsBoard");
168
  server.addOption(TB_DEVICE_NAME, tb_deviceName);
169
  server.addOption(TB_DEVICE_LAT, tb_deviceLatitude, -180.0, 180.0, 0.00001);
170
  server.addOption(TB_DEVICE_LON, tb_deviceLongitude, -180.0, 180.0, 0.00001);
171
  server.addOption(TB_SERVER, tb_serverIP);
172
  server.addOption(TB_PORT, tb_port);
173
  server.addOption(TB_DEVICE_KEY, tb_device_key);
174
  server.addOption(TB_SECRET_KEY, tb_secret_key);
175
  server.addOption(TB_DEVICE_TOKEN, tb_deviceToken);
176
  server.addHTML(thingsboard_htm, "ts", /*overwrite file*/ false);
177
 
178
  // CSS will be appended to HTML head
179
  server.addCSS(custom_css, "fetch", /*overwrite file*/ false);
180
  // Javascript will be appended to HTML body
181
  server.addJavascript(custom_script, "fetch", /*overwrite file*/ false);
182
  server.addJavascript(thingsboard_script, "ts", /*overwrite file*/ false);
183
 
184
  // Add custom page title to /setup
185
  server.setSetupPageTitle("Custom HTML Web Server");
186
  // Add custom logo to /setup page with custom size
187
  server.setLogoBase64(base64_logo, "128", "128", /*overwrite file*/ false);
188
 
189
  // Enable ACE FS file web editor and add FS info callback function    
190
  server.enableFsCodeEditor();
191
 
192
  // Inform user when config.json is saved via /edit or /upload
193
  server.setConfigSavedCallback(onConfigSaved);
194
 
195
  // Start web server
196
  if (server.init()) {
197
    Serial.print(F("\n\nWeb Server started on IP Address: "));
198
    Serial.println(server.getServerIP());
199
    Serial.println(F(
200
      "\nThis is \"customHTML.ino\" example.\n"
201
      "Open /setup page to configure optional parameters.\n"
202
      "Open /edit page to view, edit or upload example or your custom webserver source files."
203
    ));
204
    Serial.printf("Ready! Open http://%s.local in your browser\n", hostname);
205
    if (server.isAccessPointMode())
206
      Serial.print(F("Captive portal is running"));
207
  }
208
 
209
}
210
 
211
 
212
void loop() {
213
  if (server.isAccessPointMode())
214
    server.updateDNS();
215
 
216
  // Nothing to do here, just a small delay for task yield
217
  delay(10);  
218
}