1
+ /* Async Webpage server */
2
+
3
+ #include < SBHttp_Server.h>
4
+ // #include <HTTP_Server_Basic.h>
5
+ #include < smartbike_parameters.h>
6
+ #include < Main.h>
7
+
8
+ // Import required libraries
9
+ #include < Arduino.h>
10
+ #include < WiFi.h>
11
+ #include < WiFiAP.h>
12
+ #include < ESPAsyncWebServer.h>
13
+ #include < AsyncTCP.h>
14
+ #include < SPIFFS.h>
15
+ #include < FS.h>
16
+ #include < string.h>
17
+ #define MAX_BUFFER_SIZE 20
18
+
19
+ // Set LED GPIO
20
+ const int ledPin = 2 ; // Blue LED on the ESP32
21
+ // Stores LED state
22
+ String ledState;
23
+ // Webpage Parameter varables
24
+ const char * webStringParameter = " state" ;
25
+ const char * webIntParameter = " value" ;
26
+ int i; // Multi use integer for general purpose
27
+
28
+ /* *******************Added Function for SPIFFS File Testing*****************************/
29
+ void printFile (String filename) {
30
+ // Open file for reading
31
+ Serial.printf (" Contents of file: %s/n" , filename.c_str ());
32
+ File file = SPIFFS.open (filename);
33
+ if (!file) {
34
+ Serial.println (F (" Failed to read file" ));
35
+ return ;
36
+ }
37
+
38
+ // Extract each characters by one by one
39
+ while (file.available ()) {
40
+ Serial.print ((char )file.read ());
41
+ }
42
+ Serial.println ();
43
+
44
+ // Close the file
45
+ file.close ();
46
+ }
47
+
48
+ /* **************************************************************************************/
49
+ // Create AsyncWebServer object on port 80
50
+ AsyncWebServer server (80 );
51
+
52
+ // Replaces placeholder with LED state value
53
+ String processor (const String& var){
54
+ char outString[MAX_BUFFER_SIZE];
55
+ Serial.printf (" Processing Variable from HTTP Server: %s \n " , var.c_str ());
56
+
57
+ if (var == " STATE" ){
58
+ if (digitalRead (ledPin)){
59
+ ledState = " ON" ;
60
+ }
61
+ else {
62
+ ledState = " OFF" ;
63
+ }
64
+ Serial.printf (" LED State is %s \n " , ledState.c_str ());
65
+ return ledState;
66
+ }
67
+
68
+ if (var ==" HROUTPUT" ){
69
+ if (config.getSimulateHr ()){return " checked" ;}
70
+ else {return " " ;}
71
+ }
72
+
73
+ if (var ==" WATTSOUTPUT" ){
74
+ if (config.getSimulatePower ()){return " checked" ;}
75
+ else {return " " ;}
76
+ }
77
+
78
+ if (var ==" HRVALUE" ){
79
+ snprintf (outString, MAX_BUFFER_SIZE, " %d" , config.getSimulatedHr ());
80
+ strncat (outString, " BPM" , 4 );
81
+ // Serial.printf("outString: %s \n", outString);
82
+ return outString;
83
+ }
84
+
85
+ if (var ==" WATTSVALUE" ){
86
+ snprintf (outString, MAX_BUFFER_SIZE, " %d" , config.getSimulatedWatts ());
87
+ strncat (outString, " Watts" , 6 );
88
+ return outString;
89
+ }
90
+
91
+ return String ();
92
+ }
93
+
94
+ void startHttpServer (){
95
+
96
+ pinMode (ledPin, OUTPUT);
97
+
98
+ // Connect to Wi-Fi
99
+ Serial.println ();
100
+ Serial.println (" Configuring access point..." );
101
+ WiFi.softAP (config.getSsid (), config.getPassword ());
102
+ vTaskDelay (50 );
103
+ IPAddress myIP = WiFi.softAPIP ();
104
+ Serial.print (" AP IP address: " );
105
+ Serial.println (myIP);
106
+ server.begin ();
107
+ Serial.println (" WiFi Server started" );
108
+
109
+ // Route for root / web page
110
+ /* I think we need to expand this one server instance to keep from crashing instead of using multiple server.on instances */
111
+ server.on (" /" , HTTP_GET, [](AsyncWebServerRequest *request){
112
+ // request->send(SPIFFS, "/index.html", "text/html");
113
+ request->send (SPIFFS, " /index.html" , String (), true , processor);
114
+ // request->
115
+ // request->send(SPIFFS, "/style.css", "text/css");
116
+ Serial.println (" Index.html Sent" );
117
+ // printFile("/index.html");
118
+ });
119
+
120
+ // hopefully we can eventually delete all of this below
121
+ // Route to load style.css file
122
+ server.on (" /style.css" , HTTP_GET, [](AsyncWebServerRequest *request){
123
+ request->send (SPIFFS, " /style.css" , " text/css" );
124
+ Serial.println (" /style.css Sent" );
125
+ printFile (" /style.css" );
126
+ });
127
+ // Route to set GPIO to HIGH
128
+ server.on (" /on" , HTTP_GET, [](AsyncWebServerRequest *request){
129
+ digitalWrite (ledPin, HIGH);
130
+ request->send (SPIFFS, " /index.html" , String (), true , processor);
131
+ });
132
+
133
+ // Route to set GPIO to LOW
134
+ server.on (" /off" , HTTP_GET, [](AsyncWebServerRequest *request){
135
+ digitalWrite (ledPin, LOW);
136
+ request->send (SPIFFS, " /index.html" , String (), true , processor);
137
+ });
138
+
139
+ // ***************************************Trying to get a value from a range slider
140
+ server.on (" /hrslider" , HTTP_GET, [] (AsyncWebServerRequest *request) {
141
+ // GET input1 value on <ESP_IP>/hrslider?value=<inputMessage>
142
+ String inputMessage;
143
+ if (request->hasParam (webIntParameter)) {
144
+ inputMessage = request->getParam (webIntParameter)->value ();
145
+ config.setSimulatedHr (inputMessage.toInt ());
146
+ Serial.printf (" Heart Rate in Config is %d\n " , config.getSimulatedHr ());
147
+ }
148
+ else {
149
+ inputMessage = " No message sent" ;
150
+ }
151
+ Serial.println (inputMessage);
152
+ request->send (SPIFFS, " /index.html" , String (), true , processor);
153
+ // request->send(200, "text/plain", "OK");
154
+ });
155
+
156
+ server.on (" /wattsslider" , HTTP_GET, [] (AsyncWebServerRequest *request) {
157
+ // GET input1 value on <ESP_IP>/hrslider?value=<inputMessage>
158
+ String inputMessage;
159
+ if (request->hasParam (webIntParameter)) {
160
+ inputMessage = request->getParam (webIntParameter)->value ();
161
+ config.setSimulatedWatts (inputMessage.toInt ());
162
+ Serial.printf (" Watt in Config is %d\n " , config.getSimulatedWatts ());
163
+ }
164
+ else {
165
+ inputMessage = " No message sent" ;
166
+ }
167
+ Serial.println (inputMessage);
168
+ request->send (SPIFFS, " /index.html" , String (), true , processor);
169
+ // request->send(200, "text/plain", "OK");
170
+ });
171
+
172
+ server.on (" /hrenable" , HTTP_GET, [] (AsyncWebServerRequest *request) {
173
+ // GET input1 value on <ESP_IP>/hrslider?value=<inputMessage>
174
+ String inputMessage;
175
+ if (request->hasParam (webIntParameter)) {
176
+ inputMessage = request->getParam (webIntParameter)->value ();
177
+ if (inputMessage == " checked" ){config.setSimulateHr (true );}
178
+ else {config.setSimulateHr (false );}
179
+ Serial.printf (" HR output is set to %s\n " , config.getSimulateHr () ? " On" :" Off" );
180
+ }
181
+ else {
182
+ inputMessage = " No message sent" ;
183
+ }
184
+ Serial.println (inputMessage);
185
+ request->send (SPIFFS, " /index.html" , String (), true , processor);
186
+ // request->send(200, "text/plain", "OK");
187
+ });
188
+
189
+ server.on (" /wattsenable" , HTTP_GET, [] (AsyncWebServerRequest *request) {
190
+ // GET input1 value on <ESP_IP>/hrslider?value=<inputMessage>
191
+ String inputMessage;
192
+ if (request->hasParam (webIntParameter)) {
193
+ inputMessage = request->getParam (webIntParameter)->value ();
194
+ if (inputMessage == " checked" ){config.setSimulatePower (true );}
195
+ else {config.setSimulatePower (false );}
196
+ Serial.printf (" Power output is set to %s\n " , config.getSimulatePower () ? " On" :" Off" );
197
+ }
198
+ else {
199
+ inputMessage = " No message sent" ;
200
+ }
201
+ Serial.println (inputMessage);
202
+ request->send (SPIFFS, " /index.html" , String (), true , processor);
203
+ // request->send(200, "text/plain", "OK");
204
+ });//
205
+ // ********************************End Range Slider testing**********************************
206
+ // Start server
207
+ server.begin ();
208
+
209
+ Serial.println (" End of Http setup" );
210
+
211
+ }
212
+ /*
213
+ void loop(){
214
+ //Autosave every 30 seconds
215
+ vTaskDelay(30000/portTICK_PERIOD_MS);
216
+ Serial.println("Autosaving Configuration");
217
+ config.saveToSPIFFS();
218
+ config.printFile();
219
+ }*/
0 commit comments