Thanks again PaulS, for clearing this up!!
Yesterday I took another Ethershield (from Ekitzone) and upload this code to the Arduino:
#include "etherShield.h"
// serial info gewoon ontvangen via normale (pin 0) RX port op Arduino (rechtsbovenin)
// op het einde van de serial string "/" sturen i.v.m. terminate char!
// aan het begin "*" sturen i.v.m. andere serial data wat verstuurd wordt via de BT module (ARF32)
static uint8_t mymac[6] = {
0x54,0x55,0x58,0x10,0x00,0x24};
static uint8_t myip[4] = {
192,168,2,88};
static uint16_t my_port = 80; // client port
static char client_ip[] = "192.168.2.88";
static uint8_t dest_ip[4]={
192,168,2,53};
static uint8_t dest_mac[6];
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];
boolean eerste = true;
byte lezen;
byte check;
char sensorData[150];
String readString;
bool klaar = false;
#define INLENGTH 150
#define INTERMINATOR 47 //eerst was het 0 oftewel 48 nu is het /
#define BEGINNER 42
char inString[INLENGTH+1];
int inCount;
EtherShield es=EtherShield();
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);
void setup(){
//mySerial.begin(9600);
Serial.begin(9600);
Serial.println("Begin!");
/*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);
//init the ethernet/ip layer:
es.ES_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);
delay (3000);
}
void loop(){
if (eerste == true){ //test email 1x
sensorData[0] = '*';
sensorData[1] = 'e';
sensorData[2] = 'e';
sensorData[3] = 0;
client_data_ready = 1;
client_process();
eerste = false;
}
if(client_data_ready==0){
inCount = 0;
do
{
while(!Serial.available());
sensorData[inCount] = Serial.read();
if (sensorData[inCount] == '*'){
sensorData[0] = '*';
inCount = 0;
}
if (sensorData[inCount] == '.'){
sensorData[inCount] = '_';
};
if (sensorData [inCount] == INTERMINATOR) break;
}
while (++inCount < INLENGTH);
sensorData[inCount] = 0; // null terminate the string
Serial.println(sensorData);
delay(2000UL); // delay 2s
//sensorData[99] = '\0';
if (sensorData[0] == BEGINNER){
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=44&client=111" ) );
for(i=0; client_ip[i]!='\0'; i++){
buf[TCP_DATA_P+plen]=client_ip[i];
plen++;
}
plen= es.ES_fill_tcp_data_p(buf,plen, PSTR ( "&status=" ) );
for(i=0; sensorData[i]!='\0'; i++){
buf[TCP_DATA_P+plen]=sensorData[i];
plen++;
}
//sensorData[0] = '\0'; // 12-10-2011 erbij gemaakt
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.2.53\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;
}
void client_process ( void )
{
uint16_t plen;
uint8_t i;
if (client_data_ready == 0) return;
if(client_state == IDLE){ // initialize 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);
if ( plen!=0 )
{
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) {
client_state = IDLE;
client_data_ready =0;
syn_ack_timeout=0;
return;
}
}
if(client_state == ARP_REPLY){
for(i=0; i<6; i++){
dest_mac[i] = buf[ETH_SRC_MAC+i];
}
es.ES_tcp_client_send_packet (
buf,
80,
1200,
TCP_FLAG_SYN_V,
1,
1,
0,
0,
dest_mac,
dest_ip
);
client_state = SYNC_SENT;
}
if(client_state == SYNC_SENT){
plen = es.ES_enc28j60PacketReceive(BUFFER_SIZE, buf);
// no new packet incoming
if ( plen == 0 )
{
return;
}
if ( es.ES_eth_type_is_ip_and_my_ip(buf,plen)==0){
return;
}
if ( buf [ TCP_FLAGS_P ] == ( TCP_FLAG_SYN_V | TCP_FLAG_ACK_V ) )
{
// send ACK to answer SYNACK
es.ES_tcp_client_send_packet (
buf,
80,
1200,
TCP_FLAG_ACK_V,
0,
0,
1,
0,
dest_mac,
dest_ip
);
plen = gen_client_request( buf );
es.ES_tcp_client_send_packet (
buf,
80,
1200,
TCP_FLAG_ACK_V | TCP_FLAG_PUSH_V,
0,
0,
0,
plen,
dest_mac,
dest_ip
);
return;
}
if ( buf [ TCP_FLAGS_P ] == (TCP_FLAG_ACK_V|TCP_FLAG_PUSH_V) )
{
plen = es.ES_tcp_get_dlength( (uint8_t*)&buf );
es.ES_tcp_client_send_packet (
buf,
80,
1200,
TCP_FLAG_ACK_V,
0,
0,
plen,
0,
dest_mac,
dest_ip
);
;
// send finack to disconnect from web server
es.ES_tcp_client_send_packet (
buf,
80,
1200,
TCP_FLAG_FIN_V|TCP_FLAG_ACK_V,
0,
0,
0,
0,
dest_mac,
dest_ip
);
return;
}
if ( buf [ TCP_FLAGS_P ] == (TCP_FLAG_ACK_V|TCP_FLAG_FIN_V) )
{
es.ES_tcp_client_send_packet(
buf,
80,
1200,
TCP_FLAG_ACK_V,
0,
0,
1,
0,
dest_mac,
dest_ip
);
client_state = IDLE;
client_data_ready =0;
}
}
}
This code works good for a couple of hours. After that it hangs and doesn't send data to the PHP script anymore.
If I press the reset switch on the Ekitzone Ethershield (I think the Arduino is also getting reset then) it works good again.
I'm looking for an explanation, but I don't get anywhere... how is this possible? A memory leak or something?
Normally the other (sending) Arduino is spitting out the serial string once a minute. The string looks like:
*yya1ab1bc0cd65.34de23.99ef23.09fg6666gh34.33hi13.85ij17jk11kl12lm30mn45no56op2pq1qr23rs10st22tu8uv12vw59wx11x[8[]7]zzz/
The Arduino what were talking about has to receive this via UART, and pass it to my Ethershield.
For hours this goes very well... the MySQL database inserts a couple of hundred rows, and then again it all hangs and the script isn't being triggered anymore...
So there is something terrible wrong with the code above, or the hardware (2 ethershields). I think the (my) code above
but WHAT?
Anyone suggestions?
Thanks in advance,
Atmoz