arduino uno watchdog example

i have implemented a watchdog routine in the following mathlab ino. will someone guide me if this is correct.

#include <SPI.h>
#include <Ethernet.h>
#include <Wire.h>
#include <avr/wdt.h>

//Enetr a MAC address and IP address for your controller below.
// The Ip address will be dependent on your local network

byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,1,100);

char linebuffer[20];
int linenumber = 0;

boolean isTemp =false;

//Temperature Variables
float tempC;
byte byteH;
byte byteL;
int temp;

// Initialize the ethernet server library
// with the ip and and port you want to use
// (port 80 is the default for HTTP)
EthernetServer server(80);

void setup() {
  //open serial communication and wait for port to open:
  pinMode(2, OUTPUT);
  // start etehrnet connection and server
  Ethernet.begin(mac, ip);
void loop() {
  // listen for incoming clients
  EthernetClient client = server.available();
  if (client) {
    Serial.println("new client");
    // an http request ends with a blank line
    boolean currentLineIsBlank =true;
    while (client.connected()) {
      if (client.available()) {
        char c =;
        // if you have gotten to the end of line (recieved a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply
        if (c == '\n' && currentLineIsBlank) {
          // send a standard http response header
          char linebuffer[20]; // clear buffer
          linenumber = 0; // reset buffer
        if (c == '\n') {
          // you are starting a new line
          currentLineIsBlank = true;
        }else if (c != '\r') {
          if(linenumber < 20){//if there is room in the buffer
            linebuffer[linenumber] = c; //Add char to buffer
            linenumber++; //increment counter
          // you have gotten a character on the current line
          currentLineIsBlank = false;
   //give webbrowser time to recieve data
  // close the connection
 Serial.println("client disconnected");

void parseRequest(){
  if(linebuffer[5] == 'O' && linebuffer[6] == 'N'){//if request was for /ON
  // Turn LED on
    isTemp = false;
  digitalWrite(2, HIGH);
  }else if(linebuffer[5] == 'O' && linebuffer[6] == 'F'){ //if request was for /OFF
  // turn led off
    isTemp = false;
  digitalWrite(2, LOW);
  }else if(linebuffer[5] == 'T' && linebuffer[6] == 'M' && linebuffer[7] == 'P'){ // if request was for temp
  // respond with temp
    isTemp = true;
    isTemp = false;

void sendHTTPResponse(EthernetClient client){
  client.println("HTTP/1.1 200 OK");
  client.println("Content-Type: text/html"); 
  client.println("Connection: close"); // the connection will be closed after completion of the response
  client.println("<!DOCTYPE HTML>");
  if(isTemp){ //if request is for temp
    getTempCent(); //get the temp
    client.println(tempC); // send the temp
    client.println("OK"); // else send OK

void getTempCent()
  Wire.requestFrom((0x91 >> 1), 2); // request 2 bytes of data from the temp sensor
  byteH =; // read the higher byte
  byteL =; // read the lower byte
  temp = (byteH << 4); // Bitshift the MSB left
  temp |= (byteL >> 4); // Bitshift the LSB right and logic OR with the MSB
  tempC = (temp*0.0625); //scale from binary range by resolution multiplier to degree celsius

Does it do what you want ?

If not, what does it do ? And what do you want it to do ?


i am using arduino to control a 5v relay module the sketch is running ok but i am not sure if the watchdog is properly implemented.


rajbadri: i am using arduino to control a 5v relay module the sketch is running ok but i am not sure if the watchdog is properly implemented.

Have you some way to force the watchdog to intervene - that seems an essential test. Maybe write another program with the same watchdog code but something that, for example, sends it into an infinite loop.