Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ibbo committed May 6, 2016
0 parents commit 7db1f94
Show file tree
Hide file tree
Showing 21 changed files with 441 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.pyc
*.sw[op]
Binary file added db.sqlite3
Binary file not shown.
Empty file added lazytwinkle/__init__.py
Empty file.
122 changes: 122 additions & 0 deletions lazytwinkle/settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
"""
Django settings for lazytwinkle project.
Generated by 'django-admin startproject' using Django 1.9.6.
For more information on this file, see
https://docs.djangoproject.com/en/1.9/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.9/ref/settings/
"""

import os

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.9/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '_=$7elaigl)4ded013_0rgm=dh1bo$txsn_bg%&mr)24fm-g%@'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = [
'lights.apps.LightsConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]

MIDDLEWARE_CLASSES = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'lazytwinkle.urls'

TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]

WSGI_APPLICATION = 'lazytwinkle.wsgi.application'


# Database
# https://docs.djangoproject.com/en/1.9/ref/settings/#databases

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}


# Password validation
# https://docs.djangoproject.com/en/1.9/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]


# Internationalization
# https://docs.djangoproject.com/en/1.9/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'GMT'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.9/howto/static-files/

STATIC_URL = '/static/'
22 changes: 22 additions & 0 deletions lazytwinkle/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
"""lazytwinkle URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/1.9/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: url(r'^$', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: url(r'^$', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.conf.urls import url, include
2. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls'))
"""
from django.conf.urls import include, url
from django.contrib import admin

