Ethernet shield + sensor(s)

I am struggling with an Ethernet shield ( 28j60) which I want to work as a server
and showing the readings of a number of sensors. I have found somewhere here
a scetch that I have modified a bit and OK it works fine , lets me click buttons and
light up or switch out three leds and also show the voltage at the A0 pin . So far,
so good . But I have tried a million tricks to have the voltage reading displayed
continously , without me having to click any buttons … now I am short of ideas…

Any clue , someone ? If so , a million thanks in advance :slight_smile:

Karl

Here´s the code :

/*  Circuit:
 * Ethernet shield attached to pins 10, 11, 12, 13

 stift 2... också ?
 
 
 
 
 there are defacto two libraries: The EtherShield library and the EtherCard library.
The Ethershield library uses pin 10 as a chipselect
The EtherCard library uses pin 8 as a chipselect
The reason for this change  is to be able to use the Ethercard together with an SD card.

If you buy a module, rather than a shield, it is very easy to determine what chipselect pin you want to use.
If you use the older EtherShield library, use pin 10 as CS, if you use the somewhat newer Ethercard library, use  pin 8.

*/

#include <EtherCard.h>

// Ethernet Part declaration
static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };
#define BUFFER_SIZE 1000
BufferFiller bfill;
byte Ethernet::buffer[BUFFER_SIZE];
static byte myip[] = {192,168,0,3};

// Ethernet Part ends here

// Diodepins definition

const int ledPin1 = 3;
const int ledPin2 = 4;
const int ledPin3 = 5;

//Volt meter start
//Analog volt read pin
const int voltPin = 0;
float Readvalue;

char zxc[5];

long timer;

String test;


const char http_Unauthorized2[] PROGMEM =
    "HTTP/1.0 401 Unauthorized\r\n"
    "Content-Type: text/html\r\n\r\n"
    "<h1>401 Unauthorized</h1>";



void setup () {
 /*    sök efter ip-adress till Arduinon    */
 
  Serial.begin(9600);
   Serial.println("\n[Starting the Arduino Server]");

  Serial.print("MAC: ");
  for (byte i = 0; i < 6; ++i) {
    Serial.print(mymac[i], HEX);
    if (i < 5)
      Serial.print(':');
  }
  Serial.println();
  
  if (ether.begin(sizeof Ethernet::buffer, mymac) == 0) 
    Serial.println( "Failed to access Ethernet controller");

  Serial.println("Setting up DHCP (this can take a while...)");
  if (!ether.dhcpSetup())
    Serial.println( "DHCP failed");
  
  ether.printIp("IP: ", ether.myip);
  ether.printIp("Netmask: ", ether.mymask);
  ether.printIp("GW IP: ", ether.gwip);
  ether.printIp("DNS IP: ", ether.dnsip);
  ether.printIp("Surf to this IP to view the Arduino Server: ", ether.myip);


 myip ==ether.myip;
 
 
 /*    initierad ...*/
 
 
 
//Ethernet Debugging
 
ether.begin(sizeof Ethernet::buffer, mymac);

ether.staticSetup(myip);

//Ethernet Debugging Ends Here



// Turn off all diodes

  pinMode(ledPin1, OUTPUT);
  digitalWrite(ledPin1, LOW);
  pinMode(ledPin2, OUTPUT);
  digitalWrite(ledPin3, LOW);
  pinMode(ledPin3, OUTPUT);
  digitalWrite(ledPin3, LOW);
}


