#include "etherShield.h"
// Por favor modifique as seguintes linhas. mac e ip tem que ser exclusivo
// Na sua rede de área local. Você não pode ter os mesmos números em
// Dois dispositivos:
static uint8_t mymac[6] = {0x54,0x55,0x58,0x10,0x00,0x24}; //Mac do arduino
static uint8_t myip[4] = {192,168,1,88}; // Ip do Arduino
static uint16_t my_port = 80; // Porta do Arduino
static char client_ip[] = "192.168.1.88"; // para distinguir cada cliente ethershield
// Configurações do Servidor
// Modificar o ip de serviço para o seu próprio servidor
///////////////////////////////////////////////////////////////
//
static uint8_t dest_ip[4]={192,168,1,103}; // Ip Servidor // Servidor
static uint8_t dest_mac[6]; // Nao alterar //
//
///////////////////////////////////////////////////////////////
enum CLIENT_STATE
{
IDLE, ARP_SENT, ARP_REPLY, SYNC_SENT
};
static CLIENT_STATE client_state;
static uint8_t client_data_ready;
static uint8_t syn_ack_timeout = 0;
#define BUFFER_SIZE 500
static uint8_t buf[BUFFER_SIZE+1];
EtherShield es=EtherShield();
// Preparar a página, escrevendo os dados para o buffer de envio tcp
uint16_t print_webpage(uint8_t *buf);
int8_t analyse_cmd(char *str);
void setup(){
/initialize enc28j60/
es.ES_enc28j60Init(mymac);
es.ES_enc28j60clkout(2); // change clkout from 6.25MHz to 12.5MHz
delay(10);
es.ES_enc28j60PhyWrite(PHLCON,0x880);
delay(500);
es.ES_enc28j60PhyWrite(PHLCON,0x990);
delay(500);
es.ES_enc28j60PhyWrite(PHLCON,0x880);
delay(500);
es.ES_enc28j60PhyWrite(PHLCON,0x990);
delay(500);
es.ES_enc28j60PhyWrite(PHLCON,0x476);
delay(100);
//inicializa a ethernet/mac ip porta layer:
es.ES_init_ip_arp_udp_tcp(mymac,myip,80);
// intialize varible;
syn_ack_timeout =0;
client_data_ready = 0;
client_state = IDLE;
}
void loop(){
if(client_data_ready==0){
delay(20000UL); // delay 20s
client_data_ready = 1; }
client_process();
}
uint16_t gen_client_request(uint8_t *buf )
{
uint16_t plen;
byte i;
plen= es.ES_fill_tcp_data_p(buf,0, PSTR ( "GET /ethershield_log/save.php?pwd=secret&client=" ) );
for(i=0; client_ip*!='\0'; i++){*
buf[TCP_DATA_P+plen]=client_ip*;
_ plen++;_
_ }*_
* plen= es.ES_fill_tcp_data_p(buf, plen, PSTR ( " HTTP/1.0\r\n" ));
plen= es.ES_fill_tcp_data_p(buf, plen, PSTR ( "Host: 192.168.1.103\r\n" ));
plen= es.ES_fill_tcp_data_p(buf, plen, PSTR ( "User-Agent: AVR ethernet\r\n" ));
plen= es.ES_fill_tcp_data_p(buf, plen, PSTR ( "Accept: text/html\r\n" ));
plen= es.ES_fill_tcp_data_p(buf, plen, PSTR ( "Keep-Alive: 300\r\n" ));
plen= es.ES_fill_tcp_data_p(buf, plen, PSTR ( "Connection: keep-alive\r\n\r\n" ));
_ return plen;_
_}_
//*****************************************************************************************
_//_
// Função: client_process*
// Descrição: temparature enviar para o servidor web, essa opção está desabilitada por padrão.
// Você deve instalar o script de servidor web e servidor antes de ativar esta opção,
// Eu recommented servidor web Apache e script PHP.
// More detail about Apache and PHP installation please visit avrportal.com - This website is for sale! - avrportal Resources and Information.
_//*
//*****************************************************************************************
void client_process ( void )
{
* uint16_t plen;
uint8_t i;
if (client_data_ready == 0) return; // Nada para enviar*
* if(client_state == IDLE){ // Inicializar ARP*
* es.ES_make_arp_request(buf, dest_ip);*
* client_state = ARP_SENT;
_ return;_
_ }*_
* if(client_state == ARP_SENT){*
* plen = es.ES_enc28j60PacketReceive(BUFFER_SIZE, buf);
_ // destino do ip na rede*_
* if ( es.ES_arp_packet_is_myreply_arp ( buf )) {
client_state = ARP_REPLY;
syn_ack_timeout=0;
_ return; _
_ }*_
* delay(10);*
* syn_ack_timeout++;*
* if(syn_ack_timeout== 100)
_ { //timeout, server ip not found*_
* client_state = IDLE;
client_data_ready =0;
syn_ack_timeout=0;
_ return;_
_ } }_
_// send SYN packet to initial connection*_
* if(client_state == ARP_REPLY){
_ // save dest mac*_
* for(i=0; i<6; i++){*
dest_mac = buf[ETH_SRC_MAC+i];
* }*
* es.ES_tcp_client_send_packet (
_ buf,
80,
1200,_
TCP_FLAG_SYN_V, // flag*
* 1, // (bool)maximum segment size*
* 1, // (bool)clear sequence ack number*
* 0, // 0 = uso seq de idade, seqack: 1 = new seq, seqack existem dados: nova seq, seqack com dados*
* 0, // tcp data length*
* dest_mac,
dest_ip*
* );*
* client_state = SYNC_SENT;
_ }
// Obter novo pacote*_
* if(client_state == SYNC_SENT){
plen = es.ES_enc28j60PacketReceive(BUFFER_SIZE, buf);*
* if ( plen == 0 )// Não recebe novo pacote*
* {*
* return;*
* }*
* // Verificar bandeira SYNACK, após a resposta do servidor de envio AVR SYN por enviar SYNACK para AVR*
* if ( buf [ TCP_FLAGS_P ] == ( TCP_FLAG_SYN_V | TCP_FLAG_ACK_V ) )
_ {
// send ACK to answer SYNACK //Loga*
* es.ES_tcp_client_send_packet (*
* buf,
80,
1200,_
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*
* );*
* // pedido http para o servidorserver*
* plen = gen_client_request( buf );
_ // Envia pacote de solicitação de http*
* // Envia pacote com PSHACK // FAz*_
* es.ES_tcp_client_send_packet (
_ buf,
80, // destination port*
* 1200, // 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*
* );*_
* } } } *