Simple recurring GET request not working

I have made a sketch which works on ESP8266 (WeMos D1 R2 board) and a MAX7219 LED 8x8 Matrix .
Now , I have been successfully able to program the MAX7219 , it basically displays 2 numbers on device 0 and 1 . The problem is in the networking area.

void setup() {
  for (int i = 0 ; i < devices ; i++) {
    lc.shutdown(i, false);
    lc.setIntensity(i, 0);
    lc.clearDisplay(i)  ;
  }
  WiFi.mode(WIFI_AP);
  WiFi.softAP("project", "password");
  server.begin();
  Serial.begin(115200);
  IPAddress HTTPS_ServerIP = WiFi.softAPIP();
  Serial.println();
  Serial.print("Server IP is: ");
  Serial.println(HTTPS_ServerIP);
}

void loop() {
char curx = 48, cury = 48;
WiFiClient client = server.available();

  if (!client)return;
  Serial.println("Connection established...");
  client.print(s);
  String request = client.readString();
  Serial.println(request);
  String getval = client.readStringUntil('\r');


  if (request.indexOf("/1") != -1)
  { if (cury == 57)
      curx++;
    else cury++;
  }

  stationary(curx, cury);
  delay(50); }

According to me this should be simple , I even get the "GET : /1" part .. BUT from there it doesn't work.

'stationary' function just sends the two characters and displays them (this function is fully tested , I know it works , so the problem isn't here in the function...)

I'm totally lost.
Any help would be appreciated.

I seriously need a solution by today , guys please help !!! :frowning:

Sorry for the inconvenience ..

Not sure what you do exactly but you don't reset curx when you increment cury? is that intended?

what do you see on the console? there is a lot of things being printed, that could be a hint if the loop loops really.

also using String is a bad idea.

Yes it is intended . What I am trying to do is pretty simple :

  • The person presses a button on the webpage . The webpage returns /1 .
  • Arduino checks for /1 and if it is found , it increments the number.
    (It works like : 01,02,03,....,09 ; and then it increments the other no, ie: 10).
    *The stationary function just displays the number on the LED MAX7219 Matrix.

Thanks for your help .

tgr8db:
Yes it is intended . What I am trying to do is pretty simple :

  • The person presses a button on the webpage . The webpage returns /1 .
  • Arduino checks for /1 and if it is found , it increments the number.
    (It works like : 01,02,03,....,09 ; and then it increments the other no, ie: 10).
    *The stationary function just displays the number on the LED MAX7219 Matrix.

Thanks for your help .

I don't get the red part. when you arrive at 09 - if you want 10 then you want the first number to be increase 0->1 but you do want the second number to go back to 0 9->0 otherwise you have 19

You are using String with a capital S all over the place, I would move away from that type of code if this is meant to run for a long time without reboot.

still many questions:

  • Again to my question on what you see on the console?
  • what is s in   client.print(s);?
  • which WIFI library are you using?

what happens if you try this for your loop()

void loop() {
  String request;
  String getval;
  String searchString = "/1";

  // listen for incoming clients
  WiFiClient client = server.available();

  if (client) {
    if (client.connected()) {
      Serial.println("Connection established");

      // client.print(s); <-- I don't know what this is so commented out

      request = client.readString();
      Serial.println(request);
      getval = client.readStringUntil('\r');

      if (request.indexOf(searchString) != -1) {
        if (cury == 57) {
          curx++;
        } else {
          curx = 0; // NOT SURE THIS IS WHAT YOU WANT
          cury++;
        }
        stationary(curx, cury); // moved here because outside the if values do not change. so not sure needs to be called
      }
    }
    // close the connection:
    client.stop();
  }
}

Sorry for not making it clear enough...

#include <ESP8266WiFi.h>
#include <LedControl.h>

const char* ssid = "your-ssid";
const char* password = "your-password";
WiFiServer server(80);

int din = 13, clk = 2, cs = 15;
LedControl lc = LedControl(din, clk, cs, 4);

unsigned long delaytime = 100;
int devices = lc.getDeviceCount();

/************************************/
/*  All the letters and numbers...  */
/************************************/
const byte ch[313] =
{
  B01111100, B10100010, B10010010, B10001010, B01111100, /*0*/
  B00100010, B01000010, B11111110, B00000010, B00000010, /*1*/
  B01001110, B10001010, B10001010, B10001010, B01111010, /*2*/
  B10000100, B10010010, B10110010, B11010010, B10001100, /*3*/
  B00011000, B00101000, B01001000, B11111110, B00001000, /*4*/
  B11110100, B10010010, B10010010, B10010010, B10001100, /*5*/
  B01111100, B10010010, B10010010, B10010010, B01001100, /*6*/
  B10000000, B10001110, B10010000, B10100000, B11000000, /*7*/
  B01101100, B10010010, B10010010, B10010010, B01101100, /*8*/
  B01100100, B10010010, B10010010, B10010010, B01111100, /*9*/
  B01111110, B10001000, B10001000, B10001000, B01111110, /*A*/
  B11111110, B10000010, B10000010, B10010010, B01101100, /*B*/
  B01111100, B10000010, B10000010, B10000010, B01000100, /*C*/
  B10000010, B11111110, B10000010, B10000010, B01111100, /*D*/
  B11111110, B10010010, B10010010, B10000010, B10000010, /*E*/
  B11111110, B10010000, B10010000, B10010000, B10010000, /*F*/
  B11111110, B10000010, B10001010, B10001010, B10001110, /*G*/
  B11111110, B00010000, B00010000, B00010000, B11111110, /*H*/
  B10000010, B10000010, B11111110, B10000010, B10000010, /*I*/
  B10001110, B10000010, B11111110, B10000000, B10000000, /*J*/
  B11111110, B00010000, B00101000, B01000100, B10000010, /*K*/
  B11111110, B00000010, B00000010, B00000010, B00000010, /*L*/
  B11111110, B01000000, B00100000, B01000000, B11111110, /*M*/
  B11111110, B01000000, B00100000, B00010000, B11111110, /*N*/
  B01111100, B10000010, B10000010, B10000010, B01111100, /*O*/
  B01111110, B10010000, B10010000, B10010000, B01100000, /*P*/
  B01111100, B10000010, B10000010, B10000010, B01111101, /*Q*/
  B11111110, B10010000, B10001000, B10010100, B01100010, /*R*/
  B01100100, B10010010, B10010010, B10010010, B01001100, /*S*/
  B10000000, B10000000, B11111110, B10000000, B10000000, /*T*/
  B11111100, B00000010, B00000010, B00000010, B11111100, /*U*/
  B11111000, B00000100, B00000010, B00000100, B11111000, /*V*/
  B11111110, B00000100, B00001000, B00000100, B11111110, /*W*/
  B11100110, B00101000, B00010000, B00101000, B11100110, /*X*/
  B11100100, B00010010, B00010010, B00010010, B11111100, /*Y*/
  B11000110, B10001010, B10010010, B10100010, B11000110, /*Z*/
  B00101110, B00101010, B00111110, B00000010, B00000000, /*a*/
  B11111110, B00010010, B00010010, B00001100, B00000000, /*b*/
  B00111110, B00100010, B00100010, B00100010, B00000000, /*c*/
  B00011110, B00010010, B00010010, B11111110, B00000000, /*d*/
  B00111110, B00101010, B00101010, B00111010, B00000000, /*e*/
  B00010000, B11111110, B10010000, B10010000, B11000000, /*f*/
  B00011010, B00101001, B00101001, B00011110, B00100000, /*g*/
  B11111110, B00010000, B00010000, B00001110, B00000000, /*h*/
  B00000010, B01011110, B00000010, B00000000, B00000000, /*i*/
  B00000110, B00000001, B01011110, B00000000, B00000000, /*j*/
  B11111110, B00001000, B00010100, B00100010, B00000000, /*k*/
  B11111110, B00000000, B00000000, B00000000, B00000000, /*l*/
  B00111110, B00010000, B00011000, B00010000, B00011110, /*m*/
  B00100000, B00111110, B00010000, B00011110, B00000000, /*n*/
  B00011100, B00100010, B00100010, B00011100, B00000000, /*o*/
  B00111111, B00010100, B00011100, B00000000, B00000000, /*p*/
  B00011100, B00010100, B00111111, B00000001, B00000000, /*q*/
  B00111110, B00010000, B00100000, B00100000, B00000000, /*r*/
  B00011010, B00101010, B00101010, B00101100, B00000000, /*s*/
  B00100000, B11111100, B00100010, B00000100, B00000000, /*t*/
  B00111100, B00000010, B00000010, B00111100, B00000010, /*u*/
  B00111000, B00000100, B00000010, B00000100, B00111000, /*v*/
  B00111100, B00000010, B00000100, B00000010, B00111100, /*w*/
  B00100010, B00010100, B00001000, B00010100, B00100010, /*x*/
  B00111001, B00001001, B00001001, B00111111, B00000000, /*y*/
  B00100110, B00101010, B00110010, B00100010, B00000000, /*z*/
  B00000000, B00000000, B00000000                        /* */
};
const byte divider   = {B11111111};

int initloc(char x) {   //works
  int location;
  if (x >= '0' && x <= '9')  location = 5 * (x - 48)       ;
  if (x >= 'A' && x <= 'Z')  location = 5 * (x - 65) + 50  ;
  if (x >= 'a' && x <= 'z')  location = 5 * (x - 97) + 180 ;
  if (x == ' ')          location = 310            ;
  return location;
}
byte make(char c, int i) { //works
  byte x;
  int loc = initloc(c);
  x = ch[loc + i];
  return x;
}

void stationary(char x, char y) {  //works
  for (int i = 2; i < 4; i++)
    lc.setColumn(1, i, divider);
  byte pholder[12];
  for (int i = 0; i < 5; i++)
    pholder[i] = make(x, i);
  pholder[5] = B00000000;
  for (int i = 0; i < 5; i++)
    pholder[i + 6] = make(y, i);
  for (int i = 5; i < 8; i++)
    lc.setColumn(1, i, pholder[i - 5]);
  for (int i = 0; i < 8; i++)
    lc.setColumn(0, i, pholder[i + 3]);

}


void setup() {
  for (int i = 0 ; i < devices ; i++) {
    lc.shutdown(i, false);
    lc.setIntensity(i, 0);
    lc.clearDisplay(i)  ;
  }
  Serial.begin(115200);
  Serial.println();

  for (uint8_t t = 4; t > 0; t--) {
    Serial.printf("[SETUP] BOOT WAIT %d...\n", t);
    Serial.flush();
    delay(1000);
  }
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(200);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");

  server.begin();
  Serial.println("Server started");

  Serial.println(WiFi.localIP());
  server.begin();
  stationary('0', '0');

}

void loop() {
  //String s = "<!DOCTYPE html><html><body><style>.button{border-radius:999px;box-shadow:0 6px 0 0 #444;color:#000;display:inline-block;padding:10px 40px;font:normal 700 33px/1 'Droid Serif', serif;text-align:center;text-shadow:0px 0px 0 #000;background:#3d85c6;border:2px solid #569;}</style><form id='frm' name='frm' method='GET'><input type='hidden' name='i' value='1'></form<input type='button' class='button' onclick='location.href='/1'' value='Next'></body></html>");
  /** for (char i = '9'; i > 47; i--)
                                { for (char j = '9'; j > 47; j--)
                                { stationary(i, j);
                                  delay(200); lc.clearDisplay(0); lc.clearDisplay(1);
                                }
                                } //used to debug stationary function...*/
  char curx = 48, cury = 48;
  String request;
  String getval;
  String searchString = "/1";

  // listen for incoming clients
  WiFiClient client = server.available();

  if (client) {
    if (client.connected()) {
      Serial.println("Connection established");

      // client.print(s); This was for sending an HTML webpage . Check out the above string s. (In the comment)

      request = client.readString();
      Serial.println(request);
      getval = client.readStringUntil('\r');

      if (request.indexOf(searchString) != -1) {
        if (cury == 57) {
            curx++;
            cury=48; // curx will be representing the character so it will have 0's ASCII value.
        } 
        else cury++;
        
        stationary(curx, cury); // once the loop is over , this can be called to set the MAX7219 Display.
      }
    }
    // close the connection:
    client.stop(); // I want this setup to work for a long time , so when the loop executes again , will this work ?
  }
}

This code is everything necessary , though I am not able to connect to it .It would be helpful if you could provide me the steps.
( According to me I should just connect to this WiFi AP and then open the IP address . but the program isn't going out of the below loop :-

while (WiFi.status() != WL_CONNECTED) {
    delay(200);
    Serial.print(".");
  }

You have been real helpful.
Thanks.

Can You guys please help me out ?

      request = client.readString();
      Serial.println(request);
      getval = client.readStringUntil('\r');

After you've read all the data that the client object has, using readString(), I don't understand why you think that there will be more data to read, using readStringUntil().

According to me I should just connect to this WiFi AP and then open the IP address . but the program isn't going out of the below loop

So, your device is not connecting to your network. There is nothing we can do to make that happen.

Why you added any code to the example, when the example can't even connected to your network, is a real mystery.

PaulS:

      request = client.readString();

Serial.println(request);
      getval = client.readStringUntil('\r');



After you've read all the data that the client object has, using readString(), I don't understand why you think that there will be more data to read, using readStringUntil().

So I should just remove the client.readString() ?

So, your device is not connecting to your network. There is nothing we can do to make that happen.

Why you added any code to the example, when the example can't even connected to your network, is a real mystery.

It's the other way round , I made the other code first and then added the example to that code. The exact error is that the ssid of the AP of the esp8266 doesn't change even if I upload the program with a new ssid and a new password.(thousands of times).

Anyways , the device does get connected to the AP but the program doesn't enter into the client.connected() if statement.

So I should just remove the client.readString() ?

We can't see what you are storing in request or getval, so general advise is not going to be particularly useful.

It's the other way round , I made the other code first

There is a sample client example that comes with the ESP8266WiFi. Does that code work, after putting your SSID and password in?

If so, THAT is the starting point for developing your own application. If not, the problem is with your hardware/network, and adding more code will not make the example work.

The getval gets the :

"GET : /1" part of the data that the html sends.

I am trying the WebServer example , it does seem to work... BUT whenever I change the ssid or password ,it doesn't change at the same time , it changes randomly/suddenly . Maybe it is an hardware issue..

@tgr8db
I have problems getting the MAX7219 to work with my Wemos D1R1 and Wemos D1 Mini. You obviously solved that problem. I am unsure about the pin assignment. You use din = 13, clk = 2, cs = 15.
I understand:
13=D7=MOSI,
2=D4=IO (which in my opinion is the built-in LED) and
15=D8=SS.
Why don't you use the SCK pin (14=D5 which is the clock pin) instead of the built-in LED?
I tested both but it did not work although I used the respective pin numbers for the Wemos D1R1.
I use LEDControl Library 1.0.6 by Eberhard Fahle.
Any ideas?