0% found this document useful (0 votes)
12 views

Iot Lab Manual

Uploaded by

Navin Kumar
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)
12 views

Iot Lab Manual

Uploaded by

Navin Kumar
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/ 52

lOMoARcPSD|26286384

IOT Lab Manual

Computer Science (Anna University)

Scan to open on Studocu

Studocu is not sponsored or endorsed by any college or university


Downloaded by Navin Kumar ([email protected])
lOMoARcPSD|26286384

To Interface LED With Arduino And Write A Program To Turn ON LED For 1 Sec After Every 2 Seconds.

AIM:

To interface LED with Arduino and write a program to turn ON LED for 1 sec after every 2
seconds.

PROGRAM:

int led = 13; // the pin the LED is connected to

void setup()

pinMode(led, OUTPUT); // Declare the LED as an output

void loop()

digitalWrite(led, HIGH); // Turn the LED on

delay(1000);// Wait for 1000 milliseconds (1 second)

digitalWrite(led, LOW); // Turn the LED off

delay(1000);// Keep it off

Downloaded by Navin Kumar ([email protected])


lOMoARcPSD|26286384

OUTPUT:

RESULT:

The above program has been verified and executed successfully

Downloaded by Navin Kumar ([email protected])


lOMoARcPSD|26286384

To Interface Push Button/Digital Sensor (IR/LDR) With Arduino And Write A Program To Turn ON LED When Push Bu

AIM
: To interface Push button/Digital sensor (IR/LDR) with Arduino and write a
program to turn ON LED when push button is pressed or at sensor detection.

PROGRAM:

const int BUTTON

= 2; const int LED =

13;

int BUTTONstate = 0;

void setup()

pinMode(BUTTON,

INPUT); pinMode(LED,

OUTPUT);

void loop()

BUTTONstate =

digitalRead(BUTTON); if

(BUTTONstate == HIGH)

digitalWrite(LED, HIGH);

Downloaded by Navin Kumar ([email protected])


lOMoARcPSD|26286384

