1
+ /*
2
+ -----------------------------
3
+ ESPDASH Pro - Benchmark Example
4
+ -----------------------------
5
+ Use this benchmark example to test if ESP-DASH Pro is working properly on your platform.
6
+
7
+ Github: https://github.com/ayushsharma82/ESP-DASH
8
+ WiKi: https://docs.espdash.pro
9
+
10
+ Works with both ESP8266 & ESP32
11
+ -------------------------------
12
+ */
13
+
14
+ #include < Arduino.h>
15
+ #if defined(ESP8266)
16
+ /* ESP8266 Dependencies */
17
+ #include < ESP8266WiFi.h>
18
+ #include < ESPAsyncTCP.h>
19
+ #include < ESPAsyncWebServer.h>
20
+ #elif defined(ESP32)
21
+ /* ESP32 Dependencies */
22
+ #include < AsyncTCP.h>
23
+ #include < ESPAsyncWebServer.h>
24
+ #include < WiFi.h>
25
+ #endif
26
+
27
+ #include < ESPDash.h>
28
+
29
+ /* Your WiFi Credentials */
30
+ const char * ssid = " " ; // SSID
31
+ const char * password = " " ; // Password
32
+
33
+ /* Start Webserver */
34
+ AsyncWebServer server (80 );
35
+
36
+ /* Attach ESP-DASH to AsyncWebServer */
37
+ ESPDash dashboard (server, true );
38
+
39
+ // Cards
40
+ dash::FeedbackCard feedback (dashboard, " Status" , dash::Status::SUCCESS);
41
+ dash::GenericCard genericString (dashboard, " Generic String" );
42
+ dash::GenericCard<float > genericFloat (dashboard, " Generic Float" );
43
+ dash::GenericCard<int > genericInt (dashboard, " Generic Int" );
44
+ dash::HumidityCard<float , 3 > hum (dashboard, " Humidity" ); // set decimal precision is 3
45
+ dash::ProgressCard<float , 4 > progressFloat (dashboard, " Progress Float" , 0 , 1 , " kWh" );
46
+ dash::ProgressCard progressInt (dashboard, " Progress Int" , 0 , 100 , " %" );
47
+ dash::SliderCard<float , 4 > sliderFloatP4 (dashboard, " Float Slider (4)" , 0 , 1 , 0 .0001f );
48
+ dash::SliderCard<float > sliderFloatP2 (dashboard, " Float Slider (2)" , 0 , 1 , 0 .01f );
49
+ dash::SliderCard sliderInt (dashboard, " Int Slider" , 0 , 255 , 1 , " bits" );
50
+ dash::SliderCard<uint32_t > updateDelay (dashboard, " Update Delay" , 1000 , 20000 , 1000 , " ms" );
51
+ dash::SwitchCard button (dashboard, " Button" );
52
+ dash::TemperatureCard temp (dashboard, " Temperature" ); // default precision is 2
53
+
54
+ // Charts
55
+ dash::BarChart<const char *, int > bar (dashboard, " Power Usage (kWh)" );
56
+
57
+ // Custom Statistics
58
+ dash::StatisticValue stat1 (dashboard, " Statistic 1" );
59
+ dash::StatisticValue<float , 4 > stat2 (dashboard, " Statistic 2" );
60
+ dash::StatisticProvider<uint32_t > statProvider (dashboard, " Statistic Provider" );
61
+
62
+ uint8_t test_status = 0 ;
63
+
64
+ /* *
65
+ * Note how we are keeping all the chart data in global scope.
66
+ */
67
+ // Bar Chart Data
68
+ const char * BarXAxis[] = {" 1/4/22" , " 2/4/22" , " 3/4/22" , " 4/4/22" , " 5/4/22" , " 6/4/22" , " 7/4/22" , " 8/4/22" , " 9/4/22" , " 10/4/22" , " 11/4/22" , " 12/4/22" , " 13/4/22" , " 14/4/22" , " 15/4/22" , " 16/4/22" , " 17/4/22" , " 18/4/22" , " 19/4/22" , " 20/4/22" , " 21/4/22" , " 22/4/22" , " 23/4/22" , " 24/4/22" , " 25/4/22" , " 26/4/22" , " 27/4/22" , " 28/4/22" , " 29/4/22" , " 30/4/22" };
69
+ int BarYAxis[] = {0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 };
70
+
71
+ unsigned long last_update_millis = 0 ;
72
+ uint32_t update_delay = 2000 ;
73
+
74
+ void setup () {
75
+ Serial.begin (115200 );
76
+ Serial.println ();
77
+ /* Connect WiFi */
78
+
79
+ // WiFi.persistent(false);
80
+ // WiFi.mode(WIFI_STA);
81
+ // WiFi.begin(ssid, password);
82
+ // while (WiFi.status() != WL_CONNECTED) {
83
+ // delay(500);
84
+ // Serial.print(".");
85
+ // }
86
+ // Serial.print("IP Address: ");
87
+ // Serial.println(WiFi.localIP());
88
+
89
+ WiFi.mode (WIFI_AP);
90
+ WiFi.softAP (" esp-captive" );
91
+
92
+ /* Attach Button Callback */
93
+ button.onChange ([&](bool state) {
94
+ /* Print our new button value received from dashboard */
95
+ Serial.println (String (" Button Triggered: " ) + (state ? " true" : " false" ));
96
+ /* Make sure we update our button's value and send update to dashboard */
97
+ button.setValue (state);
98
+ dashboard.refresh (button);
99
+ });
100
+
101
+ // Set Slider Index
102
+ sliderInt.setIndex (1 );
103
+
104
+ /* Attach Slider Callback */
105
+ sliderInt.onChange ([&](int value) {
106
+ /* Print our new slider value received from dashboard */
107
+ Serial.println (" Slider Triggered: " + String (value));
108
+ /* Make sure we update our slider's value and send update to dashboard */
109
+ sliderInt.setValue (value);
110
+ dashboard.refresh (sliderInt);
111
+ });
112
+
113
+ sliderFloatP2.setIndex (2 );
114
+ sliderFloatP2.onChange ([&](float value) {
115
+ Serial.println (" Slider Float P2 Triggered: " + String (value));
116
+ sliderFloatP2.setValue (value);
117
+ dashboard.refresh (sliderFloatP2);
118
+ });
119
+
120
+ sliderFloatP4.setIndex (3 );
121
+ sliderFloatP4.onChange ([&](float value) {
122
+ Serial.println (" Slider Float P4 Triggered: " + String (value, 4 ));
123
+ sliderFloatP4.setValue (value);
124
+ dashboard.refresh (sliderFloatP4);
125
+ });
126
+
127
+ updateDelay.setValue (update_delay);
128
+ updateDelay.onChange ([&](uint32_t value) {
129
+ update_delay = value;
130
+ updateDelay.setValue (value);
131
+ dashboard.refresh (updateDelay);
132
+ });
133
+
134
+ stat1.setValue (" Value 1" );
135
+ stat2.setValue (10.0 / 3.0 );
136
+ statProvider.setProvider ([]() { return millis (); });
137
+
138
+ bar.setX (BarXAxis, 30 );
139
+
140
+ genericFloat.setValue (10.0 / 3.0 ); // default rounding is 2
141
+ genericString.setValue (" Hello World!" );
142
+
143
+ /* Start AsyncWebServer */
144
+ server.begin ();
145
+
146
+ server.onNotFound ([](AsyncWebServerRequest* request) {
147
+ request->send (404 );
148
+ });
149
+ }
150
+
151
+ void loop () {
152
+ // Update Everything every 2 seconds using millis if connected to WiFi
153
+ if (millis () - last_update_millis > update_delay && dashboard.hasClient ()) {
154
+ last_update_millis = millis ();
155
+
156
+ // Randomize Bar Chart YAxis Values ( for demonstration purposes only )
157
+ for (int i = 0 ; i < 30 ; i++) {
158
+ BarYAxis[i] = (int )random (0 , 200 );
159
+ }
160
+
161
+ /* Update Chart Y Axis (yaxis_array, array_size) */
162
+ bar.setY (BarYAxis, 30 );
163
+
164
+ // Update all cards with random values
165
+ genericInt.setSymbol (random (0 , 2 ) ? " unit1" : " unit2" );
166
+ genericInt.setValue ((int )random (0 , 100 ));
167
+ temp.setValue (random (0 , 100 ) / 3.0 );
168
+ hum.setValue (random (0 , 100 ) / 3.0 );
169
+
170
+ progressInt.setValue (random (0 , 200 )); // if more than max, clamped to max
171
+ progressFloat.setValue (random (0 , 1000 ) / 2000.0 ); // if more than max, clamped to max
172
+
173
+ sliderInt.setValue (random (0 , 200 )); // clamped at 255 by max
174
+ sliderFloatP2.setValue (random (0 , 100 ) / 333.0 );
175
+ sliderFloatP4.setValue (random (0 , 100 ) / 333.0 );
176
+
177
+ // Loop through statuses
178
+ if (test_status == 0 ) {
179
+ feedback.setFeedback (" Success Msg!" , dash::Status::SUCCESS);
180
+ test_status = 1 ;
181
+ } else if (test_status == 1 ) {
182
+ feedback.setFeedback (" Warning Msg!" , dash::Status::WARNING);
183
+ test_status = 2 ;
184
+ } else if (test_status == 2 ) {
185
+ feedback.setFeedback (" Danger Msg!" , dash::Status::DANGER);
186
+ test_status = 3 ;
187
+ } else if (test_status == 3 ) {
188
+ feedback.setFeedback (" Idle Msg!" , dash::Status::IDLE);
189
+ test_status = 0 ;
190
+ }
191
+
192
+ if (random (0 , 2 ))
193
+ button.toggle ();
194
+
195
+ // Get Free Heap
196
+ Serial.println (" Free Heap (Before Update): " + String (ESP.getFreeHeap ()));
197
+ dashboard.sendUpdates ();
198
+ Serial.println (" Free Heap (After Update): " + String (ESP.getFreeHeap ()));
199
+ }
200
+ }
0 commit comments