Strumenti Utente

Strumenti Sito


raspberry:relay

Esperienza con la scheda di gestione relè
La scheda l'ho comprata su ebay ed è questa: http://jap.hu/electronic/relay_module.html
Il collegamento viene effettuato tramite il bus “i2c”
poi deve essere alimentata a 12v in quanto le bobine dei relè funzionano a 12v.
si installa l'i2c-tools

apt-get install i2c-tools

si controlla la presenza della scheda nel bus corretto

i2cdetect -y 1

     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- -- 
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
20: 20 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

Quindi è correttamente identificata all'indirizzo 0x20
e' possibile attivare i relè tramite i seguenti comandi:

i2cset -y 1 0x20 0xa 0xff

il precedente comando attiva tutti i relè
in sostanza si dice di selezionare il device i2c (1)
di individuare l'indirizzo del nostro dispositivo sul bus (0x20)
di individuare l'indirizzo del dato da modificare (0xa)
e il codice corrispondente all'attivazione di uno o più relè
cioè se io faccio:

i2cset -y 1 0x20 0xa 0x01

attivo solo il primo relè
se faccio

i2cset -y 1 0x20 0xa 0x04

attivo il terzo relè
se faccio

i2cset -y 1 0x20 0xa 0x03

attivo il primo e il secondo
eccetera
insomma tramite la combinazione degli ultimi codici attivo esattamente quello che mi serve
potrei ad esempio attivare il primo, il terzo e il quarto (codice x) poi a questi devo attivare ad es il 7 e quindi mettero un'altro codice (y).

Se invece voglio leggere lo stato attuale devo usare il comando i2cget
ad es:

i2cget -y 1 0x20 0xa b

il risultato sarà ad esempio

0x07

che corrisponde all'accensione del relè 1, 2, 3

che in modo simile alla scrittura identifico il device (1)
il dispositivo sul bus (0x20)
e l'indirizzo del dato da leggere (0xa)
e l'opzione (b) che significa leggi nel formato esadecimale
Questo è l'esempio fatto con i comandi su shell minimali fatti con il programma di default i2c
per usare il python o altro devo ancora provare.
Un'altra nota riguarda il consumo testato con l'ina219.
in pratica con le bobine non eccitate non c'è consumo (2 milliwat)
ogni bobina (relè eccitato) consuma circa 360 milliwat
tre relè (1 watt abbondante)
ecc..
per un totale con tutte e 8 le bobine eccitate di 2,8 watt
ed un assorbimento minimo di 27 milliampere e un massimo di circa 220 milliampere

Esperienza con gpio

su raspbian versione 9
Abilitazione degli strumenti gpio e del python
poi installazione di apache2 e del php5
attenzione di default viene installato il php7 e per mezza giornata non ha funzionato nulla fino a che ho installato anche il php5
creare su /var/www/html il seguente file index.html

index.html
<!--
Bootstrap  -   http://getbootstrap.com/
Switch buttons  -  http://www.bootstrap-switch.org/
Jquery  -  http://jquery.com/
-->
 
<html>
<head>
<meta charset="UTF-8" />
<!-- for mobile version -->
<meta name="viewport" content="width=400px, initial-scale=1">
<link href="bootstrap.css" rel="stylesheet">
<link href="bootstrap-switch.css" rel="stylesheet">
<script src="jquery.js"></script>
<script src="bootstrap-switch.js"></script>
 
</head>
 
 
<body>
<h3>4 Array relay Web App</h3>^M
^M
<div>^M
<span>4 Array relay</span></br>^M
<!-- creating table -->^M
<table>^M
  <tr>^M
    <td><label for="relay1">Light 1</label></td>^M
    <!-- creating button for the relay -->^M
    <td><input type="checkbox" name="relay1" id="relay1"checked></td>^M
  </tr>^M
    <tr>^M
    <td><label for="relay2">Light 2</label></td>^M
    <td><input type="checkbox" name="relay2" id="relay2"checked></td>^M
  </tr>^M
    <tr>^M
    <td><label for="relay3">Light 3</label></td>^M
    <td><input type="checkbox" name="relay3" id="relay3"checked></td>^M
  </tr>^M
    <tr>^M
    <td><label for="relay4">Light 4</label></td>^M
    <td><input type="checkbox" name="relay4" id="relay4"checked></td>^M
  </tr>^M
  ^M
^M
</table>^M
^M
<!-- feedback paragraphs -->^M
^M
<p>Relay 1 is  <span id="feedback1"></span> </p>^M
^M
<p>Relay 2 is <span id="feedback2"></span> </p>^M
^M
<p>Relay 3 is <span id="feedback3"></span> </p>^M
^M
<p>Relay 4 is <span id="feedback4"></span> </p>^M
</div>^M
^M
<script type="text/javascript">^M
^M
^M
//setting all buttons off state to be red color^M
$.fn.bootstrapSwitch.defaults.offColor="danger";
 
//inicalizing the switch buttons 
$("[name='relay1']").bootstrapSwitch();
$("[name='relay2']").bootstrapSwitch();
$("[name='relay3']").bootstrapSwitch();
$("[name='relay4']").bootstrapSwitch();
 
//this will be execute when the html is ready
$(document).ready(function(){
 
  //ajax request with post method (better to be GET)
  $.ajax({
    method: "POST",
    url: "firstCheck.php",
    data: {}
  };
  .done(function( msg ) {
    // we need to parse the responce 2 times 
    msg = JSON.parse(msg);
    msg = JSON.parse(msg);
 
    //for loop that is implemented for the feedback divs and buttons state
    for(var i = 0 ; i < 4; i++){
 
      // setting the feedback divs
      if(msg[i] == true){
        $("#feedback"+(i+1)).html("Turned On");
      }else{
        $("#feedback"+(i+1)).html("Turned Off");
      } 
      //setting the current button state
      $("[name='relay"+(i+1)+"']").bootstrapSwitch('state',msg[i]);
    } 
});
});
 
// making onclick event listener for the buttons ^M
$('input[name="relay1"],'+^M
  'input[name="relay2"],'+^M
  'input[name="relay3"],'+^M
  'input[name="relay4"]').on('switchChange.bootstrapSwitch', function(event, state) {
 
// checking whitch button is clicked
var relayID = event.target.id.substring(event.target.id.length - 1);
 
//ajax POST request
$.ajax({
  method: "POST",
  url: "changeState.php",
  data: { clicked :state , relayId:relayID}
})
  .done(function( msg ) {
  // changing the feedback paragraphs
  if(msg == "true"){
    $("#feedback"+(relayID)).html("Turned On");
  }else{
    $("#feedback"+(relayID)).html("Turned Off");
  }
 
  });
 
 
});
</script>
 
</body>
 
</html>

Di default

raspberry/relay.txt · Ultima modifica: 2023/04/17 14:25 (modifica esterna)