Accessing webserver from the example provided fo Arduino Rev4 WIFI

I am for the first time working with WIFI functionality of Arduino Rev4 WIFI.
I am able to connect to the internet through my hotspot. But there is one example provided on Arduino website that sets the webserver and provides functionality to turn on or off the onboard led with :
http://yourAddress/H turns the LED on
http://yourAddress/L turns it off
I run it and get
Access Point Web Server
Creating access point named: Dziubym
SSID: Dziubym
IP Address: 192.168.4.1
To see this page in action, open a browser to http://192.168.4.1

But accessing http://192.168.4.1/ does not work. I tried to run again changing the ip to the one from the same network as the ip of my hotspot still doe not work. Can you guide me through this.

/*

  WiFi Web Server LED Blink


  A simple web server that lets you blink an LED via the web.

  This sketch will create a new access point (with no password).

  It will then launch a new server and print out the IP address

  to the Serial Monitor. From there, you can open that address in a web browser

  to turn on and off the LED on pin 13.


  If the IP address of your board is yourAddress:

    http://yourAddress/H turns the LED on

    http://yourAddress/L turns it off


  created 25 Nov 2012

  by Tom Igoe

  adapted to WiFi AP by Adafruit


  Find the full UNO R4 WiFi Network documentation here:

  https://docs.arduino.cc/tutorials/uno-r4-wifi/wifi-examples#access-point

 */


#include "WiFiS3.h"


#include "arduino_secrets.h" 


///////please enter your sensitive data in the Secret tab/arduino_secrets.h

char ssid[] = SECRET_SSID;        // your network SSID (name)

char pass[] = SECRET_PASS;        // your network password (use for WPA, or use as key for WEP)

int keyIndex = 0;                 // your network key index number (needed only for WEP)


int led =  LED_BUILTIN;

int status = WL_IDLE_STATUS;

WiFiServer server(80);


void setup() {

  //Initialize serial and wait for port to open:

  Serial.begin(9600);

  while (!Serial) {

    ; // wait for serial port to connect. Needed for native USB port only

  }

  Serial.println("Access Point Web Server");


  pinMode(led, OUTPUT);      // set the LED pin mode


  // check for the WiFi module:

  if (WiFi.status() == WL_NO_MODULE) {

    Serial.println("Communication with WiFi module failed!");

    // don't continue

    while (true);

  }


  String fv = WiFi.firmwareVersion();

  if (fv < WIFI_FIRMWARE_LATEST_VERSION) {

    Serial.println("Please upgrade the firmware");

  }


  // by default the local IP address will be 192.168.4.1

  // you can override it with the following:

  WiFi.config(IPAddress(192,48,56,2));


  // print the network name (SSID);

  Serial.print("Creating access point named: ");

  Serial.println(ssid);


  // Create open network. Change this line if you want to create an WEP network:

  status = WiFi.beginAP(ssid, pass);

  if (status != WL_AP_LISTENING) {

    Serial.println("Creating access point failed");

    // don't continue

    while (true);

  }


  // wait 10 seconds for connection:

  delay(10000);


  // start the web server on port 80

  server.begin();


  // you're connected now, so print out the status

  printWiFiStatus();

}



void loop() {

  

  // compare the previous status to the current status

  if (status != WiFi.status()) {

    // it has changed update the variable

    status = WiFi.status();


    if (status == WL_AP_CONNECTED) {

      // a device has connected to the AP

      Serial.println("Device connected to AP");

    } else {

      // a device has disconnected from the AP, and we are back in listening mode

      Serial.println("Device disconnected from AP");

    }

  }

  

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


  if (client) {                             // if you get a client,

    Serial.println("new client");           // print a message out the serial port

    String currentLine = "";                // make a String to hold incoming data from the client

    while (client.connected()) {            // loop while the client's connected

      delayMicroseconds(10);                // This is required for the Arduino Nano RP2040 Connect - otherwise it will loop so fast that SPI will never be served.

      if (client.available()) {             // if there's bytes to read from the client,

        char c = client.read();             // read a byte, then

        Serial.write(c);                    // print it out to the serial monitor

        if (c == '\n') {                    // if the byte is a newline character


          // if the current line is blank, you got two newline characters in a row.

          // that's the end of the client HTTP request, so send a response:

          if (currentLine.length() == 0) {

            // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)

            // and a content-type so the client knows what's coming, then a blank line:

            client.println("HTTP/1.1 200 OK");

            client.println("Content-type:text/html");

            client.println();


            // the content of the HTTP response follows the header:

            client.print("<p style=\"font-size:7vw;\">Click <a href=\"/H\">here</a> turn the LED on<br></p>");

            client.print("<p style=\"font-size:7vw;\">Click <a href=\"/L\">here</a> turn the LED off<br></p>");


            // The HTTP response ends with another blank line:

            client.println();

            // break out of the while loop:

            break;

          }

          else {      // if you got a newline, then clear currentLine:

            currentLine = "";

          }

        }

        else if (c != '\r') {    // if you got anything else but a carriage return character,

          currentLine += c;      // add it to the end of the currentLine

        }


        // Check to see if the client request was "GET /H" or "GET /L":

        if (currentLine.endsWith("GET /H")) {

          digitalWrite(led, HIGH);               // GET /H turns the LED on

        }

        if (currentLine.endsWith("GET /L")) {

          digitalWrite(led, LOW);                // GET /L turns the LED off

        }

      }

    }

    // close the connection:

    client.stop();

    Serial.println("client disconnected");

  }

}


