ESP8266 12E remote pump timer

I’ve been working on this code for over a week now learning as I go. I’m still not finished yet and would like to add more code for temperature readings, PH settings and more automation for the pump.

I’ve been finding this whole process a joy when it goes right and a complete misery when it doesn’t work. The latter seems to be the norm for me, I must be a masochist for doing this… Sorry, I digress.

Back to the coding. What I have so far seems to work and was wondering if the code could be optimized, or if I could be prodded in the right direction as to what else I could learn to improve what I have so far.

Thank you, a very tired noob.

#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>

ESP8266WebServer server;

char* ssid = "";
char* password = "";

const int pump = 4;                   //pump to pin 4
unsigned int pumpOnTime;              //how long pump will be on in seconds
unsigned int pumpOffTime;             //how long pump will be off in seconds
int pumpPowerValue;                   //value to be sent to MOSFET for pump pressure
unsigned long previousMillis;         //used with currentMillis to check timer
unsigned long currentMillis;

const int pumpOff = 0;                //used pumpOff as an int to make is easier to read in the code
int pumpTimerInterval = pumpOnTime;   //used with pumpOnOffToggle to turn pump on or off
boolean pumpOnOffToggle = true;


void setup() {
  pinMode(pump, OUTPUT);              //pin 4 to output
  previousMillis = millis();

//========== start web client=========

  WiFi.begin(ssid, password);
  Serial.begin(115200);
  while (WiFi.status() != WL_CONNECTED)
  {
    Serial.print(".");
    delay(500);
  }
  Serial.println("");
  Serial.print("IP Address: ");
  Serial.println(WiFi.localIP());

  server.on("/", []() {
    server.send(200, "text/plain", "This bit must be working?");
  });
  server.on("/pump", pumpsettings);
  server.begin();
}

void loop() {

  server.handleClient();
  currentMillis = millis();
  pumptimer();
}


//===========pump timer==========

void pumptimer()

{
  unsigned long minutesPumpOn = pumpOnTime * 1000;            //converts pumpOnTime to seconds, multiply by 60 to convert to minutes when bugs fixed
  unsigned long minutesPumpOff = pumpOffTime * 1000;          //same as above


  if ((currentMillis - previousMillis) >= pumpTimerInterval) {

    if (pumpOnOffToggle)
    {
      analogWrite (pump, pumpOff);
      pumpTimerInterval = minutesPumpOff;
    }
    else
    {
      analogWrite (pump, pumpPowerValue);
      pumpTimerInterval = minutesPumpOn;
    }

    pumpOnOffToggle = !(pumpOnOffToggle);


    previousMillis = currentMillis;
  }
}

//=========get values and convert to int for timer and pump settings==========

void pumpsettings()
{
  String p = server.arg("pumppower");
  pumpPowerValue = (p.toInt());
  String n = server.arg("timeron");
  pumpOnTime = (n.toInt());
  String f = server.arg("timeroff");
  pumpOffTime = (f.toInt());
  String settings = "pump power " + p + " timer on " + n + " timer off " + f;
  server.send(200, "text/plain", settings);
}

Welcome to the software world. Always striving to get a bunch of 1's and 0's in just the perfect order. Some call it a consistent endeavor towards perfection that you know from the start you will never achieve and yet you begin the journey anyway again and again... Just for that little bit of Joy when the bits are at least mostly where they belong.

Looks like you are on your way if all works as you expected. Look around at other applications that do more than yours and see how they were done. Learn from examples. This forum is full of brilliant contributors. Dream, read, try, struggle, fail, succeed, and repeat.

How about a nicer home page. Maybe a form for entering pump settings in addition to a nice page display of current timing? This would be where you might also want your other sensor readings of temp/pH/pressure, etc.

Thank you for the nice welcome.

I'm kind of learning parrot-fashion at the moment. When dissecting other code it takes me a long time to really understand what is going on, and even then I still don't feel like I really know what is going on.

I wasn't going to elaborate further on the home page as I'm going use MIT App Inventor 2 as the control. Now you've put the thought into my head though, I don't think it would be a bad idea to expand the home page.