urlpatterns = [
url(r'^lights/', include('lights.urls')),
url(r'^admin/', admin.site.urls),
]
16 changes: 16 additions & 0 deletions lazytwinkle/wsgi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""
WSGI config for lazytwinkle project.
It exposes the WSGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/1.9/howto/deployment/wsgi/
"""

import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "lazytwinkle.settings")

application = get_wsgi_application()
Empty file added lib/__init__.py
Empty file.
1 change: 1 addition & 0 deletions lib/remote_light/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__author__ = 'ibbo'
39 changes: 39 additions & 0 deletions lib/remote_light/eventClient.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import socket
import time
import socketController as sc

class EventClient:
def __init__(self, host, port):
self.host = host
self.port = port

def connect(self):
self.socket = socket.create_connection((self.host, self.port))

def defaultEventHandler(data):
print(repr(data))

def pollEvents(self, eventHandler=defaultEventHandler):
while True:
data = self.socket.recv(1024)
eventHandler(data)
time.sleep(1)

def switchHandler(data):
if not data:
return
print("Received: %s" % data)
s = data.split(':')
switchId = int(s[0])
switchState = s[1].strip() == 'true'
if switchState:
print("Switch %d is on" % switchId)
sc.turnOnSocket()
else:
print("Switch %d is off" % switchId)
sc.turnOffSocket()

if __name__ == "__main__":
ec = EventClient("localhost", 18000)
ec.connect()
ec.pollEvents(switchHandler)
94 changes: 94 additions & 0 deletions lib/remote_light/lightSwitch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import RPi.GPIO as GPIO
import signal
import sys
import time


def setupGPIO():
GPIO.setmode(GPIO.BOARD)
# K0-K3 data inputs
GPIO.setup(11, GPIO.OUT)
GPIO.setup(15, GPIO.OUT)
GPIO.setup(16, GPIO.OUT)
GPIO.setup(13, GPIO.OUT)

# ASK/FSK
GPIO.setup(18, GPIO.OUT)

# modulator
GPIO.setup(22, GPIO.OUT)

# Disable modulator
GPIO.output(22, False)

# Set modulator to ASK for On Off Keying
# by setting MODSEL pin lo
GPIO.output(18, False)

# Init K0-K3 inputs of the encoder to 0000
GPIO.output(11, False)
GPIO.output(15, False)
GPIO.output(16, False)
GPIO.output(13, False)

def lightSwitchHandler(data):
if not data:
return
s = data.split(':')
switchId = int(s[0])
switchState = s[1].strip() == 'true'
switchLight(switchState)

def programPlug():
switchLight(True)
switchLight(False)
switchAll(True)
switchAll(False)

def switchLight(on):
# Last pin determines on or off
if on:
GPIO.output(11, True)
GPIO.output(15, True)
GPIO.output(16, True)
GPIO.output(13, True)
else:
GPIO.output(11, True)
GPIO.output(15, True)
GPIO.output(16, True)
GPIO.output(13, False)

# Send the signal by pulsing the modulator
pulse_modulator()

def switchAll(on):
if on:
GPIO.output(11, True)
GPIO.output(15, True)
GPIO.output(16, False)
GPIO.output(13, True)
else:
GPIO.output(11, True)
GPIO.output(15, True)
GPIO.output(16, False)
GPIO.output(13, False)

pulse_modulator()

def pulse_modulator():
# let it settle, encoder requires this
time.sleep(0.1)
GPIO.output(22, True)
time.sleep(0.25)
GPIO.output(22, False)

def signal_handler(signal, frame):
sys.exit(0)

def cleanup():
switchLight(False)
GPIO.cleanup()

setupGPIO()
if __name__ == "__main__":
pass
67 changes: 67 additions & 0 deletions lib/remote_light/socketController.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
__author__ = 'ibbo'
#import the required modules
import RPi.GPIO as GPIO
import time

# set the pins numbering mode
GPIO.setmode(GPIO.BOARD)

# Select the GPIO pins used for the encoder K0-K3 data inputs
GPIO.setup(11, GPIO.OUT)
GPIO.setup(15, GPIO.OUT)
GPIO.setup(16, GPIO.OUT)
GPIO.setup(13, GPIO.OUT)

# Select the signal to select ASK/FSK
GPIO.setup(18, GPIO.OUT)

# Select the signal used to enable/disable the modulator
GPIO.setup(22, GPIO.OUT)

# Disable the modulator by setting CE pin lo
GPIO.output (22, False)

# Set the modulator to ASK for On Off Keying
# by setting MODSEL pin lo
GPIO.output (18, False)

# Initialise K0-K3 inputs of the encoder to 0000
GPIO.output (11, False)
GPIO.output (15, False)
GPIO.output (16, False)
GPIO.output (13, False)

# The On/Off code pairs correspond to the hand controller codes.
# True = '1', False ='0'

def turnOnSocket():
# Set K0-K3
print "sending code 1111 socket 1 on"
GPIO.output (11, True)
GPIO.output (15, True)
GPIO.output (16, True)
GPIO.output (13, True)
# let it settle, encoder requires this
time.sleep(0.1)
# Enable the modulator
GPIO.output (22, True)
# keep enabled for a period
time.sleep(0.25)
# Disable the modulator
GPIO.output (22, False)

def turnOffSocket():
# Set K0-K3
print "sending code 0111 Socket 1 off"
GPIO.output (11, True)
GPIO.output (15, True)
GPIO.output (16, True)
GPIO.output (13, False)
# let it settle, encoder requires this
time.sleep(0.1)
# Enable the modulator
GPIO.output (22, True)
# keep enabled for a period
time.sleep(0.25)
# Disable the modulator
GPIO.output (22, False)
Empty file added lights/__init__.py
Empty file.
3 changes: 3 additions & 0 deletions lights/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.contrib import admin

# Register your models here.
7 changes: 7 additions & 0 deletions lights/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from __future__ import unicode_literals

from django.apps import AppConfig


class LightsConfig(AppConfig):
name = 'lights'
Empty file added lights/migrations/__init__.py
Empty file.
5 changes: 5 additions & 0 deletions lights/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from __future__ import unicode_literals

from django.db import models

# Create your models here.
10 changes: 10 additions & 0 deletions lights/templates/lights/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<p>Use the buttons below to switch the conservatory lights on/off</p>

<form action="/lights/on" method="post">
{% csrf_token %}
<button type="submit">Turn lights on</button>
</form>
<form action="/lights/off" method="post">
{% csrf_token %}
<button type="submit">Turn lights off</button>
</form>
Loading

0 comments on commit 7db1f94

Please sign in to comment.