0% found this document useful (0 votes)
43 views31 pages

Iot Lab

Uploaded by

Abinaya
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)
43 views31 pages

Iot Lab

Uploaded by

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

INDEX

S.NO DATE CONTENT PAGE MARKS SIGN


NO
1 To study various IoT protocols – 6LowPAN,
IPv4/IPv6, Wifi, Bluetooth, MQTT
2 IoT Application Development Using sensors and
actuators (temperature sensor, light sensor,
infrared sensor)

3 To study Raspberry Pi development board and to


implement LED blinking applications.

4 To develop an application to send and receive


data with Arduino using HTTP request

5 To develop an application that measures the


room temperature and posts the
temperature value on the cloud platform.

6 To develop an application that measures the


moisture of soil and post the sensed data over
Google Firebase cloud platform.

7 To develop an application for measuring the


distance using ultrasonic sensor and post
distance value on Google Cloud IoT platform

8 Develop a simple application based on sensors.


9 Develop IoT applications using Django
Framework and Firebase/ Bluemix platform.

10 Develop a commercial IoT application.

PROGRAM:
#include <dht.h>
#define dht_apin A0
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
}
void loop()
{
DHT.read11(dht_apin);
Serial.print("Current humidity = ");
Serial.print(DHT.humidity);
Serial.print("% ");
Serial.print("temperature = ");
Serial.print(DHT.temperature);
Serial.println("C ");
delay(10000);
}

OUTPUT:
PROGRAM:
void setup() {
Serial.begin(9600);
}
void loop()
{
int analogValue = analogRead(A0);
Serial.print("Analog reading = ");
Serial.print(analogValue);
if (analogValue < 100)
{
Serial.println(" - Very bright");
}
else if (analogValue < 200)
{
Serial.println(" - Bright");
}
else if (analogValue < 500)
{
Serial.println(" - Light");
} else if (analogValue < 800)
{
Serial.println(" - Dim");
}
else
{
Serial.println(" - Dark");
}
delay(500);
}

OUTPUT:
PROGRAM:
int IRSensor = 2; // connect ir sensor to arduino pin 2
int LED = 13; // conect Led to arduino pin 13
void setup()
{
pinMode (IRSensor, INPUT); // sensor pin INPUT
pinMode (LED, OUTPUT); // Led pin OUTPUT
}
void loop()
{
int statusSensor = digitalRead (IRSensor);

if (statusSensor == 1)
{
digitalWrite(LED, LOW); // LED LOW
}

else
{
digitalWrite(LED, HIGH); // LED High
}
}

