0% found this document useful (0 votes)
10 views22 pages

IOT File Nishant

The document is a practical file for the Internet of Things course at the Fairfield Institute of Management and Technology. It includes a series of practical exercises related to Arduino programming, such as setting up the Arduino IDE, writing programs for LED control, temperature monitoring, and home automation using NodeMCU and Blynk. Each practical is detailed with steps, code examples, and necessary components for implementation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views22 pages

IOT File Nishant

The document is a practical file for the Internet of Things course at the Fairfield Institute of Management and Technology. It includes a series of practical exercises related to Arduino programming, such as setting up the Arduino IDE, writing programs for LED control, temperature monitoring, and home automation using NodeMCU and Blynk. Each practical is detailed with steps, code examples, and necessary components for implementation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

FAIRFIELD INSTITUTE OF MANAGEMENT AND TECHNOLOGY

INTERNET OF THINGS
PRACTICAL FILE
SUBJECT CODE – BCA(306)

SUBMITTED TO: - SUBMITTED BY: -


Miss. Aruna Joshi Nishant Kumar
Asst.Professor (B.C.A 6th Sem)
I.T Department Enrollment No.- 00190102022
INDEX

S. No. Practicals Page No. Sign


1) Study and Install IDE of Arduino. 03

2) Write the Steps to Add Libraries in Arduino and Setup Arduino IDE 06
for Programming.

3) Write a Program using Arduino for Blink LED. 09

Write a Program for monitoring Temperature using Arduino and LM35


4) 10
Temperature Sensors.
5) Write a Program for Controlling Raspberry Pi with WhatsApp. 12

6) Write a program to shows how to fade an LED on pin 9 using the 14


analog Write()function.
Write the steps to add blynk libraries for NodeMCU and account on
7) IFTTT for home automation. 15

8) Write a program of Fade LED using NodeMCU(ESP8266) and blynk 18


app.

Write a program for Arduino by using Ultrasonic sensors and servo


9) motor (HC- SR04), and make a smart dustbin. 20

10) Write a program for controlling bulb on/off by using Blynk app.
22
Q-1: Study and Install IDE of Arduino.
Step 1: Download the Arduino IDE
1. Visit the Arduino Website
Go to the official Arduino website at arduino.cc.
2. Navigate to the Software Section
Click on the "Software" link in the top menu.
3. Choose Your Version
Select between the latest version of the Arduino IDE or the Arduino Web Editor. The
desktop version is recommended for most users.
4. Select Your Operating System
Download the version suitable for your operating system (Windows, macOS, or
Linux).
5. Download the Installer
Click the download link to obtain the installer file.
Step 2: Install the Arduino IDE
For Windows:
1. Run the Installer
Locate the downloaded .exe file and double-click to run the installer.
2. Follow the Installation Wizard
Follow the prompts in the wizard. Allow changes to your device if prompted.
3. Install Drivers
Ensure the option to install drivers is checked to allow your computer to recognize the
Arduino board.
4. Complete Installation
Once done, launch the Arduino IDE.
For macOS:
1. Open the Disk Image
Double-click the downloaded .dmg file to open it.
2. Drag to Applications
Drag the Arduino IDE icon into your Applications folder.
3. Open the IDE
Double-click the Arduino IDE in your Applications folder. If a security warning
appears, right-click and select "Open."
For Linux:
1. Extract the Archive
Locate the .tar.xz file and extract it using a file manager or terminal.
2. Run the IDE
Navigate to the extracted folder in the terminal and run the IDE by executing
./arduino.
3. Install Dependencies
Install additional required packages as specified in the README file included in the
extracted folder.
Step 3: Set Up Your Arduino Board
1. Connect Your Arduino Board
Use a USB cable to connect your Arduino board to your computer.
2. Select the Board Type
Open the Arduino IDE, go to Tools > Board, and select your board (e.g., Arduino
Uno, Arduino Mega).
3. Select the Port
Go to Tools > Port and choose the correct port:
o On Windows: It will look like COM3.
o On macOS/Linux: It will look like /dev/ttyUSB0 or /dev/ttyACM0.
Step 4: Write and Upload Your First Sketch
1. Open a New Sketch
Go to File > New in the Arduino IDE.
2. Write Your Code
Start with a simple example like the "Blink" sketch:
void setup() {
pinMode(LED_BUILTIN, OUTPUT); // Initialize the LED pin as an output
}

void loop() {
digitalWrite(LED_BUILTIN, HIGH); // Turn the LED on
delay(1000); // Wait for a second
digitalWrite(LED_BUILTIN, LOW); // Turn the LED off
delay(1000); // Wait for a second
}

3. Upload the Code


