Τρίτη 14 Ιανουαρίου 2014

Working with ENC28J60

ENC28J60 Lan module $3.59
5V 2 channel relay module $2.00
DHT11 sensor module $2.44

today I work with my ENC28J60 module, a cheap alternative to the standard W5100 Ethernet shield. If you want to use with the 1.0.5+ IDE then the EtherCard library is one way. after many research I found the best articles here (http://www.lucadentella.it/en/category/enc28j60-arduino)
My final scope is to remotely control some relays and read sensors thru web interface. Finally I manage to write a sketch, but I want to read/learn more in order to make html page code better and of course to add security in order to not anybody open/close my relays ...



This is the sketch till now:

 /**************************************************
Remotelly read sensor data and control relays
without any security... yet ;-)
arduino UNO, ENC28J60, DT11 and two relays module
DT11 3 cables: A0 analog pin, VCC +5V, GND
ENC28J60 6 cables: SI ->D11, SO ->D12, SCK ->D13,
                   CS ->D8, VCC ->3v3, GND
Relays Module 4 cable: IN1 ->D2, IN2 ->D3,
                       VCC +5v, GND
This is sketch based to infos collected from varius
sites but the most valuable was the pages found on
http://www.lucadentella.it/en/category/enc28j60-arduino/
many many many thanks for it's hard and good work!!!
the ENC28J60 solution is very cheap but it lacks in
support, especially for EtherCard.h library which is
the only one to make small and quick sketches working
with IDE 1.0.5+

I am not experience in programming so excuse any
mistakes that I make...
any comments are welcome in my blog
http://demerduino.blogspot.gr/
sketch ver.1401.14
*******************************************************/
// declare libraries and variables
#include <EtherCard.h>
#include <dht.h>

#define dht_dpin A0
#define REL1PIN  2
#define REL2PIN  3

static byte mymac[] = {0xDD,0xDD,0xDD,0x00,0x00,0x01};
static byte myip[] = {192,168,1,2};
byte Ethernet::buffer[500];

boolean rel1Status;
boolean rel2Status;
unsigned long timer;
float temp;
float hum;
char temp_str[10];
char hum_str[10];

dht DHT;

void setup () {

  Serial.begin(9600);
 
// small delay for sensor
  delay(500);

// initialize lan CS pin on digital pin 8
  if (!ether.begin(sizeof Ethernet::buffer, mymac, 8))
    Serial.println( "Failed to access Ethernet controller");
 else
   Serial.println("Ethernet controller initialized");

  if (!ether.staticSetup(myip))
    Serial.println("Failed to set IP address");

// define relays pins
  pinMode(REL1PIN, OUTPUT);
  pinMode(REL2PIN, OUTPUT);
 
// in my board the 'LOW' (false) state put relays ON so I start with 'HIGH'
// if you want to change this change also the states OFF-ON later on the sketch
  rel1Status = true;
  rel2Status = true;

}
 
void loop() {

// applying the status on pins (relays)
 digitalWrite(REL1PIN, rel1Status);
 digitalWrite(REL2PIN, rel2Status);

// This is the "heart" of the DT11 program, the proposal sample rate
// is 1/sec after the variables fill the string variables
 if (millis() > timer) {
   timer = millis() + 1000;
   DHT.read11(dht_dpin);
   temp= DHT.temperature;
   hum = DHT.humidity;
   dtostrf(temp,6,2,temp_str);
   dtostrf(hum,6,2,hum_str);
  }

// check for ethernet and tcp packet
  word len = ether.packetReceive();
  word pos = ether.packetLoop(len);

// read the parameter and change the status of relay's pin
  if(pos) {
    
      if(strstr((char *)Ethernet::buffer + pos, "GET /?REL1") != 0) {
        rel1Status = !rel1Status;
      }
    
      else if(strstr((char *)Ethernet::buffer + pos, "GET /?REL2") != 0) {
        rel2Status = !rel2Status; 
      }
    
// bulding the html file...
// the two $S are filled with string variables
      BufferFiller bfill = ether.tcpOffset();
      bfill.emit_p(PSTR(
        "HTTP/1.0 200 OK\r\n"
        "Content-Type: text/html\r\n"
        "Pragma: no-cache\r\n\r\n"
// if you want to enable the auto refresh mode
//      "<meta http-equiv='refresh' content='5'/>"
        "<html><head><title>Web Control</title></head><body>"
        "<body bgcolor=""#ccccff"">"
        "<font size=""6"">"
        "<center><br>"
        "Temperature... : "
        "<b> $S &deg;C </b> <br>"
        "Humidity... : "
        "<b> $S %</b><br>"
        "____________________<br>"
        "<BR>"),
        temp_str,hum_str);

// and now the relays status / hyperlinks    
      if(rel1Status) bfill.emit_p(PSTR("Relay No_1  is <a href=\"/?REL1\">$F</a><br>"),PSTR("OFF"));
      else bfill.emit_p(PSTR("Relay No_1  is <a href=\"/?REL1\">$F</a><br>"),PSTR("ON"));
   
     if(rel2Status) bfill.emit_p(PSTR("Relay No_2   is <a href=\"/?REL2\">$F</a><br>"),PSTR("OFF"));
     else bfill.emit_p(PSTR("Relay No_2   is <a href=\"/?REL2\">$F</a><br>"),PSTR("ON"));

// and html close, any footer goes here...   
      bfill.emit_p(PSTR("<br></body></html>"));
      ether.httpServerReply(bfill.position());
  }
}



1 σχόλιο: