Issue with writing a variable on a webpage

Hi again, sorry I'm asking so many questions
I'm trying to get a status update (provided by my Arduino Mega via softSerial) to appear on a web page run from my Arduino Uno WiFi rev2. This is my current code:

#include <SPI.h>
#include <WiFiNINA.h>
#include <SoftwareSerial.h>
char ssid[] = "REDACTED";
char pass[] = "REDACTED";
int keyIndex = 0;
int status = WL_IDLE_STATUS;
WiFiServer server(80);
SoftwareSerial softSerial(9, 8);
int LED = 2;
void printWifiStatus() {
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
  Serial.print("To see this page in action, open a browser to http://");
  Serial.println(ip);
}
void setup() {
  Serial.begin(9600);
  pinMode(LED_BUILTIN, OUTPUT);
  if (WiFi.status() == WL_NO_MODULE) {
    Serial.println("Communication with WiFi module failed!");
    while (true);
  }
  String fv = WiFi.firmwareVersion();
  if (fv < WIFI_FIRMWARE_LATEST_VERSION) {
    Serial.println("Please upgrade the firmware");
  }
  while (status != WL_CONNECTED) {
    Serial.print("Attempting to connect to Network named: ");
    Serial.println(ssid);
    status = WiFi.begin(ssid, pass);
    delay(10000);
  }
  server.begin();
  printWifiStatus();
  softSerial.begin(9600);
  Serial.begin(9600);
  pinMode(LED, OUTPUT);
}
void loop() {
  WiFiClient client = server.available();
  if (softSerial.available()) {
    char number = softSerial.read();
    Serial.println(number);
    if (number == '0') {
      digitalWrite(LED, LOW);
      char status[9] = "activated";
      Serial.println(status);
    }
    else if (number == '1') {
      digitalWrite(LED, HIGH);
      char status[11] = "deactivated";
      Serial.println(status);
    }
  else
    char status[7] = "offline";
    Serial.println(status);
  }
  if (client) {
    Serial.println("new client");
    String currentLine = "";
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        Serial.write(c);
        if (c == '\n') {
          if (currentLine.length() == 0) {
            client.println("HTTP/1.1 200 OK");
            client.println("Content-type:text/html");
            client.println();
            client.print("Current Status:");
            client.print(status);
            client.print("<br>");
            client.print("Click <a href=\"/H\">here</a> to activate the alarm<br>");
            client.print("Click <a href=\"/L\">here</a> to deactivate the alarm<br>");
            client.println();
            break;
          } 
          else {
          currentLine = "";
          }
        } else if (c != '\r') {
        currentLine += c;
        }
      if (currentLine.endsWith("GET /H")) {
        Serial.println("H");
      }
      if (currentLine.endsWith("GET /L")) {
        Serial.println("L");
      }
    }

  }
}
}

When I run it, the Status update does not appear on either the serial monitor or the web page however instead the webpage says Current Status:3
I have no idea where this is coming from or why I'm not getting a status update so if anyone has any ideas please let me know.
Many Thanks

You have two status variables

a global one

and a very transient one

the latter does not survive the if() statement in which it's defined, so outside the if, you go back to the global status which (if WiFi was connected successfuly) is WL_CONNECTED (which is 3)

it's time to read about scope

PS/ 9 is not enough bytes for your local variable... you need space for a trailing null char.

In general it would be better to write

      char status[] = "activated";

and let the compiler calculate how many bytes are needed.... or just use a constant pointer if you don't need to modify the content

     const char * status  = "activated";
2 Likes

Thanks for that
This is how I've tried to fix it:

#include <SPI.h>
#include <WiFiNINA.h>
#include <SoftwareSerial.h>
char ssid[] = "REDACTED";
char pass[] = "REDACTED";
int keyIndex = 0;
int status = WL_IDLE_STATUS;
WiFiServer server(80);
SoftwareSerial softSerial(9, 8);
int LED = 2;
char state = (" ");
void printWifiStatus() {
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
  Serial.print("To see this page in action, open a browser to http://");
  Serial.println(ip);
}
void setup() {
  Serial.begin(9600);
  pinMode(LED_BUILTIN, OUTPUT);
  if (WiFi.status() == WL_NO_MODULE) {
    Serial.println("Communication with WiFi module failed!");
    while (true);
  }
  String fv = WiFi.firmwareVersion();
  if (fv < WIFI_FIRMWARE_LATEST_VERSION) {
    Serial.println("Please upgrade the firmware");
  }
  while (status != WL_CONNECTED) {
    Serial.print("Attempting to connect to Network named: ");
    Serial.println(ssid);
    status = WiFi.begin(ssid, pass);
    delay(10000);
  }
  server.begin();
  printWifiStatus();
  softSerial.begin(9600);
  Serial.begin(9600);
  pinMode(LED, OUTPUT);
}
void loop() {
  WiFiClient client = server.available();
  if (softSerial.available()) {
    char number = softSerial.read();
    Serial.println(number);
    if (number == '0') {
      digitalWrite(LED, LOW);
      char state[] = "activated";
      Serial.println(state);
    }
    else if (number == '1') {
      digitalWrite(LED, HIGH);
      char state[] = "deactivated";
      Serial.println(state);
    }
  else
    char state[] = "offline";
    Serial.println(state);
  }
  if (client) {
    Serial.println("new client");
    String currentLine = "";
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        Serial.write(c);
        if (c == '\n') {
          if (currentLine.length() == 0) {
            client.println("HTTP/1.1 200 OK");
            client.println("Content-type:text/html");
            client.println();
            client.print("Current Status:");
            client.print(state);
            client.print("<br>");
            client.print("Click <a href=\"/H\">here</a> to activate the alarm<br>");
            client.print("Click <a href=\"/L\">here</a> to deactivate the alarm<br>");
            client.println();
            break;
          } 
          else {
          currentLine = "";
          }
        } else if (c != '\r') {
        currentLine += c;
        }
      if (currentLine.endsWith("GET /H")) {
        Serial.println("H");
      }
      if (currentLine.endsWith("GET /L")) {
        Serial.println("L");
      }
    }

  }
}
}

