0% found this document useful (0 votes)
4 views46 pages

Pava

The document provides a comprehensive overview of Arduino boards, focusing on the Arduino Uno, its pin configuration, and various applications. It details experiments involving interfacing components like LEDs, ultrasonic sensors, and Bluetooth modules with Arduino, including circuit descriptions and sample code. The content is aimed at helping users understand and implement interactive electronic projects using Arduino technology.

Uploaded by

thakkarsujal01
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)
4 views46 pages

Pava

The document provides a comprehensive overview of Arduino boards, focusing on the Arduino Uno, its pin configuration, and various applications. It details experiments involving interfacing components like LEDs, ultrasonic sensors, and Bluetooth modules with Arduino, including circuit descriptions and sample code. The content is aimed at helping users understand and implement interactive electronic projects using Arduino technology.

Uploaded by

thakkarsujal01
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

ELEMENTS OF INTERNETOF THINGS

202045613

Experiment 1
1. Study of Arduino board with their pin diagram and Interfacing
of LED (s) with Arduino.

What is an Arduino Board?

An Arduino board is an open-source electronics platform based on easy-to-use hardware and


software. It's designed for beginners and professionals to build digital devices and interactive
objects that can sense and control physical devices.

The most popular Arduino board is the Arduino UNO, based on the ATmega328P
microcontroller.

Types of Arduino Boards


Board Name Microcontroller Digital I/O Analog Features
Pins Pins

Arduino Uno ATmega328P 14 (6 PWM) 6 Most commonly used beginner


board

Arduino Mega ATmega2560 54 (15 PWM) 16 Used for projects needing more
I/O

Arduino Nano ATmega328P 14 (6 PWM) 8 Compact size, USB mini


connector

Arduino ATmega32u4 20 (7 PWM) 12 Has built-in USB


Leonardo communication

Arduino Due Atmel SAM3X8E 54 12 32-bit board for advanced


ARM projects

Arduino Uno – Technical Specifications

Pushpraj chavda
12302080501048 Page |
ELEMENTS OF INTERNETOF THINGS
202045613

Arduino Uno Pin Diagram and Description


The Arduino Uno contains multiple types of pins used for power management, digital I/O, analog input,
and communication. Below is a breakdown of its pins:
1. Power Pins
• Vin: Voltage input from an external source.
• 5V: Provides 5V output to power external components.
• 3.3V: Provides 3.3V output for lower voltage components.
• GND: Ground reference pins.
• RESET: Used to restart the board. Can be triggered via a button or external connection.
2. Digital I/O Pins (0–13)
• Can be used as input or output using the pinMode(), digitalWrite(), and digitalRead()
functions.
• Pins 3, 5, 6, 9, 10, and 11 support PWM (Pulse Width Modulation), useful for analog-like
control (e.g., LED brightness).
[Link] Input Pins (A0–A5)
• Used to read analog signals from sensors.
• Each pin has a 10-bit ADC, converting analog voltage (0–5V) into a value between 0 and 1023.
4. Communication Pins
• Serial (Pin 0: RX, Pin 1: TX): Used for serial communication (e.g., USB).
• SPI (Pins 10–13): Used for high-speed data exchange with peripherals like SD cards.
• I2C (A4: SDA, A5: SCL): Used for communicating with sensors or displays.
5. AREF Pin
• The Analog Reference pin is used to provide an external reference voltage for the analog
inputs.

Pushpraj chavda
12302080501048 Page |
ELEMENTS OF INTERNETOF THINGS
202045613

Applications of Arduino
• Home automation systems
• Robotic control systems
• Weather monitoring stations
• IoT-based devices
• Industrial automation
• Educational tools and simulation
The Arduino Uno is an excellent tool for learning embedded systems and electronics. Its open-
source nature, simple hardware, and extensive community support make it a preferred choice
for engineers and students. Understanding the Arduino board's pin configuration, architecture,
and programming methodology is fundamental for designing and implementing electronic
projects effectively.

Interfacing of LED(s) with Arduino Uno


Objective: To interface an LED with the Arduino Uno and blink it using a basic program.

Components Required
• Arduino Uno board
• Breadboard
• LED
• 220-ohm resistor
• Jumper wires
• USB cable for Arduino

Circuit Description
• The anode (longer leg) of the LED is connected to digital pin 13 of the Arduino
through a 220-ohm resistor.
• The cathode (shorter leg) of the LED is connected to GND.
• The resistor is used to limit the current to protect the LED.

Pushpraj chavda
12302080501048 Page |
ELEMENTS OF INTERNETOF THINGS
202045613

Code:
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}

