Relay Control from Blynk dashboard and Touchscreen

Hi all,

I’m new to the forum, and just getting into programming Arduino. I’m working on a project that requires a relay to be controlled from a DWIN HMI touchscreen, and remotely from the Blynk dashboard. What is the feasibility of this, and would anyone have any ideas on how to make this happen?

The microprocessor is an Arduino Nano 33 IoT, and I have it linked to the Blynk dashboard where I can control the relay, however, when trying to turn it on, on the touchscreen, nothing happens.

My code is below:

/* Comment this out to disable prints and save space */
#define BLYNK_PRINT SerialUSB


#include <SPI.h>
#include <WiFiNINA.h>
#include <BlynkSimpleWiFiNINA.h>
#include <SimpleDHT.h>

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "MTP24";
char pass[] = "12345678";

BlynkTimer timer;

// This function is called every time the Virtual Pin 0 state changes
BLYNK_WRITE(V0)
{
  // Set incoming value from pin V0 to a variable
  int value = param.asInt();

  // Update state
  Blynk.virtualWrite(V1, value);
}

// This function is called every time the device is connected to the Blynk.Cloud
BLYNK_CONNECTED()
{
  // Change Web Link Button message to "Congratulations!"
  Blynk.setProperty(V3, "offImageUrl", "https://static-image.nyc3.cdn.digitaloceanspaces.com/general/fte/congratulations.png");
  Blynk.setProperty(V3, "onImageUrl",  "https://static-image.nyc3.cdn.digitaloceanspaces.com/general/fte/congratulations_pressed.png");
  Blynk.setProperty(V3, "url", "https://docs.blynk.io/en/getting-started/what-do-i-need-to-blynk/how-quickstart-device-was-made");
}

// This function sends Arduino's uptime every second to Virtual Pin 2.
void myTimerEvent()
{
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V2, millis() / 1000);
}


int pinDHT22 = 10;
SimpleDHT22 dht22(pinDHT22);



unsigned char Buffer[10];
#define airRelay 6
#define waterRelay 7
#define nightLight 8
#define strobeLight 9

void setup()
{
  // Debug console
  SerialUSB.begin(115200);

  Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
  // You can also specify server:
  //Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass, "blynk.cloud", 80);
  //Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass, IPAddress(192,168,1,100), 8080);

  // Setup a function to be called every second
  timer.setInterval(1000L, myTimerEvent);

  pinMode(airRelay, OUTPUT); digitalWrite(airRelay,HIGH);
  pinMode(waterRelay, OUTPUT); digitalWrite(waterRelay,HIGH);
  pinMode(strobeLight, OUTPUT); digitalWrite(strobeLight,HIGH);
  pinMode(nightLight, OUTPUT); digitalWrite(nightLight,HIGH);
  while (!Serial);
}

void loop()
{
  Blynk.run();
  timer.run();
  // You can inject your own code or combine it with other sketches.
  // Check other examples on how to communicate with Blynk. Remember
  // to avoid delay() function!


  byte temperature = 0;
  byte humidity = 0;
  int err = SimpleDHTErrSuccess;
  if ((err = dht22.read(&temperature, &humidity, NULL)) != SimpleDHTErrSuccess) {
    Serial.print("Read DHT22 failed, err="); Serial.print(SimpleDHTErrCode(err));
    Serial.print(","); Serial.println(SimpleDHTErrDuration(err)); delay(2000);
    return;
  }

  Serial.print("Sample OK: ");
  Serial.print((int)temperature); Serial.print(" *C, ");
  Serial.print((int)humidity); Serial.println(" RH%");

  if (Serial1.available())
  {
    for (int i = 0; i <= 8; i++) //5A A5 06 83 55 00 01 00 01 frame sample received
    {
      Buffer[i] = Serial1.read();
    }

    if (Buffer[0] == 0X5A)
    {
      switch (Buffer[4])
      {
        case 0x25:
          if (Buffer[8] == 1)
          {
            digitalWrite(airRelay, LOW);
            Serial.println("Relay1 ON");
          }
          else
          {
            digitalWrite(airRelay, HIGH);
            Serial.println("Relay1 OFF");            
          }
          break;

        case 0x26:
          if (Buffer[8] == 1)
          {
            digitalWrite(waterRelay, LOW);
            Serial.println("Relay2 ON");            
          }
          else
          {
            digitalWrite(waterRelay, HIGH);
            Serial.println("Relay2 Off");              
          }
          break;

        case 0x27:
          if (Buffer[8] == 1)
          {
            digitalWrite(strobeLight, LOW);
            Serial.println("Relay3 ON");
          }
          else
          {
            digitalWrite(strobeLight, HIGH);
            Serial.println("Relay3 OFF");
          }
          break;


        case 0x28:
          if (Buffer[8] == 1)
          {
            digitalWrite(nightLight, LOW);
            Serial.println("Relay4 ON");
          }
          else
          {
            digitalWrite(nightLight, HIGH);
            Serial.println("Relay4 OFF");
          }
          break;

        default:
          Serial.println("No data..");
      }
    }
  }

}

As stated above, when using the Blynk dashboard only, I can control the relay from the web dashboard, and when removing the device from the Blynk dashboard and just using the touchscreen I can control the relay, I just am lost on how to combine the two to work together.

If a schematic of the hardware wiring is necessary I can provide one, same with a parts list.

Apologies if I left anything out, I will reply with a response as soon as possible.

Thanks in advance,

Warren

When you say “removing the device from the Blynk dashboard” what exactly do you mean? What code changes are you making to do this?

I’m confused about your use of SerialUSB, Serial and Serial1 especially when only one of these serial ports is being 8nitialised with a Serial.begin() command.

Pete.

Pete, thanks for your fast response.

By “removing the device from the Blynk dashboard” I simply mean removing the code for Blynk, and uploading the code that allows the Touchscreen to control the relays offline.

Serial USB I can remove, but Serial1 allows the display to communicate with the Arduino Nano, Serial is just printing data on the Serial Monitor. I am new to this so if I’ve misinturpreted how the Serial functions work, I am open to other ideas.

Hopefully that answers your inquiry,

Warren

For better understanding of the problem, please post your code that works properly without Blynk. And also give us the full details on the hardware side.

1 Like

Here is the code that functions without Blynk.

#include "Wire.h"

unsigned char Buffer[10];
#define airRelay 6
#define waterRelay 7
#define nightLight 8
#define strobeLight 9


void setup() {
  // Initialize serial and wait for port to open:
  Serial.begin(115200);
  Serial1.begin(115200);
  pinMode(airRelay, OUTPUT); digitalWrite(airRelay,HIGH);
  pinMode(waterRelay, OUTPUT); digitalWrite(waterRelay,HIGH);
  pinMode(strobeLight, OUTPUT); digitalWrite(strobeLight,HIGH);
  pinMode(nightLight, OUTPUT); digitalWrite(nightLight,HIGH);


}

void loop() {
  if (Serial1.available())
  {
    for (int i = 0; i <= 8; i++) //5A A5 06 83 55 00 01 00 01 frame sample received
    {
      Buffer[i] = Serial1.read();
    }

    if (Buffer[0] == 0X5A)
    {
      switch (Buffer[4])
      {
        case 0x25:   //for Relay1
          if (Buffer[8] == 1)
          {
            digitalWrite(airRelay, LOW);  // switch ON relay1 , low enabled
            Serial.println("Relay1 ON");
          }
          else
          {
            digitalWrite(airRelay, HIGH);
            Serial.println("Relay1 OFF");            
          }
          break;

        case 0x26:   //for Relay2
          if (Buffer[8] == 1)
          {
            digitalWrite(waterRelay, LOW);
            Serial.println("Relay2 ON");            
          }
          else
          {
            digitalWrite(waterRelay, HIGH);
            Serial.println("Relay2 Off");              
          }
          break;

        case 0x27:   //for Relay3
          if (Buffer[8] == 1)
          {
            digitalWrite(strobeLight, LOW);
            Serial.println("Relay3 ON");
          }
          else
          {
            digitalWrite(strobeLight, HIGH);
            Serial.println("Relay3 OFF");
          }
          break;


        case 0x28:   //for Relay4
          if (Buffer[8] == 1)
          {
            digitalWrite(nightLight, LOW);
            Serial.println("Relay4 ON");
          }
          else
          {
            digitalWrite(nightLight, HIGH);
            Serial.println("Relay4 OFF");
          }
          break;

        default:
          Serial.println("No data..");
      }
    }
  }
}

