Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
prawda1 committed Apr 3, 2015
0 parents commit 23ab741
Show file tree
Hide file tree
Showing 13 changed files with 636 additions and 0 deletions.
106 changes: 106 additions & 0 deletions Heater.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/**
* Created by prawda on 12.01.2015.
*/

var relay = require('./QuadRelay.js');

exports.turnOn = turnOn;
exports.turnOff = turnOff;
exports.setPower = setPower;
exports.disconnect = disconnect;

// controlling variables
var currentPower = 0;
var heaterOn = false;
var availablePower = [300, 600, 800, 1000, 1200, 1300, 1500, 1600, 1800, 2000];

function disconnect() {
relay.disconnect();
};

function setPower(value){
if(currentPower == 0) pressPowerUp();

if(value > currentPower){
while(value > currentPower) {
pressPowerUp();
}
}
else while(value < currentPower) {
pressPowerDown();
}

return currentPower;
}

function turnOn() {
if(!heaterOn) pressOnOff();
}

function turnOff() {
if(heaterOn) {
pressOnOff();
currentPower = 0;
}
}

function pressOnOff(){
onOff();

// set bool to new state
heaterOn = !heaterOn;
}

function pressPowerUp(){
// if lowest value already reached, do nothing
if(currentPower == 2000) return;

powerUp();

// set currentPower
if(currentPower == 0) currentPower = 1300;
else {
var index = availablePower.indexOf(currentPower);
currentPower = availablePower[index+1];
}

console.log("power up: "+currentPower);
}

function pressPowerDown(){
// if lowest value already reached, do nothing
if(currentPower == 300) return;

powerDown();

// set currentPower
if(currentPower == 0) currentPower = 1300;
else {
var index = availablePower.indexOf(currentPower);
currentPower = availablePower[index-1];
}

console.log("power down: "+currentPower);
}

function onOff() {
relay.flop(1);
}

function powerUp() {
relay.flop(3);
}

function powerDown() {
relay.flop(4);
}

/* Zustände:
0: An/Aus, Gerät startet mit 1300W
1: nicht belegt
2: Leistung hoch
3: Leistung runter
Es gibt folgende Leistungswerte in Watt:
300, 600, 800, 1000, 1200, 1300, 1500, 1600, 1800, 2000
*/
25 changes: 25 additions & 0 deletions PID.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Created by prawda on 13.01.2015.
*/

var Controller = require('node-pid-controller');

exports.feedData = feed;
exports.setTarget = setTarget;
exports.getCorrection = getCorrection;

// parameters are k_P, k_I and k_D, where P is proportional, I integral and D differential
var ctr = new Controller(0.15, 0.01, 0.08);
var correction = -1;

function feed(data) {
correction = ctr.update(data);
}

function setTarget(value) {
ctr.setTarget(value);
}

function getCorrection() {
return correction;
}
48 changes: 48 additions & 0 deletions PTC.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* Created by prawda on 12.01.2015.
*/
var Tinkerforge = require('tinkerforge');

var HOST = 'localhost';
var PORT = 4223;
var UID = 'qbE'; // Change to your UID

var ipcon = new Tinkerforge.IPConnection(); // Create IP connection
var ptc = new Tinkerforge.BrickletPTC(UID, ipcon); // Create device object

exports.disconnect = disconnect;
exports.getTemp = getTemp;

var measured_temperature = 0; // let default temp be 0

function getTemp() {
return measured_temperature;
};

// Connect to brickd
ipcon.connect(HOST, PORT,
function(error) {
console.log('Error: '+error);
}
);

// Don't use device before ipcon is connected
ipcon.on(Tinkerforge.IPConnection.CALLBACK_CONNECTED,
function(connectReason) {
setInterval(function() {
// Get current temperature (unit is °C/100)
ptc.getTemperature(
function (temp) {
measured_temperature = temp / 100;
},
function (error) {
console.log('Error: ' + error);
}
)
}, 500); // delay between updates
}
);

function disconnect() {
ipcon.disconnect();
};
61 changes: 61 additions & 0 deletions QuadRelay.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/**
* Created by prawda on 12.01.2015.
*/
var Q = require('q');
var Tinkerforge = require('tinkerforge');

// connection variables
var HOST = 'localhost';
var PORT = 4223;
var UID = 'mUn';
var delay = 600; // relay delay in ms

