Soil Moisture Sensor + Multiplexing

This is a problem. Pins 0 and 1 are used by Serial.begin(), so move s0 and s1 to some other, unused pins.

I didn't understand your code, hope this can clarify! !
The sensors are individually plugged to a 5v power supply. Just the signal wire goes to Analog's pins

Hello again

If you're getting 0 then 1023 values, an average would be absolutely useless. Does the sensor read exactly 0 when fully submerged in water? If not, you could only read the value if it is greater than 0.

To be honest, I dont quite understand your schematic.

When you would like to do the same thing over and over in code, you would use a for function. For example, instead of doing this:

    Serial.print(" Percentuale umidita' vaso 1 ");
    Serial.print(" e' : ");
    Serial.println(A0);
    delay(1000);

    Serial.print(" Percentuale umidita' vaso 2 ");
    Serial.print(" e' : ");
    Serial.println(A1);
    delay(1000);

    Serial.print(" Percentuale umidita' vaso 3 ");
    Serial.print(" e' : ");
    Serial.println(A2);
    delay(1000);

You can do this:

for (int i=0;i<16;i++){//i less than 16 to go through all 16 channels
  Serial.print(" Percentuale umidita' vaso " + String(i) + "  e' : ");
  Serial.println(readMux(i));
}

So an example (test) code for reading the values would be:

#include "Arduino.h"
#include <light_CD74HC4067.h>

//Mux control pins
#define s0 D2
#define s1 D3
#define s2 D4
#define s3 D5

//Mux in "SIG" pin
#define SIG_pin = A0;

CD74HC4067 mux(s0, s1, s2, s3);

void setup(){
  //set mux pins to OUTPUT  
  pinMode(s0, OUTPUT); 
  pinMode(s1, OUTPUT); 
  pinMode(s2, OUTPUT); 
  pinMode(s3, OUTPUT); 

  //digitalwrite all the mux pins low
  digitalWrite(s0, LOW);
  digitalWrite(s1, LOW);
  digitalWrite(s2, LOW);
  digitalWrite(s3, LOW);

  //start the serial port for debugging
  Serial.begin(9600);
}


void loop() {
  for (int i=0;i<16;i++){//i less than 16 to go through all 16 channels
    Serial.print(" Percentuale umidita' vaso " + String(i) + "  e' : ");
    Serial.print(readMux(i));
    Serial.println("; Percentage: " + String(map(readMux(i), 0, 5, 0, 100)) + "%");
  }
}

float readMux(int channel){
  int controlPin[] = {s0, s1, s2, s3};

  int muxChannel[16][4]={
    {0,0,0,0}, //channel 0
    {1,0,0,0}, //channel 1
    {0,1,0,0}, //channel 2
    {1,1,0,0}, //channel 3
    {0,0,1,0}, //channel 4
    {1,0,1,0}, //channel 5
    {0,1,1,0}, //channel 6
    {1,1,1,0}, //channel 7
    {0,0,0,1}, //channel 8
    {1,0,0,1}, //channel 9
    {0,1,0,1}, //channel 10
    {1,1,0,1}, //channel 11
    {0,0,1,1}, //channel 12
    {1,0,1,1}, //channel 13
    {0,1,1,1}, //channel 14
    {1,1,1,1}  //channel 15
  };

  //loop through the 4 sig
  for(int i = 0; i < 4; i ++){
    digitalWrite(controlPin[i], muxChannel[channel][i]);
  }

  //read the value at the SIG pin
  int val = analogRead(SIG_pin);

  //return the value
  float voltage = (val * 5.0) / 1024.0;
  return voltage;
}

Take note that the MUX pins are D2, D3, D4, and D5 as #defined at the beginning of the code. Also, SIG_pin is connected to A0. Notice how the readMux() function is used. This function calculates percentage from voltage:
map(readMux(i), 0, 5, 0, 100)

Hope this helps!

1 Like

Man i really appreciate your help, but as output of this i receive a message of error.
"'D2' was not declared in this scope" if i swap digital pins, or 0 too, in my case.

About the schematic
I have 15 sensors plugged from A0 to A14.
5 sensor plugged in C0 C2 C5 C9 C15 pins on the multiplexer.

From the mux to the arduino

    1. S0 to D0
    1. S1 to D1
    1. S2 to D2
    1. S3 to D3
  • the SIGNAL to A15.

+5v from arduino to the mux.
Gnd + En from the mux to arduino.

All the sensors are plugged to a 5v power supply, only the signal wires goes to the arduino (or to the mux, for the others 5 sensors).

If you're still not understanding please let me know it so i will provide more infos.

Ok, I think I understand your connections now :+1:.

So, D0 and D1 on the Arduino are used for the serial port. Those pins are better left alone, unless you are tight on pins.

Connect MUX to Arduino:
VCC - +5V
GND - GND
EN - NO CONNECTION
S0 - D2
S1 - D3
S2 - D4
S3 - D5
SIG - A0

Sensors:
VCC - +5V
GND - GND
OUT - pins C0 through C15 of the MUX

NOTE: Do NOT connect any of the sensors to the analog pins of the Arduino, only attach them to the MUX pins C0 - C15.

Did you select the correct Arduino board in the IDE?
If you did select Mega already and it still wont work, exchange the #defines at the top with this:

#define s0 6
#define s1 7
#define s2 1
#define s3 5
1 Like

The problem is that i have 20 sensors and i wanted all of them running.
I tought taking the most of them onto the arduino board would be better then stick to the mux, considering the noise of the signal that this would generate.
What's your suggestion about this?
I might just use 16 of them, i changed idea because i realized this is starting to be too complicated for me.
I was thinking about just skip the mux part, take the print of all the Analogs, remove the 0 and the 1023 from the equation (so this will fix the "faulty" side of the game) and show a percentage of the averaged result.
This should sound easier doesn't it? :rofl:

Arduino:1.8.19 (Windows Store 1.8.57.0) (Windows 10), Scheda:"Arduino Mega or Mega 2560, ATmega2560 (Mega 2560)"





















sketch_sep29a:5:12: error: 'D2' was not declared in this scope

 #define s0 D2

            ^

C:\Users\Loller12x\Desktop\sketch_sep29a\sketch_sep29a.ino:13:16: note: in expansion of macro 's0'

 CD74HC4067 mux(s0, s1, s2, s3);

                ^~

C:\Users\Loller12x\Desktop\sketch_sep29a\sketch_sep29a.ino:5:12: note: suggested alternative: 'A2'

 #define s0 D2

            ^

C:\Users\Loller12x\Desktop\sketch_sep29a\sketch_sep29a.ino:13:16: note: in expansion of macro 's0'

 CD74HC4067 mux(s0, s1, s2, s3);

                ^~

sketch_sep29a:6:12: error: 'D3' was not declared in this scope

 #define s1 D3

            ^

C:\Users\Loller12x\Desktop\sketch_sep29a\sketch_sep29a.ino:13:20: note: in expansion of macro 's1'

 CD74HC4067 mux(s0, s1, s2, s3);

                    ^~

C:\Users\Loller12x\Desktop\sketch_sep29a\sketch_sep29a.ino:6:12: note: suggested alternative: 'A3'

 #define s1 D3

            ^

C:\Users\Loller12x\Desktop\sketch_sep29a\sketch_sep29a.ino:13:20: note: in expansion of macro 's1'

 CD74HC4067 mux(s0, s1, s2, s3);

                    ^~

sketch_sep29a:7:12: error: 'D4' was not declared in this scope

 #define s2 D4

            ^

C:\Users\Loller12x\Desktop\sketch_sep29a\sketch_sep29a.ino:13:24: note: in expansion of macro 's2'

 CD74HC4067 mux(s0, s1, s2, s3);

                        ^~

C:\Users\Loller12x\Desktop\sketch_sep29a\sketch_sep29a.ino:7:12: note: suggested alternative: 'A4'

 #define s2 D4

            ^

C:\Users\Loller12x\Desktop\sketch_sep29a\sketch_sep29a.ino:13:24: note: in expansion of macro 's2'

 CD74HC4067 mux(s0, s1, s2, s3);

                        ^~

sketch_sep29a:8:12: error: 'D5' was not declared in this scope

 #define s3 D5

            ^

C:\Users\Loller12x\Desktop\sketch_sep29a\sketch_sep29a.ino:13:28: note: in expansion of macro 's3'

 CD74HC4067 mux(s0, s1, s2, s3);

                            ^~

C:\Users\Loller12x\Desktop\sketch_sep29a\sketch_sep29a.ino:8:12: note: suggested alternative: 'A5'

 #define s3 D5

            ^

C:\Users\Loller12x\Desktop\sketch_sep29a\sketch_sep29a.ino:13:28: note: in expansion of macro 's3'

 CD74HC4067 mux(s0, s1, s2, s3);

                            ^~

C:\Users\Loller12x\Desktop\sketch_sep29a\sketch_sep29a.ino: In function 'void setup()':

sketch_sep29a:5:12: error: 'D2' was not declared in this scope

 #define s0 D2

            ^

C:\Users\Loller12x\Desktop\sketch_sep29a\sketch_sep29a.ino:17:11: note: in expansion of macro 's0'

   pinMode(s0, OUTPUT);

           ^~

C:\Users\Loller12x\Desktop\sketch_sep29a\sketch_sep29a.ino:5:12: note: suggested alternative: 'A2'

 #define s0 D2

            ^