Unfortunately, I'm now getting a current status of P.

read again about scope

you still have the same issue

with multiple local definitions that get thrown out the minute you leave the if() in which they are defined.

define a global variable

const char * state = "unknown";

then in each if just assign a new value to the pointer like

state = "activated";
state = "deactivated";
state = "offline";

but do not redefine a new variable by adding the type in front of the variable.

1 Like

Why are you using SoftwareSerial? The Mega has multiple hardware UART ports that will far out-perform the software version with fewer issues.

The code here is for "the other side", I'm assuming a hardware Serial port was used on the MEGA.

But still a good point because the Arduino Uno WiFi has 3 hardware serial ports.

  • Serial is connected to the USB interface,
  • Serial1 is connected to Pin 0 (RX) and 1 (TX),
  • Serial 2 is connected to the u-blox NINA-W13 module.

So Serial1 could be used on the receiving end rather than

Thanks for the responses, I think I now understand scope and I will look at whether I'd be better off using hardware serial later when I have time.

it's a trivial modification and you'll get a project that is more robust

1 Like

Thanks again everyone, I'm now getting a \ bizarrely. Does anyone have any suggestions please?

#include <SPI.h>
#include <WiFiNINA.h>
#include <SoftwareSerial.h>
char ssid[] = "REDACTED";
char pass[] = "REDACTED";
int keyIndex = 0;
int status = WL_IDLE_STATUS;
WiFiServer server(80);
SoftwareSerial softSerial(9, 8);
int LED = 2;
char number = (" ");
char state = (" ");
void printWifiStatus() {
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
  Serial.print("To see this page in action, open a browser to http://");
  Serial.println(ip);
}
void setup() {
  Serial.begin(9600);
  pinMode(LED_BUILTIN, OUTPUT);
  if (WiFi.status() == WL_NO_MODULE) {
    Serial.println("Communication with WiFi module failed!");
    while (true);
  }
  String fv = WiFi.firmwareVersion();
  if (fv < WIFI_FIRMWARE_LATEST_VERSION) {
    Serial.println("Please upgrade the firmware");
  }
  while (status != WL_CONNECTED) {
    Serial.print("Attempting to connect to Network named: ");
    Serial.println(ssid);
    status = WiFi.begin(ssid, pass);
    delay(10000);
  }
  server.begin();
  printWifiStatus();
  softSerial.begin(9600);
  Serial.begin(9600);
  pinMode(LED, OUTPUT);
}
void loop() {
  WiFiClient client = server.available();
  if (softSerial.available()) {
    number = softSerial.read();
    Serial.println(number);
    if (number == '0') {
      digitalWrite(LED, LOW);
      state = "activated";
      Serial.println(state);
    }
    else if (number == '1') {
      digitalWrite(LED, HIGH);
      state = "deactivated";
      Serial.println(state);
    }
  else
    state = "offline";
    Serial.println(state);
  }
  if (client) {
    Serial.println("new client");
    String currentLine = "";
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        Serial.write(c);
        if (c == 'n') {
          if (currentLine.length() == 0) {
            client.println("HTTP/1.1 200 OK");
            client.println("Content-type:text/html");
            client.println();
            client.print("Current Status:");
            client.print(state);
            client.print("<br>");
            client.print("Click <a href=\"/H\">here</a> to activate the alarm<br>");
            client.print("Click <a href=\"/L\">here</a> to deactivate the alarm<br>");
            client.println();
            break;
          } 
          else {
          currentLine = "";
          }
        } else if (c != 'r') {
        currentLine += c;
        }
      if (currentLine.endsWith("GET /H")) {
        Serial.println("H");
      }
      if (currentLine.endsWith("GET /L")) {
        Serial.println("L");
      }
    }

  }
}
}

You want \n

Sorry, that was a mistake copying over.
I have put the \ back in on that line and a similar line further down, I'm still getting status: \

Well… what type did you pick for state….

I gave you code, why do you go with some random stuff that does not make sense… :man_shrugging:

I'm so sorry, I misunderstood your first post, I have it working now. Thanks for the help.

Good !
Have fun

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