Feature: RF design
as outputs and set HIGH so that all the relays are OFF to start with. Te program then connects to the local Wi-Fi router with the specified SSID and password. Te remainder of the main program runs in as endless loop. Here, if command n = ON is received from the smartphone, the packet contents are checked and relay n turns ON. Receiving command n = OFF turns off the relay. Te program runs continuously until stopped by the user.
//--------------------------------------------------------------------------- -------------- // // //
Wi-Fi BASED REMOTE CONTROL ==========================
// In this program a 4-way relay is connected to the Arduino Uno R4 Wi-Fi. // Te relays are controlled from an Android smartphone. Valid commands // are (n is 1 to 4): //
// n=ON // n=OFF //
// Author: Dogan Ibrahim // File : RelayControl // Date : October, 2023
//--------------------------------------------------------------------------- -------------- #include "Wi-FiS3.h" #include "Wi-FiUDP.h"
#define ON LOW #define OFF HIGH
int IN[4] = {2, 3, 4, 5}; int status = WL_IDLE_STATUS;
char ssid[] = "BTHomeSpot-XNH"; // Wi-Fi SSID (name) char pass[] = "4934xyz"; const int Port = 5000;
char Packet[80]; Wi-FiUDP udp;
//
// Turn OFF all Relay contacts, configure Wi-Fi // void setup() {
for(int j =0; j < 4; j++) {
pinMode(IN[j], OUTPUT); digitalWrite(IN[j], OFF); // Relays OFF }
Serial.begin(9600);
22 February 2024
www.electronicsworld.co.uk
// Wi-Fi password // Port number used
// Relay inputs
Turn ON Relay n Turn OFF Relay n
//
// Attempt to connect to Wi-Fi network //
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid); status =
Wi-Fi.begin(ssid, pass); delay(10000); }
Serial.print("Connected...");
udp.begin(Port);
} //
// Tis is the main program loop. Inside the main program we read UDP // packets and then control the Relays as requested. // void loop() {
int PacketSize =
udp.parsePacket(); if(PacketSize) {
udp.read(Packet, PacketSize);
if(Packet[1]=='=') { for(int j = 0; j < 4; j++) {
if(Packet[0] == char((j+1)+'0') && Packet[2] == 'O' &&
Packet[3] == 'N') digitalWrite(IN[j], ON); else if(Packet[0] == char((j+1)+'0') && Packet[2] == 'O' &&
Packet[3] == 'F' && Packet[4] == 'F') digitalWrite(IN[j], OFF); } }
Figure 6: Program listing
Testing the project Te program can easily be tested using an UDP app on a smartphone. Tere are many free UDP apps in the Android Play store; I used the Android app called “UDP Sender” by Hastarin. Te server program is started on the Arduino, then the client
program on the smartphone. As shown in Figure 5, sending the 2 = ON command to turn ON relay 2. Notice that the Arduino’s IP address (19.2.168.1.196) and the used port number (5000) must be entered on the UDP app before sending commands. Ten, the relay outputs can easily be connected to control equipment such as home appliances.
Page 1 |
Page 2 |
Page 3 |
Page 4 |
Page 5 |
Page 6 |
Page 7 |
Page 8 |
Page 9 |
Page 10 |
Page 11 |
Page 12 |
Page 13 |
Page 14 |
Page 15 |
Page 16 |
Page 17 |
Page 18 |
Page 19 |
Page 20 |
Page 21 |
Page 22 |
Page 23 |
Page 24 |
Page 25 |
Page 26 |
Page 27 |
Page 28 |
Page 29 |
Page 30 |
Page 31 |
Page 32 |
Page 33 |
Page 34 |
Page 35 |
Page 36 |
Page 37 |
Page 38 |
Page 39 |
Page 40 |
Page 41 |
Page 42 |
Page 43 |
Page 44