EIOT Observation
EIOT Observation
Name :
Roll No :
Register No :
Programme :
Semester :
Vision of the Institution
To be recognized as a Renowned Technical Institution for transforming
Young Minds into Competent Professionals to serve the Industry and
Society.
OUTPUT:
1
To implement 8051 Assembly language experiment
EXP NO : 01
using simulator
AIM:
ALGORITHM:
RESULT:
The assembly language experiment using simulator has been done successfully.
2
PROGRAM:
$MOD51
MOV A, #02
MOVX @DPTR, A
OBSERVATION:
A= 02
3
Data transfer between registers and memory
EXP NO : 02
AIM:
ALGORITHM:
RESULT:
The data transfer between registers and memory has been performed.
4
8 BIT ADDITION
PROGRAM: Bit Addition (Immediate Addressing)
OBSERVATION:
#data1= 03
#data2= 02
MEMORY LOCATION DATA
4500 05
8 BIT SUBTRACTION
8 Bit Subtraction (Immediate Addressing)
ADDRESS LABEL MNEMONIC HEX CODE COMMENTS
$MOD51
4100 MOV A, #03 74, data1 Store data1 in acc
4101 SUBB A, #02 94,data2 Subtract data2 from data1
4103 MOV DPTR, # 4500 90,45,00 Initialize memory
location
4105 MOVX @ DPTR, A F0 Store difference in 4500
4108 STOP: SJMP STOP 80,FE Stop
OBSERVATION:
#data1= 03
#data2= 02
MEMORY LOCATION DATA
4500 01
5
Perform ALU Operations 8051 Micro controller
EXP NO : 03
AIM:
To implement and perform ALU Operations using 8051 Micro controller.
ALGORITHM:
1. Move the first number to A register
2. Add the second number with A register
3. Initiate data pointer
4. Store the result
ALGORITHM:
1. Move the first number to A register
2. Subtract the second number from A register
3. Initiate data pointer
4. Store the result
6
PROGRAM: 8 Bit Multiplication
ADDRESS LABEL MNEMONIC HEX COMMENTS
CODE
$MOD51
4100 MOV A ,#03 74, 03 Store data1 in acc
4102 MOV B, #02 75,02 Store data2 in B reg
4104 MUL AB F5,F0 Multiply both
4106 MOV DPTR, #4500H 90,45,00 Initialize memory location
4109 MOVX @DPTR, A F0 Store lower order result
401A INC DPTR A3 Go to next location
410B MOV A,B E5,F0 Store higher order result
410D MOV @DPTR, A F0
410E STOP: SJMP STOP 80,FE Stop
OBSERVATION:
#data1= 03
#data2= 02
MEMORY LOCATION DATA
4500 (LSB) 01
4501 (MSB) 00
PROGRAM:LOGICAL OR OPERATION:
$MOD51
4100 74 2F MOV A,#2FH Move first data to A reg
4102 44 45 ORL A,#45H OR first data1 with data2
4104 90 45 00 MOV DPTR,#4500H Store at location 4500
4107 F0 MOVX @DPTR,A
4108 80 FE STOP: SJMP STOP
OBSERVATION:
#data1= 2F
#data2= 4E
MEMORY LOCATION DATA
4500 6F
ALGORITHM:
1. Move the first number to A register
2. Move the second number to B register
3. Subtract the A and B
4. Initiate data pointer
5. Store the result
7
ALGORITHM:
1. Move the first number to A register
2. Perform logical OR with the second number
3. Initiate data pointer
4. Store the result
8
LOGICAL AND OPERATION:
$MOD51
4100 74 87 MOV A,#87H Move first data to A reg
4102 54 7E ANL A, #7EH AND first data1 with
data2
4104 90 45 00 MOV DPTR, #4500H Store at location 4500
4107 F0 MOVX @DPTR,A
4108 80 FE STOP: SJMP STOP
OBSERVATION:
#data1= 87
#data2=7E
MEMORY LOCATION DATA
4500 06
$MOD51
4100 74 87 MOV A,#87H Move first data to A reg
4102 54 7E XRL A,#7EH AND first data1 with
data2
4104 90 45 00 MOV DPTR,#4500H Store at location 4500
4107 F0 MOVX @DPTR,A
4108 80 FE STOP: SJMP STOP
OBSERVATION:
#data1= 87
#data2= 7E
MEMORY LOCATION DATA
4500 F9
9
ALGORITHM:
1. Move the first number to A register
2. Perform logical AND with the second number
3. Initiate data pointer
4. Store the result
ALGORITHM:
1. Move the first number to A register
2. Perform logical XOR with the second number
3. Initiate data pointer
4. Store the result
RESULT:
10
PROGRAM : 1
void setup() {
Serial.begin(9600);
void loop() {
Serial.println("Hello World!");
11
EXP NO : 04 To implement the basic Arduino Programming
AIM:
APPARATUS REQUIRED:
ALGORITHM:
12
PROGRAM : 2
LM 35 ARDUINO UNO
GND GND
VCC 5V
OUT A0
14
PROGRAM : 3
To print the IR sensor value in serial monitor
CIRCUIT DIAGRAM
GND GND
VCC 5V
OUT A0
void setup()
{
//pinMode(LEDpin, OUTPUT);
pinMode(obstaclePin, INPUT);
Serial.begin(9600);
}
void loop() {
hasObstacle = digitalRead(obstaclePin);
if (hasObstacle == HIGH) {
Serial.println("Stop something is ahead!!");
//digitalWrite(LEDpin, HIGH);
}
else
if (hasObstacle == LOW){
Serial.println("Path is clear");
//digitalWrite(LEDpin, LOW);
}
delay(2000);
}
15
ALGORITHM:
16
PROGRAM 4:
//DHT
#include <dht11.h>
#define DHT11PIN 4
dht11 DHT11;
void setup()
{
Serial.begin(9600);
}
void loop()
{
Serial.println();
int chk = DHT11.read(DHT11PIN);
Serial.print("Humidity (%): ");
Serial.println((float)DHT11.humidity, 2);
Serial.print("Temperature (C): ");
Serial.println((float)DHT11.temperature, 2);
delay(2000);
17
ALGORITHM:
18
PROGRAM 5:
int input_val = 0;
void setup()
Serial.begin(9600);
void loop()
input_val = analogRead(LDR);
Serial.println(input_val);
delay(1000);
19
ALGORITHM:
RESULT:
20
PROGRAM:
led2=Pin(2,Pin.OUT)
led3=Pin(3,Pin.OUT)
while True:
led2.value(1)
sleep_ms(200)
led3.value(1)
led2.value(0)
sleep_ms(200)
led3.value(0)
21
EXP NO: 05 Introduction to RP2040 with python programming
AIM:
To implement the RP2040 carrier board with python programming.
COMPONENDS RECQUIRED:
ALGORITHM:
4. Turn on LED3.
7. Repeat steps 3-6 indefinitely to create a blinking pattern between the two LEDs.
22
PROGRAM
2. Print text
import time
count=0
enable_pin = Pin(9),
d4_pin = Pin(10),
d5_pin = Pin(11),
d6_pin = Pin(12),
d7_pin = Pin(13))
lcd.move_to(0,0)
lcd.putstr("Hi hello")
lcd.move_to(0,1)
while True:
count=count+1
lcd.move_to(13,1)
lcd.putstr(str(count))
time.sleep(1)
23
ALGORITHM:
1. Initialize an LCD display with GPIO pins for control signals and data lines.
4. Print "my count :" on the second line of the LCD display.
24
PROGRAM
3. Buzzer
import time
while True:
Buzzer.value(1)
time.sleep(0.2)
Buzzer.value(0)
time.sleep(1)
25
ALGORITHM:
26
PROGRAM
4. SIO_LED
led1=Pin(2,mode=Pin.OUT)
led2=Pin(3,mode=Pin.OUT)
led3=Pin(6,mode=Pin.OUT)
addrSIO = 0xd0000000
while True:
sleep_ms(500)
sleep_ms(500)
27
ALGORITHM:
2. Define the memory address for the Serial I/O (SIO) module.
4. Write the value 0x0200000c to the SIO register offset 0x014 to turn on LED1.
6. Write the value 0x0200000c to the SIO register offset 0x018 to turn off LED1 and
turn on LED2.
8. Repeat steps 4-7 indefinitely to create a blinking pattern between LED1 and LED2.
RESULT:
Thus experiments to implement the RP2040 carrier board with python programming is
conducted successfully.
28
PROGRAM:
#include<reg51.h>
int a,b,c;
void main()
{
a=0x10;
b=0x04;
P0=a-b;
P1=a+b;
P2=a*b;
P3=a/b;
}
OUTPUT:
Port P0= _________
Port P1= _________
Port P2= _________
Port P3= _________
29
EXP NO :06 Write basic arithmetic program using Embedded C
AIM:
To implement the basic arithmetic program using Embedded C.
APPARATUS REQUIRED:
PC, RIDE Software
ALGORITHM:
1. Set variable a to the hexadecimal value 0x10 (16 in decimal).
2. Set variable b to the hexadecimal value 0x04 (4 in decimal).
3. Subtract the value of b from a and store the result in Port 0 (P0).
4. Add the values of a and b together and store the result in Port 1 (P1).
5. Multiply the values of a and b together and store the result in Port 2 (P2).
6. Divide the value of a by b and store the result in Port 3 (P3).
RESULT:
The basic arithmetic program using Embedded C is performed.
30
PROGRAM TEMPERATURE SENSOR (LM35)
from machine import Pin
import time
conversion_factor = 3.3/(65536)
adc2= machine.ADC(27)
while True:
val2 = adc2.read_u16()
temp = (val2 * conversion_factor)*100
print("===============================")
print("temperature: ",temp)
time.sleep(0.8)
31
EXP NO : 07 Interfacing sensors with Raspberry PI
AIM
APPARATUS RECQUIRED:
ALGORITHM:
32
PROGRAM: Potentiometer
from machine import Pin
import utime
POT_Value = machine.ADC(28)
conversion_factor = 3.3/(65535)
while True:
#print(POT_Value.read_u16() )
print(POT_Value.read_u16() * conversion_factor)
utime.sleep(1)
33
ALGORITHM:
34
PROGRAM: Relay
35
ALGORITHM:
RESULT:
36
PROGRAM:
Bluetooth:
bluetooth
/* Arduino Bluetooth
10 Tx
11 Rx
gnd gnd
5v vcc */
#include<SoftwareSerial.h>
void setup() {
bt.begin(9600); /* Define baud rate for software serial communication */
Serial.begin(9600); /* Define baud rate for serial communication */
bt.write("garbage");
}
void loop()
{
bt.write(Serial.read());
37
EXP NO: 08 Explore different communication methods with IoT
Device (Zigbee,GSM,Bluetooth)
AIM:
To implement the different communications with IOT Device like Zigbee, GSM,
Bluetooth.
APPARATUS REQUIRED:
Arduino UNO Kit, Bluetooth Interface, Zigbee Interface -2, Connecting wires
ALGORITHM:
38
PROGRAM: GSM CODE FOR SENDING SMS
// Download Library of SoftwareSerial link : https://github.com/PaulStoffregen/SoftwareSerial
#include <SoftwareSerial.h>
SoftwareSerial SIM900A(10,11); // SoftSerial( RX , TX );
// 10 pin connect to TX of GSM SIM 900 Module
// 11 pin connect to RX of GSM SIM 900 Module
// gsm gnd to arduino gnd
void setup()
{
SIM900A.begin(9600); // Setting the baud rate of GSM Module
Serial.begin(9600); // Setting the baud rate of Serial Monitor (Arduino)
Serial.println ("SIM900A Ready");
delay(100);
Serial.println ("Type s to send message ");
}
void loop()
{
if (Serial.available()>0)
switch(Serial.read())
{
case 's':
SendMessage();
break;
}
if (SIM900A.available()>0)
Serial.write(SIM900A.read());
}
void SendMessage()
{
Serial.println ("Sending Message");
SIM900A.println("AT+CMGF=1"); //Sets the GSM Module in Text Mode
delay(1000);
Serial.println ("Set SMS Number");
SIM900A.println("AT+CMGS=\"9790685877\"\r"); //Write Mobile number to send message
delay(1000);
Serial.println ("Set SMS Content");
SIM900A.println("hi");// Messsage content
delay(100);
Serial.println ("Finish");
SIM900A.println((char)26);// ASCII code of CTRL+Z
delay(1000);
Serial.println ("Message has been sent ->SMS Selesai dikirim");
}
39
ALGORITHM:
40
PROGRAM CODE FOR SENDING SMS
// Techatronic.com
// Download Library of SoftwareSerial link given
// https://github.com/PaulStoffregen/SoftwareSerial
#include <SoftwareSerial.h>
SoftwareSerial SIM900A(10,11); // SoftSerial( RX , TX );
// 10 pin connect to TX of GSM SIM 900 Module
// 11 pin connect to RX of GSM SIM 900 Module
void setup()
{
SIM900A.begin(9600); // Setting the baud rate of GSM Module
Serial.begin(9600); // Setting the baud rate of Serial Monitor (Arduino)
Serial.println ("SIM900A Ready");
delay(100);
Serial.println ("Type r to receive message");
}
void loop()
{
if (Serial.available()>0)
switch(Serial.read())
{
case 'r':
RecieveMessage();
break;
}
if (SIM900A.available()>0)
Serial.write(SIM900A.read());
}
void RecieveMessage()
{
Serial.println ("SIM900A Membaca SMS");
delay (1000);
SIM900A.println("AT+CNMI=2,2,0,0,0"); // AT Command to receive a live SMS
delay(1000);
Serial.write ("Unread Message done");
41
ALGORITHM:
1. Initialize communication with SIM900A and Serial Monitor.
2. Print "SIM900A Ready" and "Type r to receive message".
3. Check for input from Serial Monitor.
4. If 'r' is received, call ReceiveMessage() function.
5. Check for input from SIM900A.
6. If data available, send it to Serial Monitor.
7. Print "SIM900A Membaca SMS".
8. Set up SIM900A to receive live SMS.
9. Print "Unread Message done".
ZIGBEE:
1. Click Discover radio modules from the toolbar.
2. In the Discover radio modules dialog, select the serial port(s) in which you want
to look for radio modules. If you do not know the serial ports where your modules
are attached, select all ports. Click Next.
3. In the Set port parameters window, maintain the default values and click Finish.
4. As XCTU locates radio modules, they appear in the Discovering radio modules…
dialog box. Once the discovery process has finished, click Add selected devices.
5. At this point, assuming you have three modules connected to your computer, you
should see something like this in the Radio Modules section on the left
6. Communicate between Coordinator and Enduser.
RESULT:
The programs for Bluetooth, GSM and Zigbee has been performed.
42
PROGRAM:
import time
import network
from machine import Pin
import BlynkLib
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect("ArNet","12345678")
BLYNK_AUTH = 'yMsd4X4WnSiZQ9pZsXiOuJTy5nopIz4i'
# Connect to Blynk
blynk = BlynkLib.Blynk(BLYNK_AUTH)
43
EXP NO: 09 Setup a cloud platform to log the data
AIM
APPARATUS:
PROCEDURE:
ALGORITHM:
1. Import necessary libraries: time, network, Pin from machine, and BlynkLib.
2. Set up the Wi-Fi connection.
3. Define the Blynk authentication token (BLYNK_AUTH).
4. Connect to the Blynk server.
5. Initialize the pins connected to the relays.
6. Define virtual pin handlers for controlling relays.
7. Continuously run the Blynk service and update a virtual pin every 2 seconds.
44
# Register virtual pin handler
@blynk.on("V1") #virtual pin V1
def v1_write_handler(value): #read the value
if int(value[0]) == 1:
relay1_pin.value(1) #turn the relay1 on
else:
relay1_pin.value(0) #turn the relay1 off
@blynk.on("V2") #virtual pin V2
def v2_write_handler(value): #read the value
if int(value[0]) == 1:
relay2_pin.value(1) #turn the relay2 on
else:
relay2_pin.value(0) #turn the relay2 off
45
RESULT:
46
PROGRAM:
import utime
import network
from machine import Pin
import BlynkLib
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect("ArNet","12345678")
BLYNK_AUTH = 'yMsd4X4WnSiZQ9pZsXiOuJTy5nopIz4i'
# Connect to Blynk
blynk = BlynkLib.Blynk(BLYNK_AUTH)
47
Log Data using Raspberry PI and upload to the cloud
EXP NO: 10
platform
AIM:
APPARATUS:
ALGORITHM:
1. mport necessary modules: utime, network, Pin, BlynkLib.
Trigger the sensor and measure the time for the echo signal.
48
while True:
trigger.low()
utime.sleep_us(2)
trigger.high()
utime.sleep_us(5)
trigger.low()
while echo.value() == 0:
signaloff = utime.ticks_us()
while echo.value() == 1:
signalon = utime.ticks_us()
timepassed = signalon - signaloff
distance = (timepassed * 0.0343) / 2
print("Total distance",distance,"cm")
print("sensor")
blynk.run()
blynk.virtual_write(4, distance)
utime.sleep(2)
49
RESULT:
The program to Log Data using Raspberry PI and upload to the cloud platform has been
performed successfully.
50
CONNECTIONS IN ARDUINO
Arduino UNO Pin Arduino Development Board Bluetooth Module
2 - Tx
3 - Rx
- GND GND
- 5V 5V
PROGRAM:
MASTER
ARDUINO:
#include<SoftwareSerial.h>
SoftwareSerial mySerial(2,3); //rx,tx
void setup() {
mySerial.begin(9600);
}
void loop() {
mySerial.write('A');
delay(1000);
mySerial.write('B');
delay(1000);
}
51
EXP NO: 11 Communicate between Arduino and Raspberry Pi
AIM
To write and execute the program to Communicate between Arduino and Raspberry PI
using any wireless medium (Bluetooth)
APPARATUS REQUIRED
ALGORITHM
1. Set up a software serial communication with pins 2 and 3.
2. In the setup() function, initialize the software serial communication at a baud rate
of 9600.
3. Send the character 'A', then wait for 1 second.
4. Send the character 'B', then wait for 1 second. Repeat indefinitely.
52
CONNECTIONS IN RASPBERRY PI
Raspberry Pi Pico Pin Raspberry Pi Pico Development Board Bluetooth Module
GP16 LED -
Vcc - +5V
GND - GND
GP1 - Tx
GP0 - Rx
PROGRAM:
SLAVE
RASPBERRY PI PICO
from machine import Pin, UART
uart = UART(0, 9600)
led = Pin(16, Pin.OUT)
while True:
if uart.any() > 0:
data = uart.read()
print(data)
if "A" in data:
led.value(1)
print('LED on \n')
uart.write('LED on \n')
elif "B" in data:
led.value(0)
print('LED off \n')
uart.write('LED off \n')
53
ALGORITHM:
RESULT:
54
EXP NO: 12 Design an IOT based system
AIM:
APPARATUS RECQUIRED:
PROGRAM:
// WiFi Credentials
const char* ssid = "Home";
const char* password = "Password123";
// device names
String Device_1_Name = "device one";
String Device_2_Name = "device two";
String Device_3_Name = "device three";
String Device_4_Name = "device four";
#include <WiFi.h>
#include <Espalexa.h>
#include <Preferences.h>
#include <IRremote.h>
Preferences pref;
Espalexa espalexa;
55
// Relay State
bool toggleState_1 = LOW; //Define integer to remember the toggle state for relay 1
bool toggleState_2 = LOW; //Define integer to remember the toggle state for relay 2
bool toggleState_3 = LOW; //Define integer to remember the toggle state for relay 3
bool toggleState_4 = LOW; //Define integer to remember the toggle state for relay 4
// prototypes
boolean connectWifi();
//callback functions
void firstLightChanged(uint8_t brightness);
void secondLightChanged(uint8_t brightness);
void thirdLightChanged(uint8_t brightness);
void fourthLightChanged(uint8_t brightness);
56
digitalWrite(RelayPin2, HIGH);
Serial.println("Device2 OFF");
toggleState_2 = 0;
}
pref.putBool("Relay2", toggleState_2);
}
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.println("");
Serial.println("Connecting to WiFi");
57
// Wait for connection
Serial.print("Connecting...");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
if (i > 20) {
state = false; break;
}
i++;
}
Serial.println("");
if (state) {
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
else {
Serial.println("Connection failed.");
}
return state;
}
void addDevices(){
// Define your devices here.
espalexa.addDevice(Device_1_Name, firstLightChanged); //simplest definition, default state off
espalexa.addDevice(Device_2_Name, secondLightChanged);
espalexa.addDevice(Device_3_Name, thirdLightChanged);
espalexa.addDevice(Device_4_Name, fourthLightChanged);
espalexa.begin();
}
void bluetooth_control()
{
if(Serial2.available()) {
bt_data = Serial2.readString();
//Serial.println(bt_data.substring(bt_data.lastIndexOf(",") + 1));
if (bt_data.substring(bt_data.lastIndexOf(",") + 1) == "A1"){
digitalWrite(RelayPin1, LOW); toggleState_1 = 1; // if "A1" received Turn on Relay1
pref.putBool("Relay1", toggleState_1);
sendFeedback(0, 100);
}
else if(bt_data.substring(bt_data.lastIndexOf(",") + 1) == "A0"){
digitalWrite(RelayPin1, HIGH); toggleState_1 = 0; // if "A0" received Turn off Relay1
58
pref.putBool("Relay1", toggleState_1);
sendFeedback(0, 0);
}
else if(bt_data.substring(bt_data.lastIndexOf(",") + 1) == "B1"){
digitalWrite(RelayPin2, LOW); toggleState_2 = 1; // if "B1" received Turn on Relay2
pref.putBool("Relay2", toggleState_2);
sendFeedback(1, 100);
}
else if(bt_data.substring(bt_data.lastIndexOf(",") + 1) == "B0"){
digitalWrite(RelayPin2, HIGH); toggleState_2 = 0; // if "B0" received Turn off Relay2
pref.putBool("Relay2", toggleState_2);
sendFeedback(1, 0);
}
else if(bt_data.substring(bt_data.lastIndexOf(",") + 1) == "C1"){
digitalWrite(RelayPin3, LOW); toggleState_3 = 1; // if "C1" received Turn on Relay3
pref.putBool("Relay3", toggleState_3);
sendFeedback(2, 100);
}
else if(bt_data.substring(bt_data.lastIndexOf(",") + 1) == "C0"){
digitalWrite(RelayPin3, HIGH); toggleState_3 = 0; // if "C0" received Turn off Relay3
pref.putBool("Relay3", toggleState_3);
sendFeedback(2, 0);
}
else if(bt_data.substring(bt_data.lastIndexOf(",") + 1) == "D1"){
digitalWrite(RelayPin4, LOW); toggleState_4 = 1; // if "D1" received Turn on Relay4
pref.putBool("Relay4", toggleState_4);
sendFeedback(3, 100);
}
else if(bt_data.substring(bt_data.lastIndexOf(",") + 1) == "D0"){
digitalWrite(RelayPin4, HIGH); toggleState_4 = 0; // if "D0" received Turn off Relay4
pref.putBool("Relay4", toggleState_4);
sendFeedback(3, 0);
}
else if(bt_data.substring(bt_data.lastIndexOf(",") + 1) == "Z1"){ // if "Z1" received Turn on all Relays
all_SwitchOn();
}
else if(bt_data.substring(bt_data.lastIndexOf(",") + 1) == "Z0"){ // if "Z0" received Turn off all Relays
all_SwitchOff();
}
}
}
void all_SwitchOn(){
toggleState_1 = 1; digitalWrite(RelayPin1, LOW); pref.putBool("Relay1", toggleState_1); sendFeedback(0, 100);
delay(100);
toggleState_2 = 1; digitalWrite(RelayPin2, LOW); pref.putBool("Relay2", toggleState_2); sendFeedback(1, 100);
delay(100);
toggleState_3 = 1; digitalWrite(RelayPin3, LOW); pref.putBool("Relay3", toggleState_3); sendFeedback(2, 100);
delay(100);
toggleState_4 = 1; digitalWrite(RelayPin4, LOW); pref.putBool("Relay4", toggleState_4); sendFeedback(3, 100);
delay(100);
}
void all_SwitchOff(){
toggleState_1 = 0; digitalWrite(RelayPin1, HIGH); pref.putBool("Relay1", toggleState_1); sendFeedback(0, 0); delay(100);
59
toggleState_2 = 0; digitalWrite(RelayPin2, HIGH); pref.putBool("Relay2", toggleState_2); sendFeedback(1, 0); delay(100);
toggleState_3 = 0; digitalWrite(RelayPin3, HIGH); pref.putBool("Relay3", toggleState_3); sendFeedback(2, 0); delay(100);
toggleState_4 = 0; digitalWrite(RelayPin4, HIGH); pref.putBool("Relay4", toggleState_4); sendFeedback(3, 0); delay(100);
}
void getRelayState()
{
//Serial.println("reading data from NVS");
toggleState_1 = pref.getBool("Relay1", 0);
digitalWrite(RelayPin1, !toggleState_1);
(toggleState_1 == false) ? sendFeedback(0, 0) : sendFeedback(0, 100);
delay(200);
toggleState_2 = pref.getBool("Relay2", 0);
digitalWrite(RelayPin2, !toggleState_2);
(toggleState_2 == false) ? sendFeedback(1, 0) : sendFeedback(1, 100);
delay(200);
toggleState_3 = pref.getBool("Relay3", 0);
digitalWrite(RelayPin3, !toggleState_3);
(toggleState_3 == false) ? sendFeedback(2, 0) : sendFeedback(2, 100);
delay(200);
toggleState_4 = pref.getBool("Relay4", 0);
digitalWrite(RelayPin4, !toggleState_4);
(toggleState_4 == false) ? sendFeedback(3, 0) : sendFeedback(3, 100);
delay(200);
void setup()
{
Serial.begin(115200);
Serial2.begin(9600, SERIAL_8N1, RX2Pin, TX2Pin);
//Open namespace in read-write mode
pref.begin("Relay_State", false);
pinMode(RelayPin1, OUTPUT);
pinMode(RelayPin2, OUTPUT);
pinMode(RelayPin3, OUTPUT);
pinMode(RelayPin4, OUTPUT);
pinMode(wifiLed, OUTPUT);
digitalWrite(wifiLed, LOW);
if (wifiConnected)
60
{
addDevices();
}
else
{
Serial.println("Cannot connect to WiFi. So in Manual Mode");
}
delay(1000);
void loop()
{
if (WiFi.status() != WL_CONNECTED)
{
//Serial.print("WiFi Not Connected ");
digitalWrite(wifiLed, LOW); //Turn off WiFi LED
}
else
{
//Serial.print("WiFi Connected ");
digitalWrite(wifiLed, HIGH);
//WiFi Control
if (wifiConnected){
espalexa.loop();
delay(1);
}
else {
wifiConnected = connectWifi(); // Initialise wifi connection
if(wifiConnected){
addDevices();
}
}
}
ALGORITHM:
61
6. Implement functions to control relays via Bluetooth commands.
7. Define functions to toggle all relays on or off.
8. Initialize serial communication and preferences for relay states.
9. Set up GPIO pins as outputs and initialize relay states.
10.Check WiFi connection status and control relays accordingly.
11.Continuously loop to handle WiFi and Bluetooth control.
RESULT:
The WiFi and Bluetooth controlled relay system implemented on Raspberry Pi for home
automation is done successfully.
62