OUTPUT:
PROGRAM:
importRPi.GPIO as GPIO # Import Raspberry Pi GPIO library
from time import sleep # Import the sleep function from the time
module GPIO.setwarnings(False) # Ignore warning for now
GPIO.setmode(GPIO.BOARD) # Use physical pin numbering
GPIO.setup(8, GPIO.OUT, initial=GPIO.LOW) # Set pin 8 to be an output pin and
set initial value to low (off)
while True: # Run forever
GPIO.output(8, GPIO.HIGH) # Turn
on sleep(1) # Sleep for 1
second GPIO.output(8, GPIO.LOW)
# Turn off sleep(1) # Sleep for 1
second
importRPi.GPIO as GPIO # Import Raspberry Pi GPIO library
from time import sleep # Import the sleep function from the time module
GPIO.setwarnings(False) # Ignore warning for now
GPIO.setmode(GPIO.BOARD) # Use physical pin numbering
GPIO.setup(8, GPIO.OUT, initial=GPIO.LOW) # Set pin 8 to be an output pin and set
initial value to low (off)
while True: # Run forever
GPIO.output(8, GPIO.HIGH) # Turn
on Sleep(1) # Sleep for 1 second
GPIO.output(8, GPIO.LOW) # Turn
off sleep (1) # Sleep for 1 second
$ python blinking_led.py
OUTPUT:
PROGRAM:
I GET
#include <ESP8266WiFi.h>const
char* ssid = "Samsung";
//replace with your own wif issid const
char* password = "12345678";
//replace with your own //wifissid password const char*
host = "esp8266-shop.com";
void setup() {
Serial.begin(115200); delay(10);
// we start by connecting to a WiFi network Serial.println ();
Serial.println(); Serial.print("Connecting to ");
Serial.println(ssid);
/* explicitly set the ESP8266 to be a WiFi-client, otherwise, it by default, would try to act as
both a client and an access-point and could cause network-issues with your other WiFi-devices
on your WiFi-network. */
WiFi.Mode(WIFI_STA);
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());
} int value = 0;
void loop() {
delay(5000);
++value; Serial.print("connecting to ");
Serial.println(host); // Use WiFiClient class to create TCP connections
WiFiClient client;
constinthttpPort = 80;
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
return;
}
// We now create a URI for the request
//this url contains the informtation we want to send to the server
//if esp8266 only requests the website, the url is empty
String url = "/";
/* url += "?param1="; url
+= param1;
url += "?param2=";
url += param2;
*/
Serial.print("Requesting URL: ");
Serial.println(url); // This will send the request to the server client.print(String("GET ")
+ url + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "Connection: close\r\n\r\n");
unsigned long timeout = millis();
while (client.available() == 0) { if
(millis() - timeout > 5000)
{ Serial.println(">>> Client Timeout !");
client.stop(); return; } } // Read all the lines of the reply from server and print them to Serial
while (client.available())
{ String line = client.readStringUntil('\r'); Serial.print(line);
}
Serial.println(); Serial.println("closing
connection"); }

II POST:
#include <ESP8266WiFi.h> #include
<ESP8266HTTPClient.h> const char*
ssid = "Samsung";
const char* password = "12345678";
const char* host = "postman-echo.com"; //edit the host adress, ip address etc. String
url = "/post/"; intadcvalue=0;
void setup() {
Serial.begin(115200);
delay (10); // we start by connecting to a WiFi network
Serial.println();
Serial.println(); Serial.print("Connecting to ");
Serial.println(ssid);
/* Explicitly set the ESP8266 to be a WiFi-client, otherwise, It by default, would try to act as
both a client and an access-point and could cause network-issues with your other WiFi-devices
on your WiFi-network. */
WiFi.mode(WIFI_STA);
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()); } int
value = 0;
void loop() {
delay(5000);
adcvalue=analogRead(A0); //Read Analog value of pin A0 Serial.print("connecting to
");
Serial.println(host); // Use WiFiClient class to create TCP connections
WiFiClient client;
constinthttpPort = 80;
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
return;
}
Serial.print("Requesting URL: ");
Serial.println(url); //Post Data
String postData = "adcreading=" + String(adcvalue);
String address = host + url;
HTTPClient http;
//http.begin(address);
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
AutohttpCode = http.POST (postData);
Serial.println(httpCode); //Print HTTP return code String
payload = http.getString(); Serial.println(payload); //Print
request response payload http.end (); //Close connection
Serial.println (); Serial.println ("closing connection");
}
OUTPUT:

(I)GET

(II) POST

//POST /update-sensor HTTP/1.1


Host: postman-echo.com
api_key=api&sensor_name=name&temperature=value1&humidity=value2
&pressure=value3
Content-Type: application/x-www-form-urlencoded//
PROGRAM:
//Make sure to subscribe Technomekanics:) String ssid = "Samsung";
// SSID to connect to
String password = "12345678";
// our virtual wifi has no password String
host = "api.thingspeak.com";
// Open Weather Map API
constinthttpPort = 80;
String url = "/update?api_key=JSCPACEKTM27D8E2&field1=";
IntsetupESP8266 (void)
{
// Start our ESP8266 Serial Communication Serial.begin
(115200);
// Serial connection over USB to computer Serial.println
("AT");
// Serial connection on TX / RX port to ESP8266
delay (10);
// Wait a little for the ESP to respond if
(!Serial.find("OK")) return 1;
// Connect to 123D Circuits Simulator Wifi
Serial.println("AT+CWJAP=\"" + ssid + "\",\"" + password + "\"");
delay(10);
// Wait a little for the ESP to respond if
(!Serial.find("OK")) return 2;
// Open TCP connection to the host:
Serial.println("AT+CIPSTART=\"TCP\",\"" + host + "\"," + httpPort);
delay(50);
// Wait a little for the ESP to respond if
(!Serial.find("OK")) return 3; return 0;
}
voidanydata(void)
{
int temp = map(analogRead(A0),20,358,-40,125);
// Construct our HTTP call
String httpPacket = "GET " + url + String(temp) + " HTTP/1.1\r\nHost: " + host + "\r\n\r\n";
int length = httpPacket.length();
// Send our message length
Serial.print("AT+CIPSEND=");
Serial.println(length); delay(10);
// Wait a little for the ESP to respond if (!Serial.find(">")) return -1;
// Send our http request
Serial.print(httpPacket);
delay(10); // Wait a little for the ESP to respond if
(!Serial.find("SEND OK\r\n")) return;
}
void setup()
{
setupESP8266();
}
void loop()
{
anydata();
delay (1000);
}
OUTPUT:
PROGRAM:

