Please help me with this

Hello!
I have an arduino uno r4 wifi and some code but I don't really understand it.
Can someone help?

#include <WiFi.h>

const char* ssid = "MSG_AP";
const char* password = "password123";
WiFiServer server(80);

String messages[30];
int messageCount = 0;

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

  // Set up the ESP32 as an access point
  WiFi.softAP(ssid, password);
  IPAddress apIP(192, 168, 4, 1);
  WiFi.softAPConfig(apIP, apIP, IPAddress(255, 255, 255, 0));

  Serial.println("Access Point SSID: " + String(ssid));

  server.begin();

  Serial.println("HTTP server started");
}

void loop() {
  WiFiClient client = server.available();
  if (client) {
    while (client.connected()) {
      if (client.available()) {
        String request = client.readStringUntil('\r');
        client.flush();
        if (request.indexOf("GET /submit") != -1) {
          if (messageCount < 30) {
            String newMessage = request.substring(request.indexOf('=') + 1, request.indexOf(" HTTP"));
            messages[messageCount] = newMessage;
            messageCount++;
          }
          client.println("HTTP/1.1 303 See Other");
          client.println("Location: /");
          client.println();
          break;
        }

        String html = "<html><body>";
        html += "<h1>Message Sharing Page</h1>";
        html += "<form method='get' action='/submit'>";
        html += "<input type='text' name='message' placeholder='Enter your message'>";
        html += "<input type='submit' value='Submit'>";
        html += "</form>";

        html += "<h2>Shared Messages:</h2>";
        for (int i = 0; i < messageCount; i++) {
          html += "<p>" + messages[i] + "</p>";
        }

        html += "</body></html>";
        client.println("HTTP/1.1 200 OK");
        client.println("Content-Type: text/html");
        client.println("Connection: close");
        client.println();
        client.println(html);
        client.println();
        break;
      }
    }
    client.stop();
  }
}

Welcome to the forum

Where did you get the code from, what do you expect it to do and does it work ?

Are you familiar with the C/C++ programming languages?

It is a web server. It builds a web page in html to display on a browser and which lists all previous messages entered into it (max 30) and allows the user to add a new message.

Does the code work?

The code doesn't work. I'm new to this, and I don't really know programming. I just took the example and modified it.

Can you provide a link to where you got the code.
It looks like you have to find the network it creates (MSG_AP) using say your smartphone and join that network.
Once you do that you enter the IP address 192.168.1.4 in the web browser.

Did you succeed in compiling and loading that code from post #1 onto your Uno R4 ?

I got the code from: https://docs.arduino.cc/tutorials/uno-r4-wifi/wifi-examples
I did not manage to compile this code it gave :


C:\Users\zali\Desktop\sketch_oct15a\sketch_oct15a.ino:5:1: error: 'WiFiServer' does not name a type
 WiFiServer server(80);
 ^~~~~~~~~~
C:\Users\zali\Desktop\sketch_oct15a\sketch_oct15a.ino: In function 'void setup()':
C:\Users\zali\Desktop\sketch_oct15a\sketch_oct15a.ino:14:8: error: 'class CWifi' has no member named 'softAP'; did you mean 'softAPIP'?
   WiFi.softAP(ssid, password);
        ^~~~~~
        softAPIP
C:\Users\zali\Desktop\sketch_oct15a\sketch_oct15a.ino:16:8: error: 'class CWifi' has no member named 'softAPConfig'; did you mean 'softAPIP'?
   WiFi.softAPConfig(apIP, apIP, IPAddress(255, 255, 255, 0));
        ^~~~~~~~~~~~
        softAPIP
C:\Users\zali\Desktop\sketch_oct15a\sketch_oct15a.ino:20:3: error: 'server' was not declared in this scope
   server.begin();
   ^~~~~~
C:\Users\zali\Desktop\sketch_oct15a\sketch_oct15a.ino:20:3: note: suggested alternative: 'perror'
   server.begin();
   ^~~~~~
   perror
C:\Users\zali\Desktop\sketch_oct15a\sketch_oct15a.ino: In function 'void loop()':
C:\Users\zali\Desktop\sketch_oct15a\sketch_oct15a.ino:26:3: error: 'WiFiClient' was not declared in this scope
   WiFiClient client = server.available();
   ^~~~~~~~~~
C:\Users\zali\Desktop\sketch_oct15a\sketch_oct15a.ino:26:3: note: suggested alternative: 'DNSClient'
   WiFiClient client = server.available();
   ^~~~~~~~~~
   DNSClient
C:\Users\zali\Desktop\sketch_oct15a\sketch_oct15a.ino:27:7: error: 'client' was not declared in this scope
   if (client) {
       ^~~~~~
C:\Users\zali\Desktop\sketch_oct15a\sketch_oct15a.ino:27:7: note: suggested alternative: 'llrint'
   if (client) {
       ^~~~~~
       llrint
Multiple libraries were found for "WiFi.h"
  Used: C:\Users\zali\AppData\Local\Arduino15\packages\arduino\hardware\renesas_uno\1.0.4\libraries\WiFiS3
  Not used: C:\Users\zali\Documents\Arduino\libraries\WiFiNINA
exit status 1

Compilation error: 'WiFiServer' does not name a type

This looks odd. I guess the code is not intended for a Uno R4.

It's old, but I don't have another example for this kind of web server. (But the example works if I don't modify it.)

From the link you have given in post #8 I cannot find the code basis which you have used. All the examples there use the following WiFi library:
WiFiS3.h

Why not start with say the "simple web server" example in your link, get that working, then start modifying it ?

At some point, I tried to get help from Chatgpt.
(At first it worked)

I suggest you post the code that worked here together with a statement of the changes you need to make.

1 Like

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