Click the Upload button (right arrow icon) in the Arduino IDE. The IDE will compile
and upload the code to your board.
4. Observe the Results
The built-in LED on your Arduino board should blink if everything is set up correctly.
Step 5: Explore Further
• Examples
Explore built-in examples under File > Examples to try different projects.
• Documentation
Check the Arduino Reference for detailed information on functions and libraries.
• Community
Join the Arduino community forums and explore online tutorials to expand your
skills.
Q-2: Write the Steps to Add Libraries in Arduino and Setup Arduino IDE for
Programming.
Step 1: Setting Up the Arduino IDE
1. Install the Arduino IDE
o If not already installed, download and install the Arduino IDE following the
steps in the previous response.
2. Open the Arduino IDE
o Launch the Arduino IDE on your computer.
3. Select Your Board
o Go to Tools > Board and select the type of Arduino board you are using (e.g.,
Arduino Uno, Arduino Mega, etc.).
4. Select the Port
o Go to Tools > Port and choose the COM port corresponding to your Arduino
board.
Step 2: Adding Libraries to the Arduino IDE
There are two primary methods to add libraries to the Arduino IDE:
Method 1: Using the Library Manager
1. Open the Library Manager
o In the Arduino IDE, navigate to Sketch > Include Library > Manage
Libraries....
2. Search for Libraries
o In the Library Manager window, type keywords in the search bar to find a
specific library (e.g., type "Adafruit" to find Adafruit libraries).
3. Install a Library
o Select the desired library and click the Install button. The library will be
automatically added to your Arduino IDE.
4. Close the Library Manager
o After installing the necessary libraries, close the Library Manager.
Method 2: Manually Adding Libraries
1. Download the Library
o Download the library from a trusted source like GitHub. Ensure it is in a ZIP
format.
2. Add the Library to the IDE
o In the Arduino IDE, go to Sketch > Include Library > Add .ZIP Library....
o Locate and select the downloaded ZIP file, then click Open. The library will
be added.
3. Extracting Libraries (if needed)
o If the library is in a folder (not a ZIP file), manually copy the folder to the
libraries directory:
▪ Windows: Documents\Arduino\libraries
▪ macOS: Documents/Arduino/libraries
▪ Linux: ~/Arduino/libraries
o Restart the Arduino IDE to recognize the new library.
Step 3: Using Libraries in Your Sketch
1. Include the Library
o To use a library, include it at the top of your sketch. For example:
#include <Adafruit_Sensor.h>

2. Use Library Functions


o After including the library, you can use its functions and classes. Refer to the
library’s documentation for detailed usage instructions.
Step 4: Writing and Uploading Your Code
1. Write Your Code
o Develop your sketch using the functions provided by the included libraries.
2. Upload the Code
o Click the Upload button (right arrow icon) to compile and upload the sketch
to your Arduino board.
3. Monitor Output
o Use the Serial Monitor (found under Tools > Serial Monitor) to view any
output from your Arduino, especially if the library involves serial
communication.
Q-3: Write a Program using Arduino for Blink LED.

const int ledPin = 13;

void setup() {
pinMode(ledPin, OUTPUT);
}

void loop() {
digitalWrite(ledPin, HIGH);
delay(1000);
digitalWrite(ledPin, LOW);
delay(1000);
}
Q-4: Write a Program for monitoring Temperature using Arduino and LM35
Temperature Sensor.
Components Needed
• Arduino Board (e.g., Arduino Uno)
• LM35 Temperature Sensor
• Breadboard and jumper wires

Circuit Connection
1. LM35 Connections:
o VCC Pin → Connect to the 5V pin of the Arduino.
o GND Pin → Connect to the GND pin of the Arduino.
o OUT Pin → Connect to an analog pin on the Arduino (e.g., A0).

Arduino Code
const int lm35Pin = A0;
void setup() {
Serial.begin(9600);
}

void loop() {
int sensorValue = analogRead(lm35Pin);

float voltage = sensorValue * (5.0 / 1023.0);

float temperatureC = voltage * 100.0;

Serial.print("Temperature: ");
Serial.print(temperatureC);
Serial.println(" °C");

delay(1000);
}
Q-5: Write a Program for Controlling Raspberry Pi with WhatsApp
import network
import requests
from time import sleep
import os

# Wi-Fi credentials
ssid = 'REPLACE_WITH_YOUR_SSID'
password = 'REPLACE_WITH_YOUR_PASSWORD'

# Your phone number in international format


phone_number = 'YOUR_PHONE_NUMBER_INTERNATIONAL_FORMAT' # e.g.,
'+1234567890'

# Your CallMeBot API key


api_key = 'CALLMEBOT_API_KEY'

# Initialize Wi-Fi connection


def init_wifi(ssid, password):
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(ssid, password)
connection_timeout = 10
while connection_timeout > 0:
if wlan.status() >= 3:
break
connection_timeout -= 1
print('Waiting for Wi-Fi connection...')
sleep(1)
if wlan.status() != 3:
print('Wi-Fi connection failed!')
return False
else:
print('Wi-Fi connected successfully!')
return True

