DINo IN OUT

 

Master (Inputs) Code

 

#include "EtherShield.h"

uint8_t mymac[6] = {0xCF,0x70,0x7C,0xE4,0x8A,0xB9}; // Master MAC
uint8_t myip[4] = {192,168,1,26};  // Master IP
uint8_t udpmac[6] = {0xCF,0x70,0x7C,0xE4,0x8A,0xB8}; // Slave MAC
uint8_t udpip[4] = {192,168,1,25}; // Slave IP
#define DEST_PORT_L  0x34
#define DEST_PORT_H  0x12

const char iphdr[] PROGMEM ={0x45,0,0,0x82,0,0,0x40,0,0x20}; // 0x82 is the total len on ip, 0x20 is ttl (time to live)

#define BUFFER_SIZE 750
static uint8_t buf[BUFFER_SIZE+1];
char reply[]={0xFF, 0x00, 0x00, 0x00, 0x00};

EtherShield es=EtherShield();

uint16_t plen, dat_p, a;

static uint32_t timetosend;

///----------------------------------------------------------
void setup(){
 
  es.ES_enc28j60Init(mymac);
  es.ES_init_ip_arp_udp_tcp(mymac,myip, 80);

} // end setup
///----------------------------------------------------------
void loop(){
 
    // read packet, handle ping and wait for a tcp packet:
    dat_p=es.ES_packetloop_icmp_tcp(buf,es.ES_enc28j60PacketReceive(BUFFER_SIZE, buf));
           
    if(millis() - timetosend > 100) // every 10 seconds //
    {
    timetosend = millis();
    if ( check()  )
              {
                reply[1] = digitalRead(A5) ? 1 : 0;
                reply[2] = digitalRead(A4) ? 1 : 0;
                reply[3] = digitalRead(A3) ? 1 : 0;
                reply[4] = digitalRead(A2) ? 1 : 0;
                UDP_Send();
              }
    }
    
} // end loop


void UDP_Send( void ) {
  uint8_t i=0;
  uint16_t ck;
  // Setup the MAC addresses for ethernet header
  while(i<6){
    buf[ETH_DST_MAC +i]= udpmac[i];
    buf[ETH_SRC_MAC +i]= mymac[i];
    i++;
  }
  buf[ETH_TYPE_H_P] = ETHTYPE_IP_H_V;
  buf[ETH_TYPE_L_P] = ETHTYPE_IP_L_V;
  es.ES_fill_buf_p(&buf[IP_P],9,iphdr);

  // IP Header
  buf[IP_TOTLEN_L_P]=28+sizeof(reply);
  buf[IP_PROTO_P]=IP_PROTO_UDP_V;
  i=0;
  while(i<4){
    buf[IP_DST_P+i]=udpip[i];
    buf[IP_SRC_P+i]=myip[i];
    i++;
  }
  es.ES_fill_ip_hdr_checksum(buf);
  buf[UDP_DST_PORT_H_P]=DEST_PORT_H;
  buf[UDP_DST_PORT_L_P]=DEST_PORT_L;
  buf[UDP_SRC_PORT_H_P]=0x12;
  buf[UDP_SRC_PORT_L_P]=0x34; // lower 8 bit of src port
  buf[UDP_LEN_H_P]=0;
  buf[UDP_LEN_L_P]=8+sizeof(reply); // fixed len
  // zero the checksum
  buf[UDP_CHECKSUM_H_P]=0;
  buf[UDP_CHECKSUM_L_P]=0;
  // copy the data:
  i=0;
  // most fields are zero, here we zero everything and fill later
  uint8_t* b = (uint8_t*)&reply;
  while(i< sizeof( reply ) ){
    buf[UDP_DATA_P+i]=*b++;
    i++;
  }
  // Create correct checksum
  ck=es.ES_checksum(&buf[IP_SRC_P], 16 + sizeof( reply ),1);
  buf[UDP_CHECKSUM_H_P]=ck>>8;
  buf[UDP_CHECKSUM_L_P]=ck& 0xff;
  es.ES_enc28j60PacketSend(42 + sizeof( reply ), buf);
}


int check()
{
        if (digitalRead(A5) != reply[1] ) return(1);
        if (digitalRead(A4) != reply[2] ) return(1);
        if (digitalRead(A3) != reply[3] ) return(1);
        if (digitalRead(A2) != reply[4] ) return(1);
        return 0;

}

 


 

 

Slave (Relays) code

 

#include "EtherShield.h"

uint8_t mymac[6] = {0xCF,0x70,0x7C,0xE4,0x8A,0xB8};
uint8_t myip[4] = {192,168,1,25};
uint16_t MYWWWPORT = 80;

#define BUFFER_SIZE 750
static uint8_t buf[BUFFER_SIZE+1];

EtherShield es=EtherShield();

uint16_t plen, dat_p;

///----------------------------------------------------------
void setup(){
Serial.begin(9600);
Setup_Pins();
es.ES_enc28j60Init(mymac);
es.ES_init_ip_arp_udp_tcp(mymac,myip, MYWWWPORT);

} // end setup


///----------------------------------------------------------
void loop(){

// read packet, handle ping and wait for a tcp packet:
dat_p=es.ES_packetloop_icmp_tcp(buf,es.ES_enc28j60PacketReceive(BUFFER_SIZE, buf));

if (buf[IP_PROTO_P]==IP_PROTO_UDP_V){
Relay_Control();
buf[IP_PROTO_P]=0;
}

} // end loop


///----------------------------------------------------------
void Setup_Pins(){

pinMode(5, OUTPUT); digitalWrite(5, 0);
pinMode(6, OUTPUT); digitalWrite(6, 0);
pinMode(7, OUTPUT); digitalWrite(7, 0);
pinMode(8, OUTPUT); digitalWrite(8, 0);

} // end Setup_Pins


///----------------------------------------------------------
void Relay_Control(){
Serial.print(buf[43], HEX);
//if (buf[42]==0xFF)
// {
//Serial.print(buf[43], HEX);
digitalWrite(8, buf[43]);
digitalWrite(7, buf[44]);
digitalWrite(6, buf[45]);
digitalWrite(5, buf[46]);
// }
} // end Relay_Control

 

 


 

Read 2565 times