help needed to try and shorten code

hello i am attempting to write a program to control pumps i have 16 pumps to control
this is done via web interface . i am using a NodeMCU 1.0 (ESP-12E Module) with the

and this block of statements to read what the html posts

server.on("/", HTTP_GET, handleRoot);
  server.on("/PUMP0", HTTP_POST, pump_0);
  server.on("/PUMP0x2", HTTP_POST, pump_0x2);
  server.on("/PUMP1", HTTP_POST, pump_1);
  server.on("/PUMP1x2", HTTP_POST, pump_1x2);
  server.on("/PUMP2", HTTP_POST, pump_2);
  server.on("/PUMP2x2", HTTP_POST, pump_2x2);
  server.on("/PUMP3", HTTP_POST, pump_3);
  server.on("/PUMP3x2", HTTP_POST, pump_3x2);
  server.on("/PUMP4", HTTP_POST, pump_4);
  server.on("/PUMP4x2", HTTP_POST, pump_4x2);
  server.on("/PUMP5", HTTP_POST, pump_5);
  server.on("/PUMP5x2", HTTP_POST, pump_5x2);
  server.on("/PUMP6", HTTP_POST, pump_6);
  server.on("/PUMP6x2", HTTP_POST, pump_6x2);
  server.on("/PUMP7", HTTP_POST, pump_7);
  server.on("/PUMP7x2", HTTP_POST, pump_7x2);
  server.on("/PUMP8", HTTP_POST, pump_8);
  server.on("/PUMP8x2", HTTP_POST, pump_8x2);
  server.on("/PUMP9", HTTP_POST, pump_9);
  server.on("/PUMP9x2", HTTP_POST, pump_9x2);
  server.on("/PUMP10", HTTP_POST, pump_10);
  server.on("/PUMP10x2", HTTP_POST, pump_10x2);
  server.on("/PUMP11", HTTP_POST, pump_11);
  server.on("/PUMP11x2", HTTP_POST, pump_11x2);
  server.on("/PUMP12", HTTP_POST, pump_12);
  server.on("/PUMP12x2", HTTP_POST, pump_12x2);
  server.on("/PUMP13", HTTP_POST, pump_13);
  server.on("/PUMP13x2", HTTP_POST, pump_13x2);

each one does the same thing just points to a function to run

i just wondered if there was a cleaner shorter way to achieve this
as it does seem a long winded way to do it.


Pass the pump number as a parameter in the URL, decode in your handler

any chance of a simple example of that please

There are tons of examples on line

Here is the first one I found.

Instead of sending xxxx/?led=ON or xxxx/?led=OFF (which would trigger handleRoot) you would send xxxx/?pump=3x2 and extract 3x2 to deal with the request

Best would be to have only numbers as pump identifiers and this way you could use that as an index in an array in your code to get rid of all the if

thanks for the advice i am trying to google the method but not having much luck , very new to arduino and the IDE so worked through the examples and starting to try and write my own code rather that cut n paste , retired plumber trying to fill in the time i now have , starting so late in life my brain is not as sharp as you young guns so what seems like a really fine answer to my issue is maybe a little over my head , i know maybe i should have took up oil painting but trying to keep my brain active and this seemed like a good idea at the time now i'm not so sure but thanks for the advice one day i may even understand it :slight_smile:

LOL I’m probably your age but have been exposed to computer science for longer than you. Practice practice practice that’s the key.

just start with simple things and make sure you master the basics (micro controller architecture, language syntax/grammar, code structure, existing functions, etc) and build upon those, don’t try to get to the most complex stuff right away!

Stick to it and you’ll find it as interesting as painting I’m sure (which is also a long learning curve) and very rewarding when stuff you build work as intended

great words of encouragement gives me hope...

i did find what i think you are referring to un the examples when i switched to d1 mini in ide

void handlePlain() {
if (server.method() != HTTP_POST) {
digitalWrite(led, 1);
server.send(405, "text/plain", "Method Not Allowed");
digitalWrite(led, 0);
} else {
digitalWrite(led, 1);
server.send(200, "text/plain", "POST body was:\n" + server.arg ("plain"));
digitalWrite(led, 0);

it returns this when ran and the form is submited
POST body was:
{"pump": "selected", "was": "="}A1

but i dont see how it strips the A1 from the string

am i getting warm :?