void loop() {
  //checking for requesting client
  word len = ether.packetReceive();
  word pos = ether.packetLoop(len);
  
  
 
  if (millis() - timer >= 5000)
  

  {
        
       timer=millis();
       Readvalue=VoltageMeasure();
        Serial.println(Readvalue);
         
        bfill = ether.tcpOffset();
     
       voltage();
     
       delay(100);
      
 
        
     }
 
  if(pos)               //   pos sätts så fort websidan sänt info till servern .. 
  
  {
    
     Serial.println("---------- NEW PACKET ----------");
    Serial.println((char *)Ethernet::buffer + pos);
    Serial.println("--------------------------------");
    
    
    bfill = ether.tcpOffset();
   
    //storing buffer on data variable
    char *data = (char *) Ethernet::buffer + pos;
   
    if (strncmp("GET /", data, 5) != 0) {
            // Unsupported HTTP request
            // 304 or 501 response would be more appropriate
             
             http_Unauthorized();
        }
    else {
        data += 5;
           
            if (data[0] == ' ') {
                // Return home page
                homepage();
            }
            else if (strncmp("?cmd=Red ", data, 8) == 0) {
               homepage();
               
               digitalWrite(ledPin1, HIGH);      // Red diode on
                digitalWrite(ledPin2, LOW);
                 digitalWrite(ledPin3, LOW);


            }
            else if (strncmp("?cmd=Yellow ", data, 11) == 0) {
              homepage();
              
                digitalWrite(ledPin1, LOW);
                  digitalWrite(ledPin2, HIGH);    // Yellow diode on
                 digitalWrite(ledPin3, LOW);
            }
            else if (strncmp("?cmd=Green ", data, 10) == 0) {
                homepage();
               
                 digitalWrite(ledPin1, LOW);
                  digitalWrite(ledPin2, LOW);
                 digitalWrite(ledPin3, HIGH);     // Green diode on
              
            }
            else if (strncmp("?cmd=Off ", data, 8) == 0) {
                homepage();
               
                 digitalWrite(ledPin1, LOW);       // All off
                  digitalWrite(ledPin2, LOW);
                 digitalWrite(ledPin3, LOW);
            
            }
             else if (strncmp("?cmd=Volt ", data, 9) == 0) {
                homepage();
             
               Serial.println("-----");
             
            
            }
            else {
                // Page not found
                http_Unauthorized();
            }
            delay(1000);
           
        }
       
        
     ether.httpServerReply(bfill.position());
    len ==0;
  }
}

float VoltageMeasure()

{
  float voltage;
  //volt meter start

//Obtain RAW voltage data
voltage = analogRead(voltPin);
//Convert to actual voltage (0 - 5 Vdc)
voltage = (voltage / 1024) * 5.0;
return voltage;
}


static word Clock() {
  long t = millis() / 1000;
  word h = t / 3600;
  byte m = (t / 60) % 60;
  byte s = t % 60;
  bfill = ether.tcpOffset();
  bfill.emit_p(PSTR(
    "HTTP/1.0 200 OK\r\n"
    "Content-Type: text/html\r\n"
    "Pragma: no-cache\r\n"
    "\r\n"
    "<meta http-equiv='refresh' content='1'/>"
    "<title>Arduino Server -> Active Time</title>" 
    "<h1>$D$D:$D$D:$D$D</h1>"),
      h/10, h%10, m/10, m%10, s/10, s%10  );
      
/* 
   Definitioner ..???

   *D = unsigned integer
   %T = Floating point
   %L = long
   %H = hexadecimal
   %S = string
   %F = PROGMEM string
   %E = EEPROM string */

     
  return bfill.position();
}

void homepage(){
      bfill.emit_p(PSTR(
"<!DOCTYPE html><html><title>Ethernet Controller</title>"
"<body><div id='content' style='background-color:lightgreen;width:1100px;height:900px;'><hr><h1><p><center><font size='28'>
Kalles Arduino-website 
test
"
"</p><hr>
<table bgcolor='#58D3F7' border='1'><tr><th colspan='4' >Choose diode :</th></tr>"
"<tr><td><INPUT Type='BUTTON' VALUE='Red ' ONCLICK='window.location.href=\"/?cmd=Red\"'></td><td>"
"<INPUT Type='BUTTON' VALUE='Yellow' ONCLICK='window.location.href=\"/?cmd=Yellow\"'></td>"
"<td><INPUT Type='BUTTON' VALUE='Green' ONCLICK='window.location.href=\"/?cmd=Green\"'></td>"
"<td><INPUT Type='BUTTON' VALUE='Off' ONCLICK='window.location.href=\"/?cmd=Off\"'></td></tr>"
"<td><INPUT Type='BUTTON' VALUE='Voltage' ONCLICK='window.location.href=\"/?cmd=Volt\"'></td></tr>"

 "
Voltage
" 
    "<hr4>$S V</hr4>"), dtostrf(Readvalue,3,2,zxc)
    );



}




