Linear Pressure Sensor Readout

I'm in a bit over my head, and I'm not on Scuba. :smiley: This is my first Arduino Project, and it's not doing what I want it to. I did cheat, getting the code from ChatGPT. I want to read the pressure of my shop air compressor on my phone. It's set for 85psi to 130psi and I have it controlled by Alexa.

I'm using a 5V linear pressure sensor from Amazon. 0.5V=0psi and 4.5V=200psi. I have set the SSID and Password for my home network and have tried to push it to use an unused IP: .120. When I ping that, another IP (.96) pings back. Here is my sketch:

#include <WiFiS3.h>

// WiFi credentials
const char* ssid = "Heckawee";
const char* password = "4075090947";

// Static IP configuration
IPAddress local_IP(192, 168, 77, 120);
IPAddress gateway(192, 168, 77, 1);
IPAddress subnet(255, 255, 255, 0);

// Web server on port 80
WiFiServer server(80);

// Sensor settings
const int sensorPin = A0; // Analog pin for sensor
const float minVoltage = 0.5;  // Minimum sensor voltage
const float maxVoltage = 4.5;  // Maximum sensor voltage
const float minPSI = 0;        // Minimum PSI
const float maxPSI = 200;      // Maximum PSI

void setup() {
    Serial.begin(115200);
    
    // Connect to WiFi
    WiFi.config(local_IP, gateway, subnet);
    WiFi.begin(ssid, password);
    Serial.print("Connecting to WiFi...");
    while (WiFi.status() != WL_CONNECTED) {
        delay(1000);
        Serial.print(".");
    }
    
    Serial.println("\nWiFi connected.");
    Serial.print("IP Address: ");
    Serial.println(WiFi.localIP());

    server.begin();
}

void loop() {
    WiFiClient client = server.available();
    if (client) {
        Serial.println("New client connected.");
        String request = "";
        while (client.available()) {
            char c = client.read();
            request += c;
            if (c == '\n' && request.endsWith("\r\n\r\n")) break;
        }

        // Read sensor
        int rawValue = analogRead(sensorPin);
        float voltage = (rawValue / 1023.0) * 5.0;
        float psi = ((voltage - minVoltage) / (maxVoltage - minVoltage)) * (maxPSI - minPSI);

        // Send response
        client.println("HTTP/1.1 200 OK");
        client.println("Content-Type: text/html");
        client.println("Connection: close");
        client.println();
        client.println("<!DOCTYPE html><html><head><title>PSI Monitor</title></head><body>");
        client.println("<h1>Pressure Sensor Data</h1>");
        client.println("<p>Current PSI: " + String(psi, 2) + " PSI</p>");
        client.println("</body></html>");
        
        delay(10);
        client.stop();
        Serial.println("Client disconnected.");
    }
}

Also, being completely new to Arduino (Uno R4 WiFi), as I'm trying to get this all set up, the Arduino is being powered by my laptop via USBC to USBC connector. Is there a way to read the value directly on my PC? It's not hooked to any pressure yet, so I expect to see 0psi.

I do see the tX LED flashing, and I included the R4HttpClient library for the include.

Give us an annotated schematic showing your design. Post links to technical information on non generic parts.

1 Like

Consider asking the bot what is wrong with its code.

Begin debugging by printing the value in rawValue right after reading it. Does it seem right?

Thanks for all the replies!!! I would rather begin by debugging the WiFi portion. I want to be able to see something which I am not. I have no idea if the sensor is being read, because I can't see a value.

However, I'm guessing I'm adding the statement you provided somewhere in the void. setup or void. loop?

The circuit is stupid simple using this sensor: AULINK 200 PSI Pressure Transducer Sender Sensor with 1/8" -27 NPT 316 Stainless Steel Thread and Harness Connector for Oil Fuel Air Water Pressure: Amazon.com: Industrial & Scientific

There are only three wires: Black to Gnd, Brown to +5V and Blue to A0.

So sry... I did not completely grok what you were posting. That statement is already in my sketch. How do I print the rawValue?

This was my first time ever using ChatGPT, so I was already out of my league when I asked the first question. In fact, it took me a few tries to get it almost right.

Same as you are printing other stuff.

1 Like

Why not just let your router pick the IP? I'd delete the three static IP lines and the WiFi.config, and just use whatever IP this reports:

1 Like

Awesome. Being absolutely new to Arduino, I will freely admit I've never printed other "stuff". Not even a Double Stuff. :smiley:

Thanks! I'll try that.

This is an example of YOU printing other stuff! Remove the quotes and the text and put in the constant you want to print.

1 Like

Like PSI?

Again, thanks for taking the time with this old fart. It's hard to teach this 68 yo dog new tricks.

Not when the teacher is 85!

1 Like

You youngsters have to learn fast as we older farts will not be around for a long time. You will have to be the Somebody will have to answer these questions.

2 Likes

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