Writing code to show sensor values to be displayed via Ethernet shield to internet

Hi everyone
I would like to measure the water level in my tanks using a HC-SRO4 sensor. If I connect it to the Arduino UNO and read from the serial port, I get the measurement displayed on my PC ok.
ie. Distance to Target is 9.03 Centimeters

However, the tanks are some distance from the house so I need to use the Ethernet to get back to the router via Cat 5e cable.
(I used an example for my sketch) shown below.



#include <Ethernet.h>

#include <Ethernet.h>

#include <b64.h>
#include <HttpClient.h>

#include <Ethernet.h>

/*
  Web Server

 A simple web server that shows the value of the analog input pins.
 using an Arduino Wiznet Ethernet shield.

 Circuit:
 * Ethernet shield attached to pins 10, 11, 12, 13
 * Analog inputs attached to pins A0 through A5 (optional)

 created 18 Dec 2009
 by David A. Mellis
 modified 9 Apr 2012
 by Tom Igoe
 modified 02 Sept 2015
 by Arturo Guadalupi
 
 */

#include <SPI.h>
#include <Ethernet.h>
// Enter 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, 0, 106);

// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);

void setup() {Ethernet.init(10);
 
   // You can use Ethernet.init(pin) to configure the CS pin
  //Ethernet.init(10);  // Most Arduino shields
  //Ethernet.init(5);   // MKR ETH shield
  //Ethernet.init(0);   // Teensy 2.0
  //Ethernet.init(20);  // Teensy++ 2.0
  //Ethernet.init(15);  // ESP8266 with Adafruit Featherwing Ethernet
  //Ethernet.init(33);  // ESP32 with Adafruit Featherwing Ethernet

  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
  Serial.println("Ethernet WebServer");

  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip);

  // Check for Ethernet hardware present
  if (Ethernet.hardwareStatus() == EthernetNoHardware) {
    Serial.println("Ethernet shield was not found.  Sorry, can't run without hardware. :(");
    while (true) {
      delay(1); // do nothing, no point running without Ethernet hardware
    }
  }
  if (Ethernet.linkStatus() == LinkOFF) {
    Serial.println("Ethernet cable is not connected.");
  }

  // start the server
  server.begin();
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());
}


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 = client.read();
        Serial.write(c);
        // if you've gotten to the end of the line (received 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
          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("Refresh: 5");  // refresh the page automatically every 5 sec
          client.println();
          client.println("<!DOCTYPE HTML>");
          client.println("<html>");
          // output the value of each analog input pin
          for (int digitalChannel = 4; digitalChannel < 9; digitalChannel++) {
            int sensorReading = digitalRead(5);
            client.print("digital output");
            client.print(digitalChannel);
            client.print(" is ");
            client.print(sensorReading);
            client.println("<br />");
          }
          client.println("</html>");
          break;
        }
        if (c == '\n') {
          // you're starting a new line
          currentLineIsBlank = true;
        } else if (c != '\r') {
          // you've gotten a character on the current line
          currentLineIsBlank = false;
        }
      }
    }
    // give the web browser time to receive the data
    delay(1);
    // close the connection:
    client.stop();
    Serial.println;
  }
}

