IOT File Nishant
IOT File Nishant
INTERNET OF THINGS
PRACTICAL FILE
SUBJECT CODE – BCA(306)
2) Write the Steps to Add Libraries in Arduino and Setup Arduino IDE 06
for Programming.
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
}
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);
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'
# 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
}
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>
void setup() {
Serial.begin(115200);
Blynk.begin(auth, ssid, pass);
}
void loop() {
Blynk.run(); // Run Blynk
}
▪ 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>
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 Servo
Servo dustbinLid;
const int servoPin = 6;
void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
dustbinLid.attach(servoPin);
dustbinLid.write(0); // Start with lid closed
void loop() {
long duration;
int distance;
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
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
}