0

I'm trying to send some http requests from Arduino to my server.

My code can upload once, but is disconnecting after uploading first time.

Any idea how I can modify my code so the Arduino will send a http request every 2-3 second?

Here is my code:

#include <SPI.h>
#include <Ethernet.h>

int counter = 0;

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress server(xxx, xx, xx, xx); 
IPAddress ip(192, 168, 1, 1);
EthernetClient client;

void setup() { //connect to server
  Serial.begin(9600);

  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    Ethernet.begin(mac, ip);
  }

  // give the Ethernet shield a second to initialize:
  delay(1000);
  Serial.println("connecting...");
  connect();
}

void loop()
{

    send(counter);
    delay(1000);
  // if there are incoming bytes available 
  // from the server, read them and print them:
  if (client.available()) {
    char c = client.read();
    Serial.print(c);
  }

  // if the server's disconnected, stop the client:
  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
    // do nothing forevermore:
    while(true); 
  }
  counter++;
}

void connect(){
// if you get a connection, report back via serial:
  client.connect(server, 80);
  Serial.println("CONNECTED");
  delay(1000);
}

void send(int value){
      // Make a HTTP request:
    client.print("GET /arduino.php?rom=D201");
    client.print("&count=");
    client.print(value);
    client.println(" HTTP/1.0");
    client.println("Host: 158.36.70.36");
    client.println("Connection: close");
    client.println();
    Serial.print("Sending value");
    Serial.println(value);
}
2
  • Connect, send and disconnect in a loop? Commented Oct 21, 2014 at 7:39
  • @JoachimPileborg, I've tried with connect() right before send(counter). But that didnt work neither -.- Commented Oct 21, 2014 at 7:46

1 Answer 1

1

This Web client sketch has been tested and works flawlessly.

#include <SPI.h>
#include <Ethernet.h>

// this must be unique
byte mac[] = {  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

// change to your network settings
IPAddress ip(192,168,2,2);
IPAddress gateway(192, 168, 2, 1);
IPAddress subnet(255, 255, 255, 0);

// change to your server
IPAddress server(74,125,227,16); // Google

//Change to your domain name for virtual servers
char serverName[] = "www.google.com";
// If no domain name, use the ip address above
// char serverName[] = "74.125.227.16";

// change to your server's port
int serverPort = 80;

EthernetClient client;
int totalCount = 0;
char pageAdd[64];

// set this to the number of milliseconds delay
// this is 30 seconds
#define delayMillis 30000UL

unsigned long thisMillis = 0;
unsigned long lastMillis = 0;

void setup() {
  Serial.begin(9600);

  // disable SD SPI
  pinMode(4,OUTPUT);
  digitalWrite(4,HIGH);

  // Start ethernet
  Serial.println(F("Starting ethernet..."));
  Ethernet.begin(mac, ip, gateway, gateway, subnet);

  // If using dhcp, comment out the line above 
  // and uncomment the next 2 lines

  // if(!Ethernet.begin(mac)) Serial.println(F("failed"));
  // else Serial.println(F("ok"));

  Serial.println(Ethernet.localIP());

  delay(2000);
  Serial.println(F("Ready"));
}

void loop()
{
  thisMillis = millis();

  if(thisMillis - lastMillis > delayMillis)
  {
    lastMillis = thisMillis;

    // Modify next line to load different page
    // or pass values to server
    sprintf(pageAdd,"/",totalCount);

    // sprintf(pageAdd,"/arduino.php?test=%u",totalCount);

    if(!getPage(server,serverPort,pageAdd)) Serial.print(F("Fail "));
    else Serial.print(F("Pass "));
    totalCount++;
    Serial.println(totalCount,DEC);
  }    
}

byte getPage(IPAddress ipBuf,int thisPort, char *page)
{
  int inChar;
  char outBuf[128];

  Serial.print(F("connecting..."));

  if(client.connect(ipBuf,thisPort) == 1)
  {
    Serial.println(F("connected"));

    sprintf(outBuf,"GET %s HTTP/1.1",page);
    client.println(outBuf);
    sprintf(outBuf,"Host: %s",serverName);
    client.println(outBuf);
    client.println(F("Connection: close\r\n"));
  } 
  else
  {
    Serial.println(F("failed"));
    return 0;
  }

  // connectLoop controls the hardware fail timeout
  int connectLoop = 0;

  while(client.connected())
  {
    while(client.available())
    {
      inChar = client.read();
      Serial.write(inChar);
      // set connectLoop to zero if a packet arrives
      connectLoop = 0;
    }

    connectLoop++;

    // if more than 10000 milliseconds since the last packet
    if(connectLoop > 10000)
    {
      // then close the connection from this end.
      Serial.println();
      Serial.println(F("Timeout"));
      client.stop();
    }
    // this is a delay for the connectLoop timing
    delay(1);
  }

  Serial.println();

  Serial.println(F("disconnecting."));
  // close client end
  client.stop();

  return 1;
}

If for some reason it keeps hanging, you can implement a watchdog timer mechanism. For more info about WDT on ATmega328P Datasheet. I have an example from one of my projects:

void watchdogSetup(void) {
cli(); // disable all interrupts
wdt_reset(); // reset the WDT timer
/*
WDTCSR configuration:
WDIE = 1: Interrupt Enable
WDE = 1 :Reset Enable
WDP3 = 0 :For 2000ms Time-out
WDP2 = 1 :For 2000ms Time-out
WDP1 = 1 :For 2000ms Time-out
WDP0 = 1 :For 2000ms Time-out
*/
// Enter Watchdog Configuration mode:
WDTCSR |= (1<<WDCE) | (1<<WDE); //x |= y is the same as  x = x | y
// Set Watchdog settings:
WDTCSR = (1<<WDIE) | (1<<WDE) | (0<<WDP3) | (1<<WDP2) | (1<<WDP1) | (1<<WDP0);
sei();
}

ISR(WDT_vect){// Watchdog timer interrupt.
  digitalWrite(ETHERNET_SHIELD_RESET_PIN, LOW); 
}
Sign up to request clarification or add additional context in comments.

3 Comments

what is ETHERNET_SHIELD_RESET_PIN defined as? I do not see a #define for it in the library, nor do I see IO on the shield routed to a reset of the W5100.
Sometimes the when the WDT resets the board the Ethernet shield doesnt. So i had to make a 'hack'. I cross - wired a hardware reset input to a digital output on the board itself. Just before the board resets, it triggers the interrupt, the digital output is set to LOW and the ethernet shield resets. Then the board resets. So EHTERNET_SHIELD_RESET_PIN is just a digital pin
The typical Ethernet shield has a CAT811T that an AND of the AVR's reset and 3.3V level detector. To cause reset of the W5100. However, looking at the datasheet of the 328 Section 10.6, I see that the external RESET is only an input, and WD only affects internal reset. Hence your 'hack'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.