var ipcon = new Tinkerforge.IPConnection(); // Create IP connection
var iqr = new Tinkerforge.BrickletIndustrialQuadRelay(UID, ipcon); // Create device object

exports.flop = flopRelais;

// controlling variables
var connected = false;

// Connect to brickd
ipcon.connect(HOST, PORT,
function(error) {
console.log('Error: '+error);
}
);

// Don't do stuff on device before ipcon is connected
ipcon.on(Tinkerforge.IPConnection.CALLBACK_CONNECTED,
function(connectReason) {
connected = true;
}
);

function disconnect() {
ipcon.disconnect();
connected = false;
};


var queue = Q.Promise(function(resolve, reject, notify) {
resolve();
});

function flopRelais(id) {
var shitzens = function() {
return Q.Promise(function(resolve, reject, notify) {
//close and open relais (1=0001, 2=0010, 4=0100, 8=1000)
var relais = {1:1, 2:2, 3:4, 4:8};
iqr.setMonoflop(relais[id], relais[id], delay);

console.log("flop "+relais[id]+"!");

// resolve after delay
//setTimeout(function() { resolve(); }, delay+10);
setTimeout(resolve, delay+10);
});
};

queue = queue.then(shitzens).fail(function(err) { console.log( err )});
}
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Tempberry-PID

There are several temperature controlling needs in every household. You might want to control the temperature in your room, the roast meat in your cooking pot, the schnaps distillation on your balcony or just build your own fridge from scratch.
Well, in the past I have done such things with an Arduino, some transducers and relais, but now it is 2015 and it is at least appropriate to have one code base, which controls the heater, reads out the temperature, calculates the PID regulation and
of course pushes everything to a webserver. There you can see the trend of the temperature, as well as the state of power regulation.

For a connection to the real world, Tinkerforge is used (http://www.tinkerforge.com), there are other solutions out there as well, but for me this looks like the most reliable and flexible.
27 changes: 27 additions & 0 deletions angular/app/bower.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "schnapsberry",
"version": "0.1.0",
"description": "controls temperature via heater",
"keywords": [
"jojo"
],
"authors": [
"wieth"
],
"license": "MIT",
"ignore": [
"**/.*",
"node_modules",
"bower_components",
"test",
"tests"
],
"dependencies":{
"bootstrap":"*",
"jquery":"*",
"jquery-flot":"*",
"angular":"1.2.x",
"angular-route":"~1.2.x",
"angular-flot":"*"
}
}
33 changes: 33 additions & 0 deletions angular/app/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<!doctype html>
<html lang="en" ng-app="Tempberry">
<head>
<meta charset="utf-8">
<title>Tempberry PID</title>
<link rel="stylesheet" href="bower_components/bootstrap/dist/css/bootstrap.css">
<!--link rel="stylesheet" href="css/app.css"-->
<script src="bower_components/angular/angular.js"></script>
<script src="bower_components/angular-route/angular-route.js"></script>

<script src="bower_components/jquery/dist/jquery.min.js"></script>

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.9/angular.min.js"></script>
<script src="bower_components/jquery-flot/jquery.flot.js"></script>
<!--script src="bower_components\jquery-flot/jquery.flot.orderBars.js"></script-->
<script src="bower_components/jquery-flot/jquery.flot.pie.js"></script>
<script src="bower_components/jquery-flot/jquery.flot.resize.js"></script>
<script src="lib/flang.js"></script> <!-- see https://github.com/ErikAugust/angularjs-flot-chart-directive -->
<!--script src="app.js"></script-->

<script src="bower_components/angular-flot/angular-flot.js"></script>

<script src="js/app.js"></script>
<script src="js/controllers.js"></script>

</head>
<body>

<div ng-view></div>

</body>
</html>

23 changes: 23 additions & 0 deletions angular/app/js/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use strict';

/* App Module */
var Tempberry = angular.module('Tempberry', [
'ngRoute',
'Controllers',
'angular-flot',
'flang'
]);


Tempberry.config(['$routeProvider',
function($routeProvider) {
$routeProvider.
when('/overview', {
templateUrl: 'partials/overview.html',
controller: 'OverviewCtrl'
}).otherwise({
redirectTo: '/overview'
});
}]);


Loading

0 comments on commit 23ab741

Please sign in to comment.