Relee controlate printr-o pagina web

Thumbnail: 

De multe ori avem nevoie sa pornim sau sa oprim niste consumatori electrici de la distanta, prin internet.

Este foarte usor sa facem asta folosind o interfata web.

Pentru asta, noi am folosit o placa Ethernet Pro, compatibila Arduino (puteti folosi la fel de bine orice Arduino, impreuna cu un shield Ethernet) si un modul cu 4 relee produs chiar de inventeaza.ro.

Modulul cu relee l-am conectat la Ethernet Pro exact ca in imaginea de mai jos.

Un screenshot cu interfata web se poate vedea aici:

Iar codul Arduino pe care l-am scris se gaseste mai jos, si poate fi descarcat din subsolul paginii.

Codul programului principal:

#include <EEPROM.h>
#include <SPI.h>
#include <Ethernet.h>
#include <avr/pgmspace.h>

byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x01};
byte ip[] = {192,168,10,210};

byte timeServer[]    = {192, 43, 244, 18};
// the router's gateway address:
byte gateway[] = { 192, 168, 1, 1 };
// the subnet:
byte subnet[] = { 255, 255, 255, 0 };

unsigned int localPort = 8888;
const int NTP_PKT_SIZE = 48;
byte packetBuffer[NTP_PKT_SIZE];
byte isTimeSynced = 0;
Server server(80);

void doWebServerLoop();

// Analog Inputs
unsigned int AnalogValues[6];

unsigned char RelayPins[] = {4, 5, 6, 7};
unsigned char RelayVals[] = {0, 0, 0, 0};

unsigned long uptime;

void readEEPROMValues() {
  for(int i=0; i<4; i++) {
    RelayVals[i] = EEPROM.read(i);
  }
}

void saveDataToEEPROM() {
  for(int i=0; i<4; i++) {
    EEPROM.write(i, RelayVals[i]);
  }
}

void updateRelayState(unsigned char relay, unsigned char state) {
  RelayVals[relay] = state;
  digitalWrite(RelayPins[relay], state);
};

void bounceRelayState(unsigned char relay) {
  Serial.print("bounceRelay ");
  Serial.println(relay, DEC);
  if(RelayVals[relay] == HIGH)
    updateRelayState(relay, LOW);
  else
    updateRelayState(relay, HIGH);
}

void setup() { 
  Serial.begin(9600);

  Ethernet.begin(mac, ip,gateway, subnet);
  server.begin();

  readEEPROMValues();

  for (int i=0; i<4; i++) {
    pinMode(RelayPins[i], OUTPUT);
    digitalWrite(RelayPins[i], RelayVals[i]);
  }
  Serial.println("Init done");
}

void updateData() {
  uptime = millis();

for(int i=0; i++; i<6) {
   AnalogValues[i] = analogRead(i);
}
}

void loop()
{
  updateData();
 
  doWebServerLoop();
}

Codul interfetei web:

