Unit-5 Iot
Unit-5 Iot
2. Exemplary Device:
Raspberry Pi is a low-cost mini-computer with the physical size of a credit
card.
Raspberry Pi runs various flavors of Linux and can perfrom almost all tassks
that a normal desktop computer can do.
Raspberry Pi also allows interfacing sensors and actuators through the
general purpose I/O PINS.
Since Raspberry Pi runs Linux operating system, it supports Python “out of
the box”.
Python program
import RP1.GPIO as GPIO
import time
GPIO.setmode (GPIO.BCM)
1dr_threshold - 1000
LDR PIN 18
LIGHT PIN 25
def readLDR (PIN):
reading-0
GPIO.setup(LIGHT_PIN, GPIO.OUT)
GPIO.output (PIN, False)
time.sleep(0.1)
GPIO.setup(PIN, GPIO.IN)
while (GPIO.input (PIN)==False):
reading reading+1
AMRITASAI-AUTONOMOUS 14 DEPARTMENT OF ECE
return reading
def switchOnLight (PIN):
GPIO.setup(PIN, GPIO.OUT)
GPIO.output (PIN, True)
def switchOffLight (PIN):
GPIO.setup(PIN, GPIO.OUT)
GPIO.output (PIN, False)
while True:
ldr_reading readLDR (LDR_PIN)
if ldr_reading < 1dr_threshold:
switchOnLight (LIGHT_PIN)
else:
switchOffLight (LIGHT_PIN)
time.sleep (1)
After installing the Django REST framework, let us create a new Django
project me rest fulapi, and then start a new app called myapp, as follows:
The REST API described in this section allows you to create, view, update
and deletes collection of resources where each resource represents a sensor
data reading from a weathe monitoring station.
Below shows the settings for the REST API Django project.
Django model for Weather Station - models.py
from django.db import models
class Station (models. Model):
AMRITASAI-AUTONOMOUS 17 DEPARTMENT OF ECE
Name=models.CharField (max_length-10).
Timestamp= models.CharField (max_length=10)
temperature =models.CharField (max_length-5)
Lat= models.CharField (max_length-10)
Lon= models. CharField (max_length-10)
Django views for Weather Station REST API - views.py
from myapp.models import Station
from rest_framework import viewsets
from django.shortcuts import render_to_response
from django.template import RequestContext
from myapp.serializers import StationSerializer
import requests
import json
class StationViewSet (viewsets.ModelViewSet):
queryset Station.objects.all()
serializer_class StationSerializer
def home (request):
r-requests.get('http://127.0.0.1:8000/station/",
auth=('username', 'password'))
result-r.text
output json.loads(result)
count-output['count']
count-int (count)-1
name output['results') [count] ['name']
temperature-output['results'] [count] ['temperature']
lat-output['results'] [count] ['lat']
lon-output ['results'] [count] ['lon']
return render_to_response ('index.html', 'name': name, 'temperature':
temperature, 'lat': lat, 'lon': lon, context_instance-RequestContext(request))