Hallo liebe Community
Ich habe in den letzen Tagen mal endlich Zeit gefunden um ein bischen am RPi zu experimentieren und wollte schnell mehr.
Unterdessen habe ich einige Breadboards, ein 4-Kanal-Relaisboard und DS18b20-Sensoren hier.
Nun wollte ich zum Anfang mal die GPIO-Ports (also das Relais) übers Internet steuern.
Dazu habe ich dieses Tutorial von OpenHomeAutomation gefunden.
Dieses habe ich nach ein bischen Pröbeln schnell zum laufen gebracht.
Ich hab heute einmal den Code analysiert, wobei ich besser anschauen sagen sollte, da ich mich nur sehr begrenzt mit den Code's auskenne. Der Code ist zur Zeit noch für nur ein Relais geschrieben, ich will aber alle vier ansteuern.
Dazu habe ich als erstes das HTML-Interface ergänzt:
Spoiler anzeigen
<head>
<LINK href="/css/interface.css" rel="stylesheet" type="text/css" />
<LINK href="/css/flat-ui.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="/js/jquery-2.0.3.min.js"></script>
<script type="text/javascript" src="/js/interface.js"></script>
</script>
</head>
<body>
<div class="mainContainer">
<div class="title">Relay Control 1</div>
<div class="buttonBlock"><span class="buttonTitle">Relay</span>
<button class="btn btn-block btn-lg btn-primary" type="button" id="1" onClick="buttonClick(this.id)">On</button>
<button class="btn btn-block btn-lg btn-danger" type="button" id="2" onClick="buttonClick(this.id)">Off</button>
</div>
</div>
</body>
<body>
<div class="mainContainer">
<div class="title">Relay Control 2</div>
<div class="buttonBlock"><span class="buttonTitle">Relay</span>
<button class="btn btn-block btn-lg btn-primary" type="button" id="3" onClick="buttonClick(this.id)">On</button>
<button class="btn btn-block btn-lg btn-danger" type="button" id="4" onClick="buttonClick(this.id)">Off</button>
</div>
</div>
</body>
<body>
//...und dies jetzt noch 2 mal
Alles anzeigen
Der Code hat noch ein dazugehörendes CSS File, aber ich denke dieses muss ich hier nicht posten.
Danach wollte ich die Datei interface.js anpassen, leider habe ich da aber den Zusammenhang zwischen interface.js und app.js nicht ganz verstanden.
Die interface.js fragt an, ob der Button im Web gedrückt wird, ist das richtig?
Wenn dieser gedrückt wird, leited sie dies an die app.js weiter.
Wenn ich also nun die interface.js ergänze, wie muss ich dann in der app.js dies auswerten, da ist ja nur "on" und "off" ausgewertet (stimmt das?), wie sieht das bei vier mal "on" und "off" aus?
Müsste ich da on1..on2...on3... erstellen?
Spoiler anzeigen
// Function to control the relay
function buttonClick(clicked_id){
if (clicked_id == "1"){
$.ajax({
type: 'GET',
url: '/send',
async: false,
data: {state: 'on'},
success: function( data ) {
json_data = jQuery.parseJSON(data);
}
});
}
if (clicked_id == "2"){
$.ajax({
type: 'GET',
url: '/send',
async: false,
data: {state: 'off'},
success: function( data ) {
json_data = jQuery.parseJSON(data);
}
});
}
//von hier an von mir ergänzt
if (clicked_id == "3"){
$.ajax({
type: 'GET',
url: '/send',
async: false,
data: {state: 'on'},
success: function( data ) {
json_data = jQuery.parseJSON(data);
}
});
}
if (clicked_id == "4"){
$.ajax({
type: 'GET',
url: '/send',
async: false,
data: {state: 'off'},
success: function( data ) {
json_data = jQuery.parseJSON(data);
}
});
}
if (clicked_id == "5"){
$.ajax({
type: 'GET',
url: '/send',
async: false,
data: {state: 'on'},
success: function( data ) {
json_data = jQuery.parseJSON(data);
}
});
}
if (clicked_id == "6"){
$.ajax({
type: 'GET',
url: '/send',
async: false,
data: {state: 'off'},
success: function( data ) {
json_data = jQuery.parseJSON(data);
}
});
}
if (clicked_id == "7"){
$.ajax({
type: 'GET',
url: '/send',
async: false,
data: {state: 'on'},
success: function( data ) {
json_data = jQuery.parseJSON(data);
}
});
}
if (clicked_id == "8"){
$.ajax({
type: 'GET',
url: '/send',
async: false,
data: {state: 'off'},
success: function( data ) {
json_data = jQuery.parseJSON(data);
}
});
}
}
Alles anzeigen
und die app.js, noch originalbelassen:
Spoiler anzeigen
// Requires
var express = require('express');
var path = require('path');
var querystring = require("querystring");
var url = require('url');
var gpio = require('pi-gpio');
// Create app
var app = express();
var port = 3700;
// Set views
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.static(path.join(__dirname, 'views')));
// Serve files
app.get('/interface', function(request, response){
response.sendfile('views/interface.html')
});
// Send commands to PI
app.get("/send", function(request, response){
// Get data
var queryData = url.parse(request.url, true).query;
console.log("State " + queryData.state + " received.");
// Apply command
if (queryData.state == 'on') {
gpio.open(7, "output", function(err) {
gpio.write(7, 1, function() {
gpio.close(7);
});
});
}
if (queryData.state == 'off') {
gpio.open(7, "output", function(err) {
gpio.write(7, 0, function() {
gpio.close(7);
});
});
}
// Answer
response.writeHead(200, {"Content-Type": "text/html"});
response.end();
});
// Start server
app.listen(port);
console.log("Listening on port " + port);
Alles anzeigen
queryData.state fragt den Status ab?
Vielen Dank für eure Unterstützung
housedoor