C:\Users\Loller12x\Desktop\sketch_sep29a\sketch_sep29a.ino:17:11: note: in expansion of macro 's0'

   pinMode(s0, OUTPUT);

           ^~

sketch_sep29a:6:12: error: 'D3' was not declared in this scope

 #define s1 D3

            ^

C:\Users\Loller12x\Desktop\sketch_sep29a\sketch_sep29a.ino:18:11: note: in expansion of macro 's1'

   pinMode(s1, OUTPUT);

           ^~

C:\Users\Loller12x\Desktop\sketch_sep29a\sketch_sep29a.ino:6:12: note: suggested alternative: 'A3'

 #define s1 D3

            ^

C:\Users\Loller12x\Desktop\sketch_sep29a\sketch_sep29a.ino:18:11: note: in expansion of macro 's1'

   pinMode(s1, OUTPUT);

           ^~

sketch_sep29a:7:12: error: 'D4' was not declared in this scope

 #define s2 D4

            ^

C:\Users\Loller12x\Desktop\sketch_sep29a\sketch_sep29a.ino:19:11: note: in expansion of macro 's2'

   pinMode(s2, OUTPUT);

           ^~

C:\Users\Loller12x\Desktop\sketch_sep29a\sketch_sep29a.ino:7:12: note: suggested alternative: 'A4'

 #define s2 D4

            ^

C:\Users\Loller12x\Desktop\sketch_sep29a\sketch_sep29a.ino:19:11: note: in expansion of macro 's2'

   pinMode(s2, OUTPUT);

           ^~

sketch_sep29a:8:12: error: 'D5' was not declared in this scope

 #define s3 D5

            ^

C:\Users\Loller12x\Desktop\sketch_sep29a\sketch_sep29a.ino:20:11: note: in expansion of macro 's3'

   pinMode(s3, OUTPUT);

           ^~

C:\Users\Loller12x\Desktop\sketch_sep29a\sketch_sep29a.ino:8:12: note: suggested alternative: 'A5'

 #define s3 D5

            ^

C:\Users\Loller12x\Desktop\sketch_sep29a\sketch_sep29a.ino:20:11: note: in expansion of macro 's3'

   pinMode(s3, OUTPUT);

           ^~

C:\Users\Loller12x\Desktop\sketch_sep29a\sketch_sep29a.ino: In function 'float readMux(int)':

sketch_sep29a:5:12: error: 'D2' was not declared in this scope

 #define s0 D2

            ^

C:\Users\Loller12x\Desktop\sketch_sep29a\sketch_sep29a.ino:42:23: note: in expansion of macro 's0'

   int controlPin[] = {s0, s1, s2, s3};

                       ^~

C:\Users\Loller12x\Desktop\sketch_sep29a\sketch_sep29a.ino:5:12: note: suggested alternative: 'A2'

 #define s0 D2

            ^

C:\Users\Loller12x\Desktop\sketch_sep29a\sketch_sep29a.ino:42:23: note: in expansion of macro 's0'

   int controlPin[] = {s0, s1, s2, s3};

                       ^~

sketch_sep29a:6:12: error: 'D3' was not declared in this scope

 #define s1 D3

            ^

C:\Users\Loller12x\Desktop\sketch_sep29a\sketch_sep29a.ino:42:27: note: in expansion of macro 's1'

   int controlPin[] = {s0, s1, s2, s3};

                           ^~

C:\Users\Loller12x\Desktop\sketch_sep29a\sketch_sep29a.ino:6:12: note: suggested alternative: 'A3'

 #define s1 D3

            ^

C:\Users\Loller12x\Desktop\sketch_sep29a\sketch_sep29a.ino:42:27: note: in expansion of macro 's1'

   int controlPin[] = {s0, s1, s2, s3};

                           ^~

sketch_sep29a:7:12: error: 'D4' was not declared in this scope

 #define s2 D4

            ^

C:\Users\Loller12x\Desktop\sketch_sep29a\sketch_sep29a.ino:42:31: note: in expansion of macro 's2'

   int controlPin[] = {s0, s1, s2, s3};

                               ^~

C:\Users\Loller12x\Desktop\sketch_sep29a\sketch_sep29a.ino:7:12: note: suggested alternative: 'A4'

 #define s2 D4

            ^

C:\Users\Loller12x\Desktop\sketch_sep29a\sketch_sep29a.ino:42:31: note: in expansion of macro 's2'

   int controlPin[] = {s0, s1, s2, s3};

                               ^~

sketch_sep29a:8:12: error: 'D5' was not declared in this scope

 #define s3 D5

            ^

C:\Users\Loller12x\Desktop\sketch_sep29a\sketch_sep29a.ino:42:35: note: in expansion of macro 's3'

   int controlPin[] = {s0, s1, s2, s3};

                                   ^~

C:\Users\Loller12x\Desktop\sketch_sep29a\sketch_sep29a.ino:8:12: note: suggested alternative: 'A5'

 #define s3 D5

            ^

C:\Users\Loller12x\Desktop\sketch_sep29a\sketch_sep29a.ino:42:35: note: in expansion of macro 's3'

   int controlPin[] = {s0, s1, s2, s3};

                                   ^~

sketch_sep29a:11:17: error: expected primary-expression before '=' token

 #define SIG_pin = A15;

                 ^

C:\Users\Loller12x\Desktop\sketch_sep29a\sketch_sep29a.ino:69:24: note: in expansion of macro 'SIG_pin'

   int val = analogRead(SIG_pin);

                        ^~~~~~~

sketch_sep29a:69:31: error: expected primary-expression before ')' token

   int val = analogRead(SIG_pin);

                               ^

exit status 1

'D2' was not declared in this scope.

Ah, I forgot you wanted to do 20 sensors. So, I would attach 15 of them to the Mega's analog pins like you already have I think, and I would hookup the other 5 to the mux.
Wire 5 of the sensors to the first 5 C# pins of the MUX.

One of those errors was my fault :face_with_diagonal_mouth:.

Try this:

#include "Arduino.h"
#include <light_CD74HC4067.h>

//Mux control pins
#define s0 6 //D2
#define s1 7 //D3
#define s2 1 //D4
#define s3 5 //D5

//soil moisture sensor pins
struct {
  int pin;
  int value;
} sensor[15];

//Mux in "SIG" pin
#define SIG_pin A15

CD74HC4067 mux(s0, s1, s2, s3);

void setup(){
  //set mux pins to OUTPUT  
  pinMode(s0, OUTPUT); 
  pinMode(s1, OUTPUT); 
  pinMode(s2, OUTPUT); 
  pinMode(s3, OUTPUT); 

  //set sensor pins
  sensor[0].pin = A0;
  sensor[1].pin = A1;
  sensor[2].pin = A2;
  sensor[3].pin = A3;
  sensor[4].pin = A4;
  sensor[5].pin = A5;
  sensor[6].pin = A6;
  sensor[7].pin = A7;
  sensor[8].pin = A8;
  sensor[9].pin = A9;
  sensor[10].pin = A10;
  sensor[11].pin = A11;
  sensor[12].pin = A12;
  sensor[13].pin = A13;
  sensor[14].pin = A14;

  //set soil sensor pins to INPUT
  for (int i=0;i<15;i++){
    pinMode(sensor[i].pin, INPUT);
  }

  //set MUX signal pin to INPUT
  pinMode(SIG_pin, INPUT);
  
  //digitalwrite all the mux pins low
  digitalWrite(s0, LOW);
  digitalWrite(s1, LOW);
  digitalWrite(s2, LOW);
  digitalWrite(s3, LOW);

  //start the serial port for debugging
  Serial.begin(9600);
}


void loop() {
  for (int i=0;i<5;i++){//i less than 16 to go through all 16 channels
    Serial.print(" Percentuale umidita' vaso " + String(i) + "  e' : ");
    Serial.print(readMux(i));
    Serial.println("; Percentage: " + String(map(readMux(i), 0, 5, 0, 100)) + "%");
  }
  for (int i=0;i<15;i++){
    Serial.print(" Percentuale umidita' vaso " + String(i+5) + "  e' : ");
    Serial.print(analogRead(sensor[i].pin));
    Serial.println("; Percentage: " + String(map(analogRead(sensor[i].pin), 0, 5, 0, 100)) + "%");
  }
  delay(500);
}

float readMux(int channel){
  int controlPin[] = {s0, s1, s2, s3};

  int muxChannel[16][4]={
    {0,0,0,0}, //channel 0
    {1,0,0,0}, //channel 1
    {0,1,0,0}, //channel 2
    {1,1,0,0}, //channel 3
    {0,0,1,0}, //channel 4
    {1,0,1,0}, //channel 5
    {0,1,1,0}, //channel 6
    {1,1,1,0}, //channel 7
    {0,0,0,1}, //channel 8
    {1,0,0,1}, //channel 9
    {0,1,0,1}, //channel 10
    {1,1,0,1}, //channel 11
    {0,0,1,1}, //channel 12
    {1,0,1,1}, //channel 13
    {0,1,1,1}, //channel 14
    {1,1,1,1}  //channel 15
  };

  //loop through the 4 sig
  for(int i = 0; i < 4; i ++){
    digitalWrite(controlPin[i], muxChannel[channel][i]);
  }

  //read the value at the SIG pin
  int val = analogRead(SIG_pin);

  //return the value
  float voltage = (val * 5.0) / 1024.0;
  return voltage;
}
1 Like