void http_Unauthorized(){
      bfill.emit_p(PSTR("HTTP/1.0 401 Unauthorized\r\n"
    "Content-Type: text/html\r\n\r\n"
    "<h1>401 Unauthorized</h1>"   
      ));
}




void voltage(){

      bfill.emit_p(PSTR(
"<!DOCTYPE html><html><title>Ethernet Controller</title>"


 "
Voltage
" 
    "<hr4>$S V</hr4>"), dtostrf(Readvalue,3,2,zxc)
    );
}

Dear Karl,

Use the function delayMicroseconds instead of delay() as this does not halt the interrupts as in delay() function.

For more information please consult the page Arduino - DelayMicroseconds

Adlihajarat,

Grateful for your answer and tht was a good advise ... but unfortunately
that did not change my problem ... i added the instruction homepage() ,
as in the example joined ( inside the loop ) , hoping to achieve the updating
of the voltage every 5 seconds ... but ... nothing happens ... I still have
to push a button on the hompage to get an update ..

 ....

 if (millis() - timer >= 5000)
  

  {
        
       timer=millis();
       Readvalue=VoltageMeasure();
        Serial.println(Readvalue);
         
        bfill = ether.tcpOffset();
     
       voltage();
      homepage();     //   here is the line I added to the original scetch
       delayMicroseconds(100);
      
    .....
        
     }

KalleL:
I have tried a million tricks to have the voltage reading displayed
continously , without me having to click any buttons … now I am short of ideas…

I understand you are displaying a value in the web page and want this value to update automatically. This is a web design issue, not really an Arduino issue. The sensible options to achieve this depend how familiar you are with web development, and how frequently you need to have the value updated. The crudest and simplest option would be to just add a meta refresh tag to the page so that it reloads itself at regular intervals.

I am struggling with an Ethernet shield ( 28j60) which I want to work as a server
and showing the readings of a number of sensors.

Spend $8 for the below Ethernet shield and save yourself a lot of time and trouble.

http://www.ebay.com/itm/1x-Ethernet-Shield-W5100-For-Arduino-Main-Board-2009-UNO-ATMega328-1280-MEGA2560-/261425719620?pt=LH_DefaultDomain_0&hash=item3cde2fe944

Answer to Peter:

Thanks for the advise but as you might have guessed I am not so familiar with
web development ...and I do not know how to make a meta refresh tag...

Zoomkat :

Yes I have one of those as well .. but having troubles with that one too... the
28j60 is the one that has sofar given most of result because my router seems
not wanting to recognise the W5100 ...

<meta http-equiv="refresh" content="5">

Content is number of seconds to wait between refreshes. This can be a bit of a resource hog if you set the refresh interval too fast, especially if you have multiple users online simultaneously.

Thanks billsey … but you might curse me , I am nil at HTML … and
still stuck … including your suggestion like this :

void homepage(){
      bfill.emit_p(PSTR(
"<!DOCTYPE html><html><title>Ethernet Controller</title>"
"<meta http-equiv="refresh"  content="5">"
"<body><div id='content' style='background-color:lightgreen;width:1100px;height:900px;'><hr><h1><p><center><font size='28'>
Kalles Arduino-website 
test
"
"</p><hr>
<table bgcolor='#58D3F7' border='1'><tr><th colspan='4' >Choose diode :</th></tr>"
"<tr><td><INPUT Type='BUTTON' VALUE='Red ' ONCLICK='window.location.href=\"/?cmd=Red\"'></td><td>"
"<INPUT Type='BUTTON' VALUE='Yellow' ONCLICK='window.location.href=\"/?cmd=Yellow\"'></td>"
"<td><INPUT Type='BUTTON' VALUE='Green' ONCLICK='window.location.href=\"/?cmd=Green\"'></td>"
"<td><INPUT Type='BUTTON' VALUE='Off' ONCLICK='window.location.href=\"/?cmd=Off\"'></td></tr>"
"<td><INPUT Type='BUTTON' VALUE='Voltage' ONCLICK='window.location.href=\"/?cmd=Volt\"'></td></tr>"

 "
Voltage
" 
    "<hr4>$S V</hr4>"), dtostrf(Readvalue,3,2,zxc)
   
    );

…returns errror message : "expected ´)´ before ´refresh´ "…

so , I am still stuck…sigh