void doWebServerLoop() {
  // listen for incoming clients
  Client client = server.available();
  unsigned char have_http_get = 0;
  unsigned char i= 0;
  if (client) {
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        if (c == '?') {
          have_http_get = 1;
          char buf[10];
          memset(buf, 0, sizeof(buf));
          char cursor = 0;
          // We have a GET request

          // Read R0 value (Relay0)
          memset(buf, 0, sizeof(buf));
          cursor = 0;
          c = client.read();
          while(c!='&') {
            buf[cursor] = c;
            cursor++;
            if(c == 'R')
              cursor=0;
            if(c == '0')
              cursor=0;
            if(c == '=')
              cursor=0;
            c = client.read();
          }
          int bounce_rel = atoi(buf);
          if(bounce_rel == 1)
            bounceRelayState(0);
          // Read R1 value (Relay1)
          memset(buf, 0, sizeof(buf));
          cursor = 0;
          c = client.read();
          while(c!='&') {
            buf[cursor] = c;
            cursor++;
            if(c == 'R')
              cursor=0;
            if(c == '1')
              cursor=0;
            if(c == '=')
              cursor=0;
            c = client.read();
          }
          bounce_rel = atoi(buf);
          if(bounce_rel == 1)
            bounceRelayState(1);
          // Read R2 value (Relay2)
          memset(buf, 0, sizeof(buf));
          cursor = 0;
          c = client.read();
          while(c!='&') {
            buf[cursor] = c;
            cursor++;
            if(c == 'R')
              cursor=0;
            if(c == '2')
              cursor=0;
            if(c == '=')
              cursor=0;
            c = client.read();
          }
          bounce_rel = atoi(buf);
          if(bounce_rel == 1)
            bounceRelayState(2);
          // Read R3 value (Relay3)
          memset(buf, 0, sizeof(buf));
          cursor = 0;
          c = client.read();
          while(c!='&') {
            buf[cursor] = c;
            cursor++;
            if(c == 'R')
              cursor=0;
            if(c == '3')
              cursor=0;
            if(c == '=')
              cursor=0;
            c = client.read();
          }
          bounce_rel = atoi(buf);
          if(bounce_rel == 1)
            bounceRelayState(3);
          // Save data to EEPROM
          saveDataToEEPROM();
 
        }

        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply
        if (c == '\n' && currentLineIsBlank) {
         if(have_http_get == 1) {
            // send refresh to shorten URL
            client.println("HTTP/1.1 200 OK");
            client.println("Refresh: 0; url=/");
            break;
          } else {
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-type: text/html\r\n");
          client.println("<HTML><HEAD>");
          client.println("<TITLE>Relays2Web</TITLE>");
          client.println("</HEAD><BODY>");
  client.println("<div id='wrapper'>");
          client.println("<FORM METHOD='GET' ACTION='index.ard'>");
          // output the value of each analog input pin
          client.println("<table cellspacing='0' cellpadding='0'>");
          client.println(" <tr><th colspan='2'>Values</th></tr>");
          for(i = 0; i<6; i++) {
            client.print(" <tr><td>Analog input ");
            client.print(i, DEC);
            client.print(": </td><td class='td_center'>");
            client.print(AnalogValues[i],DEC);
            client.print(" Volts </td>");
            client.print("</tr>");
          }
          client.println("</table>");
          for (i=0; i<4; i++) {
            client.print("<INPUT TYPE='hidden' NAME='R");
            client.print(i, DEC);
            client.print("' VALUE='");
            client.print(0, DEC);
            client.print("' ID='Rel");
            client.print(i, DEC);
            client.println("'>");
          }
          for (int i=0; i<4; i++) {
            client.println("<script type = 'text/javascript'>");
            client.print(" function switchRel");
            client.print(i, DEC);
            client.println(" () {");
            client.print(" document.getElementById('Rel");
            client.print(i, DEC);
            client.println("').value= '1' ;");
            client.println("document.forms[0].submit();");
            client.println("}");
            client.println("</script>");
          }
          client.println("<INPUT TYPE='hidden' NAME='Z' VALUE='0'>");
          client.println("</INPUT>");
          client.println("</FORM>");
         
          client.println("<table cellspacing='0' cellpadding='0'>"
                         "<tr>"
                         "<th colspan='3'>"
                         "Relay status"
                         "</th>"
                         "</tr>");
          for(i=0; i<4; i++) {
            client.print("<tr><td>Relay ");
            client.print(i, DEC);
            client.print("</td><td> &nbsp;State:");
            client.println(RelayVals[i], DEC);
            client.println("</td><td width='100px' class='td_center'>");
            client.print("<INPUT TYPE='BUTTON' VALUE='Change this' onClick='switchRel");
            client.print(i, DEC);
            client.print("()'></td></tr>");
          }
          client.print("</table>");
          client.print("<div id='footer'>System uptime:");
          unsigned long upTime = millis()/1000;
          unsigned int upDays = upTime/86400;
          unsigned int upHours = (upTime/3600) - (upDays * 24);
          unsigned int upMinutes = (upTime/60) - (upHours * 60) - (upDays * 1440);
          upTime = upTime - (upMinutes *60);
          upTime = upTime - (upHours * 3600);
          upTime = upTime - (upDays * 86400);
         
          if(upDays > 0) {
            client.print(upDays, DEC);
            client.print(" days & ");
          }
          if(upHours > 0) {
            client.print(upHours, DEC);
            client.print(" hours & ");
          }
          if(upMinutes > 0) {
            client.print(upMinutes, DEC);
            client.print(" minutes & ");
          }
          client.print(upTime, DEC);
          client.print(" seconds");
  client.println("</div>"
                         "</div>"
                         "</body></html>");
          break;
          }
        }
        if (c == '\n') {
          // you're starting a new line
          currentLineIsBlank = true;
        }
        else if (c != '\r') {
          // you've gotten a character on the current line
          currentLineIsBlank = false;
        }
      }
    }
    // give the web browser time to receive the data
    delay(1);
    // close the connection:
    client.stop();
  }
}

Toate materialele folosite pentru realizarea acestui proiect se pot achizitiona de la RoboFun.

File attachements: 

Cursuri

Promovarea unui ONG este o adevarata provocare, in special atunci cand nu exista suficiente resur

Ti-a trecut pe la ureche termenul de “Arduino”, nu prea stii cu ce se mananca, dar vrei sa inveti

Proiecte

Ionut Cotoi, membru al echipei inventeaza.ro si Dumitru Zagan de la firma Z.EL au realizat o bicicleta cu care poti efectua plimbari virtuale in mijl

Wouldn't you like to have an RFID lock at your hackerspace, that posts on Twitter when you get there to work on your projects?

Stiri

Uniunea Studentilor din Romania va invita intre 11 si 12 mai sa participati la Congresul Studenti

In data de 28 aprilie 2012, Centrul de Creatie Tehnica pentru Tineret a organizat un workshop gra