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