All pastes #2011076 Raw Edit

Something

public text v1 · immutable
#2011076 ·published 2010-12-05 11:27 UTC
rendered paste body
#define F_CPU 12500000UL  // 12.5 MHz

//#include "WProgram.h"
#include <inttypes.h>
extern "C" {
	#include "enc28j60.h"
	#include "ip_arp_udp_tcp.h"
 
}
#include "net.h"
#include <avr/interrupt.h>
#include <util/delay.h>
//#include <avr/pgmspace.h>

// please modify the following lines. mac and ip have to be unique
// in your local area network. You can not have the same numbers in
// two devices:
static uint8_t mymac[6] = {0x54,0x55,0x58,0x10,0x00,0x24}; 
static uint8_t myip[4]  = {192,168,0,200};
static uint16_t src_port = 1200;     // client port

// client_ip - modify it when you have multiple client on the network
// for server to distinguish each ethershield client
//static char client_ip[] = "192.168.0.200";

// server settings - modify the service ip to your own server
static uint8_t dest_ip[4] = {192,168,0,10};
static uint8_t dest_mac[6] = {0x00,0x0E,0x2E,0x3F,0xF7,0xE8};
static uint16_t dest_port = 4000;     // client port

enum CLIENT_STATE
{
  IDLE, ARP_SENT, ARP_REPLY, SYNC_SENT
};

static CLIENT_STATE client_state;

static uint8_t client_data_ready;

static uint16_t syn_ack_timeout = 0;


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

uint16_t plen;
uint8_t connected = 0;
bool sender_flag = false;
char sensorData[10];
// prepare the webpage by writing the data to the tcp send buffer
uint16_t print_webpage (uint8_t *buf);
int8_t analyse_cmd (char *str);
// get current temperature
#define TEMP_PIN  3
void getCurrentTemp (char *temperature);
void client_process (void);
uint16_t gen_client_request (uint8_t *buf );

ISR(INT1_vect)
{
  PORTD ^= (1 << PD7);
  sender_flag = true;
}

void setup() {

  /*initialize enc28j60*/
  enc28j60Init (mymac);
  enc28j60clkout (2); // change clkout from 6.25MHz to 12.5MHz
  _delay_ms(10);

  /* Magjack leds configuration, see enc28j60 datasheet, page 11 */
  // LEDA=greed LEDB=yellow

  // 0x880 is PHLCON LEDB=on, LEDA=on
  // enc28j60PhyWrite (PHLCON,0b0000 1000 1000 00 00);
  enc28j60PhyWrite (PHLCON,0x880);
  _delay_ms(500);

  // 0x990 is PHLCON LEDB=off, LEDA=off
  // enc28j60PhyWrite (PHLCON,0b0000 1001 1001 00 00);
  enc28j60PhyWrite (PHLCON,0x990);
  _delay_ms(500);

  // 0x880 is PHLCON LEDB=on, LEDA=on
  // enc28j60PhyWrite (PHLCON,0b0000 1000 1000 00 00);
  enc28j60PhyWrite (PHLCON,0x880);
  _delay_ms(500);

  // 0x990 is PHLCON LEDB=off, LEDA=off
  // enc28j60PhyWrite (PHLCON,0b0000 1001 1001 00 00);
  enc28j60PhyWrite (PHLCON,0x990);
  _delay_ms(500);

  // 0x476 is PHLCON LEDA=links status, LEDB=receive/transmit
  // enc28j60PhyWrite (PHLCON,0b0000 0100 0111 01 10);
  enc28j60PhyWrite (PHLCON,0x476);
  _delay_ms(100);

  //init the ethernet/ip layer:
  init_ip_arp_udp_tcp (mymac,myip,80);

  // intialize varible;
  syn_ack_timeout = 0;
  client_data_ready = 0;
  client_state = IDLE;
  // initialize DS18B20 datapin
/*  digitalWrite (TEMP_PIN, LOW);
  pinMode (TEMP_PIN, INPUT);      // sets the digital pin as input (logic 1)
*/
}