# Send a WhatsApp message using CallMeBot


def send_message(phone_number, api_key, message):
url =
f'https://api.callmebot.com/whatsapp.php?phone={phone_number}&text={message}&apik
ey={api_key}'
response = requests.get(url)
if response.status_code == 200:
print('Message sent successfully!')
else:
print(f'Error sending message: {response.text}')

# Check and execute a command


def check_command(command):
if command == "status":
return os.popen('uptime').read()
elif command == "shutdown":
os.system('sudo shutdown now')
return "Shutting down..."
elif command == "restart":
os.system('sudo reboot')
return "Restarting..."
else:
return "Unknown command."

# Main execution
try:
if init_wifi(ssid, password): # Connect to Wi-Fi
while True:
# Simulate command retrieval (replace with actual logic for reading commands
from WhatsApp)
command = "status" # Replace this with dynamic command retrieval
response = check_command(command) # Execute the command
send_message(phone_number, api_key, response) # Send response via WhatsApp
sleep(10) # Delay before checking the next command
except Exception as e:
print(f'Error: {e}')
Q-6: Write a Program to shows how to Fade an LED on pin9 using the analog Write()
function.
// Define the pin where the LED is connected
const int ledPin = 9; // LED connected to pin 9

void setup() {
// Set the LED pin as an output
pinMode(ledPin, OUTPUT);
}

void loop() {
// Fade in from 0 to 255
for (int brightness = 0; brightness <= 255; brightness++) {
analogWrite(ledPin, brightness); // Set the brightness of the LED
delay(10); // Wait for 10 milliseconds to see the fade effect
}

// Fade out from 255 to 0


for (int brightness = 255; brightness >= 0; brightness--) {
analogWrite(ledPin, brightness); // Set the brightness of the LED
delay(10); // Wait for 10 milliseconds to see the fade effect
}
}
Q-7: Write the steps to add blynk libraries for NodeMCU and account on IFTTT for
Home Automation.
Step 1: Install Blynk Library in Arduino IDE
Method 1: Using Library Manager (Recommended)
1. Open Arduino IDE.
2. Navigate to Sketch > Include Library > Manage Libraries.
3. Type "Blynk" in the search bar.
4. Locate "Blynk by Volodymyr Shymanskyy" and click Install.
Method 2: Manual Installation
1. Download the Blynk library from its GitHub Repository.
2. Extract the downloaded ZIP file.
3. Move the extracted folder to the libraries directory:
o Windows: Documents/Arduino/libraries/
o Mac/Linux: ~/Arduino/libraries/
4. Restart the Arduino IDE to recognize the newly added library.
Step 2: Install ESP8266 Board for NodeMCU
1. Open the Arduino IDE.
2. Go to File > Preferences.
3. In the Additional Board Manager URLs, paste the following URL:
http://arduino.esp8266.com/stable/package_esp8266com_index.json

4. Click OK.
5. Navigate to Tools > Board > Boards Manager.
6. Search for ESP8266 and install the package ESP8266 by ESP8266 Community.
Step 3: Create a Blynk Project
1. Download and install the Blynk App on your smartphone (available for both Android
and iOS).
2. Sign up or log in using your email address.
3. Create a New Project:
o Device: Select ESP8266 (NodeMCU).
o Connection Type: Choose Wi-Fi.
o Click Create.
4. A unique Authentication Token will be sent to your email. Keep it handy for the
next steps.
Step 4: Upload Blynk Code to NodeMCU
Sample Code
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

char auth[] = "YourAuthToken"; // Enter Blynk Auth Token


char ssid[] = "YourWiFiSSID"; // Enter your Wi-Fi SSID
char pass[] = "YourWiFiPassword"; // Enter your Wi-Fi Password

void setup() {
Serial.begin(115200);
Blynk.begin(auth, ssid, pass);
}

void loop() {
Blynk.run(); // Run Blynk
}

1. Replace YourAuthToken, YourWiFiSSID, and YourWiFiPassword with your


respective values.
2. Connect the NodeMCU to your computer via a USB cable.
3. Select the correct Board (NodeMCU 1.0) and Port from the Tools menu.
4. Upload the code to the NodeMCU.
Step 5: Create an Account on IFTTT
1. Go to the IFTTT Website and create an account.
2. After logging in, click on "Create".
3. Configure the trigger:
o Click "If This" and search for "Google Assistant".
o Choose "Say a simple phrase".
o Example phrase: "Turn on the light".
4. Configure the action:
o Click "Then That" and search for "Webhooks".
o Select "Make a web request".
o Enter the following details:
▪ URL: Replace YourAuthToken and D2 with your token and pin:
http://blynk-cloud.com/YourAuthToken/update/D2

