IoT lab file
IoT lab file
PRADESH, GWALIOR
PRACTICAL FILE OF
INTERNET OF THINGS (IOT) LAB
CSE 623
SUBMITTED TO SUBMITTED BY
Dr. Shyam Sundar Gupta Pragya Gupta
A60205221026
B.Tech(CSE)
Section – B
2021-25
Applications of Python:
1. Web Development: Frameworks like Django and Flask are used to build dynamic web
applications and APIs.
2. Data Science and Machine Learning: Python's libraries such as NumPy, Pandas,
Matplotlib, and scikit-learn are widely used for data analysis, visualization, and
machine learning tasks.
3. Scripting: Python is popular for scripting tasks, automation, and system administration
due to its simplicity and versatility.
4. Scientific Computing: Python, along with libraries like SciPy and OpenCV, is used for
scientific computing, simulations, and image processing.
5. Game Development: Python, with libraries like Pygame, is utilized in game
development for prototyping, scripting, and building simple games.
6. Desktop GUI Applications: Libraries like Tkinter and PyQt enable the development
of desktop graphical user interface (GUI) applications.
7. Network Programming: Python's socket library facilitates network programming,
including server-client applications and networking tools.
8. Education: Python is extensively used in educational institutions for teaching
programming due to its simplicity and readability.
1
9. Embedded Systems: Python can be used for developing applications for embedded
systems and IoT devices, leveraging microcontroller platforms like Raspberry Pi and
Arduino.
10. Finance and Trading: Python is employed in finance for quantitative analysis,
algorithmic trading, and building financial models due to its robust libraries and tools.
2
LAB EXPERIMENT – 2
AIM: To understand 5 libraries of python and their uses along with code implementing those
libraries.
Theory: 5 libraries of Python and their uses:
1. NumPy (Numerical Python): NumPy is a fundamental package for numerical
computing in Python. It provides support for arrays, matrices, and a wide range of
mathematical functions to operate on these arrays efficiently.
Applications:
2. Pandas: Pandas is a powerful library for data manipulation and analysis, particularly
with labeled and relational data. It provides data structures like DataFrame and Series,
along with functions to manipulate and analyze them efficiently.
Applications:
3
Applications:
5. TensorFlow (or PyTorch): TensorFlow and PyTorch are leading deep learning
frameworks used for building and training neural networks. They provide high-level
APIs for constructing complex neural network architectures, along with efficient
implementations of automatic differentiation for training models.
Applications:
• These frameworks are used in various deep learning tasks, including image
recognition, natural language processing, speech recognition, and reinforcement
learning.
• They are utilized in both research and industry for developing state-of-the-art
deep learning models and deploying them in production environments.
Programs:
1. Code for Numpy:
Code:
import numpy as np
4
data = {'Name': ['Alice', 'Bob', 'Charlie'],
'Age': [25, 30, 35],
'City': ['New York', 'Los Angeles', 'Chicago']}
df = pd.DataFrame(data)
# Create data
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
# Plot data
plt.plot(x, y)
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Simple Line Plot')
plt.show()
5
iris = load_iris()
X, y = iris.data, iris.target
6
model.fit(X_train, y_train, epochs=5, batch_size=32)
OUTPUT:
For Numpy:
For Pandas:
For Matplotlib:
For Scikt-learn:
7
LAB EXPERIMENT – 3
Step - 2: Click on the Install Now, & Double-click the executable file, which is downloaded.
8
The following window will open. Click on the Add Path check box, it will set the Python path
automatically.
Now, Select Customize installation and proceed. We can also click on the customize installation
to choose desired location and features. Other important thing is install launcher for the all user
must be checked.
Here, under the advanced options, click on the checkboxes of " Install Python 3.11 for all users
", which is previously not checked in. This will checks the other option " Precompile standard
library " automatically. And the location of the installation will also be changed. We can change
it later, so we leave the install location default. Then, click on the install button to finally install.
The set up is in progress. All the python libraries, packages, and other python default files will
be installed in our system. Once the installation is successful, the following page will appear
saying " Setup was successful ".
9
Step - 4: Verifying the Python Installation
To verify whether the python is installed or not in our system, we have to do the following.
Go to "Start" button, and search " cmd ".
Then type, " python - - version ".
If python is successfully installed, then we can see the version of the python installed.
If not installed, then it will print the error as " 'python' is not recognized as an internal or
external command, operable program or batch file. ".
10
LAB EXPERIMENT – 6
# Float
float_var = 3.14
# String
string_var = "Hello, World!"
# Boolean
boolean_var = True
# List
list_var = [1, 2, 3, 4, 5]
# Tuple
tuple_var = (6, 7, 8, 9, 10)
# Dictionary
dictionary_var = {"apple": 5, "banana": 3, "orange": 2}
# Set
set_var = {1, 2, 3, 4, 5}
Output:
Output:
AIM: To write a program for looping statement in Python.
Program:
# For loop example
print("For loop example:")
for i in range(1, 6):
print("Iteration", i)
# Sample data
x = [1, 2, 3, 4, 5]
y = [2, 3, 5, 7, 11]
Output:
LAB EXPERIMENT – 1
AIM: Study and Install IDE of Arduino. Study of different types of Arduino boards.
Theory: The Arduino Integrated Development Environment - or Arduino Software (IDE) -
contains a text editor for writing code, a message area, a text console, a toolbar with buttons
for common functions and a series of menus. It connects to the Arduino hardware to upload
programs and communicate with them.
The process will extract and install all the required files to execute properly the Arduino
Software (IDE) and then IDE is ready to use.
The Uno is a huge option for your initial Arduino. This Arduino board depends on an
ATmega328P based microcontroller. As compared with other types of arduino boards, it is very
simple to use like the Arduino Mega type board. .It consists of 14-digital I/O pins, where 6-
pins can be used as PWM(pulse width modulation outputs), 6-analog inputs, a reset button, a
power jack, a USB connection, an In-Circuit Serial Programming header (ICSP), etc. It
includes everything required to hold up the microcontroller; simply attach it to a PC with the
help of a USB cable and give the supply to get started with an AC-to-DC adapter or battery.
Arduino Uno is the most frequently used board and it is the standard form apart from all the
existing Arduino Boards. This board is very useful for beginners.
Arduino Nano
This is a small board based on the microcontrollers like ATmega328P otherwise ATmega628
but the connection of this board is the same as to the Arduino UNO board. This kind of
microcontroller board is very small in size, sustainable, flexible, and reliable.
Arduino Nano
As compared with the Arduino Uno board, it is small in size. The devices like mini USB and
Arduino IDE are necessary to build the projects. This board mainly includes analog pins-8,
digital pins-14 with the set of an I/O pin, power pins-6 & RST (reset) pins-2.
Arduino Micro
The Arduino Micro board mainly depends on the ATmega32U4 based Microcontroller that
includes 20-sets of pins where the 7-pins are PWM pins, 12-analog input pins. This board
includes different components like an ICSP header, RST button, small USB connection, crystal
oscillator-16MHz. The USB connection is inbuilt and this board is the shrunk version of the
Leonardo board.
Arduino Micro
Arduino Due
This Arduino board depends on the ARM Cortex-M3 and it is the first Arduino microcontroller
board. This board includes digital I/O pins-54 where 12-pins are PWM o/p pins, analog pins -
12, UARTs-4, a CLK with 84 MHz, an USB OTG, DAC-2, a power jack, TWI-2, a JTAG
header, an SPI header, two buttons for reset & erase.
Arduino Due
This board works with 3.3V where the highest voltage that the pins of input/output can stand
is 3.3V because providing a high voltage to any I/O pin can lead to damage the board This
board is simply connected to a computer through a small USB cable otherwise it can be
powered through an AC to DC adapter. This Arduino Due board is suitable with all shields of
Arduino at 3.3V.
digitalWrite(11,HIGH);
delay(100);
digitalWrite(11,LOW);
delay(100);
digitalWrite(13,HIGH);
delay(100);
digitalWrite(13,LOW);
delay(100);
}
Output:
LAB EXPERIMENT – 4
digitalWrite(13, LOW);
delay(1000);
digitalWrite(12, HIGH);
delay(1000);
digitalWrite(12, LOW);
delay(1000);
digitalWrite(11, HIGH);
delay(1000);
digitalWrite(11, LOW);
delay(1000);
}
Output:
LAB EXPERIMENT – 2
Output:
LAB EXPERIMENT – 13
void setup() {
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
Serial.print("Distance: ");
Serial.print(distanceCM);
Serial.println(" cm");
delay(1000);
}
Output:
LAB EXPERIMENT – 15
AIM: Study the Temperature sensor and Write Program for monitor temperature using
Arduino.
Theory: LM35 Temperature Sensor is an integrated analog temperature sensor whose electrical
output is proportional to Degree Centigrade. LM35 Sensor does not require any external
calibration or trimming to provide typical accuracies. The LM35’s low output impedance,
linear output, and precise inherent calibration make interfacing to readout or control circuitry
especially easy.
Program:
const int lm35Pin = A0;
30
void setup() {
Serial.begin(9600);
}
void loop() {
int sensorValue = analogRead(lm35Pin);
Serial.print("Temperature: ");
Serial.print(temperatureCelsius);
Serial.println(" degrees Celsius");
delay(1000);
}
Output:
31
LAB EXPERIMENT – 16
Code:
#include <SPI.h>
#include <MFRC522.h>
#define SS_PIN 10
#define RST_PIN 5
MFRC522 rfid(SS_PIN, RST_PIN);
void setup() {
Serial.begin(9600);
SPI.begin();
rfid.PCD_Init();
Serial.println("Tap RFID/NFC Tag on reader");
}
void loop() {
if (rfid.PICC_IsNewCardPresent()) {
if (rfid.PICC_ReadCardSerial()) {
MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak);
32
//Serial.print("RFID/NFC Tag Type: ");
//Serial.println(rfid.PICC_GetTypeName(piccType));
Serial.print("UID:");
for (int i = 0; i < rfid.uid.size; i++) {
Serial.print(rfid.uid.uidByte[i] < 0x10 ? " 0" : " ");
Serial.print(rfid.uid.uidByte[i], HEX);
}
Serial.println();
rfid.PICC_HaltA();
rfid.PCD_StopCrypto1();
}
}
}
Output:
On Arduino IDE, Go to Tools -> Manage Libraries
33
Run the above code and open with Arduino IDE.
Click Upload button on Arduino Ide to upload code to Arduino.
Open Serial Monitor.
Tap several RFID/NFC tags on RFID-RC522 module.
See UID on Serial Monitor.
34
LAB EXPERIMENT – 17
void setup() {
Serial.begin(9600);
Ethernet.begin(mac, ip);
delay(3000);
mqttClient.setServer(server, 1883);
if (mqttClient.connect("myClientID")) {
Serial.println("Connection has been established, well done");
mqttClient.setCallback(subscribeReceive);
} else {
Serial.println("Looks like the server connection failed...");
}
}
35
void loop() {
mqttClient.loop();
mqttClient.subscribe("MakerIOTopic");
if (mqttClient.publish("MakerIOTopic", "Hello World")) {
Serial.println("Publish message success");
} else {
Serial.println("Could not send message :(");
}
delay(4000);
}
void subscribeReceive(char* topic, byte* payload, unsigned int length) {
Serial.print("Topic: ");
Serial.println(topic);
Serial.print("Message: ");
for(int i = 0; i < length; i++) {
Serial.print(char(payload[i]));
}
Serial.println("");
}
Output:
36
LAB EXPERIMENT – 18
Specs of the Computer: – The computer has a quad-core ARM processor that doesn’t support
the same instruction as an X86 desktop CPU. It has 1GB of RAM, One HDMI port, four USB
ports, one Ethernet connection, Micro SD slot for storage, one combined 3.5mm audio/video
port, and a Bluetooth connection. It has got a series of input and output pins that are used for
making projects like – home security cameras, Encrypted Door lock, etc.
Raspberry Pi - Configuration
For configuring Raspberry Pi in Raspbian, we are using Raspbian with PIXEL desktop. It is
one of the best ways to get Raspbian started with the Raspberry Pi. Once we finish booting, we
will be in the PIXEL desktop environment.
37
Now to open the menu, you need to click the button that has the Raspberry Pi logo on it. This
button will be in the top left. After clicking the button, choose Raspberry Pi configuration from
the preferences.
Configuration tool
Following is the configuration tool in PIXEL desktop –
By default, the configuration tool opens to its system tab which has the following options −
• Change Password − The default password is raspberry. You can change it by clicking
the change password button.
• Change the hostname − The default name is raspberry pi. You can also change it to
the name, which you want to use on the network.
• Boot − You can choose from the two options and control whether Raspberry Pi boots
into the desktop or CLI i.e., command line interface.
• Auto Login − With the help of this option, you can set whether the user should
automatically log in or not.
• Network at Boot − By choosing this option, you can set whether the pi user is
automatically logged in or not.
• Splash screen − You can enable or disable it. On enabling, it will display the graphical
splash screen that shows when Raspberry Pi is booting.
• Resolution − With the help of this option, you can configure the resolution of your
screen.
• Underscan − There are two options, enable or disable. It is used to change the size of
the displayed screen image to optimally fill the screen. If you see a black border around
38
the screen, you should disable the underscan. Whereas, you should enable the
underscan, if your desktop does not fit your screen.
There are three other tabs namely Interfaces, Performance, and Localization. The job of
interface tab is to enable or disable various connection options on your Raspberry Pi.
You can enable the Pi camera from the interface tab. You can also set up a secure connection
between computers by using SSH (short for Secure Shell) option.
If you want to remote access your Pi with a graphical interface then, you can enable RealVNC
software from this tab. SPI, I2C, Serial, 1-wire, and Remote GPIO are some other interfaces
you can use.
There is another tab called Performance, which will give you access to the options for
overclocking and changing the GPU memory.
The localization tab, as the name implies, enable us to set −
• The character set used in our language.
• Our time zone.
• The keyboard setup as per our choice.
• Our Wi-Fi country.
Configure Wi-Fi
You can check at the top right, there would be icons for Bluetooth and Wi-Fi. The fan-shaped
icon is on the Wi-Fi. To configure your Wi-Fi, you need to click on that icon. Once clicked, it
will open a menu showing the available networks. It also shows the option to turn off your Wi-
Fi.
Among those available networks, you need to select a network. After selecting, it will prompt
for entering the Wi-Fi password i.e., the Pre Shared Key.
If you see a red cross on the icon, it means your connection has been failed or dropped. To test
whether your Wi-Fi is working correctly, open a web browser and visit a web page.
39
Data Partition Setup
As we know that data partition is that area on your memory card (SD or MicroSD) which can
be shared by various distributions. One of the best examples of use of a data partition is
transferring the files between distributions.
The data partition has the label data.
You can use this labeled data to make a directory point to it as follows −
40
LAB EXPERIMENT – 7
while l:
print(“LED ON”)
GPIO.output(4,GPIO.HIGH)
GPIO.output(5.GPIO.HIGH)
Time.sleep(1)
print(“LED OFF”)
GPIO.output(4,GPIO.LOW)
GPIO.output(5.GPIO.LOW)
Time.sleep(1)
Output:
41
LAB EXPERIMENT – 20
AIM: Study and Implement ZIGBEE Protocol using Arduino / Raspberry Pi.
Theory: Zigbee is a standards-based wireless technology developed to enable low-cost, low-
power wireless machine-tomachine (M2M) and internet of things (IoT) networks. Zigbee is
for low-data rate, low-power applications and is an open standard.
In above circuit. TX and RX pin of zigbee and Arduino are connected to each other. Arduino
will send some instruction to zigbee and according to these instruction zigbee respond.
Similaryzigbee receive intructions from other zigbee to which it have address. After receiving
instructions or data from other zigbee. It send data to arduino through serial pins as shown in
connection. Similarly other module can be connected with one more Arduino or computer.
Same connection diagram is used for other zigbee and arduino pair.
Code:
void setup() {
Serial.begin(9600);
}
void loop() {
while (Serial.available()) {
Serial.write(Serial.read());
}
}
After connecting Arduino with it, upload the above code in Arduino, Arduino start receiving
whatever zigbee send to Arduino through serial communication.
42