[Beginer] I need help :( (convert string to number)

I'm so new to this, so I'm sorry for the low-level question.

I got the value of the sensor as a number in Arduino and made it float in the ip address window.

The current situation is as follows.

ex)
Link 1: http://111.111.111.111
Link 2: http://111.111.111.111/value

Link 1 showed all the measured sensor values, and if a specific value or higher comes out, it was recorded separately at the bottom.

Link 2 has made only the numerical values of the current sensor output.
value

In Web viewer 1, I type link 1 in url and see it as the default screen of the app,
In web1 (connectivity), I want to type link 2 to make the app inventory go to the alarm when the value of the numerical value is above 50.

However, there seems to be a problem that the value output on link 2 is a number, but it is treated as a string value, so it does not work as I want.

So I need to convert the string value in link 2 to a number but I don't know how.

The picture below is the block code I have made now.

I would appreciate it if you could suggest a solution.
Or if you can proceed in a different way, I would greatly appreciate it if you could also tell me how to do it.

What did you get when you test the above code?
Doesnot work?

Especially with web got text

(post deleted by author)

The result value of the sensor received from the /value link seems to be accepted as a character, not a number, within the App Inventor.

In the /value link, only numbers are output, but this value seems to be treated as a character, not a number.

I want to know how to convert this character into a number.

When I run the simulation with the app I created, the alarm does not come even if the sensor's result value is more than 50.

This is arduino code

#include <WiFiNINA.h>
#include <WiFiUdp.h>
#include <NTPClient.h>

char ssid[x] = "U+NetF4E8";
char pass[ ] = "";
int status = WL_IDLE_STATUS;

WiFiServer server(80);
const int sensorPin = A0;

// NTP ์„ค์ •
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "pool.ntp.org", 9 * 3600, 60000); // UTC+9 (ํ•œ๊ตญ ์‹œ๊ฐ„)

// ๋กœ๊ทธ ๊ตฌ์กฐ
#define MAX_LOGS 10
struct SensorLog {
  int value;
  String timestamp;
};
SensorLog sensorLogs[MAX_LOGS];
int logIndex = 0;

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

  while (status != WL_CONNECTED) {
    Serial.print("Connecting to: ");
    Serial.println(ssid);
    status = WiFi.begin(ssid, pass);
    delay(1000);
  }

  Serial.println("Connected!");
  timeClient.begin();
  timeClient.update();
  server.begin();
}

void loop() {
  timeClient.update();

  WiFiClient client = server.available();
  if (client) {
    Serial.println("New client connected");

    String request = "";
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        request += c;

        if (request.endsWith("\r\n\r\n")) {
          int sensorValue = analogRead(sensorPin);
          String nowTime = getFormattedDateTime();

          // 40 ์ด์ƒ์ด๋ฉด ๊ธฐ๋ก ์ €์žฅ
          if (sensorValue >= 40) {
            sensorLogs[logIndex].value = sensorValue;
            sensorLogs[logIndex].timestamp = nowTime;
            logIndex = (logIndex + 1) % MAX_LOGS;
          }

          // /value: ์ˆซ์ž๋งŒ ์‘๋‹ต
          if (request.indexOf("GET /value ") >= 0) {
            client.println("HTTP/1.1 200 OK");
            client.println("Content-Type: text/plain");
            client.println("Connection: close");
            client.println();
            client.println(sensorValue);
          }

          // /valuehtml: ์ˆซ์ž๋งŒ ํฐ ๊ธ€์”จ + ์ž๋™ ์ƒˆ๋กœ๊ณ ์นจ
          else if (request.indexOf("GET /valuehtml") >= 0) {
            client.println("HTTP/1.1 200 OK");
            client.println("Content-Type: text/html");
            client.println("Connection: close");
            client.println();
            client.println("<html><head>");
            client.println("<meta http-equiv='refresh' content='5'>");
            client.println("<title>Sensor Value</title></head><body>");
            client.print("<h1 style='font-size:80px; text-align:center;'>");
            client.print(sensorValue);
            client.println("</h1>");
            client.println("</body></html>");
          }

          // ๊ธฐ๋ณธ ํŽ˜์ด์ง€ (/): ์›น์‚ฌ์ดํŠธ ์ „์ฒด
          else {
            client.println("HTTP/1.1 200 OK");
            client.println("Content-Type: text/html; charset=utf-8");
            client.println("Connection: close");
            client.println();
            client.println("<html><head><meta http-equiv='refresh' content='5'></head><body>");
            client.println("<h1>FC-22 CO Sensor</h1>");
            client.print("<p><b>Current Time:</b> ");
            client.print(nowTime);
            client.println("</p>");
            client.print("<p><b>Sensor Value:</b> ");
            client.print(sensorValue);
            client.println("</p>");
            if (sensorValue > 40) {
              client.println("<p style='color:red; font-weight:bold;'>โš ๏ธ Warning: CO level is high! Please ventilate the area!</p>");
            }

            client.println("<hr><h3>Recent High Values (โ‰ฅ40)</h3><ul>");
            for (int i = 0; i < MAX_LOGS; i++) {
              int idx = (logIndex + i) % MAX_LOGS;
              if (sensorLogs[idx].value >= 40) {
                client.print("<li>");
                client.print(sensorLogs[idx].timestamp);
                client.print(" - ");
                client.print(sensorLogs[idx].value);
                client.println("</li>");
              }
            }
            client.println("</ul></body></html>");
          }
          break;
        }
      }
    }
    delay(10);
    client.stop();
    Serial.println("Client disconnected");
  }
}

// ๋‚ ์งœ ๋ฐ ์‹œ๊ฐ„ ํฌ๋งท
String getFormattedDateTime() {
  unsigned long epochTime = timeClient.getEpochTime();

  int currentHour = (epochTime  % 86400L) / 3600;
  int currentMinute = (epochTime % 3600) / 60;
  int currentSecond = epochTime % 60;

  int days = epochTime / 86400L;
  int year = 1970;

  while (true) {
    bool leap = (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
    int daysInYear = leap ? 366 : 365;
    if (days >= daysInYear) {
      days -= daysInYear;
      year++;
    } else {
      break;
    }
  }

  int month = 0;
  int day = 0;
  int monthLength[] = {31,28,31,30,31,30,31,31,30,31,30,31};

  if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
    monthLength[1] = 29;
  }

  for (int i = 0; i < 12; i++) {
    if (days >= monthLength[i]) {
      days -= monthLength[i];
    } else {
      month = i + 1;
      day = days + 1;
      break;
    }
  }

  char buffer[25];
  sprintf(buffer, "%04d-%02d-%02d %02d:%02d:%02d",
          year, month, day, currentHour, currentMinute, currentSecond);
  return String(buffer);
}

It's a block code that I've changed to get push notifications, but I still don't get notifications when the sensor's value exceeds 40.

In the web got text first catch the response content on a label/global variable to know actually what does it return so that we can suggest you better.

1 Like