0% found this document useful (0 votes)
47 views36 pages

Iot Lab Record

Uploaded by

Jasvan Sundar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views36 pages

Iot Lab Record

Uploaded by

Jasvan Sundar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 36

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

MC5315 - INTERNET OF THINGS LABORATORY 1


OUTPUT:

RESULT:

The above program has been verified and executed successfully

MC5315 - INTERNET OF THINGS LABORATORY 2


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.

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);

else{

digitalWrite(LED, LOW);

MC5315 - INTERNET OF THINGS LABORATORY 3


}

OUTPUT:

RESULT:

The above program has been verified and executed successfully

MC5315 - INTERNET OF THINGS LABORATORY 4


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);

MC5315 - INTERNET OF THINGS LABORATORY 5


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()

MC5315 - INTERNET OF THINGS LABORATORY 6


Output:

RESULT:

The above program has been verified and executed successfully

MC5315 - INTERNET OF THINGS LABORATORY 7


To Interface Motor Using Relay With Arduino And Write A Program To Turn On
Motor When 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//

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

MC5315 - INTERNET OF THINGS LABORATORY 8


Output:

RESULT:

The above program has been verified and executed successfully

MC5315 - INTERNET OF THINGS LABORATORY 9


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()

digitalWrite(LED, LOW);

BUTTONstate = digitalRead(BUTTON);

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

MC5315 - INTERNET OF THINGS LABORATORY 10


Serial.print(BUTTONstate);

if (BUTTONstate == HIGH)

digitalWrite(LED, LOW);

else

digitalWrite(LED, HIGH);

delay(1000);

MC5315 - INTERNET OF THINGS LABORATORY 11


Output:

RESULT:

The above program has been verified and executed successfully

MC5315 - INTERNET OF THINGS LABORATORY 12


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 = digitalRead(BUTTON);

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

MC5315 - INTERNET OF THINGS LABORATORY 13


Serial.print(BUTTONstate);

if (BUTTONstate == HIGH)

digitalWrite(LED, HIGH);

else

digitalWrite(LED, LOW);

delay(1000);

MC5315 - INTERNET OF THINGS LABORATORY 14


Output:

Result:

The above program has been verified and executed successfully

MC5315 - INTERNET OF THINGS LABORATORY 15


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 -------------//

unsigned long Channel_ID = 1624039; // Channel ID

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

MC5315 - INTERNET OF THINGS LABORATORY 16


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_value();

upload();

void upload()

internet();

x = ThingSpeak.writeField(Channel_ID, Field_number, outputValue, WriteAPIKey);

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

if (x != 200)

MC5315 - INTERNET OF THINGS LABORATORY 17


{

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

delay(15000);

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.");

void get_value()

MC5315 - INTERNET OF THINGS LABORATORY 18


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 -----------//

MC5315 - INTERNET OF THINGS LABORATORY 19


Output:

Result:

The above program has been verified and executed successfully

MC5315 - INTERNET OF THINGS LABORATORY 20


To Interface Wifi 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);

delay(10);

MC5315 - INTERNET OF THINGS LABORATORY 21


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

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

MC5315 - INTERNET OF THINGS LABORATORY 22


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>");

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>");

MC5315 - INTERNET OF THINGS LABORATORY 23


// 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.");

MC5315 - INTERNET OF THINGS LABORATORY 24


}

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

* 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());

server.begin();

MC5315 - INTERNET OF THINGS LABORATORY 25


Output:

MC5315 - INTERNET OF THINGS LABORATORY 26


Result:

The above program has been verified and executed successfully

MC5315 - INTERNET OF THINGS LABORATORY 27


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.BCM)

# Set LED pin as output

GPIO.setup(led_pin, GPIO.OUT)

# Blink forever

try:

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

MC5315 - INTERNET OF THINGS LABORATORY 28


time.sleep(1) # Delay for 1 second

# When you press ctrl+c, nicely release GPIO resources

finally:

GPIO.cleanup()

MC5315 - INTERNET OF THINGS LABORATORY 29


Output:

Result:

The above program has been verified and executed successfully

MC5315 - INTERNET OF THINGS LABORATORY 30


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.

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:

GPIO.output(led, True)

print('Button Pressed...')

MC5315 - INTERNET OF THINGS LABORATORY 31


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()

MC5315 - INTERNET OF THINGS LABORATORY 32


Output:

Result:

The above program has been verified and executed successfully

MC5315 - INTERNET OF THINGS LABORATORY 33


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

print response.status, response.reason

data = response.read()

conn.close()

MC5315 - INTERNET OF THINGS LABORATORY 34


except:

print "connection failed"

break

if __name__ == "__main__":

while True:

thermometer()

MC5315 - INTERNET OF THINGS LABORATORY 35


Output:

Result:

The above program has been verified and executed successfully

MC5315 - INTERNET OF THINGS LABORATORY 36

You might also like