▪ Method: GET
▪ Content Type: application/json
▪ Body: Leave empty.
5. Click Create Action, then Finish.
Step 6: Test Home Automation
1. Use the Google Assistant and say the configured phrase (e.g., "Turn on the light").
2. IFTTT will send a Webhook request to Blynk.
3. The NodeMCU will execute the command and control the connected device (e.g.,
light).
Q-8: Write a program of Fade an LED Using NodeMCU (ESP8266) and the Blynk App.
Step-by-Step Explanation
1. Header Files:
o #include <ESP8266WiFi.h> and #include <BlynkSimpleEsp8266.h> are
included to enable Wi-Fi connectivity and interaction with the Blynk platform.
2. Authentication and Wi-Fi Credentials:
o Replace YourAuthToken with the token received from Blynk.
o Replace YourWiFiSSID and YourWiFiPassword with your Wi-Fi network
details.
3. LED Pin Configuration:
o int ledPin = D2; sets the LED to GPIO4 (NodeMCU pin D2).
4. Setup Function:
o Initializes the serial communication and connects to Blynk using
Blynk.begin(auth, ssid, pass);.
o Configures the LED pin as an output using pinMode(ledPin, OUTPUT);.
5. BLYNK_WRITE(V1):
o This function is triggered whenever the value of virtual pin V1 in the Blynk
app changes.
o The brightness value (from 0 to 1023) is read from the Blynk app's slider
widget using param.asInt();.
o The analogWrite(ledPin, brightness); function adjusts the LED brightness.
6. Loop Function:
o Ensures continuous communication with the Blynk server using Blynk.run();.
Blynk App Configuration
1. Open the Blynk app and create a new project.
2. Choose ESP8266 as the device and Wi-Fi as the connection type.
3. Add a Slider Widget to the project:
o Virtual Pin: Set to V1.
o Range: Set from 0 to 1023.
4. Save the project and copy the Auth Token to use in the code.
Complete Code
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

char auth[] = "YourAuthToken"; // Blynk Auth Token


char ssid[] = "YourWiFiSSID"; // Wi-Fi Name
char pass[] = "YourWiFiPassword"; // Wi-Fi Password

int ledPin = D2; // LED connected to GPIO4 (D2)

void setup() {
Serial.begin(115200);
Blynk.begin(auth, ssid, pass);
pinMode(ledPin, OUTPUT);
}

BLYNK_WRITE(V1) {
int brightness = param.asInt(); // Get slider value from Blynk (0-1023)
analogWrite(ledPin, brightness); // Adjust LED brightness
}

void loop() {
Blynk.run(); // Keep Blynk connected
}
Q-9: Write a program for Arduino by using Ultrasonic Sensor and Servo Motor (HC-
SR04), and make a smart dustbin.
#include <Servo.h>

// Define pins for Ultrasonic Sensor


const int trigPin = 9;
const int echoPin = 10;

// Define Servo
Servo dustbinLid;
const int servoPin = 6;

// Define distance threshold (in cm)


const int openDistance = 15; // Open lid when an object is within 15 cm
const int closeDistance = 20; // Close lid if object is beyond 20 cm

void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);

dustbinLid.attach(servoPin);
dustbinLid.write(0); // Start with lid closed

Serial.begin(9600); // For debugging


}

void loop() {
long duration;
int distance;

// Send ultrasonic pulse


digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

// Read echo response


duration = pulseIn(echoPin, HIGH);
distance = duration * 0.034 / 2; // Convert to cm

Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");

// Open lid if object is close


if (distance > 0 && distance <= openDistance) {
dustbinLid.write(90); // Open lid (90°)
delay(3000); // Keep it open for 3 seconds
}
// Close lid if no object is nearby
else if (distance > closeDistance) {
dustbinLid.write(0); // Close lid (0°)
}

delay(500); // Small delay for stable readings


}
Q-10: Write a program for controlling bulb on/off by using Blynk App.

#define BLYNK_PRINT Serial


#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

char auth[] = "YourAuthToken"; // Enter your Blynk Auth Token


char ssid[] = "YourWiFiSSID"; // Enter your WiFi Name
char pass[] = "YourWiFiPassword"; // Enter your WiFi Password

int relayPin = D2; // Relay connected to GPIO4 (D2)

void setup() {
Serial.begin(115200);
Blynk.begin(auth, ssid, pass);
pinMode(relayPin, OUTPUT);
digitalWrite(relayPin, HIGH); // Default: Relay OFF (Normally Open)
}

BLYNK_WRITE(V1) {
int value = param.asInt(); // Read button value (0 or 1)
digitalWrite(relayPin, value ? LOW : HIGH); // Turn ON/OFF relay
}

void loop() {
Blynk.run(); // Keep Blynk connected
}

You might also like