Pushpraj chavda
12302080501048 Page |
ELEMENTS OF INTERNETOF THINGS
202045613

Experiment 2
2. Study and implementation of Buzzer, Switches, LCD, keypad,
LDR, Ultrasonic sensors and PWM interfacing with Arduino.
1. Introduction
Using Arduino, we can easily interface a variety of sensors and modules to build
real-world interactive systems.
In this experiment, we have used the Arduino Uno board to interface the
following components:
• Ultrasonic Sensor (HC-SR04)
• LDR (Light Dependent Resistor)
• Heartbeat Sensor
• Joystick Module
Each component is implemented separately with its description, circuit
explanation, and sample code.
Ultrasonic Sensor (HC-SR04)
Working Principle:
The ultrasonic sensor measures distance by emitting an ultrasonic pulse and
calculating the time it takes for the echo to return.
Distance (cm)=(Time×0.0343)/2
Pin Configuration:
• VCC → 5V
• GND → GND
• TRIG → Digital Pin 9
• ECHO → Digital Pin 10

Pushpraj chavda
12302080501048 Page |
ELEMENTS OF INTERNETOF THINGS
202045613

Circuit Description:
• Connect the TRIG pin to pin 9 and ECHO to pin 10 on the Arduino.
• VCC to 5V and GND to Arduino’s GND.

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

void setup() {
[Link](9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}

void loop() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);

Pushpraj chavda
12302080501048 Page |
ELEMENTS OF INTERNETOF THINGS
202045613

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

long duration = pulseIn(echoPin, HIGH);


float distance = (duration * 0.0343) / 2;
[Link]("Distance: ");
[Link](distance);
[Link](" cm");
delay(500);
}

Output:

Pushpraj chavda
12302080501048 Page |
ELEMENTS OF INTERNETOF THINGS
202045613

LDR (Light Dependent Resistor)


Working Principle:
An LDR changes its resistance with light intensity. It can be used to detect
brightness levels.
Circuit Description:
• One end of the LDR is connected to 5V.
• The other end connects to analog pin A0 and a 10kΩ resistor to GND
(voltage divider).

Code:
void setup() {
[Link](9600);
}

void loop() {
int lightValue = analogRead(A0);
[Link]("Light Intensity: ");
[Link](lightValue);
delay(500);
}

Pushpraj chavda
12302080501048 Page |
ELEMENTS OF INTERNETOF THINGS
202045613

Output:

Heartbeat Sensor
Working Principle
The heartbeat sensor detects the pulse by sensing the change in blood flow
using an infrared LED and photodiode.
Pin Configuration
• VCC → 5V
• GND → GND
• OUT → Analog Pin A1

Pushpraj chavda
12302080501048 Page |
ELEMENTS OF INTERNETOF THINGS
202045613

Circuit Description
Connect the sensor’s OUT pin to A1, VCC to 5V, and GND to Arduino GND.

Code:
int k = 0;
float y,x;
void setup()
{
pinMode(A0, INPUT);
pinMode(8,OUTPUT);
[Link](9600);
}
void loop()
{
while (k < 5) {
//[Link]((double)analogRead(A0)/1024);
x = (-40 + 0.488155 * (analogRead(A0) - 20));
y = (x - 32) * (5.0/9.0);
[Link]("Fahrenheit : " );
[Link](x);

Pushpraj chavda
12302080501048 Page |
ELEMENTS OF INTERNETOF THINGS
202045613

[Link]("Celcius : " );
[Link](y);
if (y > 30){
digitalWrite(8,HIGH);
}
else{
digitalWrite(8,LOW);
}
delay(1000); // Wait for 1000 millisecond(s)
k += 1;
}
}

Output:

Pushpraj chavda
12302080501048 Page |
ELEMENTS OF INTERNETOF THINGS
202045613

Joystick Module
Working Principle
A joystick module includes two potentiometers (X and Y axes) and a push-
button. It outputs analog values depending on the direction of movement.
Pin Configuration
• VCC → 5V
• GND → GND
• VRx → Analog Pin A2
• VRy → Analog Pin A3
• SW (Switch) → Digital Pin 7
Circuit Description
Connect VRx to A2, VRy to A3, SW to pin 7, VCC to 5V, and GND to GND.