else{

digitalWrite(LED, LOW);

Downloaded by Navin Kumar ([email protected])


lOMoARcPSD|26286384

OUTPUT:

RESULT:

The above program has been verified and executed successfully

Downloaded by Navin Kumar ([email protected])


lOMoARcPSD|26286384

To Interface DHT11 Sensor With Arduino And Write A Program To Print Temperature And Humidity Readings.

Aim:

To interface DHT11 sensor with Arduino and write a program to print temperature and
humidity readings.

Program:

#include "dht.h"

#define dht_apin A0 // Analog Pin sensor is connected to

dht DHT;

void setup(){

Serial.begin(9600);

delay(500);//Delay to let system

boot

Serial.println("DHT11 Humidity & temperature

Sensor\n\n"); delay(1000);//Wait before accessing Sensor

}//end "setup()"

void loop(){

//Start of Program

DHT.read11(dht_apin);

Downloaded by Navin Kumar ([email protected])


lOMoARcPSD|26286384

Serial.print("Current humidity =

"); Serial.print(DHT.humidity);

Serial.print("% ");

Serial.print("temperature = ");

Serial.print(DHT.temperature);

Serial.println("C ");

delay(5000);//Wait 5 seconds before accessing sensor again.

//Fastest should be once every two seconds.

}// end loop()

Downloaded by Navin Kumar ([email protected])


lOMoARcPSD|26286384

Output:

RESULT:

The above program has been verified and executed successfully

Downloaded by Navin Kumar ([email protected])


lOMoARcPSD|26286384

To Interface Motor Using Relay With Arduino And Write A Program To Turn On MotorWhen Push Button Is Pressed

Aim:

To interface motor using relay with Arduino and write a program to turn on motor when push
button is pressed

Program:

#define CW 7 //CW is defined as pin #7//

#define CCW 8 //CCW is defined as pin

#8//

void setup() { //Setup runs once//

pinMode(CW, OUTPUT); //Set CW as an output//

pinMode(CCW, OUTPUT); //Set CCW as an

output//

void loop() { //Loop runs forever//

digitalWrite(CW,HIGH); //Motor runs

clockwise// delay(1000); //for 1 second//

digitalWrite(CW, LOW); //Motor stops//

digitalWrite(CCW, HIGH);//Motor runs counter-

clockwise// delay(1000); //For 1 second//

Downloaded by Navin Kumar ([email protected])


lOMoARcPSD|26286384

digitalWrite(CCW, LOW); //Motor stops//

Downloaded by Navin Kumar ([email protected])


lOMoARcPSD|26286384

Output:

RESULT:

The above program has been verified and executed successfully

Downloaded by Navin Kumar ([email protected])


lOMoARcPSD|26286384

To Interface Push Button/Digital Sensor (IR/LDR) With Nodemcu And Write A Program To Print LDR Values

Aim:

To interface push button/Digital sensor (IR/LDR) with NodeMCU and write a program to
print LDR values

Program:

// Digital Input and Output on LED

// Hardware: NodeMCU

const int BUTTON

= 0; const int LED =

2;

int BUTTONstate = 0;

void setup()

Serial.begin(115200);

pinMode(LED, OUTPUT);

pinMode (BUTTON,

INPUT);

void loop()

Downloaded by Navin Kumar ([email protected])


lOMoARcPSD|26286384

digitalWrite(LED, LOW);

BUTTONstate =

digitalRead(BUTTON);

Serial.print("\t BUTTONstat = ");

Downloaded by Navin Kumar ([email protected])


lOMoARcPSD|26286384

Serial.print(BUTTONstat

e); if (BUTTONstate ==

HIGH)

digitalWrite(LED, LOW);

else

digitalWrite(LED, HIGH);

delay(1000);

Downloaded by Navin Kumar ([email protected])


lOMoARcPSD|26286384

Output:

RESULT:

The above program has been verified and executed successfully

Downloaded by Navin Kumar ([email protected])


lOMoARcPSD|26286384

To Interface Buzzer With Nodemcu And Write A Program To Turn On Buzzer When Push Button Is Pressed

Aim
: To interface buzzer with NodeMCU and write a program to turn on buzzer when push
button is pressed

Program:

// Digital Input and Output on LED

// Hardware: NodeMCU

const int BUTTON

= 0; const int LED =

4;

int BUTTONstate = 0;

void setup()

Serial.begin(115200);

pinMode(LED, OUTPUT);

pinMode (BUTTON,

INPUT);

void loop()

digitalWrite(LED, LOW);

BUTTONstate =

Downloaded by Navin Kumar ([email protected])


lOMoARcPSD|26286384

digitalRead(BUTTON);

Serial.print("\t BUTTONstat = ");

Downloaded by Navin Kumar ([email protected])


lOMoARcPSD|26286384

Serial.print(BUTTONstat

e); if (BUTTONstate ==

HIGH)

digitalWrite(LED, HIGH);

else

digitalWrite(LED, LOW);

delay(1000);

Downloaded by Navin Kumar ([email protected])


lOMoARcPSD|26286384

Output:

Result:

The above program has been verified and executed successfully

Downloaded by Navin Kumar ([email protected])


lOMoARcPSD|26286384

Write А Program On Nodemcu To Upload Temperature And Humidity Data


To Thingspeak Cloud.

Aim:

Write а program on NodeMCU to upload temperature and humidity data to thingspeak cloud.

Program:

// ----------(c) Electronics-Project-Hub-----------//

#include "ThingSpeak.h"

#include

<ESP8266WiFi.h>

//float value

= 0; int x =

0;

const int analogInPin = A0;

float sensorValue = 0; // value read from the pot

float outputValue = 0; // value to output to a PWM pin

//----------- Enter you Wi-Fi Details---------//

char ssid[] = "sankar"; // your network SSID (name)

char pass[] = "saan3611"; // your network password

// //

//----------- Channel Details-------------//

Downloaded by Navin Kumar ([email protected])


lOMoARcPSD|26286384

unsigned long Channel_ID = 1624039; // Channel

ID const int Field_number = 1; // Don't change

Downloaded by Navin Kumar ([email protected])


lOMoARcPSD|26286384

const char * WriteAPIKey = "YIYPW7C1WXLR4I8R"; // Your write API Key

// //

WiFiClient client;

void setup()

Serial.begin(115200);

WiFi.mode(WIFI_STA);

ThingSpeak.begin(client);

void loop()

internet(

);

get_valu

e();

upload()

void upload()

internet();

Downloaded by Navin Kumar ([email protected])


lOMoARcPSD|26286384

x = ThingSpeak.writeField(Channel_ID, Field_number, outputValue,

WriteAPIKey); if (x == 200)Serial.println("Data Updated.");

if (x != 200)

Downloaded by Navin Kumar ([email protected])


lOMoARcPSD|26286384

Serial.println("Data upload failed, retrying.. .");

delay(150

00);

upload();

void internet()

if (WiFi.status() != WL_CONNECTED)

Serial.print("Attempting to connect to SSID:

"); Serial.println(ssid);

while (WiFi.status() != WL_CONNECTED)

WiFi.begin(ssid,

pass);

Serial.print(".");

delay(5000);

Serial.println("\nConnected.");

Downloaded by Navin Kumar ([email protected])


lOMoARcPSD|26286384

void get_value()

Downloaded by Navin Kumar ([email protected])


lOMoARcPSD|26286384

sensorValue = analogRead(analogInPin);

// map it to the range of the PWM out

outputValue = map(sensorValue, 0, 1024, 0,

255);

// print the readings in the Serial

Monitor Serial.print("sensor = ");

Serial.print(sensorValue);

Serial.print("\t output = ");

Serial.println(outputValue);

delay(1000);

// ----------(c) Electronics-Project-Hub-----------//

Downloaded by Navin Kumar ([email protected])


lOMoARcPSD|26286384

Output:

Result:

The above program has been verified and executed successfully

Downloaded by Navin Kumar ([email protected])


lOMoARcPSD|26286384

To Interface Wif With Nodemcu And Write A Program To Send Data To Webpage

Aim:

To interface Wifi with NodeMCU and write a program to send data to Webpage

Program:

#include <ESP8266WiFi.h>

const char* ssid =

"aadhavan"; const char*

password = "sara1980";

WiFiServer server(80);

/* DHT Temperature and Humidity Sensor */

/* LED */

#define LED_PIN 2

//Analog Input

#define ANALOG_PIN_0

A0 int analog_value = 0;

void setup()

Serial.begin(115200);

pinMode(LED_PIN,

OUTPUT);

Downloaded by Navin Kumar ([email protected])


lOMoARcPSD|26286384

delay(10);

Downloaded by Navin Kumar ([email protected])


lOMoARcPSD|26286384

connectWiFi();

int value = 0;

void loop()

analog_value =

analogRead(ANALOG_PIN_0);

WiFiLocalWebPageCtrl();

delay(100);

/***************************************************

* Send and receive data from Local Page

**************************************************

**/ void WiFiLocalWebPageCtrl(void)

WiFiClient client = server.available(); // listen for incoming clients

//client = server.available();

if (client) { // if you get a client,

Serial.println("New Client."); // print a message out the serial port

Downloaded by Navin Kumar ([email protected])


lOMoARcPSD|26286384

String currentLine = ""; // make a String to hold incoming data from the client

Downloaded by Navin Kumar ([email protected])


lOMoARcPSD|26286384

while (client.connected()) { // loop while the client's

connected if (client.available()) { // if there's bytes to read

from the client, char c = client.read(); // read a byte, then

Serial.write(c); // print it out the serial monitor

if (c == '\n') { // if the byte is a newline character

// if the current line is blank, you got two newline characters in a row.

// that's the end of the client HTTP request, so send a

response: if (currentLine.length() == 0) {

// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)

// and a content-type so the client knows what's coming, then a blank line:

client.println("HTTP/1.1 200 OK");

client.println("Content-type:text/html");

client.println();

// the content of the HTTP response follows the header:

//WiFiLocalWebPageCtrl();

client.print("Analog Data:

");

client.print(analog_value);

client.print("<br>");

client.print("<br>");

Downloaded by Navin Kumar ([email protected])


lOMoARcPSD|26286384

client.print("Click <a href=\"/H\">here</a> to turn the LED

on.<br>"); client.print("Click <a href=\"/L\">here</a> to turn the

LED off.<br>");

Downloaded by Navin Kumar ([email protected])


lOMoARcPSD|26286384

// The HTTP response ends with another blank

line: client.println();

// break out of the while loop:

break;

} else { // if you got a newline, then clear

currentLine: currentLine = "";

} else if (c != '\r') { // if you got anything else but a carriage return

character, currentLine += c; // add it to the end of the currentLine

// Check to see if the client request was "GET /H" or "GET

/L": if (currentLine.endsWith("GET /H")) {

digitalWrite(LED_PIN, HIGH); // GET /H turns the LED on

if (currentLine.endsWith("GET /L")) {

digitalWrite(LED_PIN, LOW); // GET /L turns the LED off

// close the

connection:

client.stop();

Serial.println("Client Disconnected.");

Downloaded by Navin Kumar ([email protected])


lOMoARcPSD|26286384

Downloaded by Navin Kumar ([email protected])


lOMoARcPSD|26286384

/***************************************************

* Connecting to a WiFi network

**************************************************

**/ void connectWiFi(void)

Serial.println();

Serial.println();

Serial.print("Connecting to

"); Serial.println(ssid);

WiFi.begin(ssid,

password);

while (WiFi.status() != WL_CONNECTED)

delay(500);

Serial.print("

.");

Serial.println("");

Serial.println("WiFi

connected.");

Serial.println("IP address: ");

Serial.println(WiFi.localIP());

Downloaded by Navin Kumar ([email protected])


lOMoARcPSD|26286384

server.begin();

Downloaded by Navin Kumar ([email protected])


lOMoARcPSD|26286384

Output:

Downloaded by Navin Kumar ([email protected])


lOMoARcPSD|26286384

Result:

The above program has been verified and executed successfully

Downloaded by Navin Kumar ([email protected])


lOMoARcPSD|26286384

To Interface LED With Raspberry Pi And Write A Program To Turn ON LED For 1 Sec After Every 2 Seconds.

Aim
: To interface LED with raspberry pi and write a program to turn ON LED for 1 sec after
every 2 seconds.

Program:

import time

import RPi.GPIO as GPIO

# Pin

definitions

led_pin = 12

# Use "GPIO" pin

numbering

GPIO.setmode(GPIO.BC

M)

# Set LED pin as output

GPIO.setup(led_pin,

GPIO.OUT)

# Blink

forever try:

Downloaded by Navin Kumar ([email protected])


lOMoARcPSD|26286384

while True:

GPIO.output(led_pin, GPIO.HIGH) # Turn LED

on time.sleep(1) # Delay for 1 second

GPIO.output(led_pin, GPIO.LOW) # Turn LED

off

Downloaded by Navin Kumar ([email protected])


lOMoARcPSD|26286384

time.sleep(1) # Delay for 1 second

# When you press ctrl+c, nicely release GPIO

resources finally:

GPIO.cleanup()

Downloaded by Navin Kumar ([email protected])


lOMoARcPSD|26286384

Output:

Result:

The above program has been verified and executed successfully

Downloaded by Navin Kumar ([email protected])


lOMoARcPSD|26286384

To Interface Push Button/Digital Sensor (IR/LDR) With Raspberry Pi And WriteA Program To Turn ON LED When Push

Aim:

To interface Push button/Digital sensor (IR/LDR) with raspberry pi and write a program
to turn ON LED when push button is pressed or at sensor detection.

Program:

import RPi.GPIO as

GPIO import time

button = 16

led = 18

def setup():

GPIO.setmode(GPIO.BOARD)

GPIO.setup(button, GPIO.IN, pull_up_down=GPIO.PUD_UP)

GPIO.setup(led, GPIO.OUT)

def loop():

while True:

button_state =

GPIO.input(button) if

button_state == False:

Downloaded by Navin Kumar ([email protected])


lOMoARcPSD|26286384

GPIO.output(led,

True) print('Button

Pressed...')

Downloaded by Navin Kumar ([email protected])


lOMoARcPSD|26286384

while GPIO.input(button) ==

False: time.sleep(0.2)

else:

GPIO.output(led, False)

def endprogram():

GPIO.output(led,

False)

GPIO.cleanup()

if name == ' main ':

setup()

try:

loop()

except KeyboardInterrupt:

print 'keyboard interrupt

detected' endprogram()

Downloaded by Navin Kumar ([email protected])


lOMoARcPSD|26286384

Output:

Result:

The above program has been verified and executed successfully

Downloaded by Navin Kumar ([email protected])


lOMoARcPSD|26286384

Write А Program On Raspberry Pi To Upload Temperature And Humidity


Data To Thingspeak Cloud

Aim:
Write а program on raspberry pi to upload temperature and humidity data to thingspeak
cloud

Program:

import

httplib

import

urllib

import

time

key = "I77Y0MZLZ6UUYFHJ" # Put your API Key

here def thermometer():

while True:

#Calculate CPU temperature of Raspberry Pi in Degrees C

temp = int(open('/sys/class/thermal/thermal_zone0/temp').read()) / 1e3 # Get Raspberry


Pi CPU temp

params = urllib.urlencode({'field1': temp, 'key':key })

headers = {"Content-typZZe": "application/x-www-form-urlencoded","Accept":


"text/plain"}

conn =

httplib.HTTPConnection("api.thingspeak.com:80") try:

conn.request("POST", "/update", params,

headers) response = conn.getresponse()

print temp

Downloaded by Navin Kumar ([email protected])


lOMoARcPSD|26286384

print response.status,

response.reason data =

response.read()

conn.close()

Downloaded by Navin Kumar ([email protected])


lOMoARcPSD|26286384

except:

print "connection

failed" break

if name == " main ":

while True:

thermometer()

Downloaded by Navin Kumar ([email protected])


lOMoARcPSD|26286384

Output:

Result:

The above program has been verified and executed successfully

Downloaded by Navin Kumar ([email protected])

You might also like