• Skip to main content
  • Skip to primary sidebar

RNTLab.com

The Ultimate Shortcut to Learn Electronics and Programming with Open Source Hardware and Software

  • Courses
  • Forum
    • Forum
    • Ask Question
  • Shop
  • Account
  • Blog
  • Login

Declaring const(s)

Q&A Forum › Category: ESP32 › Declaring const(s)
0 Vote Up Vote Down
Richard Funnell asked 3 years ago

Hi, I am following the Build Web Servers ESP32 course, I am using “4.1 Web Server with input fields” as a base for my project. 
Where this only uses 2 inputs:
// Search for parameter in HTTP POST request
const char* PARAM_INPUT_1 = “input1”;
const char* PARAM_INPUT_2 = “input2”;
//Variables to save values from HTML form
String input1;
String input2;
// File paths to save input values permanently
const char* input1Path = “/input1.txt”;
const char* input2Path = “/input2.txt”;
I am using (SO FAR) :

// Search for parameter in HTTP POST request
const char* PARAM_INPUT_1 = “input1”;
const char* PARAM_INPUT_1A = “input1A”;
const char* PARAM_INPUT_1B = “input1B”;
const char* PARAM_INPUT_1C = “input1C”;
const char* PARAM_INPUT_2 = “input2”;
const char* PARAM_INPUT_2A = “input2A”;
const char* PARAM_INPUT_2B = “input2B”;
const char* PARAM_INPUT_2C = “input2C”;
const char* PARAM_INPUT_3 = “input3”;
const char* PARAM_INPUT_3A = “input3A”;
const char* PARAM_INPUT_3B = “input3B”;
const char* PARAM_INPUT_3C = “input3C”;
const char* PARAM_INPUT_4 = “input4”;
const char* PARAM_INPUT_4A = “input4A”;
const char* PARAM_INPUT_4B = “input4B”;
const char* PARAM_INPUT_4C = “input4C”;
const char* PARAM_INPUT_5 = “input5”;
const char* PARAM_INPUT_5A = “input5A”;
const char* PARAM_INPUT_5B = “input5B”;
const char* PARAM_INPUT_5C = “input5C”;

//Variables to save values from HTML form
String input1;
String input1A;
String input1B;
String input1C;
String input2;
String input2A;
String input2B;
String input2C;
String input3;
String input3A;
String input3B;
String input3C;
String input4;
String input4A;
String input4B;
String input4C;
String input5;
String input5A;
String input5B;
String input5C;

// File paths to save input values permanently
const char* input1Path = “/input1.txt”;
const char* input1APath = “/input1A.txt”;
const char* input1BPath = “/input1B.txt”;
const char* input1CPath = “/input1C.txt”;
const char* input2Path = “/input2.txt”;
const char* input2APath = “/input2A.txt”;
const char* input2BPath = “/input2B.txt”;
const char* input2CPath = “/input2C.txt”;
const char* input3Path = “/input3.txt”;
const char* input3APath = “/input3A.txt”;
const char* input3BPath = “/input3B.txt”;
const char* input3CPath = “/input3C.txt”;
const char* input4Path = “/input4.txt”;
const char* input4APath = “/input4A.txt”;
const char* input4BPath = “/input4B.txt”;
const char* input4CPath = “/input4C.txt”;
const char* input5Path = “/input5.txt”;
const char* input5APath = “/input5A.txt”;
const char* input5BPath = “/input5B.txt”;
const char* input5CPath = “/input5C.txt”;

(NEED TO USE up to 12)
 
Is there anyway you can use a for loop to assign these instead of one per line?
 
Would be grateful for any help, (beginner, but learning!!)

14 Answers
0 Vote Up Vote Down
Richard Funnell answered 3 years ago

Forgot these:

