5
5
6
6
* This file is part of the Commander-API project.
7
7
* Modified 2022.04.24
8
- *
8
+ *
9
9
* This is a simple example sketch that shows how
10
10
* to use Commander-API library.
11
11
*/
15
15
#include " Commander-API.hpp"
16
16
#include " Commander-IO.hpp"
17
17
18
+ // This example assumes that there is a LED on GPIO-2,
19
+ // like on ESP32 NodeMCU boards.
20
+ #define LED_PIN 2
21
+
18
22
// We have to create an object from Commander class.
19
23
Commander commander;
20
24
@@ -58,16 +62,16 @@ WiFiServer server( SERVER_PORT );
58
62
void setup () {
59
63
60
64
// Set the LED pin to output, and turn it off.
61
- pinMode ( LED_BUILTIN , OUTPUT );
62
- digitalWrite ( LED_BUILTIN , 0 );
63
-
65
+ pinMode ( LED_PIN , OUTPUT );
66
+ digitalWrite ( LED_PIN , 0 );
67
+
64
68
// In this example we will use the Serial for communication,
65
69
// so we have to initialize it.
66
70
Serial.begin ( 115200 );
67
71
68
72
// Step 1.
69
73
Serial.println ( " Step 1." );
70
-
74
+
71
75
// There is an option to attach a debug channel to Commander.
72
76
// It can be handy to find any problems during the initialization
73
77
// phase. In this example we will use Serial for this.
@@ -99,7 +103,7 @@ void setup() {
99
103
// To execute a command we have to use the execute command. Let's try
100
104
// the led command. This command just toggles the built-in LED.
101
105
commander.execute ( " led" );
102
-
106
+
103
107
// Example 2.
104
108
Serial.println ();
105
109
Serial.println ( " Example 2." );
@@ -184,10 +188,10 @@ void setup() {
184
188
Serial.println ( SERVER_PORT );
185
189
Serial.println ( " Now you can play with commander with serial port or with socket." );
186
190
Serial.println ( " To try socket communication I suggest PuTTY." );
187
-
191
+
188
192
189
193
server.begin ();
190
-
194
+
191
195
}
192
196
193
197
// Continous example.
@@ -214,48 +218,48 @@ void loop() {
214
218
while ( client.connected () ){
215
219
216
220
while ( client.available () ){
217
-
221
+
218
222
// Read the next incoming character.
219
223
c = client.read ();
220
-
224
+
221
225
// Every command from Serial is terminated with a new-line
222
226
// character. If a new-line character arrives we have to
223
227
// terminate the string in the commandFromSerial buffer,
224
228
// and execute it. After execution we have to reset the
225
229
// commandIndex counter to zero.
226
230
if ( c == ' \n ' ){
227
-
231
+
228
232
commandFromSerial[ commandIndex ] = ' \0 ' ;
229
233
commander.execute ( commandFromSerial, &client );
230
234
commandIndex = 0 ;
231
-
235
+
232
236
}
233
-
237
+
234
238
// If we have a carriage-return character we simply
235
239
// ignore it.
236
240
else if ( c == ' \r ' ){
237
241
continue ;
238
242
}
239
-
243
+
240
244
// Every other case we just put the data to the next
241
245
// free space in the commandFromSerial buffer, increment
242
246
// the commandIndex, and check if it want's to overflow.
243
247
else {
244
-
248
+
245
249
commandFromSerial[ commandIndex ] = c;
246
250
commandIndex++;
247
251
if ( commandIndex >= 20 ){
248
252
commandIndex = 19 ;
249
253
}
250
-
254
+
251
255
}
252
-
256
+
253
257
}
254
258
255
259
delay ( 10 );
256
-
260
+
257
261
}
258
-
262
+
259
263
}
260
264
261
265
// Check if there is any data incoming.
@@ -274,7 +278,7 @@ void loop() {
274
278
commandFromSerial[ commandIndex ] = ' \0 ' ;
275
279
commander.execute ( commandFromSerial, &Serial );
276
280
commandIndex = 0 ;
277
-
281
+
278
282
}
279
283
280
284
// If we have a carriage-return character we simply
@@ -293,9 +297,9 @@ void loop() {
293
297
if ( commandIndex >= 20 ){
294
298
commandIndex = 19 ;
295
299
}
296
-
300
+
297
301
}
298
-
302
+
299
303
}
300
304
301
305
}
@@ -321,7 +325,7 @@ void dog_func(char *args, commandResponse *response )
321
325
void led_func (char *args, commandResponse *response )
322
326
{
323
327
324
- digitalWrite ( LED_BUILTIN , !digitalRead ( LED_BUILTIN ) );
328
+ digitalWrite ( LED_PIN , !digitalRead ( LED_PIN ) );
325
329
326
330
}
327
331
@@ -353,7 +357,7 @@ void sum_func(char *args, commandResponse *response )
353
357
354
358
// Sadly we have to stop the command execution and return.
355
359
return ;
356
-
360
+
357
361
}
358
362
359
363
// Calculate the sum.
0 commit comments