Relay works only with internet connection, i need it to work regardless the internet connection

Hi all expert here,

i need a help where my code work only with internet connection or it get connected to blynk, i need the relay continue to work with pressure sensor regardless it is connected to internet or not. Which means that my pressure sensor will trigger the relay to turn on or off with certain parameter, and blynk is used to trigger users about the anormaly and monitor the live pressure reading.

Below is my code.

#define BLYNK_TEMPLATE_ID "xxxxx"
#define BLYNK_TEMPLATE_NAME "xxxxx"
#define BLYNK_AUTH_TOKEN "xxxxx"
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "xxxxx";
char pass[] = "xxxxx

Blockquote

";

const int pressureSensorPin = A0;
const int redLedWiFiPin = D3;
const int redLedPressurePin = D4;
const int greenLedWiFiPin = D5;
const int greenLedPowerPin = D6;
const int buzzerPin = D8;
const int relayPin = D1;

const float pressureThreshold = 20.0; // PSI
const float pressureOffset = -30.0;   // Offset value

WiFiClient client;

void setup() {
  pinMode(redLedWiFiPin, OUTPUT);
  pinMode(redLedPressurePin, OUTPUT);
  pinMode(greenLedWiFiPin, OUTPUT);
  pinMode(greenLedPowerPin, OUTPUT);
  pinMode(buzzerPin, OUTPUT);
  pinMode(relayPin, OUTPUT);

  Serial.begin(115200);
  connectToWiFi();
  Blynk.begin(auth, ssid, pass);
}

void loop() {
  Blynk.run();

  float pressure = readPressure();

  Blynk.virtualWrite(V5, pressure); // Stream pressure data to virtual pin V5 on Blynk app

  Serial.print("Pressure (PSI): ");
  Serial.println(pressure);

  if (pressure < pressureThreshold) {
    digitalWrite(buzzerPin, HIGH);
    digitalWrite(relayPin, LOW);
    digitalWrite(redLedPressurePin, HIGH);
    digitalWrite(greenLedPowerPin, LOW);
  } else {
    digitalWrite(buzzerPin, LOW);
    digitalWrite(relayPin, HIGH);
    digitalWrite(redLedPressurePin, LOW);
    digitalWrite(greenLedPowerPin, HIGH);
  }

  delay(1000); // Wait for 1 second
}

void connectToWiFi() {
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.begin(ssid, pass);

  while (WiFi.status() != WL_CONNECTED) {
    digitalWrite(redLedWiFiPin, HIGH); // Blinking red LED indicates not connected to WiFi
    delay(500);
    digitalWrite(redLedWiFiPin, LOW);
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
  
  digitalWrite(greenLedWiFiPin, HIGH); // Green LED indicates connected to WiFi
}

float readPressure() {
  int rawValue = analogRead(pressureSensorPin);
  // Apply offset to raw sensor value
  rawValue += pressureOffset;

  // Convert raw value to voltage
  float voltage = rawValue * (3.3 / 1023.0); 
  // Convert voltage to pressure assuming 0-3.3V input range corresponds to 0-100 PSI
  float pressure = voltage * 100; 
  
  return pressure;
}

@bwlow Please edit your post, using the pencil icon at the bottom, and add triple backticks at the beginning and end of your code so that it displays correctly.
Triple backticks look like this:
```

Copy and paste these if you can’t find the correct symbol on your keyboard.

Pete.

1 Like

Blynk.begin() is a blocking command, which attempts to connect to WiFi then the Blynk server (so when using Blynk.begin() there’s no need to create the WiFi connection as you are doing). If Blynk.begin() can’t connect to WiFi or Blynk then all code execution stops.

The alternative non-blocking solution is to manually create the WiFi connection (which you’re already doing - but not doing it correctly) then use Blynk.config() and Blynk.connect().

If you search the forum for Blynk.config() examples you learn about how to do this correctly.

However, you also need to clean-up your void loop. You should read this…

Pete.

1 Like