String getCurrentInputValues(){
  values[“numberValue1”] = input1;
  values[“numberValue1A”] = input1A;
  values[“numberValue1B”] = input1B;
  values[“numberValue1C”] = input1C;  
  values[“numberValue2”] = input2;
  values[“numberValue2A”] = input2A;
  values[“numberValue2B”] = input2B;
  values[“numberValue2C”] = input2C;
  values[“numberValue3”] = input3;
  values[“numberValue3A”] = input3A;
  values[“numberValue3B”] = input3B;
  values[“numberValue3C”] = input3C;
  values[“numberValue4”] = input4;
  values[“numberValue4A”] = input4A;
  values[“numberValue4B”] = input4B;
  values[“numberValue4C”] = input4C;
  values[“numberValue5”] = input5;
  values[“numberValue5A”] = input5A;
  values[“numberValue5B”] = input5B;
  values[“numberValue5C”] = input5C;

0 Vote Up Vote Down
Sara Santos Staff answered 3 years ago

Hi.
Take a look at “arrays” with Arduino. You can save your values in arrays and then, loop through them.
This is just the basic definition: https://www.arduino.cc/en/reference/array
And here’s a basic example: https://docs.arduino.cc/built-in-examples/control-structures/Arrays
This is just the starting point, then, you should be able to search for more information if you want to apply it to your specific project.
I hope this helps.
Regards,
Sara
 
 

0 Vote Up Vote Down
Richard Funnell answered 3 years ago

Many thanks Sara, will look into it.

0 Vote Up Vote Down
Steve Mercer answered 3 years ago

Also, you should look into structs

0 Vote Up Vote Down
Richard Funnell answered 3 years ago

Thanks Steve, A new command for me, will see if I can relate it to my problem. many thanks.

0 Vote Up Vote Down
Steve Mercer answered 3 years ago

Once you understand structs and arrays, then you can think about arrays of structs. Basically each item, in your case an “input”, would be one struct with all of the properties that you want to keep in it. Adding another item would only require adding a new struct rather than having to declare all of the properties over again. If you need another property it’s just a simple matter of adding it to the struct definition.

0 Vote Up Vote Down
Steve Mercer answered 3 years ago

Perhaps an example:
 

struct input {

  String request; // Parameter in HTTP POST request HTML form
  String formValue; //Variable to save value from HTML form to search for
  String path; // File path to save input value permanently
  String value; // Current input value
  int pin; // Input pin
}

 
input input 1 = {“input1”, “”, “/input1.txt”, “”, 1};
etc
 
 

0 Vote Up Vote Down
Richard Funnell answered 3 years ago

wow, I will try and follow your example, and digest it on a small scale. Many thanks Steve

0 Vote Up Vote Down
Richard Funnell answered 3 years ago

Hi,
Have read a lot about struct & arrays and tried a simple program to try and understand, see below, but I have an error, “expected identifier before numeric constant” in it is on line 4   ‘struct tablet[2] {‘ I don’t know if I am on the right track, or if I need a Library ? please could you help. 
 
 
 
 
struct tablet[2] {
  String request;
  String formValue1;
  String formValue2;
  String formValue3;
  String path;
}
int i;
 
void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
 
}
 
void loop() {
  // put your main code here, to run repeatedly:
 
for (i=0; i<2; i++){
 
  Serial.print (tablet[i].request);
  Serial.print (tablet[i].formValue1);
  Serial.print (tablet[i].formValue2);
  Serial.print (tablet[i].formValue3);
  Serial.print (tablet[i].path);
 
 
delay(5000);
}

0 Vote Up Vote Down
Steve Mercer answered 3 years ago

When you declare the struct you don’t use array notation. You use that only with the variables that you create. So take out the [2] after the struct name.

 

let’s see if I can explain it better. Think of a struct as declaring a blueprint for something, let’s say a drawer. It will have sides, a back, a front, a pull etc. each time you create a drawer it may have different lengths, widths etc. The blueprint has a name you give it and then you create instances of the blueprint. Each instance can be placed in an array.

0 Vote Up Vote Down
Richard Funnell answered 3 years ago

Thanks Steve, removed [2],  and put it on the end with a variable array.
Also tested changing the values.
So I have been working with project 4.1 in the ESP32 Web server book and will go back to using only 2 input forms (instead of the 5 inputs I have got working) to see if I can change the way it works using Struct instead.
This code works OK  thanks to you.  many thanks
struct tablet_def {
  String request;
  String formValue1;
  String formValue2;
  String formValue3;
  String path;
} tablet_monitor[2];
int i;
 
 
 
void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
 
}
 
void loop() {
  // put your main code here, to run repeatedly:
 
for (i=0; i<2; i++){
  Serial.println (i);
  Serial.print (“request “); Serial.println (tablet_monitor[i].request);
  Serial.print (“formValue1 “);  Serial.println (tablet_monitor[i].formValue1);
  Serial.print (“formValue2 “); Serial.println(tablet_monitor[i].formValue2);
  Serial.print (“formValue3 “); Serial.println(tablet_monitor[i].formValue3);
  Serial.print (“path “); Serial.println (tablet_monitor[i].path);
 
  }
delay(5000);
 
tablet_monitor[0].request = “This  Request”;
 
tablet_monitor[1].path = “/input1.txt”;
}

0 Vote Up Vote Down
Steve Mercer answered 3 years ago

Structs are a good intro to Object Oriented Programming. This is where everyone should be going.

0 Vote Up Vote Down
Sara Santos Staff answered 3 years ago

Thank you all for this informative discussion.
I’ll close this issue now.
Regards,
Sara

0 Vote Up Vote Down
Richard Funnell answered 3 years ago

Thank you

Primary Sidebar

Login to Ask or Answer Questions

This Forum is private and it’s only available for members enrolled in our Courses.

Login »

Latest Course Updates

  • [eBook Updated] Learn Raspberry Pi Pico/Pico W with MicroPython eBook – Version 1.2 May 26, 2025
  • [New Edition] Build ESP32-CAM Projects eBook – 2nd Edition April 16, 2025

You must be logged in to view this content.

Contact Support - Refunds - Privacy - Terms - MakerAdvisor.com - Member Login

Copyright © 2013-2025 · RandomNerdTutorials.com · All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.