void printWiFiStatus() {

  // print the SSID of the network you're attached to:

  Serial.print("SSID: ");

  Serial.println(WiFi.SSID());


  // print your WiFi shield's IP address:

  IPAddress ip = WiFi.localIP();

  Serial.print("IP Address: ");

  Serial.println(ip);


  // print where to go in a browser:

  Serial.print("To see this page in action, open a browser to http://");

  Serial.println(ip);


}


the Uno creates a new WiFi network as AP. you have to connect your computer to that new WiFi. don't set in that sketch the same SSID as you regular WiFi

Good point. I will check it. I was rewriting it anew based on the info I found in the net and I made it work. If this code works I still do not know why the previous didn't work. It had some different ways to check the status of the actions . Maybe there something did not work

#include <WiFiS3.h>

char ssid[] = "******"; // your network SSID (name)
char pass[] = "******"; // your network password
int status = WL_IDLE_STATUS; // WiFi status variable

WiFiServer server(80); // Create a web server listening on port 80

int led = LED_BUILTIN; // LED pin

void setup() {
  Serial.begin(9600);

  // Attempt to connect to WiFi network
  Serial.println("Connecting to WiFi...");
  while (WiFi.begin(ssid, pass) != WL_CONNECTED) {
    Serial.print(".");
    delay(1000);
  }

  // Print WiFi status and server IP address
  printWiFiStatus();

  // Start the web server
  server.begin();
}

void loop() {
  WiFiClient client = server.available(); // Check if a client has connected
  if (client) { // If a client has connected
    Serial.println("New client connected");
    String request = client.readStringUntil('\r'); // Read the client's request
    Serial.println(request); // Print the request to the serial monitor

    // Respond to client request
    if (request.indexOf("/RED") != -1) { // If the request contains "/H"
      digitalWrite(led, HIGH); // Turn LED on
    } else if (request.indexOf("/L") != -1) { // If the request contains "/L"
      digitalWrite(led, LOW); // Turn LED off
    }

    // Send HTTP response to client
    client.println("HTTP/1.1 200 OK");
    client.println("Content-type:text/html");
    client.println();
    client.println("<html><body>");
    client.println("<h1>Hello from Arduino!</h1>");
    client.println("</body></html>");

    // Close the connection
    client.stop();
    Serial.println("Client disconnected");
  }
}

void printWiFiStatus() {
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  Serial.print("IP Address: ");
  Serial.println(WiFi.localIP());
}

this one connects to existing AP. it doesn't start its own

I know. I will change it. But it does control the led

Oh yes it does

By default it creates an AP named testAP and the test page can be accessed at 192.48.56.2

Access Point Web Server
Creating access point named: testAP
SSID: testAP
IP Address: 192.48.56.2
To see this page in action, open a browser to http://192.48.56.2

I think that the confusion arises because of these lines

///////please enter your sensitive data in the Secret tab/arduino_secrets.h
char ssid[] = SECRET_SSID;        // your network SSID (name)
char pass[] = SECRET_PASS;        // your network password (use for WPA, or use as key for WEP)
int keyIndex = 0;                 // your network key index number (needed only for WEP)

The SSID and password required are not those of your normal WiFi but those of the AP that you want the sketch to create

Once the sketch has been uploaded you need to connect your 'phone (or other device) to the new AP and open a browser at 192.48.56.2 to see and use the LED on/off links

To be honest I do not see this testAP as the accessible access point.

In the last sketch I posted I clearly connect to my home AP with provided credentials. The server is started on IP 192.168.68.128 and from any device at hope I can control the led with 192.168.68.128/H and 192.168.68.128/L

Undestood. So basically if run beginAp with SSID I provide in the code and providing the password for the new AP, then the AP will be created. If I start the server with IP from the same network range as my home AP then they will see each other. But I am probably running the risk of having the conflict if my home AP assigns the IP of my Arduino to a new device connecting to it . Is it DHCP that assigns those addresses. I would have to somehow restrict that IP address from future assigments

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.