#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <ESP8266HTTPClient.h>
// user config: TODO
#define MOISTURE_THRESHOLD 55 // moisture alert threshold
const char* wifi_ssid = "Samsung"; // SSID
const char* wifi_password = "12345678"; // WIFI
const char* apiKeyIn = "3LC2DKPVZ1YXS1VI"; // API KEY IN
const unsigned intwriteInterval = 25000; // write interval (in ms)
// ASKSENSORS config.
String host = "http://api.asksensors.com"; // ASKSENSORS API host name
ESP8266WiFiMulti WiFiMulti;
intmoisture_Pin= 0; // Soil Moisture Sensor input at Analog PIN A0
intmoisture_value= 0, moisture_state = 0xFF;
void setup() {
Serial.begin(115200);
Serial.println("*****************************************************");
Serial.println("********** Program Start : Soil Moisture monitoring using
ESP8266 and AskSensorsIoT cloud");
Serial.println("Wait for WiFi... ");
Serial.print("********** connecting to WIFI : ");
Serial.println(wifi_ssid);
WiFi.begin(wifi_ssid, wifi_password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");}
Serial.println("");
Serial.println("->WiFi connected");
Serial.println("-> IP address: ");
Serial.println(WiFi.localIP());} void
loop() {
Serial.print("MOISTURE LEVEL : ");
moisture_value= analogRead(moisture_Pin);
moisture_value= moisture_value/10;
Serial.println(moisture_value);
if(moisture_value> MOISTURE_THRESHOLD) moisture_state = 0;
elsemoisture_state = 1;
// wait for WiFi connection
if (WiFi.status() == WL_CONNECTED){
HTTPClient http;
Serial.print("[HTTP] begin...\n");
// Create a URL for the request
String url = "";
url += host;
url += "/write/";
url += apiKeyIn;
url += "?module1="; url
+= moisture_value; url
+= "&module2="; url +=
moisture_state;
Serial.print("********** requesting URL: ");
Serial.println(url);
http.begin(url); //HTTP
Serial.println("> Soil moisture level and state were sent to ASKSENSORS");
Serial.print("[HTTP] GET...\n");
// start connection and send HTTP header
inthttpCode = http.GET();
// httpCode will be negative on error
if(httpCode> 0)
{// HTTP header has been send and Server response header has been handled
Serial.printf("[HTTP] GET... code: %d\n", httpCode);
// file found at server
if(httpCode == HTTP_CODE_OK)
{
String payload = http.getString();
Serial.println(payload);
}
} else {
Serial.printf("[HTTP] GET... failed, error: %s\n",
http.errorToString(httpCode).c_str());} http.end();
Serial.println("********** End ");
Serial.println("*****************************************************");
}
delay(writeInterval);
}
OUTPUT:
PROGRAM:

#include <ESP8266WiFi.h>
#include <OneWire.h> #include
<PubSubClient.h> Const char
*ssid= “Samsung";
//Your Access Point or Personal Hotspot, cannot be longer than 32 characters!// Const
char *pass = “12345678";
//Your Access Point or Personal Hotspot password//
const char* serverTS = "api.thingspeak.com"; String
apiKey = "D9T05A1ZTR4PAI8U";
//Insert your Channel API Key here
//constintpingPin = 2; //Ultrasonic connected to GPIO0
int TRIGGER = 5; //Pin D1 = TRIGGER
int ECHO = 4; //Pin D2 = ECHO void
setup()
{
pinMode(0,OUTPUT);
//LED connected to GPIO2
Serial.begin(115200);
//Recommended speed is 115200
pinMode(TRIGGER,OUTPUT);
pinMode(ECHO,INPUT);
connectWifi();
}
void loop()
{
// establish variables for duration of the ping,
// and the distance result in inches and centimeters: Long
duration, inches, cm;
// The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse: digitalWrite(TRIGGER,
LOW);
delayMicroseconds(2);
digitalWrite(TRIGGER, HIGH);
delayMicroseconds(10);
digitalWrite(TRIGGER, LOW);
// the same pin is used to read the signal from the PING))): a HIGH
// pulse whose duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
duration = pulseIn(ECHO, HIGH);
// convert the time into a distance
inches = microsecondsToInches(duration); cm =
microsecondsToCentimeters(duration);
Serial.print(inches);
Serial.print("in, ");
Serial.print (cm);
Serial.print ("cm");
Serial.println ();
delay (100);
digitalWrite (2, HIGH);
// turn the LED on (HIGH is the voltage level)
delay(1000);
// wait for a second
digitalWrite(2, LOW);
// turn the LED off by making the voltage LOW
delay(1000);
// wait for a second
sendHeight(cm);
}
voidconnectWifi()
{
Serial.print ("Connecting to "+*ssid);
WiFi.begin (ssid, pass);
while (WiFi.status()!= WL_CONNECTED) {
delay (1000);
Serial.print (".");
}
Serial.println ("");
Serial.println("Connected");
Serial.println("");
}//end connect
longmicrosecondsToInches(long microseconds)
{ // According to Parallax's datasheet for the PING))), there are
// 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
// second). This gives the distance travelled by the ping, outbound
// and return, so we divide by 2 to get the distance of the obstacle.
// See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
return microseconds / 74 / 2;
}
longmicrosecondsToCentimeters(long microseconds) {
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;
}
voidsendHeight(float cm)
{
WiFiClienttclient;//not to be confused with "client" in PubSub{}, and wclient for mqtt if
(tclient.connect(serverTS, 80)) {
// use ip 184.106.153.149 or api.thingspeak.com
//Serial.println("WiFi Client connected "); String
postStr = apiKey;
postStr += "&field1=";
postStr += String(cm);
postStr += "\r\n\r\n";
tclient.print("POST /update HTTP/1.1\n");
tclient.print("Host: api.thingspeak.com\n");
tclient.print("Connection: close\n");
tclient.print("X-THINGSPEAKAPIKEY: " + apiKey + "\n");
tclient.print("Content-Type: application/x-www-form-urlencoded\n");
tclient.print("Content-Length: ");
tclient.print(postStr.length());
tclient.print("\n\n");
tclient.print(postStr);
delay(1000);
}
//end if
tclient.stop();
}
OUTPUT:
PROGRAM:

// Arduino IR Sensor Code intIRSensor


= 9;
// connect ir sensor module to Arduino pin 9 int
LED = 13;
// conect LED to Arduino pin 13 void
setup()
{
Serial.begin(115200);
// InitSerila at 115200 Baud
Serial.println("Serial Working");
// Test to check if serial is working or not
pinMode(IRSensor, INPUT);
// IR Sensor pin INPUT
pinMode(LED, OUTPUT);
// LED Pin Output
}
void loop()
{
intsensorStatus = digitalRead(IRSensor); // Set the GPIO as Input if
(sensorStatus == 1) // Check if the pin high or not
{
// if the pin is high turn off the onboard Led
digitalWrite(LED, LOW); // LED LOW
else
Serial.println("Motion Ended!");
// print Motion Detected! on the serial monitor window
}
{
//else turn on the onboard LED
digitalWrite(LED, HIGH); // LED High
Serial.println("Motion Detected!");
// print Motion Ended! on the serial monitor window
}
}
OUTPUT:
PROGRAM:

#include <ESP8266WiFi.h>
#include <FirebaseArduino.h>
// Set these to run example.
#define FIREBASE_HOST "example.firebaseio.com"
#define FIREBASE_AUTH "token_or_secret" #define
WIFI_SSID "Samsung"
#define WIFI_PASSWORD "12345678"
void setup() {
Serial.begin(9600);
// connect to wifi.
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
Serial.print("connecting");
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println();
Serial.print("connected: ");
Serial.println(WiFi.localIP());
Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);
}
int n = 0;
void loop() {
// set value
Firebase.setFloat("number", 42.0);
// handle error
if (Firebase.failed()) {
Serial.print("setting /number failed:");
Serial.println(Firebase.error()); return;
}
delay(1000);
// update value
Firebase.setFloat("number", 43.0);
// handle error
if (Firebase.failed()) {
Serial.print("setting /number failed:");
Serial.println(Firebase.error()); return;
}
delay(1000);
// get value
Serial.print("number: ");
Serial.println(Firebase.getFloat("number")); delay(1000);
// remove value
Firebase.remove("number");
delay(1000);
// set string value Firebase.setString("message",
"hello world");
// handle error
if (Firebase.failed()) { Serial.print("setting
/message failed:");
Serial.println(Firebase.error()); return; }
delay(1000);
// set bool value
Firebase.setBool("truth", false);
// handle error
if (Firebase.failed()) {
Serial.print("setting /truth failed:");
Serial.println(Firebase.error()); return;
}
delay(1000);
// append a new value to /logs
String name = Firebase.pushInt("logs", n++);
// handle error
if (Firebase.failed()) {
Serial.print("pushing /logs failed:");
Serial.println(Firebase.error()); return;
}
Serial.print("pushed: /logs/");
Serial.println(name);
delay(1000);
}
OUTPUT:
PROGRAM:

// defines pins numbers


constinttrigPin = 9;
constintechoPin = 10;
// defines variables
long duration;
int distance;
void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(9600); // Starts the serial communication
}
void loop() {
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds digitalWrite(trigPin,
HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds duration =
pulseIn(echoPin, HIGH);
// Calculating the distance
distance = duration * 0.034 / 2;
// Prints the distance on the Serial Monitor
Serial.print("Distance: "); Serial.println(distance);
}
OUTPUT:

You might also like