Code:
void setup()
{
[Link](9600);
pinMode(9,INPUT); // SW pin
digitalWrite(9,HIGH);
}
int prev_state=0; // previous state of switch
void loop() {

Pushpraj chavda
12302080501048 Page |
ELEMENTS OF INTERNETOF THINGS
202045613

int z=0,xpos=0,ypos=0;
int x=analogRead(A0);
int y=analogRead(A1);
int sensitivity=10; // you can adjust the sensitivity based on your comfort
if(x>=550) // when moved up
xpos=map(x,550,1023,0,sensitivity);
if(x<=450) // when moved down
xpos=map(x,450,0,0,-sensitivity);
if(y>=550) // when moved right
ypos=map(y,550,1023,0,sensitivity);
if(y<=450) // when moved left
ypos=map(y,450,0,0,-sensitivity);
int curr_state=digitalRead(9);
if(curr_state==1 && prev_state==0) // when SW is pressed
z=1;
else
z=0;
if(xpos!=0 or ypos!=0 or z==1) // prints only when the joystick is moved
{
[Link](xpos); // print the data and separating by ":"
[Link](":");
[Link](ypos);
[Link](":");
[Link](z);
}
prev_state=curr_state;
delay(10); // for normal operation
}

Pushpraj chavda
12302080501048 Page |
ELEMENTS OF INTERNETOF THINGS
202045613

Output:

Pushpraj chavda
12302080501048 Page |
ELEMENTS OF INTERNETOF THINGS
202045613

Experiment 3
3. To interface Bluetooth with Arduino/Raspberry Pi and
write a program to send sensor data to smartphone using
Bluetooth.
Requirements: HC-05 Bluetooth Module, Arduino UNO Board, Breadboard,
jumper wires, Bluetooth App.

Arduino + Bluetooth (HC-05)


Hardware:
• Arduino UNO
• Bluetooth module (HC-05):
o Bluetooth Communication is a 2.4GHz frequency based RF
Communication with a range of approximately 10 meters. It is
one of the most popular and most frequently used low range
communication for data transfer, audio systems, handsfree,
computer peripherals etc.
o HC-05 module is an easy-to-use Bluetooth SPP (Serial Port
Protocol) module, designed for transparent wireless serial
connection setup.
o The HC-05 Bluetooth Module can be used in a Master or Slave
configuration, making it a great solution for wireless
communication.
o This serial port bluetooth module is fully qualified Bluetooth V2.0
+ EDR (Enhanced Data Rate) 3Mbps Modulation with complete
2.4GHz radio trans- ceiver and baseband.

Pin Description:
• EN: The module is enabled if the pin to EN is connected to 3.3V
supply, the module remains disabled if connected to GND.
• +5V: This is the supply pin for connecting +5V. As the module has
on-board 3.3V regulator, you can provide +5V supply.

Pushpraj chavda
12302080501048 Page |
ELEMENTS OF INTERNETOF THINGS
202045613

• GN D: The ground pin.


• TX: It is the Transmitter pin of UART Communication.
• RX: It is the Receive Pin of UART

• STATE: This is a status indicator pin. This pin goes LOW when the
module is not connected to any device. When the module is
paired with any device, this pin goes HIGH.

Procedure:
1. Make the connections as shown in the above image. Don't connect the
RX & TX pins before uploading the code!
2. Copy the code given below.
3. Download the app called Blue Control from Google Play Store. You can
down-load it from the following link Bluetooth Controller. You can make
your own Android app by using MIT App inventor.

Pushpraj chavda
12302080501048 Page |
ELEMENTS OF INTERNETOF THINGS
202045613

4. Open the app blue control (It will automatically turn on the device's
Blue-tooth). Go to options. Click on "Connect to Robot". Choose the device
HC 05.
5. When you are connecting to the Bluetooth module for the first time, it
will ask you the password. Enter 0000 or 1234.
6. When the device gets successfully paired with the sensor, the LED lights
on sensor will start blinking at a slower rate than usual.

Code for Arduino:


CODE: -
#include <ESP8266WiFi.h>

const char* WIFI_SSID = "Sneh"

const char* WIFI_PASS = "12345678";