Hey bud sorry for late answer.
Ho can i implement your code to this one:

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

#include <SPI.h>
#include <Ethernet.h>
#include "Arduino.h"
#include <light_CD74HC4067.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, 1, 180);
EthernetServer server(80);

 int analogPin1 = A0;
   int analogPin2 = A1;
    int analogPin3 = A2;
     int analogPin4 = A3;
      int analogPin5 = A4;
       int analogPin6 = A5;
        int analogPin7 = A6;
         int analogPin8 = A7;
          int analogPin9 = A8;
           int analogPin10 = A9;
            int analogPin11 = A10;
             int analogPin12 = A11;
              int analogPin13 = A12;
               int analogPin14 = A13;
                int analogPin15 = A14;
                 int analogPin16 = A15;
                
 int moisture1;
 int moisture2;
 int moisture3;
 int moisture4;
 int moisture5;
 int moisture6;
 int moisture7;
 int moisture8;
 int moisture9;
 int moisture10;
 int moisture11;
 int moisture12;
 int moisture13;
 int moisture14;
 int moisture15;
 int moisture16;
 int percentage;

int Asciutto1 = 929;
int Bagnato1 = 490;

int Asciutto2 = 900;
int Bagnato2 = 470;

int Asciutto3 = 925;
int Bagnato3 = 455;

int Asciutto4 = 890;
int Bagnato4 = 470;

int Asciutto5 = 845;
int Bagnato5 = 501;

int Asciutto6 = 943;
int Bagnato6 = 516;

int Asciutto7 = 924;
int Bagnato7 = 473;

int Asciutto8 = 890;
int Bagnato8 = 473;

int Asciutto9 = 905;
int Bagnato9 = 473;

int Asciutto10 = 911;
int Bagnato10 = 462;

int Asciutto11 = 927;
int Bagnato11 = 491;

int Asciutto12 = 925;
int Bagnato12 = 477;

int Asciutto13 = 904;
int Bagnato13 = 484;

int Asciutto14 = 929;
int Bagnato14 = 491;

int Asciutto15 = 875;
int Bagnato15 = 505 ;

int Asciutto16 = 915;
int Bagnato16 = 490;


void setup() {
  // 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 Example");

  // 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: 20");  // 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 analogChannel = 0; analogChannel < 16; analogChannel++) {
            int sensorReading = analogRead(analogChannel);
            }
            
  moisture1 = analogRead(analogPin1);
  client.print("Vaso 1 Dati Raw: ");
  client.print(moisture1);

  percentage = map(moisture1, Asciutto1, Bagnato1, 0, 100);

  client.print(" | Percentuale: ");
  client.print(percentage);

  client.println("%<br><br>");

  delay(500);

  moisture2 = analogRead(analogPin2);
  client.print("Vaso 2 Dati Raw: ");
  client.print(moisture2);

  percentage = map(moisture2, Asciutto2, Bagnato2, 0, 100); 

  client.print(" | Percentuale: ");
  client.print(percentage);

  client.println("%<br><br>");

  delay(500);

  moisture3 = analogRead(analogPin3);
  client.print("Vaso 3 Dati Raw: ");
  client.print(moisture3);

  percentage = map(moisture3, Asciutto3, Bagnato3, 0, 100);

  client.print(" | Percentuale: ");
  client.print(percentage);

  client.println("%<br><br>");

  delay(500);

  moisture4 = analogRead(analogPin4);
  client.print("Vaso 4  Dati Raw: ");
  client.print(moisture4);

  percentage = map(moisture4, Asciutto4, Bagnato4, 0, 100);

  client.print(" | Percentuale: ");
  client.print(percentage);

  client.println("%<br><br>");

  delay(500);

  moisture5 = analogRead(analogPin5);
  client.print("Vaso 5  Dati Raw: ");
  client.print(moisture5);

  percentage = map(moisture5, Asciutto5, Bagnato5, 0, 100);

  client.print(" | Percentuale: ");
  client.print(percentage);

  client.println("%<br><br>");

  delay(500);

  moisture6 = analogRead(analogPin6);
  client.print("Vaso 6  Dati Raw: ");
  client.print(moisture6);

  percentage = map(moisture6, Asciutto6, Bagnato6, 0, 100);

  client.print(" | Percentuale: ");
  client.print(percentage);

  client.println("%<br><br>");

  delay(500);

  moisture7 = analogRead(analogPin7);
  client.print("Vaso 7  Dati Raw: ");
  client.print(moisture7);

  percentage = map(moisture7, Asciutto7, Bagnato7, 0, 100);

  client.print(" | Percentuale: ");
  client.print(percentage);

  client.println("%<br><br>");

  delay(500);

  moisture8 = analogRead(analogPin8);
  client.print("Vaso 8  Dati Raw: ");
  client.print(moisture8);

  percentage = map(moisture8, Asciutto8, Bagnato8, 0, 100);

  client.print(" | Percentuale: ");
  client.print(percentage);

  client.println("%<br><br>");

  delay(500);

  moisture9 = analogRead(analogPin9);
  client.print("Vaso 9  Dati Raw: ");
  client.print(moisture9);

  percentage = map(moisture9, Asciutto9, Bagnato9, 0, 100);

  client.print(" | Percentuale: ");
  client.print(percentage);

  client.println("%<br><br>");

  delay(500);

  moisture10 = analogRead(analogPin10);
  client.print("Vaso 10  Dati Raw: ");
  client.print(moisture10);

  percentage = map(moisture10, Asciutto10, Bagnato10, 0, 100);

  client.print(" | Percentuale: ");
  client.print(percentage);

  client.println("%<br><br>");

  delay(500);

  moisture11 = analogRead(analogPin11);
  client.print("Vaso 11 Dati Raw: ");
  client.print(moisture11);

  percentage = map(moisture11, Asciutto11, Bagnato11, 0, 100);

  client.print(" | Percentuale: ");
  client.print(percentage);

  client.println("%<br><br>");

  delay(500);

  moisture12 = analogRead(analogPin12);
  client.print("Vaso 12 Dati Raw: ");
  client.print(moisture12);

  percentage = map(moisture12, Asciutto12, Bagnato12, 0, 100);

  client.print(" | Percentuale: ");
  client.print(percentage);

  client.println("%<br><br>");

  delay(500);

  moisture13 = analogRead(analogPin13);
  client.print("Vaso 13 Dati Raw: ");
  client.print(moisture13);

  percentage = map(moisture13, Asciutto13, Bagnato13, 0, 100);

  client.print(" | Percentuale: ");
  client.print(percentage);

  client.println("%<br><br>");

  delay(500);

  moisture14 = analogRead(analogPin14);
  client.print("Vaso 14 Dati Raw: ");
  client.print(moisture14);

  percentage = map(moisture14, Asciutto14, Bagnato14, 0, 100);

  client.print(" | Percentuale: ");
  client.print(percentage);

  client.println("%<br><br>");

  delay(500);

  moisture15 = analogRead(analogPin15);
  client.print("Vaso 15 Dati Raw: ");
  client.print(moisture15);

  percentage = map(moisture15, Asciutto15, Bagnato15, 0, 100);

  client.print(" | Percentuale: ");
  client.print(percentage);

  client.println("%<br><br>");

  delay(500);

  moisture16 = analogRead(analogPin16);
  client.print("Vaso 16 Dati Raw: ");
  client.print(moisture16);

  percentage = map(moisture16, Asciutto16, Bagnato16, 0, 100);

  client.print(" | Percentuale: ");
  client.print(percentage);

  client.println("%<br><br>");

  delay(500);

          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();
    client.println("client disconnected");
  }
}

 