As far as hardware goes, I’m using:

  • Arduino Nano 33 IoT
  • 5v Relays
  • External Power Supply
  • DWIN 4.3in HMI Display

I can also draw a schematic if required for troubleshooting.

First of all you should read this

Can you see what you’re missing from your Blynk version now?

Pete.

Ah I completely see it now, I will test later today and respond with an update on how everything worked.

Thanks for your help!

Well after making the changes suggested everything is working now!

I thank everyone for their support.

The second thing I’ve ran into is when I unplug the Arduino from my PC it’s ability to connect to the Blynk Cloud seems to not work. The device is only showing online when it’s connected through its USB cable to my PC, but when I unplug it from my PC and power it from an external power source it never comes “online”. Any ideas on how to fix this?

Here is the updated code if needed:

#define BLYNK_PRINT SerialUSB


#include <SPI.h>
#include <WiFiNINA.h>
#include <BlynkSimpleWiFiNINA.h>
#include <DHT.h>

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "MTP24";
char pass[] = "12345678";

BlynkTimer timer;

// This function is called every time the Virtual Pin 0 state changes
BLYNK_WRITE(V0)
{
  // Set incoming value from pin V0 to a variable
  int value = param.asInt();

  // Update state
  Blynk.virtualWrite(V1, value);
}

// This function is called every time the device is connected to the Blynk.Cloud
BLYNK_CONNECTED()
{
  // Change Web Link Button message to "Congratulations!"
  Blynk.setProperty(V3, "offImageUrl", "https://static-image.nyc3.cdn.digitaloceanspaces.com/general/fte/congratulations.png");
  Blynk.setProperty(V3, "onImageUrl",  "https://static-image.nyc3.cdn.digitaloceanspaces.com/general/fte/congratulations_pressed.png");
  Blynk.setProperty(V3, "url", "https://docs.blynk.io/en/getting-started/what-do-i-need-to-blynk/how-quickstart-device-was-made");
}

// This function sends Arduino's uptime every second to Virtual Pin 2.
void myTimerEvent()
{
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V2, millis() / 1000);
}




unsigned char Buffer[10];
#define airRelay 6
#define waterRelay 7
#define nightLight 8
#define strobeLight 9

#define DHTPIN 10
#define DHTTYPE DHT22

DHT dht(DHTPIN,DHTTYPE);

float t;
float h;

const int pressureInput = A7; //select the analog input pin for the pressure transducer
const int pressureZero = 192.4; //analog reading of pressure transducer at 0psi
const int pressureMax = 921.6; //analog reading of pressure transducer at 100psi
const int pressuretransducermaxPSI = 100; //psi value of transducer being used
const int baudRate = 9600; //constant integer to set the baud rate for serial monitor
const int sensorreadDelay = 250; //constant integer to set the sensor read delay in milliseconds

float pressureValue = 0; //variable to store the value coming from the pressure transducer

void setup()
{
  // Debug console
  SerialUSB.begin(115200);
  Serial1.begin(115200);
  Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
  // You can also specify server:
  //Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass, "blynk.cloud", 80);
  //Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass, IPAddress(192,168,1,100), 8080);

  // Setup a function to be called every second
  timer.setInterval(1000L, myTimerEvent);

  pinMode(airRelay, OUTPUT); digitalWrite(airRelay,HIGH);
  pinMode(waterRelay, OUTPUT); digitalWrite(waterRelay,HIGH);
  pinMode(strobeLight, OUTPUT); digitalWrite(strobeLight,HIGH);
  pinMode(nightLight, OUTPUT); digitalWrite(nightLight,HIGH);
  while (!Serial);

  dht.begin();
}

void loop()
{
  Blynk.run();
  timer.run();
  // You can inject your own code or combine it with other sketches.
  // Check other examples on how to communicate with Blynk. Remember
  // to avoid delay() function!

  t = dht.readTemperature(true);
  h = dht.readHumidity();
  Serial.print("Temperature:"); Serial.print(t);
  Serial.print("Humidity:"); Serial.print(h);

  Blynk.virtualWrite(10,t);
  Blynk.virtualWrite(11,h);

  pressureValue = analogRead(pressureInput); //reads value from input pin and assigns to variable
  pressureValue = ((pressureValue-pressureZero)*pressuretransducermaxPSI)/(pressureMax-pressureZero); //conversion equation to convert analog reading to psi
  Serial.print(pressureValue, 1); //prints value from previous line to serial
  Serial.println("psi"); //prints label to serial

  Blynk.virtualWrite(1,pressureValue);

  if (Serial1.available())
  {
    for (int i = 0; i <= 8; i++) //5A A5 06 83 55 00 01 00 01 frame sample received
    {
      Buffer[i] = Serial1.read();
    }

    if (Buffer[0] == 0X5A)
    {
      switch (Buffer[4])
      {
        case 0x25:
          if (Buffer[8] == 1)
          {
            digitalWrite(airRelay, LOW);
            Serial.println("Relay1 ON");
          }
          else
          {
            digitalWrite(airRelay, HIGH);
            Serial.println("Relay1 OFF");            
          }
          break;

        case 0x26:
          if (Buffer[8] == 1)
          {
            digitalWrite(waterRelay, LOW);
            Serial.println("Relay2 ON");            
          }
          else
          {
            digitalWrite(waterRelay, HIGH);
            Serial.println("Relay2 Off");              
          }
          break;

        case 0x27:
          if (Buffer[8] == 1)
          {
            digitalWrite(strobeLight, LOW);
            Serial.println("Relay3 ON");
          }
          else
          {
            digitalWrite(strobeLight, HIGH);
            Serial.println("Relay3 OFF");
          }
          break;


        case 0x28:
          if (Buffer[8] == 1)
          {
            digitalWrite(nightLight, LOW);
            Serial.println("Relay4 ON");
          }
          else
          {
            digitalWrite(nightLight, HIGH);
            Serial.println("Relay4 OFF");
          }
          break;

        default:
          Serial.println("No data..");
      }
    }
  } 
}

Make sure that the external power supply that you are using is capable of meeting the voltage(V) and current(I) demands of the device.

Your code is still a mess, because you’ve not taken the advice regarding cleaning-up your void loop.
You’re breaking the golden rule of Blynk which is never to have Blynk.virtualWrites in your void loop.

I realise that you probably need the touch screen code in your void loop, but the DHT code, and its associated virtualWrites needs to be in a separate function called using a BlynkTimer.

Also, you still have this…

and although prints to the SerialUSB port may be re-directed to Serial it’s still messy. You should almost certainly change that to #define BLYNK_PRINT Serial

What voltage of external PSU are you using, and which pin on the Arduino are you applying it to?

How are you powering your touch screen?

Pete.

Apologies for that, I think I have cleaned it up a bit if you don’t mind just checking to ensure I’m not spamming the Blynk server. I plan on cleaning up the spacing/syntax/comments as my next priority.

#define BLYNK_PRINT Serial


#include <SPI.h>
#include <WiFiNINA.h>
#include <BlynkSimpleWiFiNINA.h>
#include <DHT.h>

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "MTP24";
char pass[] = "12345678";

BlynkTimer timer;

// This function is called every time the Virtual Pin 0 state changes
BLYNK_WRITE(V0)
{
  // Set incoming value from pin V0 to a variable
  int value = param.asInt();

  // Update state
  Blynk.virtualWrite(V1, value);
}

// This function is called every time the device is connected to the Blynk.Cloud
BLYNK_CONNECTED()
{
  // Change Web Link Button message to "Congratulations!"
  Blynk.setProperty(V3, "offImageUrl", "https://static-image.nyc3.cdn.digitaloceanspaces.com/general/fte/congratulations.png");
  Blynk.setProperty(V3, "onImageUrl",  "https://static-image.nyc3.cdn.digitaloceanspaces.com/general/fte/congratulations_pressed.png");
  Blynk.setProperty(V3, "url", "https://docs.blynk.io/en/getting-started/what-do-i-need-to-blynk/how-quickstart-device-was-made");
}

// This function sends Arduino's uptime every second to Virtual Pin 2.





unsigned char Buffer[10];
#define airRelay 6
#define waterRelay 7
#define nightLight 8
#define strobeLight 9

#define DHTPIN 10
#define DHTTYPE DHT22

DHT dht(DHTPIN,DHTTYPE);

float t;
float h;

const int pressureInput = A7; //select the analog input pin for the pressure transducer
const int pressureZero = 192.4; //analog reading of pressure transducer at 0psi
const int pressureMax = 921.6; //analog reading of pressure transducer at 100psi
const int pressuretransducermaxPSI = 100; //psi value of transducer being used
const int baudRate = 9600; //constant integer to set the baud rate for serial monitor
const int sensorreadDelay = 250; //constant integer to set the sensor read delay in milliseconds

float pressureValue = 0; //variable to store the value coming from the pressure transducer

void setup()
{
  // Debug console
  Serial.begin(115200);
  Serial1.begin(115200);
  Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
  // You can also specify server:
  //Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass, "blynk.cloud", 80);
  //Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass, IPAddress(192,168,1,100), 8080);

  // Setup a function to be called every second
  timer.setInterval(1000L, myTimerEvent);

  pinMode(airRelay, OUTPUT); digitalWrite(airRelay,HIGH);
  pinMode(waterRelay, OUTPUT); digitalWrite(waterRelay,HIGH);
  pinMode(strobeLight, OUTPUT); digitalWrite(strobeLight,HIGH);
  pinMode(nightLight, OUTPUT); digitalWrite(nightLight,HIGH);
  while (!Serial);

  dht.begin();
}

void loop()
{
  Blynk.run();
  timer.run();
  // You can inject your own code or combine it with other sketches.
  // Check other examples on how to communicate with Blynk. Remember
  // to avoid delay() function!

  if (Serial1.available())
  {
    for (int i = 0; i <= 8; i++) //5A A5 06 83 55 00 01 00 01 frame sample received
    {
      Buffer[i] = Serial1.read();
    }

    if (Buffer[0] == 0X5A)
    {
      switch (Buffer[4])
      {
        case 0x25:
          if (Buffer[8] == 1)
          {
            digitalWrite(airRelay, LOW);
            Serial.println("Relay1 ON");
          }
          else
          {
            digitalWrite(airRelay, HIGH);
            Serial.println("Relay1 OFF");            
          }
          break;

        case 0x26:
          if (Buffer[8] == 1)
          {
            digitalWrite(waterRelay, LOW);
            Serial.println("Relay2 ON");            
          }
          else
          {
            digitalWrite(waterRelay, HIGH);
            Serial.println("Relay2 Off");              
          }
          break;

        case 0x27:
          if (Buffer[8] == 1)
          {
            digitalWrite(strobeLight, LOW);
            Serial.println("Relay3 ON");
          }
          else
          {
            digitalWrite(strobeLight, HIGH);
            Serial.println("Relay3 OFF");
          }
          break;


        case 0x28:
          if (Buffer[8] == 1)
          {
            digitalWrite(nightLight, LOW);
            Serial.println("Relay4 ON");
          }
          else
          {
            digitalWrite(nightLight, HIGH);
            Serial.println("Relay4 OFF");
          }
          break;

        default:
          Serial.println("No data..");
      }
    }
  } 
}


void myTimerEvent()
{
  t = dht.readTemperature(true);
  h = dht.readHumidity();
  Serial.print("Temperature:"); Serial.print(t);
  Serial.print("Humidity:"); Serial.print(h);

  Blynk.virtualWrite(10,t);
  Blynk.virtualWrite(11,h);

  pressureValue = analogRead(pressureInput); //reads value from input pin and assigns to variable
  pressureValue = ((pressureValue-pressureZero)*pressuretransducermaxPSI)/(pressureMax-pressureZero); //conversion equation to convert analog reading to psi
  Serial.print(pressureValue, 1); //prints value from previous line to serial
  Serial.println("psi"); //prints label to serial

  Blynk.virtualWrite(1,pressureValue);
  Blynk.virtualWrite(V2, millis() / 5000);
}

I believe the reason it’s not powering up when not plugged into my PC is power-related, unless you think something else could be wrong.

Thanks,

Warren

DHT sensors don’t really like to be read this frequently, because they are very slow sensors.
I’d extend this to at least 5000ms, but do you really need to check the temperature and humidity this often?

Pete.

1 Like