void connectToWiFi() {

[Link]("Connecting to WiFi: ");

[Link](WIFI_SSID);

[Link](WIFI_SSID, WIFI_PASS);

int retries = 0;

while ([Link]() != WL_CONNECTED && retries < 20) {

delay(500);

[Link](".");

retries++;

if ([Link]() == WL_CONNECTED) {

[Link]("\n WiFi Connected!");

[Link](" IP Address: ");

[Link]([Link]());

} else {

Pushpraj chavda
12302080501048 Page |
ELEMENTS OF INTERNETOF THINGS
202045613

[Link]("\n WiFi Connection Failed!");

void setup() {

[Link](115200);

delay(100);

connectToWiFi();

void loop() {

// Check connection status every 5 seconds

if ([Link]() != WL_CONNECTED) {

[Link](" WiFi Disconnected! Reconnecting...");

connectToWiFi();

delay(5000);

Pushpraj chavda
12302080501048 Page |
ELEMENTS OF INTERNETOF THINGS
202045613

Experiment 4
4. Study NodeMCU with their pin diagram and Implementation
LED
(s) Interfacing with NodeMCU.
1. Introduction
The NodeMCU is an open-source development board built around the ESP8266 Wi-Fi
module, which is widely used in IoT (Internet of Things) projects due to its low cost,
built-in Wi-Fi capability, and ease of programming. It provides a simple and efficient
platform for developers to connect physical devices to the internet and control them
wirelessly.
One of the fundamental steps in learning to work with NodeMCU is understanding its pin
diagram, which includes GPIO (General Purpose Input/Output) pins, power pins, and
communication interfaces such as UART, SPI, and I2C. Familiarity with these pins is
crucial for interfacing the board with external components like sensors, actuators, and
LEDs.

2. Features
• Built-in Wi-Fi module (ESP8266) for IoT applications.
• Based on ESP8266 Wi-Fi SoC
• Supports standard Arduino IDE for programming
• Multiple GPIO pins available for interfacing
• Low Power Consumption: Ideal for battery-powered and portable devices.
• Analog Input: Single ADC (A0) pin for analog sensor readings.

Pin diagram of NodeMCU

Pushpraj chavda
12302080501048 Page |
ELEMENTS OF INTERNETOF THINGS
202045613

Interface with LED


An LED can be connected to one of the GPIO pins of the NodeMCU along with an appropriate
resistor. The pin can be programmed to output HIGH or LOW signals to turn the LED ON or OFF.

3. Program code for implementing


NodeMCU supports MicroPython, which allows using Python to control the pins. Below
is a simple example to blink an LED connected to GPIO pin 2:
Code:
#define led D5
void setup() {
// put your setup code here, to run once:
pinMode(led, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(led, HIGH);
delay(1000);
digitalWrite(led, LOW);
delay(1000);
}

Output:

Pushpraj chavda
12302080501048 Page |
ELEMENTS OF INTERNETOF THINGS
202045613

Applications:
• IoT based smart home projects
• Wireless sensor data transmission
• Remote monitoring and control
• Prototyping connected devices

Conclusion
NodeMCU is a versatile and affordable platform for IoT development. Its Wi-Fi
capabilities, multiple GPIO pins, and support for MicroPython make it ideal for
a wide range of smart device projects.

Pushpraj chavda
12302080501048 Page |
ELEMENTS OF INTERNETOF THINGS
202045613

Experiment 5
5. Write a program on Arduino/Raspberry Pi to retrieve Temperature and humidity
data from things peak cloud.
Required Components
• ESP8266 Wi-Fi Module (NodeMCU)
• HC-SR04 Ultrasonic Sensor
• Breadboard
• Jumper Wires
Circuit Connections
The wiring for this project is straightforward. Connect the components as detailed below:

• Ultrasonic VCC Pin to ESP8266 VIN Pin (This provides 5V)


• Ultrasonic GND Pin to ESP8266 GND Pin
• Ultrasonic Trig Pin to ESP8266 D1 Pin
• Ultrasonic Echo Pin to ESP8266 D2 Pin

Pushpraj chavda
12302080501048 Page |
ELEMENTS OF INTERNETOF THINGS
202045613

ThingSpeak Platform Setup:


Step 1: Create a ThingSpeak Account
First, you need to set up an account on the ThingSpeak platform.
1. Navigate to [Link].
2. If you are a new user, sign up and follow the verification process. If you already have
an account, simply sign in.
Step 2: Configure a New Channel
A channel is used to store the data from your sensor.
1. After signing in, click on the "New Channel" button.
2. Give your channel a unique Name (e.g., "Practical 5") and a brief Description.
3. Enable two data fields by checking their boxes:
4. In Field 1, enter " Distance(cm)".
5. Click "Save Channel" to create it.
Step 3: Obtain the API Key
The API key authorizes your device to send data to your channel.
1. Open the "API Keys" tab for your new channel.
2. Copy the Write API Key. This key must be pasted into the Arduino code for the
project to work.

Pushpraj chavda
12302080501048 Page |
ELEMENTS OF INTERNETOF THINGS
202045613

Arduino Program Code:


#include <ESP8266WiFi.h>
#include "ThingSpeak.h"
// Your WiFi credentials.
const char* ssid = "Iphone 13";
const char* password = "12345678";

// ThingSpeak Channel settings.


unsigned long myChannelNumber = 3086648;
const char * myWriteAPIKey = "EV6XGUU20RKRPSVJ";

// Ultrasonic Sensor pins.


const int trigPin = D1; // D1 is GPIO5
const int echoPin = D2; // D2 is GPIO4

// Variables to store sensor readings.


long duration;
int distanceCm;
WiFiClient client;
void setup() {
[Link](115200);

Pushpraj chavda
12302080501048 Page |
ELEMENTS OF INTERNETOF THINGS
202045613

// Configure the ultrasonic sensor pins.


pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
[Link](WIFI_STA);
[Link](client);
}
void loop() {
// Check WiFi connection status
if ([Link]() != WL_CONNECTED) {
[Link]("Attempting to connect to SSID: ");
[Link](ssid);
while ([Link]() != WL_CONNECTED) {
[Link](ssid, password);
delay(5000);
}
[Link]("\nConnected.");
}
// Clear the trigPin to ensure a clean start.
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Send a 10-microsecond pulse to the Trig pin.
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

// Read the duration of the pulse on the Echo pin.


duration = pulseIn(echoPin, HIGH);

// Calculate the distance based on the speed of sound (0.034 cm/µs).

Pushpraj chavda
12302080501048 Page |
ELEMENTS OF INTERNETOF THINGS
202045613

distanceCm = duration * 0.034 / 2;


// Print distance to the Serial Monitor.
[Link]("Distance: ");
[Link](distanceCm);
[Link](" cm");
// Write the distance to the ThingSpeak channel's field 1.
int x = [Link](myChannelNumber, 1, distanceCm, myWriteAPIKey);
if (x == 200) {
[Link]("Channel update successful.");
} else {
[Link]("Problem updating channel. HTTP error code " + String(x));
}
// ThingSpeak needs a delay of at least 15 seconds between updates.
delay(20000);
}
OUTPUT:

Pushpraj chavda
12302080501048 Page |
ELEMENTS OF INTERNETOF THINGS
202045613

Experiment 6
[Link] a program on Arduino/Raspberry Pi to publish
temperature data to MQTT broker.
Prerequisites
Before starting, ensure your development environment is correctly configured. You need to
have the ESP8266 board manager installed in your Arduino IDE. Additionally, the
PubSubClient library by Nick O'Leary is required for MQTT communication and must be
installed through the Arduino IDE's library manager. This experiment will utilize a free,
public MQTT broker, so a local setup is not needed. The broker details are:
• Broker Address: [Link]
• Port: 1883 (Standard unencrypted MQTT port)
Hardware Components
• NodeMCU ESP8266 Board
• LM35 Temperature Sensor
• Breadboard for prototyping
• Jumper Cables for connections
• Micro-USB Cable for power and programming

Hardware and Setup

1. ESP32: A low-cost, low-power microcontroller with integrated Wi-Fi and Bluetooth.


It acts as the central processing unit, running the code, connecting to the network, and
communicating with the sensor.
2. DHT11 Sensor: A basic and inexpensive sensor that measures both temperature and
humidity. It is connected to a GPIO pin on the ESP32.

Pushpraj chavda
12302080501048 Page |
ELEMENTS OF INTERNETOF THINGS
202045613

The program enables the NodeMCU to first connect to a specified Wi-Fi network. Once
online, it establishes a connection with the public HiveMQ MQTT broker. The device will
then periodically, every 10 seconds, read the analog value from the LM35 temperature
sensor. This value is converted into a temperature reading in degrees Celsius. Finally, this
temperature data is "published" (sent) to the MQTT topic iot/college/temperature. Any
other client or application subscribed to this specific topic will receive the temperature data in
real-time.

Arduino Program Code:


#include <WiFi.h>
#include <PubSubClient.h>
#include "DHT.h"

// --- WiFi Settings ---


const char *ssid = "Realme 7";
const char *password = "12345678S";
const char *mqtt_broker = "[Link]";
const char *mqtt_topic = "esp32/dht11/data"; // Topic to publish data
const int mqtt_port = 1883;
#define DHTPIN 4 // The GPIO pin connected to the DHT11 data pin
#define DHTTYPE DHT11 // The type of DHT sensor
DHT dht(DHTPIN, DHTTYPE);

Pushpraj chavda
12302080501048 Page |
ELEMENTS OF INTERNETOF THINGS
202045613

WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0; // To store the last time a message was published
void setup() {
[Link](115200);
[Link]();
connectToWiFi();

[Link](mqtt_broker, mqtt_port);
}
void connectToWiFi() {
[Link]("Connecting to ");
[Link](ssid);
[Link](ssid, password);

while ([Link]() != WL_CONNECTED) {


delay(500);
[Link](".");
}
[Link]("\nWiFi connected!");
[Link]("IP address: ");
[Link]([Link]());
}
void reconnectMQTT() {
// Loop until we're reconnected
while (![Link]()) {
[Link]("Attempting MQTT connection...");

// Create a random client ID


String clientId = "ESP32Client-";

Pushpraj chavda
12302080501048 Page |
ELEMENTS OF INTERNETOF THINGS
202045613

clientId += String(random(0xffff), HEX);

// Attempt to connect
if ([Link](clientId.c_str())) {
[Link]("connected!");
} else {
[Link]("failed, rc=");
[Link]([Link]());
[Link](" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}

void loop() {
// Ensure the MQTT client is connected
if (![Link]()) {
reconnectMQTT();
}
[Link]();

// Non-blocking delay: only publish every 10 seconds


long now = millis();
if (now - lastMsg > 10000) {
lastMsg = now;

// Read humidity and temperature from the DHT11 sensor


float humidity = [Link]();
float temperature = [Link](); // Read temperature as Celsius

Pushpraj chavda
12302080501048 Page |
ELEMENTS OF INTERNETOF THINGS
202045613

// Check if any reads failed and exit early (to try again).
if (isnan(humidity) || isnan(temperature)) {
[Link]("Failed to read from DHT sensor!");
return;
}

// Create a JSON payload


char jsonPayload[100];
snprintf(jsonPayload, sizeof(jsonPayload), "{\"temperature\":%.2f, \"humidity\":%.2f}",
temperature, humidity);

[Link](mqtt_topic, jsonPayload);
[Link]("Published to topic: ");
[Link](mqtt_topic);
[Link]("Payload: ");
[Link](jsonPayload);
}
}
Output:

Pushpraj chavda
12302080501048 Page |
ELEMENTS OF INTERNETOF THINGS
202045613

Pushpraj chavda
12302080501048 Page |
ELEMENTS OF INTERNETOF THINGS
202045613

Experiment 7
1. Controlling Appliances using NodeMCU MQTT over the
Internet. (Adafruit Cloud).
• Requirements:
➢ Hardware:
NodeMCU (ESP8266)
Relay module (to control AC appliances safely)
Jumper wires, Breadboard
LED
➢ Software
Arduino IDE (with ESP8266 board installed)
Required libraries:
Adafruit_MQTT and Adafruit_MQTT_Client
ESP8266WiFi

Procedure

Step 1: Configure Arduino IDE for ESP8266

1. Launch the Arduino IDE and navigate to File > Preferences.


2. In the "Additional Board Manager URLs" field, enter the following link:
[Link]
n
3. Open the Boards Manager by going to Tools > Board > Boards
Manager.
4. Search for esp8266 in the Boards Manager, select the result, and click
Install.
5. Once installed, select your specific board from the menu: Tools > Board
> NodeMCU 1.0 (ESP-12E Module).

Step 2: Set Up Your Adafruit IO Account

1. Navigate to the Adafruit IO website at [Link]


2. Sign up to create a new, free account.
3. Access your Username and AIO Key by clicking on the "My Key"
option on your dashboard. These credentials are required for your code to
connect to the service.

Pushpraj chavda
12302080501048 Page |
ELEMENTS OF INTERNETOF THINGS
202045613

4. Create a new Feed and give it a name, for instance, appliance1. This
feed will hold the on/off state of your device.
5. Finally, create a new Dashboard. Add a control element like a Toggle
Button and link it to the appliance1 feed you just created. This button
will be your remote control.

Code:
#include <ESP8266WiFi.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
#define WIFI_SSID "Realme 7"
#define WIFI_PASS "12345678S"
#define AIO_SERVER "[Link]"
#define AIO_SERVERPORT 1883
#define AIO_USERNAME "Sneh5824"
#define AIO_KEY " aio_zacU53fhiF7iS8U1gc5a79VgOT6v"
// Create WiFi and MQTT clients
WiFiClient client;
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME,
AIO_KEY);
// Feed (appliance control)
Adafruit_MQTT_Subscribe appliance1 = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME
"/feeds/appliance1")
// Relay pin
#define RELAY_PIN D1
void setup() {
[Link](115200);
delay(10);
pinMode(RELAY_PIN, OUTPUT);

Pushpraj chavda
12302080501048 Page |
ELEMENTS OF INTERNETOF THINGS
202045613

digitalWrite(RELAY_PIN, HIGH); // Relay OFF (depends on module type)


// Connect to WiFi
[Link](); [Link]("Connecting to "); [Link](WIFI_SSID);
[Link](WIFI_SSID, WIFI_PASS);
while ([Link]() != WL_CONNECTED) {
delay(500); [Link](".");
}
[Link]("WiFi connected");

// Setup MQTT subscription


[Link](&appliance1);
}
void loop() {
// Ensure MQTT connection
MQTT_connect();
// Check for new messages
Adafruit_MQTT_Subscribe *subscription;
while ((subscription = [Link](5000))) {
if (subscription == &appliance1) {
[Link]("Received: ");
[Link]((char *)[Link]);

// Control relay
if (strcmp((char *)[Link], "ON") == 0) {
digitalWrite(RELAY_PIN, LOW); // Turn ON appliance
}
else if (strcmp((char *)[Link], "OFF") == 0) {
digitalWrite(RELAY_PIN, HIGH); // Turn OFF appliance
}
}
}
}

Pushpraj chavda
12302080501048 Page |
ELEMENTS OF INTERNETOF THINGS
202045613

// Function to keep MQTT connected


void MQTT_connect() {
int8_t ret;
// Stop if already connected
if ([Link]()) return;

[Link]("Connecting to MQTT... ");


while ((ret = [Link]()) != 0) {
[Link]([Link](ret));
[Link]("Retrying in 5 seconds...");
[Link]();
delay(5000);
}
[Link]("MQTT Connected!");
}
Step 4 – Wiring (NodeMCU + LED
1. NodeMCU D1 → Relay IN
2. NodeMCU GND → Relay GND
3. NodeMCU 3.3V → Relay VCC
4. Relay COM → AC Live
5. Relay NO → Appliance Live
6. Neutral wire direct to appliance.

Pushpraj chavda
12302080501048 Page |
ELEMENTS OF INTERNETOF THINGS
202045613

Step 5 – Testing
1. Upload the code to NodeMCU.
2. Open Arduino Serial Monitor (115200 baud) → Check WiFi + MQTT connection.
3. Go to Adafruit IO Dashboard → Toggle the switch.
4. The relay will turn ON/OFF accordingly → controlling the appliance over the Internet.
Output:

Pushpraj chavda
12302080501048 Page |
ELEMENTS OF INTERNETOF THINGS
202045613

Experiment 8
[Link] and Setup Raspberry Pi board and Implementation of LED(s)
Interfacing with raspberry pi.
Raspberry Pi is a compact, single-board computer (SBC) about the size of a credit card,
designed for educational and hobbyist projects. It functions as a complete computer, typically
running a version of the Linux operating system like Raspberry Pi OS. Its most important
feature for electronics projects is the set of

General Purpose Input/Output (GPIO) pins, which allow it to connect to and control
external components like sensors, motors, and LEDs.

The Pi is popular because it is budget-friendly, small, and power-efficient, making it perfect


for learning to code and building a wide range of DIY electronics and Internet of Things
(IoT) projects.

Key Components
• System on a Chip (SoC): The "brain" of the Pi, combining the CPU, GPU, and other
controllers into a single power-efficient chip.
• RAM: Volatile memory used to run programs, with capacities ranging from 1GB to 8GB.
• Storage: A microSD card holds the operating system and user files, allowing for easy
swapping and upgrades.
• GPIO Header: A 40-pin header that provides access to power, ground, and data pins for
interfacing with electronics.
• Connectivity: Includes HDMI for displays, connectors for cameras and touchscreens,
USB ports, Ethernet, and built-in Wi-Fi and Bluetooth on newer models.
• Power Input: Typically powered by a 5V supply through a micro-USB or USB-C port.

Implementation: Interfacing an LED


This practical demonstrates how to make an LED blink using a simple Python script and the
Raspberry Pi's GPIO pins.
1. Required Components
• Raspberry Pi
• LED
• Breadboard
• Jumper Wires

Pushpraj chavda
12302080501048 Page |
ELEMENTS OF INTERNETOF THINGS
202045613

2. Circuit Setup The provided code uses GPIO pin 18. You will connect
your LED to this pin.
1. Connect the LED's longer leg (anode) to GPIO 18 on the
Raspberry Pi.
2. Connect the LED's shorter leg (cathode) to a Ground (GND) pin on
the Raspberry Pi.

The Python Code:


# Import the necessary libraries
import [Link] as GPIO [cite: 49]
from time import sleep [cite: 50]
[Link](False)
[Link]([Link])
[Link](18, [Link], initial=[Link]) [cite: 52]
while True:
[Link](18, [Link])
# Wait for 1 second
sleep(1) [cite: 55]
[Link](18, [Link]) [cite: 56]
sleep(1)

Pushpraj chavda
12302080501048 Page |
ELEMENTS OF INTERNETOF THINGS
202045613

Pushpraj chavda
12302080501048 Page |
ELEMENTS OF INTERNETOF THINGS
202045613

Experiment 9
9. Study of Node-red programming tool.
What is Node-RED?
It's a programming tool used for connecting hardware devices, APIs, and online services.
Key Features:
It offers a browser-based editor for creating "flows" by wiring together a variety of pre-built
nodes.
Flows can be deployed to its runtime with a single click.
It has a lightweight runtime built on [Link], leveraging its event-driven, non-blocking
model.
This makes it suitable for running on low-cost hardware like the Raspberry Pi, as well as in
the cloud.
Its functionality can be extended with over 225,000 modules available in Node's package
repository.
Deployment Options: Node-RED can be run locally, using Docker, on devices (like
Raspberry Pi or BeagleBone Black), or in the cloud (FlowFuse, Amazon Web Services,
Microsoft Azure). It also mentions interacting with Arduino and Android.
Node-RED Architecture
Flow Editor: The browser-based interface where users design flows.
Runtime: The part that executes the flows; it handles communication between nodes.
Nodes: Building blocks of Node-RED that represent various types of logic, hardware, or
services.
Dashboard: An optional feature for creating user interfaces and visualizations for the flows.
Setting up and Using Node-RED
4. Installation and Setup
• System Requirements: [Link], npm, and a supported operating system (Windows,
Linux, macOS).
• Installing Node-RED:
• For Windows: Use the [Link] package (includes npm) to install Node-RED.

Pushpraj chavda
12302080501048 Page |
ELEMENTS OF INTERNETOF THINGS
202045613

. Working with the Flow Editor


• Adding Nodes: In the flow editor, drag nodes from the left sidebar and connect them
using wires.
• Common Nodes:
o Input Nodes: For receiving data (e.g., HTTP, MQTT).
o Output Nodes: For sending data (e.g., Debug, HTTP Response).
o Function Nodes: For custom JavaScript code.
• Deploying Flows: Once a flow is created, clicking on the "Deploy" button will make
it active and start executing.
Conclusion
• Summary: Node-RED is an accessible and powerful tool for building automation,
IoT, and data integration projects using a visual flow-based programming approach.
• Future Learning: Explore the vast library of available nodes and start building more
complex systems with Node-RED.
• Resources: For further study, visit the Node-RED website or explore their online
tutorials and documentation.

Pushpraj chavda
12302080501048 Page |
ELEMENTS OF INTERNETOF THINGS
202045613

Pushpraj chavda
12302080501048 Page |
ELEMENTS OF INTERNETOF THINGS
202045613

Experiment 10
10. Study and implementation of processing data from different
sensors and visualize data on Node-red dashboard.
Required Components
Hardware:
• Raspberry Pi
• LED
• Breadboard
• Jumper Wires
Software:
• Node-RED installed on the Raspberry Pi.
• The node-red-node-pi-gpiod package for GPIO control.
• The node-red-dashboard package for the user interface.
Creating a Test Flow with Inject Nodes
Before building the dashboard, it's good practice to create a simple flow to test
the hardware connection. The image in the practical shows how to do this.
A. Add the "Turn On" Button
1. Drag an inject node onto your workspace.
2. Double-click it, set its Payload to the boolean value true, and give it a
Name like "Turn On".
B. Add the "Turn Off" Button
1. Drag another inject node onto the workspace.
2. Double-click it, set its Payload to boolean false, and give it a Name like
"Turn Off".
C. Add the GPIO Output Node
1. Find and drag an rpi-gpio out node onto the workspace.
2. Double-click it to configure it.
3. Set the Pin to GPIO4 - 7.
Pushpraj chavda
12302080501048 Page |
ELEMENTS OF INTERNETOF THINGS
202045613

4. Ensure the Type is Digital Output.


D. Connect the Nodes
1. Draw a wire from the "Turn On" node to the rpi-gpio out node.
2. Draw another wire from the "Turn Off" node to the same rpi-gpio out
node.
E. Deploy and Test
1. Click the red Deploy button.
2. Click the inject button on the "Turn On" node. The physical LED should
light up.
3. Click the inject button on the "Turn Off" node. The LED should turn off
Building the Dashboard Interface
Now, let's replace the test buttons with a real user interface, which is the main
goal of this practical.
1. Delete the two inject nodes from your flow.
2. Find the switch node in the "dashboard" section of the palette and drag
it onto the workspace.
3. Double-click the switch node to configure it:
o Set the On Payload to boolean -> true.
o Set the Off Payload to boolean -> false.
o Assign it to a UI Group and Tab (e.g., "Controls").
4. Connect the output of this new switch node to the input of your existing
rpi-gpio out node.
5. Click the Deploy button.

Pushpraj chavda
12302080501048 Page |
ELEMENTS OF INTERNETOF THINGS
202045613

Pushpraj chavda
12302080501048 Page |

You might also like