void loop() {
  
  plen = enc28j60PacketReceive (BUFFER_SIZE, buf);
  
  // plen will be unequal to zero if there is a valid packet (without crc error)
  if (plen != 0){
    if (eth_type_is_arp_and_my_ip (buf,plen)) {
      make_arp_answer_from_request (buf);
    }
    // check if ip packets (icmp or udp) are for us:
    if (eth_type_is_ip_and_my_ip (buf,plen)!=0) {
      if (buf[IP_PROTO_P] == IP_PROTO_ICMP_V && buf[ICMP_TYPE_P] == ICMP_TYPE_ECHOREQUEST_V) {
        // a ping packet, let's send pong
        make_echo_reply_from_request (buf, plen);
      }
    }
    else
      return;
  }

//if(connected == 0) {

  // send SYN packet to initial connection
  if (client_state == IDLE) {

    tcp_client_send_packet (
      buf,
      dest_port,
      src_port,
      TCP_FLAG_SYN_V,     // flag
      1,                  // (bool)maximum segment size
      1,                  // (bool)clear sequence ack number
      0,                  // 0=use old seq, seqack : 1=new seq,seqack no data : new seq,seqack with data
      0,                  // tcp data length
      dest_mac,
      dest_ip
    );

    client_state = SYNC_SENT;
  }
  // get new packet
  if (client_state == SYNC_SENT) {

    // check SYNACK flag, after AVR send SYN server response by send SYNACK to AVR
    if (buf[TCP_FLAGS_P] == (TCP_FLAG_SYN_V | TCP_FLAG_ACK_V))
    {
      // send ACK to answer SYNACK
      tcp_client_send_packet (
        buf,
        dest_port,
        src_port,
        TCP_FLAG_ACK_V,      // flag
        0,                   // (bool)maximum segment size
        0,                   // (bool)clear sequence ack number
        1,                   // 0=use old seq, seqack : 1=new seq,seqack no data : new seq,seqack with data
        0,                   // tcp data length
        dest_mac,
        dest_ip
      );
      return;
    }
    if(sender_flag)
    {  
      // setup http request to server
      plen = gen_client_request (buf);
      // send http request packet
      // send packet with PSHACK
      tcp_client_send_packet (
        buf,
        dest_port,                                 // destination port
        src_port,                               // source port
        TCP_FLAG_ACK_V | TCP_FLAG_PUSH_V,   // flag
        0,                                  // (bool)maximum segment size
        0,                                  // (bool)clear sequence ack number
        0,                                  // 0=use old seq, seqack : 1=new seq,seqack no data : >1 new seq,seqack with data
        plen,                               // tcp data length
        dest_mac,
        dest_ip
      );
      sender_flag = false;
      return
    }
    // after AVR send http request to server, server response by send data with PSHACK to AVR
    // AVR answer by send ACK and FINACK to server
    if (buf [ TCP_FLAGS_P ] == (TCP_FLAG_ACK_V|TCP_FLAG_PUSH_V))
    {
      plen = tcp_get_dlength ( (uint8_t*)&buf );

      // send ACK to answer PSHACK from server
      tcp_client_send_packet (
        buf,
        dest_port,                 // destination port
        src_port,               // source port
        TCP_FLAG_ACK_V,     // flag
        0,                  // (bool)maximum segment size
        0,                  // (bool)clear sequence ack number
        plen,               // 0=use old seq, seqack : 1=new seq,seqack no data : >1 new seq,seqack with data
        0,                  // tcp data length
        dest_mac,
        dest_ip
      );
      
      tcp_client_send_packet (
        buf,
        dest_port,                               // destination port
        src_port,                             // source port
        TCP_FLAG_ACK_V,			    // flag
        0,                                // (bool)maximum segment size
        0,                                // (bool)clear sequence ack number
        0,                                // 0=use old seq, seqack : 1=new seq,seqack no data : >1 new seq,seqack with data
        0,
        dest_mac,
        dest_ip
      );

/*
      // send finack to disconnect from web server
      tcp_client_send_packet (
        buf,
        dest_port,                               // destination port
        src_port,                             // source port
        TCP_FLAG_FIN_V|TCP_FLAG_ACK_V,    // flag
        0,                                // (bool)maximum segment size
        0,                                // (bool)clear sequence ack number
        0,                                // 0=use old seq, seqack : 1=new seq,seqack no data : >1 new seq,seqack with data
        0,
        dest_mac,
        dest_ip
      );
      client_state = IDLE;   // return to IDLE state
*///      client_state = SYNC_SENT;   // return to IDLE state
      return;


    }

    // answer FINACK from web server by send ACK to web server
    if (buf[TCP_FLAGS_P] == (TCP_FLAG_ACK_V|TCP_FLAG_FIN_V) )
    {
      // send ACK with seqack = 1
      tcp_client_send_packet (
        buf,
        dest_port,                  // destination port
        src_port,                // source port
        TCP_FLAG_ACK_V,      // flag
        0,                   // (bool)maximum segment size
        0,                   // (bool)clear sequence ack number
        1,                   // 0=use old seq, seqack : 1=new seq,seqack no data : >1 new seq,seqack with data
        0,
        dest_mac,
        dest_ip
      );
      client_state = IDLE;   // return to IDLE state
      client_data_ready = 0; // client data sent
    }
  }
}

//char *getStr = PSTR("GET /et");
//uint16_t plen = fill_tcp_data_p (buf, 0, getStr);

uint16_t gen_client_request (uint8_t *buf )
{
  uint16_t plen = 0;
  uint8_t i;

  //plen = fill_tcp_data_p (buf, 0, "GET /ethershield_log/save.php?pwd=secret&client=" );
  //plen = fill_tcp_data_p (buf, 0, PSTR ( "GET /ethershield_log/save.php?pwd=secret&client=" ) );
/*  for (i = 0; client_ip[i] != '\0'; i++) {
    buf[TCP_DATA_P+plen] = client_ip[i];
    plen++;
  }*/
//  plen = fill_tcp_data_p (buf, plen, PSTR ( "&status=temperature-" ) );
  plen = fill_tcp_data (buf, plen, "&status=temperature-" );
  for (i = 0; sensorData[i] != '\0'; i++) {
    buf[TCP_DATA_P+plen]=sensorData[i];
    plen++;
  }
/*
  plen = fill_tcp_data_p (buf, plen, PSTR ( " HTTP/1.0\r\n" ));
  plen = fill_tcp_data_p (buf, plen, PSTR ( "Host: 192.168.1.4\r\n" ));
  plen = fill_tcp_data_p (buf, plen, PSTR ( "User-Agent: AVR ethernet\r\n" ));
  plen = fill_tcp_data_p (buf, plen, PSTR ( "Accept: text/html\r\n" ));
  plen = fill_tcp_data_p (buf, plen, PSTR ( "Keep-Alive: 300\r\n" ));
  plen = fill_tcp_data_p (buf, plen, PSTR ( "Connection: keep-alive\r\n\r\n" ));
*/
  return plen;
}
int main(void)
{
	//sei();
	//init();
  GICR |= ( 1 << INT1);
  MCUCR |= ( 1 << ISC10);//|
        ///   (1 << ISC11);
DDRD |= (1<<PD7);
PORTD |= (1<<PD7);
sei();
	setup();
	
	for (;;)
		loop();
        
	return 0;
}