`

I tried this

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

#include <SPI.h>
#include <Ethernet.h>
#include "Arduino.h"
#include <light_CD74HC4067.h>

#define s0 6 //D2
#define s1 7 //D3
#define s2 1 //D4
#define s3 5 //D5
struct {
  int pin;
  int value;
} sensor[15];
#define SIG_pin A15

CD74HC4067 mux(s0, s1, s2, s3);
// 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, 1, 180);
EthernetServer server(80);

 int analogPin1 = A0;
   int analogPin2 = A1;
    int analogPin3 = A2;
     int analogPin4 = A3;
      int analogPin5 = A4;
       int analogPin6 = A5;
        int analogPin7 = A6;
         int analogPin8 = A7;
          int analogPin9 = A8;
           int analogPin10 = A9;
            int analogPin11 = A10;
             int analogPin12 = A11;
              int analogPin13 = A12;
               int analogPin14 = A13;
                int analogPin15 = A14;
                 int analogPin16 = A15;
                
 int moisture1;
 int moisture2;
 int moisture3;
 int moisture4;
 int moisture5;
 int moisture6;
 int moisture7;
 int moisture8;
 int moisture9;
 int moisture10;
 int moisture11;
 int moisture12;
 int moisture13;
 int moisture14;
 int moisture15;
 int moisture16;
 int percentage;

int Asciutto1 = 929;
int Bagnato1 = 490;

int Asciutto2 = 900;
int Bagnato2 = 470;

int Asciutto3 = 925;
int Bagnato3 = 455;

int Asciutto4 = 890;
int Bagnato4 = 470;

int Asciutto5 = 845;
int Bagnato5 = 501;

int Asciutto6 = 943;
int Bagnato6 = 516;

int Asciutto7 = 924;
int Bagnato7 = 473;

int Asciutto8 = 890;
int Bagnato8 = 473;

int Asciutto9 = 905;
int Bagnato9 = 473;

int Asciutto10 = 911;
int Bagnato10 = 462;

int Asciutto11 = 927;
int Bagnato11 = 491;

int Asciutto12 = 925;
int Bagnato12 = 477;

int Asciutto13 = 904;
int Bagnato13 = 484;

int Asciutto14 = 929;
int Bagnato14 = 491;

int Asciutto15 = 875;
int Bagnato15 = 505 ;

int Asciutto16 = 915;
int Bagnato16 = 490;


void setup() {{
  //set mux pins to OUTPUT  
  pinMode(s0, OUTPUT); 
  pinMode(s1, OUTPUT); 
  pinMode(s2, OUTPUT); 
  pinMode(s3, OUTPUT); 

  //set sensor pins
  sensor[0].pin = A0;
  sensor[1].pin = A1;
  sensor[2].pin = A2;
  sensor[3].pin = A3;
  sensor[4].pin = A4;
  sensor[5].pin = A5;
  sensor[6].pin = A6;
  sensor[7].pin = A7;
  sensor[8].pin = A8;
  sensor[9].pin = A9;
  sensor[10].pin = A10;
  sensor[11].pin = A11;
  sensor[12].pin = A12;
  sensor[13].pin = A13;
  sensor[14].pin = A14;

  //set soil sensor pins to INPUT
  for (int i=0;i<15;i++){
    pinMode(sensor[i].pin, INPUT);
  }

  //set MUX signal pin to INPUT
  pinMode(SIG_pin, INPUT);
  
  //digitalwrite all the mux pins low
  digitalWrite(s0, LOW);
  digitalWrite(s1, LOW);
  digitalWrite(s2, LOW);
  digitalWrite(s3, LOW);

  //start the serial port for debugging
  Serial.begin(9600);
}
  // 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 Example");

  // 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() {{
  for (int i=0;i<5;i++){//i less than 16 to go through all 16 channels
    Serial.print(" Percentuale umidita' vaso " + String(i) + "  e' : ");
    Serial.print(readMux(i));
    Serial.println("; Percentage: " + String(map(readMux(i), 0, 5, 0, 100)) + "%");
  }
  for (int i=0;i<15;i++){
    Serial.print(" Percentuale umidita' vaso " + String(i+5) + "  e' : ");
    Serial.print(analogRead(sensor[i].pin));
    Serial.println("; Percentage: " + String(map(analogRead(sensor[i].pin), 0, 5, 0, 100)) + "%");
  }
  delay(500);
}

float readMux(int channel){
  int controlPin[] = {s0, s1, s2, s3};

  int muxChannel[16][4]={
    {0,0,0,0}, //channel 0
    {1,0,0,0}, //channel 1
    {0,1,0,0}, //channel 2
    {1,1,0,0}, //channel 3
    {0,0,1,0}, //channel 4
    {1,0,1,0}, //channel 5
    {0,1,1,0}, //channel 6
    {1,1,1,0}, //channel 7
    {0,0,0,1}, //channel 8
    {1,0,0,1}, //channel 9
    {0,1,0,1}, //channel 10
    {1,1,0,1}, //channel 11
    {0,0,1,1}, //channel 12
    {1,0,1,1}, //channel 13
    {0,1,1,1}, //channel 14
    {1,1,1,1}  //channel 15
  };

  //loop through the 4 sig
  for(int i = 0; i < 4; i ++){
    digitalWrite(controlPin[i], muxChannel[channel][i]);
  }

  //read the value at the SIG pin
  int val = analogRead(SIG_pin);

  //return the value
  float voltage = (val * 5.0) / 1024.0;
  return voltage;
}
  // 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: 20");  // 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 analogChannel = 0; analogChannel < 16; analogChannel++) {
            int sensorReading = analogRead(analogChannel);
            }
            
  moisture1 = analogRead(analogPin1);
  client.print("Vaso 1 Dati Raw: ");
  client.print(moisture1);

  percentage = map(moisture1, Asciutto1, Bagnato1, 0, 100);

  client.print(" | Percentuale: ");
  client.print(percentage);

  client.println("%<br><br>");

  delay(500);

  moisture2 = analogRead(analogPin2);
  client.print("Vaso 2 Dati Raw: ");
  client.print(moisture2);

  percentage = map(moisture2, Asciutto2, Bagnato2, 0, 100); 

  client.print(" | Percentuale: ");
  client.print(percentage);

  client.println("%<br><br>");

  delay(500);

  moisture3 = analogRead(analogPin3);
  client.print("Vaso 3 Dati Raw: ");
  client.print(moisture3);

  percentage = map(moisture3, Asciutto3, Bagnato3, 0, 100);

  client.print(" | Percentuale: ");
  client.print(percentage);

  client.println("%<br><br>");

  delay(500);

  moisture4 = analogRead(analogPin4);
  client.print("Vaso 4  Dati Raw: ");
  client.print(moisture4);

  percentage = map(moisture4, Asciutto4, Bagnato4, 0, 100);

  client.print(" | Percentuale: ");
  client.print(percentage);

  client.println("%<br><br>");

  delay(500);

  moisture5 = analogRead(analogPin5);
  client.print("Vaso 5  Dati Raw: ");
  client.print(moisture5);

  percentage = map(moisture5, Asciutto5, Bagnato5, 0, 100);

  client.print(" | Percentuale: ");
  client.print(percentage);

  client.println("%<br><br>");

  delay(500);

  moisture6 = analogRead(analogPin6);
  client.print("Vaso 6  Dati Raw: ");
  client.print(moisture6);

  percentage = map(moisture6, Asciutto6, Bagnato6, 0, 100);

  client.print(" | Percentuale: ");
  client.print(percentage);

  client.println("%<br><br>");

  delay(500);

  moisture7 = analogRead(analogPin7);
  client.print("Vaso 7  Dati Raw: ");
  client.print(moisture7);

  percentage = map(moisture7, Asciutto7, Bagnato7, 0, 100);

  client.print(" | Percentuale: ");
  client.print(percentage);

  client.println("%<br><br>");

  delay(500);

  moisture8 = analogRead(analogPin8);
  client.print("Vaso 8  Dati Raw: ");
  client.print(moisture8);

  percentage = map(moisture8, Asciutto8, Bagnato8, 0, 100);

  client.print(" | Percentuale: ");
  client.print(percentage);

  client.println("%<br><br>");

  delay(500);

  moisture9 = analogRead(analogPin9);
  client.print("Vaso 9  Dati Raw: ");
  client.print(moisture9);

  percentage = map(moisture9, Asciutto9, Bagnato9, 0, 100);

  client.print(" | Percentuale: ");
  client.print(percentage);

  client.println("%<br><br>");

  delay(500);

  moisture10 = analogRead(analogPin10);
  client.print("Vaso 10  Dati Raw: ");
  client.print(moisture10);

  percentage = map(moisture10, Asciutto10, Bagnato10, 0, 100);

  client.print(" | Percentuale: ");
  client.print(percentage);

  client.println("%<br><br>");

  delay(500);

  moisture11 = analogRead(analogPin11);
  client.print("Vaso 11 Dati Raw: ");
  client.print(moisture11);

  percentage = map(moisture11, Asciutto11, Bagnato11, 0, 100);

  client.print(" | Percentuale: ");
  client.print(percentage);

  client.println("%<br><br>");

  delay(500);

  moisture12 = analogRead(analogPin12);
  client.print("Vaso 12 Dati Raw: ");
  client.print(moisture12);

  percentage = map(moisture12, Asciutto12, Bagnato12, 0, 100);

  client.print(" | Percentuale: ");
  client.print(percentage);

  client.println("%<br><br>");

  delay(500);

  moisture13 = analogRead(analogPin13);
  client.print("Vaso 13 Dati Raw: ");
  client.print(moisture13);

  percentage = map(moisture13, Asciutto13, Bagnato13, 0, 100);

  client.print(" | Percentuale: ");
  client.print(percentage);

  client.println("%<br><br>");

  delay(500);

  moisture14 = analogRead(analogPin14);
  client.print("Vaso 14 Dati Raw: ");
  client.print(moisture14);

  percentage = map(moisture14, Asciutto14, Bagnato14, 0, 100);

  client.print(" | Percentuale: ");
  client.print(percentage);

  client.println("%<br><br>");

  delay(500);

  moisture15 = analogRead(analogPin15);
  client.print("Vaso 15 Dati Raw: ");
  client.print(moisture15);

  percentage = map(moisture15, Asciutto15, Bagnato15, 0, 100);

  client.print(" | Percentuale: ");
  client.print(percentage);

  client.println("%<br><br>");

  delay(500);

  moisture16 = analogRead(analogPin16);
  client.print("Vaso 16 Dati Raw: ");
  client.print(moisture16);

  percentage = map(moisture16, Asciutto16, Bagnato16, 0, 100);

  client.print(" | Percentuale: ");
  client.print(percentage);

  client.println("%<br><br>");

  delay(500);

          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();
    client.println("client disconnected");
  }
}

 



but i get this C:\Users\Loller12x\Desktop\sketch_sep29a\sketch_sep29a.ino: In function 'void loop()': sketch_sep29a:193:18: error: 'readMux' was not declared in this scope Serial.print(readMux(i)); ^~~~~~~ sketch_sep29a:204:27: error: a function-definition is not allowed here before '{' token float readMux(int channel){ ^ Più di una libreria trovata per "Ethernet.h" Usata: C:\Users\Loller12x\Documents\Arduino\libraries\Ethernet Non usata: C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.57.0_x86__mdqgnx93n4wtt\libraries\Ethernet exit status 1 'readMux' was not declared in this scope

Try auto formatting your code.

Did you notice the


void loop() {{
  for (int i=0;i<5;i++){//i less than 16 to go through all 16 channels
    Ser

Each { must have a } I bet if you count { and } the number is not equal.

You were right, but that didn't change much
"Serial.print(readMux(i));" gives "'readMux' was not declared in this scope"

did you happen post the changed code in a new post?

Afterall, if you made that error, you made others. One error at a time.

1 Like
#include <SPI.h>
#include <Ethernet.h>
#include "Arduino.h"
#include <light_CD74HC4067.h>

#define s0 6 //D2
#define s1 7 //D3
#define s2 1 //D4
#define s3 5 //D5
struct {
  int pin;
  int value;
} sensor[15];
#define SIG_pin A15

CD74HC4067 mux(s0, s1, s2, s3);
// 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, 1, 180);
EthernetServer server(80);

 int analogPin1 = A0;
   int analogPin2 = A1;
    int analogPin3 = A2;
     int analogPin4 = A3;
      int analogPin5 = A4;
       int analogPin6 = A5;
        int analogPin7 = A6;
         int analogPin8 = A7;
          int analogPin9 = A8;
           int analogPin10 = A9;
            int analogPin11 = A10;
             int analogPin12 = A11;
              int analogPin13 = A12;
               int analogPin14 = A13;
                int analogPin15 = A14;
                 int analogPin16 = A15;
                
 int moisture1;
 int moisture2;
 int moisture3;
 int moisture4;
 int moisture5;
 int moisture6;
 int moisture7;
 int moisture8;
 int moisture9;
 int moisture10;
 int moisture11;
 int moisture12;
 int moisture13;
 int moisture14;
 int moisture15;
 int moisture16;
 int percentage;

int Asciutto1 = 929;
int Bagnato1 = 490;

int Asciutto2 = 900;
int Bagnato2 = 470;

int Asciutto3 = 925;
int Bagnato3 = 455;

int Asciutto4 = 890;
int Bagnato4 = 470;

int Asciutto5 = 845;
int Bagnato5 = 501;

int Asciutto6 = 943;
int Bagnato6 = 516;

int Asciutto7 = 924;
int Bagnato7 = 473;

int Asciutto8 = 890;
int Bagnato8 = 473;

int Asciutto9 = 905;
int Bagnato9 = 473;

int Asciutto10 = 911;
int Bagnato10 = 462;

int Asciutto11 = 927;
int Bagnato11 = 491;

int Asciutto12 = 925;
int Bagnato12 = 477;

int Asciutto13 = 904;
int Bagnato13 = 484;

int Asciutto14 = 929;
int Bagnato14 = 491;

int Asciutto15 = 875;
int Bagnato15 = 505 ;

int Asciutto16 = 915;
int Bagnato16 = 490;


void setup() {{
  //set mux pins to OUTPUT  
  pinMode(s0, OUTPUT); 
  pinMode(s1, OUTPUT); 
  pinMode(s2, OUTPUT); 
  pinMode(s3, OUTPUT); 

  //set sensor pins
  sensor[0].pin = A0;
  sensor[1].pin = A1;
  sensor[2].pin = A2;
  sensor[3].pin = A3;
  sensor[4].pin = A4;
  sensor[5].pin = A5;
  sensor[6].pin = A6;
  sensor[7].pin = A7;
  sensor[8].pin = A8;
  sensor[9].pin = A9;
  sensor[10].pin = A10;
  sensor[11].pin = A11;
  sensor[12].pin = A12;
  sensor[13].pin = A13;
  sensor[14].pin = A14;

  //set soil sensor pins to INPUT
  for (int i=0;i<15;i++){
    pinMode(sensor[i].pin, INPUT);
  }

  //set MUX signal pin to INPUT
  pinMode(SIG_pin, INPUT);
  
  //digitalwrite all the mux pins low
  digitalWrite(s0, LOW);
  digitalWrite(s1, LOW);
  digitalWrite(s2, LOW);
  digitalWrite(s3, LOW);

  //start the serial port for debugging
  Serial.begin(9600);
}
  // 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 Example");

  // 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() {
  for (int i=0;i<5;i++){//i less than 16 to go through all 16 channels
    Serial.print(" Percentuale umidita' vaso " + String(i) + "  e' : ");
    Serial.print(readMux(i));
    Serial.println("; Percentage: " + String(map(readMux(i), 0, 5, 0, 100)) + "%");
  }
  for (int i=0;i<15;i++){
    Serial.print(" Percentuale umidita' vaso " + String(i+5) + "  e' : ");
    Serial.print(analogRead(sensor[i].pin));
    Serial.println("; Percentage: " + String(map(analogRead(sensor[i].pin), 0, 5, 0, 100)) + "%");
  }
  delay(500);
}

float readMux(int channel){
  int controlPin[] = {s0, s1, s2, s3};

  int muxChannel[16][4]={
    {0,0,0,0}, //channel 0
    {1,0,0,0}, //channel 1
    {0,1,0,0}, //channel 2
    {1,1,0,0}, //channel 3
    {0,0,1,0}, //channel 4
    {1,0,1,0}, //channel 5
    {0,1,1,0}, //channel 6
    {1,1,1,0}, //channel 7
    {0,0,0,1}, //channel 8
    {1,0,0,1}, //channel 9
    {0,1,0,1}, //channel 10
    {1,1,0,1}, //channel 11
    {0,0,1,1}, //channel 12
    {1,0,1,1}, //channel 13
    {0,1,1,1}, //channel 14
    {1,1,1,1}  //channel 15
  };

  //loop through the 4 sig
  for(int i = 0; i < 4; i ++){
    digitalWrite(controlPin[i], muxChannel[channel][i]);
  }

  //read the value at the SIG pin
  int val = analogRead(SIG_pin);

  //return the value
  float voltage = (val * 5.0) / 1024.0;
  return voltage;
}
  // 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: 20");  // 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 analogChannel = 0; analogChannel < 16; analogChannel++) {
            int sensorReading = analogRead(analogChannel);
            }
            
  moisture1 = analogRead(analogPin1);
  client.print("Vaso 1 Dati Raw: ");
  client.print(moisture1);

  percentage = map(moisture1, Asciutto1, Bagnato1, 0, 100);

  client.print(" | Percentuale: ");
  client.print(percentage);

  client.println("%<br><br>");

  delay(500);

  moisture2 = analogRead(analogPin2);
  client.print("Vaso 2 Dati Raw: ");
  client.print(moisture2);

  percentage = map(moisture2, Asciutto2, Bagnato2, 0, 100); 

  client.print(" | Percentuale: ");
  client.print(percentage);

  client.println("%<br><br>");

  delay(500);

  moisture3 = analogRead(analogPin3);
  client.print("Vaso 3 Dati Raw: ");
  client.print(moisture3);

  percentage = map(moisture3, Asciutto3, Bagnato3, 0, 100);

  client.print(" | Percentuale: ");
  client.print(percentage);

  client.println("%<br><br>");

  delay(500);

  moisture4 = analogRead(analogPin4);
  client.print("Vaso 4  Dati Raw: ");
  client.print(moisture4);

  percentage = map(moisture4, Asciutto4, Bagnato4, 0, 100);

  client.print(" | Percentuale: ");
  client.print(percentage);

  client.println("%<br><br>");

  delay(500);

  moisture5 = analogRead(analogPin5);
  client.print("Vaso 5  Dati Raw: ");
  client.print(moisture5);

  percentage = map(moisture5, Asciutto5, Bagnato5, 0, 100);

  client.print(" | Percentuale: ");
  client.print(percentage);

  client.println("%<br><br>");

  delay(500);

  moisture6 = analogRead(analogPin6);
  client.print("Vaso 6  Dati Raw: ");
  client.print(moisture6);

  percentage = map(moisture6, Asciutto6, Bagnato6, 0, 100);

  client.print(" | Percentuale: ");
  client.print(percentage);

  client.println("%<br><br>");

  delay(500);

  moisture7 = analogRead(analogPin7);
  client.print("Vaso 7  Dati Raw: ");
  client.print(moisture7);

  percentage = map(moisture7, Asciutto7, Bagnato7, 0, 100);

  client.print(" | Percentuale: ");
  client.print(percentage);

  client.println("%<br><br>");

  delay(500);

  moisture8 = analogRead(analogPin8);
  client.print("Vaso 8  Dati Raw: ");
  client.print(moisture8);

  percentage = map(moisture8, Asciutto8, Bagnato8, 0, 100);

  client.print(" | Percentuale: ");
  client.print(percentage);

  client.println("%<br><br>");

  delay(500);

  moisture9 = analogRead(analogPin9);
  client.print("Vaso 9  Dati Raw: ");
  client.print(moisture9);

  percentage = map(moisture9, Asciutto9, Bagnato9, 0, 100);

  client.print(" | Percentuale: ");
  client.print(percentage);

  client.println("%<br><br>");

  delay(500);

  moisture10 = analogRead(analogPin10);
  client.print("Vaso 10  Dati Raw: ");
  client.print(moisture10);

  percentage = map(moisture10, Asciutto10, Bagnato10, 0, 100);

  client.print(" | Percentuale: ");
  client.print(percentage);

  client.println("%<br><br>");

  delay(500);

  moisture11 = analogRead(analogPin11);
  client.print("Vaso 11 Dati Raw: ");
  client.print(moisture11);

  percentage = map(moisture11, Asciutto11, Bagnato11, 0, 100);

  client.print(" | Percentuale: ");
  client.print(percentage);

  client.println("%<br><br>");

  delay(500);

  moisture12 = analogRead(analogPin12);
  client.print("Vaso 12 Dati Raw: ");
  client.print(moisture12);

  percentage = map(moisture12, Asciutto12, Bagnato12, 0, 100);

  client.print(" | Percentuale: ");
  client.print(percentage);

  client.println("%<br><br>");

  delay(500);

  moisture13 = analogRead(analogPin13);
  client.print("Vaso 13 Dati Raw: ");
  client.print(moisture13);

  percentage = map(moisture13, Asciutto13, Bagnato13, 0, 100);

  client.print(" | Percentuale: ");
  client.print(percentage);

  client.println("%<br><br>");

  delay(500);

  moisture14 = analogRead(analogPin14);
  client.print("Vaso 14 Dati Raw: ");
  client.print(moisture14);

  percentage = map(moisture14, Asciutto14, Bagnato14, 0, 100);

  client.print(" | Percentuale: ");
  client.print(percentage);

  client.println("%<br><br>");

  delay(500);

  moisture15 = analogRead(analogPin15);
  client.print("Vaso 15 Dati Raw: ");
  client.print(moisture15);

  percentage = map(moisture15, Asciutto15, Bagnato15, 0, 100);

  client.print(" | Percentuale: ");
  client.print(percentage);

  client.println("%<br><br>");

  delay(500);

  moisture16 = analogRead(analogPin16);
  client.print("Vaso 16 Dati Raw: ");
  client.print(moisture16);

  percentage = map(moisture16, Asciutto16, Bagnato16, 0, 100);

  client.print(" | Percentuale: ");
  client.print(percentage);

  client.println("%<br><br>");

  delay(500);

          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();
    client.println("client disconnected");
  }
}

And i get:










sketch_sep29a:240:3: error: expected unqualified-id before 'if'
   if (client) {
   ^~
sketch_sep29a:491:1: error: expected declaration before '}' token
 }
 ^
More then a library found for "Ethernet.h"
Usata: C:\Users\Loller12x\Documents\Arduino\libraries\Ethernet
Not used: C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.57.0_x86__mdqgnx93n4wtt\libraries\Ethernet
exit status 1
expected unqualified-id before 'if'

the function


float readMux(int channel) {
  int controlPin[] = {s0, s1, s2, s3};

  int muxChannel[16][4] = {
    {0, 0, 0, 0}, //channel 0
    {1, 0, 0, 0}, //channel 1
    {0, 1, 0, 0}, //channel 2
    {1, 1, 0, 0}, //channel 3
    {0, 0, 1, 0}, //channel 4
    {1, 0, 1, 0}, //channel 5
    {0, 1, 1, 0}, //channel 6
    {1, 1, 1, 0}, //channel 7
    {0, 0, 0, 1}, //channel 8
    {1, 0, 0, 1}, //channel 9
    {0, 1, 0, 1}, //channel 10
    {1, 1, 0, 1}, //channel 11
    {0, 0, 1, 1}, //channel 12
    {1, 0, 1, 1}, //channel 13
    {0, 1, 1, 1}, //channel 14
    {1, 1, 1, 1} //channel 15
  };

  //loop through the 4 sig
  for (int i = 0; i < 4; i ++) {
    digitalWrite(controlPin[i], muxChannel[channel][i]);
  }

  //read the value at the SIG pin
  int val = analogRead(SIG_pin);

  //return the value
  float voltage = (val * 5.0) / 1024.0;
  return voltage;
}

ends after the return.
so.
which function is the

EthernetClient client = server.available();
if (client) {
  Serial.println("new client");
  // an http request ends with a blank line

code in? Did you do the auto-format thing to see that the code does not align?

post the new changed code in a new post when you fix that issue.

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

#include <SPI.h>
#include <Ethernet.h>
#include "Arduino.h"
#include <light_CD74HC4067.h>

#define s0 6 //D2
#define s1 7 //D3
#define s2 1 //D4
#define s3 5 //D5
struct {
  int pin;
  int value;
} sensor[15];
#define SIG_pin A15

CD74HC4067 mux(s0, s1, s2, s3);
// 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, 1, 180);
EthernetServer server(80);

int analogPin1 = A0;
int analogPin2 = A1;
int analogPin3 = A2;
int analogPin4 = A3;
int analogPin5 = A4;
int analogPin6 = A5;
int analogPin7 = A6;
int analogPin8 = A7;
int analogPin9 = A8;
int analogPin10 = A9;
int analogPin11 = A10;
int analogPin12 = A11;
int analogPin13 = A12;
int analogPin14 = A13;
int analogPin15 = A14;
int analogPin16 = A15;

int moisture1;
int moisture2;
int moisture3;
int moisture4;
int moisture5;
int moisture6;
int moisture7;
int moisture8;
int moisture9;
int moisture10;
int moisture11;
int moisture12;
int moisture13;
int moisture14;
int moisture15;
int moisture16;
int percentage;

int Asciutto1 = 929;
int Bagnato1 = 490;

int Asciutto2 = 900;
int Bagnato2 = 470;

int Asciutto3 = 925;
int Bagnato3 = 455;

int Asciutto4 = 890;
int Bagnato4 = 470;

int Asciutto5 = 845;
int Bagnato5 = 501;

int Asciutto6 = 943;
int Bagnato6 = 516;

int Asciutto7 = 924;
int Bagnato7 = 473;

int Asciutto8 = 890;
int Bagnato8 = 473;

int Asciutto9 = 905;
int Bagnato9 = 473;

int Asciutto10 = 911;
int Bagnato10 = 462;

int Asciutto11 = 927;
int Bagnato11 = 491;

int Asciutto12 = 925;
int Bagnato12 = 477;

int Asciutto13 = 904;
int Bagnato13 = 484;

int Asciutto14 = 929;
int Bagnato14 = 491;

int Asciutto15 = 875;
int Bagnato15 = 505 ;

int Asciutto16 = 915;
int Bagnato16 = 490;


void setup() {
  {
    //set mux pins to OUTPUT
    pinMode(s0, OUTPUT);
    pinMode(s1, OUTPUT);
    pinMode(s2, OUTPUT);
    pinMode(s3, OUTPUT);

    //set sensor pins
    sensor[0].pin = A0;
    sensor[1].pin = A1;
    sensor[2].pin = A2;
    sensor[3].pin = A3;
    sensor[4].pin = A4;
    sensor[5].pin = A5;
    sensor[6].pin = A6;
    sensor[7].pin = A7;
    sensor[8].pin = A8;
    sensor[9].pin = A9;
    sensor[10].pin = A10;
    sensor[11].pin = A11;
    sensor[12].pin = A12;
    sensor[13].pin = A13;
    sensor[14].pin = A14;

    //set soil sensor pins to INPUT
    for (int i = 0; i < 15; i++) {
      pinMode(sensor[i].pin, INPUT);
    }

    //set MUX signal pin to INPUT
    pinMode(SIG_pin, INPUT);

    //digitalwrite all the mux pins low
    digitalWrite(s0, LOW);
    digitalWrite(s1, LOW);
    digitalWrite(s2, LOW);
    digitalWrite(s3, LOW);

    //start the serial port for debugging
    Serial.begin(9600);
  }
  // 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 Example");

  // 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() {
  for (int i = 0; i < 5; i++) { //i less than 16 to go through all 16 channels
    Serial.print(" Percentuale umidita' vaso " + String(i) + "  e' : ");
    Serial.print(readMux(i));
    Serial.println("; Percentage: " + String(map(readMux(i), 0, 5, 0, 100)) + "%");
  }
  for (int i = 0; i < 15; i++) {
    Serial.print(" Percentuale umidita' vaso " + String(i + 5) + "  e' : ");
    Serial.print(analogRead(sensor[i].pin));
    Serial.println("; Percentage: " + String(map(analogRead(sensor[i].pin), 0, 5, 0, 100)) + "%");
  }
  delay(500);
}

float readMux(int channel) {
  int controlPin[] = {s0, s1, s2, s3};

  int muxChannel[16][4] = {
    {0, 0, 0, 0}, //channel 0
    {1, 0, 0, 0}, //channel 1
    {0, 1, 0, 0}, //channel 2
    {1, 1, 0, 0}, //channel 3
    {0, 0, 1, 0}, //channel 4
    {1, 0, 1, 0}, //channel 5
    {0, 1, 1, 0}, //channel 6
    {1, 1, 1, 0}, //channel 7
    {0, 0, 0, 1}, //channel 8
    {1, 0, 0, 1}, //channel 9
    {0, 1, 0, 1}, //channel 10
    {1, 1, 0, 1}, //channel 11
    {0, 0, 1, 1}, //channel 12
    {1, 0, 1, 1}, //channel 13
    {0, 1, 1, 1}, //channel 14
    {1, 1, 1, 1} //channel 15
  };

  //loop through the 4 sig
  for (int i = 0; i < 4; i ++) {
    digitalWrite(controlPin[i], muxChannel[channel][i]);
  }

  //read the value at the SIG pin
  int val = analogRead(SIG_pin);

  //return the value
  float voltage = (val * 5.0) / 1024.0;
  return voltage;

// 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: 20");  // 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 analogChannel = 0; analogChannel < 16; analogChannel++) {
            int sensorReading = analogRead(analogChannel);
          }

          moisture1 = analogRead(analogPin1);
          client.print("Vaso 1 Dati Raw: ");
          client.print(moisture1);

          percentage = map(moisture1, Asciutto1, Bagnato1, 0, 100);

          client.print(" | Percentuale: ");
          client.print(percentage);

          client.println("%<br><br>");

          delay(500);

          moisture2 = analogRead(analogPin2);
          client.print("Vaso 2 Dati Raw: ");
          client.print(moisture2);

          percentage = map(moisture2, Asciutto2, Bagnato2, 0, 100);

          client.print(" | Percentuale: ");
          client.print(percentage);

          client.println("%<br><br>");

          delay(500);

          moisture3 = analogRead(analogPin3);
          client.print("Vaso 3 Dati Raw: ");
          client.print(moisture3);

          percentage = map(moisture3, Asciutto3, Bagnato3, 0, 100);

          client.print(" | Percentuale: ");
          client.print(percentage);

          client.println("%<br><br>");

          delay(500);

          moisture4 = analogRead(analogPin4);
          client.print("Vaso 4  Dati Raw: ");
          client.print(moisture4);

          percentage = map(moisture4, Asciutto4, Bagnato4, 0, 100);

          client.print(" | Percentuale: ");
          client.print(percentage);

          client.println("%<br><br>");

          delay(500);

          moisture5 = analogRead(analogPin5);
          client.print("Vaso 5  Dati Raw: ");
          client.print(moisture5);

          percentage = map(moisture5, Asciutto5, Bagnato5, 0, 100);

          client.print(" | Percentuale: ");
          client.print(percentage);

          client.println("%<br><br>");

          delay(500);

          moisture6 = analogRead(analogPin6);
          client.print("Vaso 6  Dati Raw: ");
          client.print(moisture6);

          percentage = map(moisture6, Asciutto6, Bagnato6, 0, 100);

          client.print(" | Percentuale: ");
          client.print(percentage);

          client.println("%<br><br>");

          delay(500);

          moisture7 = analogRead(analogPin7);
          client.print("Vaso 7  Dati Raw: ");
          client.print(moisture7);

          percentage = map(moisture7, Asciutto7, Bagnato7, 0, 100);

          client.print(" | Percentuale: ");
          client.print(percentage);

          client.println("%<br><br>");

          delay(500);

          moisture8 = analogRead(analogPin8);
          client.print("Vaso 8  Dati Raw: ");
          client.print(moisture8);

          percentage = map(moisture8, Asciutto8, Bagnato8, 0, 100);

          client.print(" | Percentuale: ");
          client.print(percentage);

          client.println("%<br><br>");

          delay(500);

          moisture9 = analogRead(analogPin9);
          client.print("Vaso 9  Dati Raw: ");
          client.print(moisture9);

          percentage = map(moisture9, Asciutto9, Bagnato9, 0, 100);

          client.print(" | Percentuale: ");
          client.print(percentage);

          client.println("%<br><br>");

          delay(500);

          moisture10 = analogRead(analogPin10);
          client.print("Vaso 10  Dati Raw: ");
          client.print(moisture10);

          percentage = map(moisture10, Asciutto10, Bagnato10, 0, 100);

          client.print(" | Percentuale: ");
          client.print(percentage);

          client.println("%<br><br>");

          delay(500);

          moisture11 = analogRead(analogPin11);
          client.print("Vaso 11 Dati Raw: ");
          client.print(moisture11);

          percentage = map(moisture11, Asciutto11, Bagnato11, 0, 100);

          client.print(" | Percentuale: ");
          client.print(percentage);

          client.println("%<br><br>");

          delay(500);

          moisture12 = analogRead(analogPin12);
          client.print("Vaso 12 Dati Raw: ");
          client.print(moisture12);

          percentage = map(moisture12, Asciutto12, Bagnato12, 0, 100);

          client.print(" | Percentuale: ");
          client.print(percentage);

          client.println("%<br><br>");

          delay(500);

          moisture13 = analogRead(analogPin13);
          client.print("Vaso 13 Dati Raw: ");
          client.print(moisture13);

          percentage = map(moisture13, Asciutto13, Bagnato13, 0, 100);

          client.print(" | Percentuale: ");
          client.print(percentage);

          client.println("%<br><br>");

          delay(500);

          moisture14 = analogRead(analogPin14);
          client.print("Vaso 14 Dati Raw: ");
          client.print(moisture14);

          percentage = map(moisture14, Asciutto14, Bagnato14, 0, 100);

          client.print(" | Percentuale: ");
          client.print(percentage);

          client.println("%<br><br>");

          delay(500);

          moisture15 = analogRead(analogPin15);
          client.print("Vaso 15 Dati Raw: ");
          client.print(moisture15);

          percentage = map(moisture15, Asciutto15, Bagnato15, 0, 100);

          client.print(" | Percentuale: ");
          client.print(percentage);

          client.println("%<br><br>");

          delay(500);

          moisture16 = analogRead(analogPin16);
          client.print("Vaso 16 Dati Raw: ");
          client.print(moisture16);

          percentage = map(moisture16, Asciutto16, Bagnato16, 0, 100);

          client.print(" | Percentuale: ");
          client.print(percentage);

          client.println("%<br><br>");

          delay(500);

          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();
    client.println("client disconnected");
  }
}
}

but now it does work as serial monitor and only for the Multiplexer.
All the Html and the others sensors are not shown.

Huh?

Do you know what the "{ " is for?

At the end of the code is

Doesn't that look strange?

seems the {'s and the }'s are still an issue.

Well, i tried to fix doing this:

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

#include <SPI.h>
#include <Ethernet.h>
#include "Arduino.h"
#include <light_CD74HC4067.h>

#define s0 6 //D2
#define s1 7 //D3
#define s2 1 //D4
#define s3 5 //D5
struct {
  int pin;
  int value;
} sensor[15];
#define SIG_pin A15

CD74HC4067 mux(s0, s1, s2, s3);
// 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, 1, 180);
EthernetServer server(80);

int analogPin1 = A0;
int analogPin2 = A1;
int analogPin3 = A2;
int analogPin4 = A3;
int analogPin5 = A4;
int analogPin6 = A5;
int analogPin7 = A6;
int analogPin8 = A7;
int analogPin9 = A8;
int analogPin10 = A9;
int analogPin11 = A10;
int analogPin12 = A11;
int analogPin13 = A12;
int analogPin14 = A13;
int analogPin15 = A14;


int moisture1;
int moisture2;
int moisture3;
int moisture4;
int moisture5;
int moisture6;
int moisture7;
int moisture8;
int moisture9;
int moisture10;
int moisture11;
int moisture12;
int moisture13;
int moisture14;
int moisture15;
int moisture16;
int moisture17;
int moisture18;
int moisture19;
int moisture20;

int percentage;

int Asciutto1 = 929;
int Bagnato1 = 490;

int Asciutto2 = 900;
int Bagnato2 = 470;

int Asciutto3 = 925;
int Bagnato3 = 455;

int Asciutto4 = 890;
int Bagnato4 = 470;

int Asciutto5 = 845;
int Bagnato5 = 501;

int Asciutto6 = 943;
int Bagnato6 = 516;

int Asciutto7 = 924;
int Bagnato7 = 473;

int Asciutto8 = 890;
int Bagnato8 = 473;

int Asciutto9 = 905;
int Bagnato9 = 473;

int Asciutto10 = 911;
int Bagnato10 = 462;

int Asciutto11 = 927;
int Bagnato11 = 491;

int Asciutto12 = 925;
int Bagnato12 = 477;

int Asciutto13 = 904;
int Bagnato13 = 484;

int Asciutto14 = 929;
int Bagnato14 = 491;

int Asciutto15 = 875;
int Bagnato15 = 505 ;

int Asciutto16 = 915;
int Bagnato16 = 490;

int Asciutto17 = 929;
int Bagnato17 = 490;

int Asciutto18 = 929;
int Bagnato18 = 490;

int Asciutto19 = 929;
int Bagnato19 = 490;

int Asciutto20 = 929;
int Bagnato20 = 490;

void setup() {
  {
    //set mux pins to OUTPUT
    pinMode(s0, OUTPUT);
    pinMode(s1, OUTPUT);
    pinMode(s2, OUTPUT);
    pinMode(s3, OUTPUT);

    //set sensor pins
    sensor[0].pin = A0;
    sensor[1].pin = A1;
    sensor[2].pin = A2;
    sensor[3].pin = A3;
    sensor[4].pin = A4;
    sensor[5].pin = A5;
    sensor[6].pin = A6;
    sensor[7].pin = A7;
    sensor[8].pin = A8;
    sensor[9].pin = A9;
    sensor[10].pin = A10;
    sensor[11].pin = A11;
    sensor[12].pin = A12;
    sensor[13].pin = A13;
    sensor[14].pin = A14;

    //set soil sensor pins to INPUT
    for (int i = 0; i < 14; i++) {
      pinMode(sensor[i].pin, INPUT);
    }

    //set MUX signal pin to INPUT
    pinMode(SIG_pin, INPUT);

    //digitalwrite all the mux pins low
    digitalWrite(s0, LOW);
    digitalWrite(s1, LOW);
    digitalWrite(s2, LOW);
    digitalWrite(s3, LOW);

    //start the serial port for debugging
    Serial.begin(9600);
  }
  // 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 Example");

  // 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() {
  for (int i = 0; i < 5; i++) { //i less than 16 to go through all 16 channels
    Serial.print(" Percentuale umidita' vaso " + String(i) + "  e' : ");
    Serial.print(readMux(i));
    Serial.println("; Percentage: " + String(map(readMux(i), 0, 5, 0, 100)) + "%");
  }
  for (int i = 0; i < 15; i++) {
    Serial.print(" Percentuale umidita' vaso " + String(i + 5) + "  e' : ");
    Serial.print(analogRead(sensor[i].pin));
    Serial.println("; Percentage: " + String(map(analogRead(sensor[i].pin), 0, 5, 0, 100)) + "%");
  }
  delay(500);
}

float readMux(int channel) {
  int controlPin[] = {s0, s1, s2, s3};

  int muxChannel[16][4] = {
    {0, 0, 0, 0}, //channel 0
    {1, 0, 0, 0}, //channel 1
    {0, 1, 0, 0}, //channel 2
    {1, 1, 0, 0}, //channel 3
    {0, 0, 1, 0}, //channel 4
    {1, 0, 1, 0}, //channel 5
    {0, 1, 1, 0}, //channel 6
    {1, 1, 1, 0}, //channel 7
    {0, 0, 0, 1}, //channel 8
    {1, 0, 0, 1}, //channel 9
    {0, 1, 0, 1}, //channel 10
    {1, 1, 0, 1}, //channel 11
    {0, 0, 1, 1}, //channel 12
    {1, 0, 1, 1}, //channel 13
    {0, 1, 1, 1}, //channel 14
    {1, 1, 1, 1} //channel 15
  };

  //loop through the 4 sig
  for (int i = 0; i < 4; i ++) {
    digitalWrite(controlPin[i], muxChannel[channel][i]);
  }

  //read the value at the SIG pin
  int val = analogRead(SIG_pin);

  //return the value
  float voltage = (val * 5.0) / 1024.0;
  return voltage;

// 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: 20");  // 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 analogChannel = 0; analogChannel < 16; analogChannel++) {
            int sensorReading = analogRead(analogChannel);
          }

          moisture1 = analogRead(analogPin1);
          client.print("Vaso 1 Dati Raw: ");
          client.print(moisture1);

          percentage = map(moisture1, Asciutto1, Bagnato1, 0, 100);

          client.print(" | Percentuale: ");
          client.print(percentage);

          client.println("%<br><br>");

          delay(500);

          moisture2 = analogRead(analogPin2);
          client.print("Vaso 2 Dati Raw: ");
          client.print(moisture2);

          percentage = map(moisture2, Asciutto2, Bagnato2, 0, 100);

          client.print(" | Percentuale: ");
          client.print(percentage);

          client.println("%<br><br>");

          delay(500);

          moisture3 = analogRead(analogPin3);
          client.print("Vaso 3 Dati Raw: ");
          client.print(moisture3);

          percentage = map(moisture3, Asciutto3, Bagnato3, 0, 100);

          client.print(" | Percentuale: ");
          client.print(percentage);

          client.println("%<br><br>");

          delay(500);

          moisture4 = analogRead(analogPin4);
          client.print("Vaso 4  Dati Raw: ");
          client.print(moisture4);

          percentage = map(moisture4, Asciutto4, Bagnato4, 0, 100);

          client.print(" | Percentuale: ");
          client.print(percentage);

          client.println("%<br><br>");

          delay(500);

          moisture5 = analogRead(analogPin5);
          client.print("Vaso 5  Dati Raw: ");
          client.print(moisture5);

          percentage = map(moisture5, Asciutto5, Bagnato5, 0, 100);

          client.print(" | Percentuale: ");
          client.print(percentage);

          client.println("%<br><br>");

          delay(500);

          moisture6 = analogRead(analogPin6);
          client.print("Vaso 6  Dati Raw: ");
          client.print(moisture6);

          percentage = map(moisture6, Asciutto6, Bagnato6, 0, 100);

          client.print(" | Percentuale: ");
          client.print(percentage);

          client.println("%<br><br>");

          delay(500);

          moisture7 = analogRead(analogPin7);
          client.print("Vaso 7  Dati Raw: ");
          client.print(moisture7);

          percentage = map(moisture7, Asciutto7, Bagnato7, 0, 100);

          client.print(" | Percentuale: ");
          client.print(percentage);

          client.println("%<br><br>");

          delay(500);

          moisture8 = analogRead(analogPin8);
          client.print("Vaso 8  Dati Raw: ");
          client.print(moisture8);

          percentage = map(moisture8, Asciutto8, Bagnato8, 0, 100);

          client.print(" | Percentuale: ");
          client.print(percentage);

          client.println("%<br><br>");

          delay(500);

          moisture9 = analogRead(analogPin9);
          client.print("Vaso 9  Dati Raw: ");
          client.print(moisture9);

          percentage = map(moisture9, Asciutto9, Bagnato9, 0, 100);

          client.print(" | Percentuale: ");
          client.print(percentage);

          client.println("%<br><br>");

          delay(500);

          moisture10 = analogRead(analogPin10);
          client.print("Vaso 10  Dati Raw: ");
          client.print(moisture10);

          percentage = map(moisture10, Asciutto10, Bagnato10, 0, 100);

          client.print(" | Percentuale: ");
          client.print(percentage);

          client.println("%<br><br>");

          delay(500);

          moisture11 = analogRead(analogPin11);
          client.print("Vaso 11 Dati Raw: ");
          client.print(moisture11);

          percentage = map(moisture11, Asciutto11, Bagnato11, 0, 100);

          client.print(" | Percentuale: ");
          client.print(percentage);

          client.println("%<br><br>");

          delay(500);

          moisture12 = analogRead(analogPin12);
          client.print("Vaso 12 Dati Raw: ");
          client.print(moisture12);

          percentage = map(moisture12, Asciutto12, Bagnato12, 0, 100);

          client.print(" | Percentuale: ");
          client.print(percentage);

          client.println("%<br><br>");

          delay(500);

          moisture13 = analogRead(analogPin13);
          client.print("Vaso 13 Dati Raw: ");
          client.print(moisture13);

          percentage = map(moisture13, Asciutto13, Bagnato13, 0, 100);

          client.print(" | Percentuale: ");
          client.print(percentage);

          client.println("%<br><br>");

          delay(500);

          moisture14 = analogRead(analogPin14);
          client.print("Vaso 14 Dati Raw: ");
          client.print(moisture14);

          percentage = map(moisture14, Asciutto14, Bagnato14, 0, 100);

          client.print(" | Percentuale: ");
          client.print(percentage);

          client.println("%<br><br>");

          delay(500);

          moisture15 = analogRead(analogPin15);
          client.print("Vaso 15 Dati Raw: ");
          client.print(moisture15);

          percentage = map(moisture15, Asciutto15, Bagnato15, 0, 100);

          client.print(" | Percentuale: ");
          client.print(percentage);

          client.println("%<br><br>");

          delay(500);

          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();
    client.println("client disconnected");
  }
}
}

and the result i get is the same, no active html server and the serial monitor that goes with:

 Percentuale umidita' vaso 0  e' : 4.48; Percentage: 80%
 Percentuale umidita' vaso 1  e' : 4.47; Percentage: 80%
 Percentuale umidita' vaso 2  e' : 4.49; Percentage: 80%
 Percentuale umidita' vaso 3  e' : 4.48; Percentage: 80%
 Percentuale umidita' vaso 4  e' : 4.49; Percentage: 80%
 Percentuale umidita' vaso 5  e' : 922; Percentage: 18400%
 Percentuale umidita' vaso 6  e' : 896; Percentage: 17880%
 Percentuale umidita' vaso 7  e' : 918; Percentage: 18380%
 Percentuale umidita' vaso 8  e' : 878; Percentage: 17560%
 Percentuale umidita' vaso 9  e' : 933; Percentage: 18660%
 Percentuale umidita' vaso 10  e' : 937; Percentage: 18740%
 Percentuale umidita' vaso 11  e' : 917; Percentage: 18360%
 Percentuale umidita' vaso 12  e' : 885; Percentage: 17720%
 Percentuale umidita' vaso 13  e' : 898; Percentage: 17920%
 Percentuale umidita' vaso 14  e' : 904; Percentage: 18100%
 Percentuale umidita' vaso 15  e' : 919; Percentage: 18360%
 Percentuale umidita' vaso 16  e' : 921; Percentage: 18440%
 Percentuale umidita' vaso 17  e' : 899; Percentage: 17980%
 Percentuale umidita' vaso 18  e' : 927; Percentage: 18480%
 Percentuale umidita' vaso 19  e' : 923; Percentage: 18460%

Your percentages are off because you're assuming that analogRead returns a voltage in the range 0-5. It actually gives an abstract range of 0-1023.

1 Like