This results in the serial port displaying: ("client disconnected")
new client
GET/HTTP /1.1
Host: 192.168.0.106
I was unable to copy the rest but it seems the ethernet shield is working.
If I type the IP address of the shield (192.168.0.106 into my phone or PC I get the following:
Digital output4 is 0
Digital output5 is 0
Digital output6 is 0
Digital output7 is 0
Digital output8 is 0

Which is I guess what I asked for!

My problem is that as I can only load 1 sketch at a time, I probably need to combine the two sketches. I have spent weeks trying to get this to work so any help would be greatly appreciated.
Many thanks muso

Sorry, I meant to include that. The code came from a utube tutorial by "Top Tech Boy" and it seems to work, I may not have explained my problem all that well, but it seems to come down to (as far as I can understand as a novice in the field) to the fact that I have 2 programs that seem to work? but I don't know enough to combine them. My understanding is that the Arduino UNO can only read one sketch at a time. However I my be wrong.


int trigPin = 6;
int echoPin = 5;
int pingTravelTime;
float pingTravelDistance;
float distanceToTarget;


void setup() {
  // put your setup code here, to run once:
  pinMode (trigPin,OUTPUT);
  pinMode (echoPin,INPUT);
  Serial.begin(9600);

}

void loop() {
  // put your main code here, to run repeatedly:
  digitalWrite(trigPin, LOW);
  delayMicroseconds (10);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds (10);
  digitalWrite(trigPin, LOW);
  pingTravelTime = pulseIn(echoPin,HIGH);
  delay(25);
  pingTravelDistance=(pingTravelTime *0.034);
  distanceToTarget=pingTravelDistance/2;
  Serial.print("Distance to Target is:");
  Serial.print(distanceToTarget);
  Serial.println("centimeters");
  
  
 
  Serial.println(pingTravelTime);
  delay(1000);
  
}

You just need to merge the two sketches. Put the "#include" lines and global variable definitions at the top. Then merge the two 'setup()' functions and two 'loop()' functions into one function each.

#include <Ethernet.h>
// #include <b64.h>

int trigPin = 6;
int echoPin = 5;

int pingTravelTime;
float pingTravelDistance;
float distanceToTarget;

/*
  Web Server

  A simple web server that shows the value of the analog input pins.
  using an Arduino Wiznet Ethernet shield.

  Circuit:
   Ethernet shield attached to pins 10, 11, 12, 13
   Analog inputs attached to pins A0 through A5 (optional)

  created 18 Dec 2009
  by David A. Mellis
  modified 9 Apr 2012
  by Tom Igoe
  modified 02 Sept 2015
  by Arturo Guadalupi

*/

#include <SPI.h>
#include <Ethernet.h>
// Enter 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, 0, 106);

// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);

void setup()
{
  Ethernet.init(10);

  // You can use Ethernet.init(pin) to configure the CS pin
  //Ethernet.init(10);  // Most Arduino shields
  //Ethernet.init(5);   // MKR ETH shield
  //Ethernet.init(0);   // Teensy 2.0
  //Ethernet.init(20);  // Teensy++ 2.0
  //Ethernet.init(15);  // ESP8266 with Adafruit Featherwing Ethernet
  //Ethernet.init(33);  // ESP32 with Adafruit Featherwing Ethernet

  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial)
  {
    ; // wait for serial port to connect. Needed for native USB port only
  }
  Serial.println("Ethernet WebServer");

  // put your setup code here, to run once:
  pinMode (trigPin, OUTPUT);
  pinMode (echoPin, INPUT);

  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip);

  // Check for Ethernet hardware present
  if (Ethernet.hardwareStatus() == EthernetNoHardware)
  {
    Serial.println("Ethernet shield was not found.  Sorry, can't run without hardware. :(");
    while (true)
    {
      delay(1); // do nothing, no point running without Ethernet hardware
    }
  }
  if (Ethernet.linkStatus() == LinkOFF)
  {
    Serial.println("Ethernet cable is not connected.");
  }

  // start the server
  server.begin();
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());
}


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 = client.read();
        Serial.write(c);
        // if you've gotten to the end of the line (received 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
          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("Refresh: 5");  // refresh the page automatically every 5 sec
          client.println();
          client.println("<!DOCTYPE HTML>");
          client.println("<html>");

          // Send HTML page contents
          digitalWrite(trigPin, LOW);
          delayMicroseconds (10);
          digitalWrite(trigPin, HIGH);
          delayMicroseconds (10);
          digitalWrite(trigPin, LOW);
          pingTravelTime = pulseIn(echoPin, HIGH);
          delay(25);
          pingTravelDistance = (pingTravelTime * 0.034);
          distanceToTarget = pingTravelDistance / 2;
          client.print("Distance to Target is:");
          client.print(distanceToTarget);
          client.println("centimeters");
          client.println(pingTravelTime);

          client.println("</html>");
          break;
        }
        if (c == '\n')
        {
          // you're starting a new line
          currentLineIsBlank = true;
        }
        else if (c != '\r')
        {
          // you've gotten a character on the current line
          currentLineIsBlank = false;
        }
      }
    }
    // give the web browser time to receive the data
    delay(1);
    // close the connection:
    client.stop();
    Serial.println();
  }
}

Hi John, thank you very much for taking the time to reply to my question. I have implemented the
changes you suggested and most of the sketch seems to compile, however about 11 lines from the bottom, it flags the line if (c =='n') It gives me the message "c is not defined in this scope"
I have tried moving it to various parts of the sketch but to no avail. Any ideas. I should add the 6 weeks ago I had never heard of Arduino. I just saw a need to solve a problem I had.

  

#include <Ethernet.h>

#include <Ethernet.h>

#include <b64.h>
#include <HttpClient.h>

#include <Ethernet.h>
int trigPin=6;
int echoPin=5;
int pingTravelTime;
float pingTravelDistance;
float distanceToTarget;


/*
  Web Server

 A simple web server that shows the value of the analog input pins.
 using an Arduino Wiznet Ethernet shield.

 Circuit:
 * Ethernet shield attached to pins 10, 11, 12, 13
 * Analog inputs attached to pins A0 through A5 (optional)

 created 18 Dec 2009
 by David A. Mellis
 modified 9 Apr 2012
 by Tom Igoe
 modified 02 Sept 2015
 by Arturo Guadalupi
 
 */

#include <SPI.h>
#include <Ethernet.h>
// Enter 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, 0, 106);

// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);

void setup() {Ethernet.init(10);
 
   // You can use Ethernet.init(pin) to configure the CS pin
  //Ethernet.init(10);  // Most Arduino shields
  //Ethernet.init(5);   // MKR ETH shield
  //Ethernet.init(0);   // Teensy 2.0
  //Ethernet.init(20);  // Teensy++ 2.0
  //Ethernet.init(15);  // ESP8266 with Adafruit Featherwing Ethernet
  //Ethernet.init(33);  // ESP32 with Adafruit Featherwing Ethernet

  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
  Serial.println("Ethernet WebServer");
  
  //put your setup code here to run once:
  pinMode (trigPin,OUTPUT);
  pinMode (echoPin,INPUT);
 
// start the Ethernet connection and the server:
  Ethernet.begin(mac, ip);

  // Check for Ethernet hardware present
  if (Ethernet.hardwareStatus() == EthernetNoHardware) {
    Serial.println("Ethernet shield was not found.  Sorry, can't run without hardware. :(");
    while (true) {
      delay(1); // do nothing, no point running without Ethernet hardware
    }
  }
  if (Ethernet.linkStatus() == LinkOFF) {
    Serial.println("Ethernet cable is not connected.");
  }

  // start the server
  server.begin();
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());
}


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 = client.read();
        Serial.write(c);
        
        // if you've gotten to the end of the line (received 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
          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("Refresh: 5");  // refresh the page automatically every 5 sec
          client.println();
          client.println("<!DOCTYPE HTML>");
          client.println("<html>");
          
          // Send HTML page contents
          digitalWrite (trigPin,LOW);
          delayMicroseconds(10);
          digitalWrite (trigPin,HIGH);
          delayMicroseconds(10);
          digitalWrite (trigPin,LOW);
          pingTravelTime = pulseIn (echoPin, HIGH);
          delay(25);
          pingTravelDistance = (pingTravelTime * 0.034);
          distanceToTarget = pingTravelDistance / 2;
          client.print("Distance to Target is:");
          client.print (distanceToTarget);
          client.println("centimeters");
          client.println(pingTravelTime);
          }
          client.println("</html>");
          break;
        }
       ***if(c == '\n')***  Exit status 1. 'c' was not declared in this scope
       {
         
          // you're starting a new line
          currentLineIsBlank = true;
       
        } else if (c != '\r') 
        {
          // you've gotten a character on the current line
          currentLineIsBlank = false;
        }
      }
    }
    // give the web browser time to receive the data
    delay(1);
    // close the connection:
    client.stop();
    Serial.println("client disconnected");
  }
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.