Skip to content

Instantly share code, notes, and snippets.

@LarsBergqvist
LarsBergqvist / SRController.js
Last active January 11, 2016 04:02
An AngularJS controller for fetching all channel info from the Swedish Radio open API
var myApp = angular.module('myApp', []);
myApp.controller('SRChannelsController', ['$scope', '$http', function($scope, $http) {
var promise = $http.get("http://api.sr.se/api/v2/channels/?format=json&pagination=false&filter=channel.channeltype&filtervalue=Riks");
promise.then(function(response) {
var channels = response.data.channels;
$scope.channels = channels;
@LarsBergqvist
LarsBergqvist / webrelay.py
Last active March 9, 2016 21:23
webrelay.py
import RPi.GPIO as GPIO
from flask import Flask, jsonify, abort, request, render_template
from relaydefinitions import relays, relayIdToPin
app = Flask(__name__)
GPIO.setmode(GPIO.BCM)
relayStateToGPIOState = {
'off' : GPIO.LOW,
relays = [
{ 'id' : 1, 'name' : 'Window lamp', 'state' : 'off'},
{ 'id' : 2, 'name' : 'Floor lamp', 'state' : 'off'},
{ 'id' : 3, 'name' : 'TV etc', 'state' : 'off'},
{ 'id' : 4, 'name' : 'Guitar equipment', 'state' : 'off'}
]
relayIdToPin = {
1 : 24,
2 : 25,
@LarsBergqvist
LarsBergqvist / webrelay_with_indexhtml.py
Created March 9, 2016 20:03
Return html with flask
@app.route('/WebRelay/', methods=['GET'])
def index():
return render_template('Index.html');
@LarsBergqvist
LarsBergqvist / Controller.js
Last active March 9, 2016 21:23
WebRelay Controller
var myApp = angular.module('myApp', [])
.config(function($interpolateProvider) {
$interpolateProvider.startSymbol('[[').endSymbol(']]');
});
var addImageToRelayObjects = function(relayObjects) {
for (var i=0; i < relayObjects.length; i++) {
var relay = relayObjects[i];
if (relay.state == 'on') {
relay.image = '/static/on_button.gif';
@LarsBergqvist
LarsBergqvist / Index.html
Last active March 9, 2016 21:23
WebRelay index.html
<!DOCTYPE html>
<html ng-app="myApp">
<head>
<script data-require="angular.js@1.4.8" data-semver="1.4.8" src="https://code.angularjs.org/1.4.8/angular.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.2/css/bootstrap.css" />
<script src="/static/Controller.js"></script>
<link rel="stylesheet" href="/static/style.css" />
</head>
@LarsBergqvist
LarsBergqvist / SendLightSensingViaRF.ino
Last active March 15, 2016 21:29
SendLightSensingViaRF
// Depends on the RCSwitch library
// https://github.com/sui77/rc-switch
#include "RCSwitch.h"
// Light measurement goes to A0 pin
#define LIGHT_IN 0
// A unique ID (4 bits, 0-15) for each device so that the receiver
// understands which device sent what message
#define ARDUINO_ID 3
@LarsBergqvist
LarsBergqvist / RaspberryReceiver.py
Last active March 16, 2016 20:57
RaspberryReceiver
# Uses pi_switch from https://github.com/lexruee/pi-switch-python
# See pi_switch readme for details on setup
from pi_switch import RCSwitchReceiver
import time
receiver = RCSwitchReceiver()
receiver.enableReceive(2)
acceptedTypes = { 1 : "Light", 2 : "Temp [C]", 3: "Humidity [%]" }
@LarsBergqvist
LarsBergqvist / SendLightTempAndHumidityViaRF.ino
Created March 16, 2016 20:20
SendLightTempAndHumidityViaRF
#include "RCSwitch.h"
#include <DHT.h>
#define LIGHT_IN 0 // Light measurement goes to A0 pin
#define DHTPIN 2 // Signal in from DHT11 goes to digital pin 2
#define DHTTYPE DHT11
// Unique ID:s (4 bits, 0-15) for each measurement type so that the receiver
// understands how to interpret the data on arrival
#define LDR_MEASUREMENT_ID 1
@LarsBergqvist
LarsBergqvist / SendFourValuesViaRF.ino
Created March 20, 2016 17:31
SendFourValuesViaRF
// Depends on the RCSwitch library https://github.com/sui77/rc-switch
// and the DHT sensor-, OneWire- and DallasTemperature libraries
#include "RCSwitch.h"
#include <DHT.h>
#include <OneWire.h>
#include <DallasTemperature.h>
// Unique ID:s (4 bits, 0-15) for each measurement type so that the receiver
// understands how to interpret the data on arrival