controling and displaying data in same time HELP !!

Hi for All i found some codes source on Net about iot and control over web using arduino so i modified it : the 1st source code its for send sensors data(DHT11,ULTRASONIC ) to ThingSpeak (open IoT platform) the 2nd its for controling over web page( relay , Led , servo ) the 2 sources codes work 100 % i want to make it in just one code so how is that I tried but its not worknig i neeeeeeed HELP

1st source code for sending sensors data to Thingspeak.com

#include <ThingSpeak.h>
#include <DHT.h>        // You have to download DHT11  library
#include <SPI.h> 
#include <Ethernet.h>
 
  #ifdef SPARK
    #include "ThingSpeak/ThingSpeak.h"    // You have to download ThingSpeak  library
  #else
    #include "ThingSpeak.h"
  #endif


#define WRITE_DELAY_FOR_THINGSPEAK 15000
#define DHTPIN 5   
#define DHTTYPE DHT11 
DHT dht(DHTPIN, DHTTYPE);
#define echoPin 9 // Echo Pin
#define trigPin 8 // Trigger Pin

long duration, distance; // Duration used to calculate distance
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE,0xED};
  IPAddress ip(192,168,8,20);                             //Check your router IP
  EthernetClient client;

  
  unsigned long mySensorsChannelNumber = xxxxx;            //Replase this code with your thingSpeak channel ID
  unsigned long myCommandsChannelNumber = xxxx;           //Replase this code with your thingSpeak channel ID
  const char * myWriteAPIKey_sensors = "xxxxxxxxxx";  //Replace with your channel Write Key
  const char * myWriteAPIKey_commands = "xxxxxxxxxx"; //Replace with your channel Write Key. This key is not used in this example
                                                            //Use this key from android device  

long lastWriteTime=0;    
long lastReadTime=0;
float responseValue=0;

void setup() {
  Serial.begin(9600);
  Ethernet.begin(mac, ip);
  ThingSpeak.begin(client);
  dht.begin();
  
  pinMode(7,OUTPUT);
   pinMode(8,OUTPUT);  //Trigger    FW
  pinMode(9,INPUT);   //Echo
  Serial.println("Setup completed");
  delay(1000);
}


void loop() {
  
  //-------- Example 1 write DHT11 sensors' values to thingSpeak channel sensors
     long duration1, distance1;
   
  
  if (millis()-lastWriteTime>20000) {       // every 20 secs.  Must be >15000.    
      Serial.println("Read sensors' values...");
      
     
  // FW 1 pin distance calculation
    digitalWrite(8,LOW);
    delayMicroseconds(2);
    digitalWrite(8,HIGH);
    delayMicroseconds(10);
    digitalWrite(8,LOW);


    duration1 = pulseIn(9,HIGH);
    distance1 = (duration1/2)/29.1;

    delay(500); 

    
      float u = distance1 ;
      float h = dht.readHumidity();
      float t = dht.readTemperature();
      
      if (isnan(t) || isnan(h)) {
        Serial.println("Failed to read from DHT");
      }
      else {
          Serial.println("Temp="+String(t)+" *C");
          Serial.println("Humidity="+String(h)+" %");
         Serial.println("distance="+String(u)+" cm");
          ThingSpeak.setField(1,t);
          ThingSpeak.setField(2,h);
          ThingSpeak.setField(3,u);             // This is the value of commands channel
          ThingSpeak.writeFields(mySensorsChannelNumber, myWriteAPIKey_sensors);                  // write two values
          lastWriteTime=millis();               // store last write time
      }
  }
delay(8000);
}

the 2nd code its for controling ( relay , Led , servo motor) over page web

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

int led = 4;
Servo microservo; 
int pos = 0; 
const byte mainlightPin = 5;
const byte chandelierPin = 6;
const byte fanPin = 8;
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };   
byte ip[] = { 192, 168, 8, 20 };                      
byte gateway[] = { 192, 168, 8, 20 };                  
byte subnet[] = { 255, 255, 255, 0 };                  
EthernetServer server(80);                             
String readString;
void setup() {
  
  
   Serial.begin(9600);
   while (!Serial) {
    ; 
  }
  pinMode(led, OUTPUT);
  microservo.attach(7);
  pinMode(mainlightPin, OUTPUT);
  pinMode(chandelierPin, OUTPUT);
  pinMode(fanPin, OUTPUT);
  Ethernet.begin(mac, ip, gateway, subnet);
  server.begin();
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());
}
void loop() {
   
 EthernetClient client = server.available();
  if (client) {
    while (client.connected()) {   
      if (client.available()) {
        char c = client.read();
       if (readString.length() < 100) {
        readString += c;
          
         }
if (c == '\n') {          
           Serial.println(readString);
           client.println("HTTP/1.1 200 OK"); 
           client.println("Content-Type: text/html");
           client.println();     
           client.println("<HTML>");
           client.println("<HEAD>");
           client.println("<link rel='stylesheet' type='text/css' href='http://randomnerdtutorials.com/ethernetcss.css' />");
           client.println("</HEAD>");
           client.println("<BODY>");
           client.println("<H1>Control over web page</H1>");
           client.println("<hr />");
           client.println("<a href=\"/?button1off\"\">OFF Light</a>");
           client.println("<a href=\"/?button1on\"\">ON light</a>
");   
           client.println("
");     
           client.println("
"); 
           client.println("<a href=\"/?button5on\"\">OFF Motor 2</a>");
           client.println("<a href=\"/?button5off\"\">ON Motor 2</a>
");   
           client.println("
");     
           client.println("
"); 
           client.println("<a href=\"/??button6on\"\">OFF Motor 3</a>");
           client.println("<a href=\"/??button6off\"\">ON Motor 3</a>
");   
           client.println("
");     
           client.println("
"); 
           client.println("<a href=\"/?button7on\"\">OFF Motor 4</a>");
           client.println("<a href=\"/?button7off\"\">ON Motor 4</a>
");   
           client.println("
");     
           client.println("
"); 
           client.println("<a href=\"/?button2on\"\">Rotate Left</a>");
           client.println("<a href=\"/?button2off\"\">Rotate Right</a>
"); 
           client.println("
"); 
           client.println("</BODY>");
           
          client.println("
");
           
           client.println("</HTML>");
    delay(1);
           client.stop();
           if (readString.indexOf("?button1on") >0){digitalWrite(led, HIGH);}
           if (readString.indexOf("?button1off")>0){digitalWrite(led, LOW);}
           if (readString.indexOf("?button5on") >0) {digitalWrite(mainlightPin, HIGH);} 
           if (readString.indexOf("?button5off")>0 ){ digitalWrite(mainlightPin, LOW);} 
           if (readString.indexOf("?button6on")>0 ) {digitalWrite(chandelierPin, HIGH);} 
           if (readString.indexOf("?button6off")>0 ) {digitalWrite(chandelierPin, LOW);} 
           if (readString.indexOf("?button7on")>0 ) {digitalWrite(fanPin, HIGH);} 
           if (readString.indexOf("?button7off")>0) {digitalWrite(fanPin, LOW);} 
           
       if (readString.indexOf("?button2on") >0)
        {
                for(pos = 0; pos < 180; pos += 3)  
                {                                 
                  microservo.write(pos);              
                  delay(15);                       
                } 
           }
           if (readString.indexOf("?button2off") >0){
                for(pos = 180; pos>=1; pos-=3)     
                {                                
                  microservo.write(pos);              
                  delay(15);                       
                } 
           }
            
            readString="";  
              }
       }
    }
}
}

Where is the code you tried that does not work? Post it and describe what happens. Give compile error messages and/or serial output (in code tags).

this is what i did but its not work

there is no compile error messages !!

the code connect to 192.168.1.20 but its cant read data from DHT 11 sensor Also no sending data to Thingspeak

#include <ThingSpeak.h>
#include <DHT.h>        // You have to download DHT11  library
#include <SPI.h>
#include <Ethernet.h>
#include <Servo.h>
int led = 4;
Servo microservo;
int pos = 0;
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };   //physical mac address
byte ip[] = { 192, 168, 1, 20 };                      // ip in lan (that's what you need to use in your browser. ("192.168.1.178")
byte gateway[] = { 192, 168, 1, 1 };                   // internet access via router
byte subnet[] = { 255, 255, 255, 0 };                  //subnet mask
#ifdef SPARK
#include "ThingSpeak/ThingSpeak.h"    // You have to download ThingSpeak  library
#else
#include "ThingSpeak.h"
#endif
#define WRITE_DELAY_FOR_THINGSPEAK 15000
#define DHTPIN 5
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
#define echoPin 9 // Echo Pin
#define trigPin 8 // Trigger Pin
long duration, distance; // Duration used to calculate distance
EthernetServer server(80);           //server port
EthernetClient client;
String readString;
unsigned long mySensorsChannelNumber = 123456789;            //Replase this code with your thingSpeak channel ID
unsigned long myCommandsChannelNumber = 987654321;           //Replase this code with your thingSpeak channel ID
const char * myWriteAPIKey_sensors = "abcdespcsisc";  //Replace with your channel Write Key
const char * myWriteAPIKey_commands = "abcdertidoq"; //Replace with your channel Write Key. This key is not used in this example                                                           //Use this key from android device
long lastWriteTime = 0;
long lastReadTime = 0;
float responseValue = 0;
void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
  Serial.begin(9600);
  Ethernet.begin(mac, ip, gateway, subnet);
  ThingSpeak.begin(client);
  dht.begin();
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT); //Trigger    FW
  pinMode(9, INPUT);  //Echo
  Serial.println("Setup completed");
  delay(1000);
  pinMode(led, OUTPUT);
  microservo.attach(7);
  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip, gateway, subnet);
  server.begin();
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());
}
void loop() {
  //-------- Example 1 write DHT11 sensors' values to thingSpeak channel sensors
  long duration, distance;
  if (millis() - lastWriteTime > 20000) {   // every 20 secs.  Must be >15000.
    Serial.println("Read sensors' values...");
    //  distance calculation
    digitalWrite(8, LOW);
    delayMicroseconds(2);
    digitalWrite(8, HIGH);
    delayMicroseconds(10);
    digitalWrite(8, LOW);
    duration = pulseIn(9, HIGH);
    distance = (duration / 2) / 29.1;
    Serial.print(distance);
    Serial.println("cm//");
    delay(500);
    Serial.println("distance");
    Serial.println(distance);
    float h = dht.readHumidity();
    float t = dht.readTemperature();
    float ultrasonicValue = distance ; // your ultrasonic value

    if (isnan(t) || isnan(h) ) {
      Serial.println("Failed to read from DHT");
    }
    else {
      Serial.println("Temp=" + String(t) + " *C");
      Serial.println("Humidity=" + String(h) + " %");
      ThingSpeak.setField(1, t);
      ThingSpeak.setField(2, h);
      ThingSpeak.setField(3, ultrasonicValue);
      ThingSpeak.writeFields(mySensorsChannelNumber, myWriteAPIKey_sensors);                  // write two values
      lastWriteTime = millis();             // store last write time
    }
  }
  delay(8000);

  // Create a client connection
  EthernetClient client = server.available();
  if (client) {
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();

        //read char by char HTTP request
        if (readString.length() < 100) {
          //store characters to string
          readString += c;
          //Serial.print(c);
        }

        //if HTTP request has ended
        if (c == '\n') {
          Serial.println(readString); //print to serial monitor for debuging
          client.println("HTTP/1.1 200 OK"); //send new page
          client.println("Content-Type: text/html");
          client.println();
          client.println("<HTML>");
          client.println("<HEAD>");
          client.println("<meta name='apple-mobile-web-app-capable' content='yes' />");
          client.println("<meta name='apple-mobile-web-app-status-bar-style' content='black-translucent' />");
          client.println("<link rel='stylesheet' type='text/css' href='http://randomnerdtutorials.com/ethernetcss.css' />");
          client.println("<TITLE>Random Nerd Tutorials Project</TITLE>");
          client.println("</HEAD>");
          client.println("<BODY>");
          client.println("<H1>Random Nerd Tutorials Project</H1>");
          client.println("<hr />");
          client.println("
");
          client.println("<H2>Arduino with Ethernet Shield</H2>");
          client.println("
");
          client.println("<a href=\"/?button1on\"\">Turn On LED</a>");
          client.println("<a href=\"/?button1off\"\">Turn Off LED</a>
");
          client.println("
");
          client.println("
");
          client.println("<a href=\"/?button2on\"\">Rotate Left</a>");
          client.println("<a href=\"/?button2off\"\">Rotate Right</a>
");
          client.println("<p>Created by Rui Santos. Visit http://randomnerdtutorials.com for more projects!</p>");
          client.println("
");
          client.println("</BODY>");
          client.println("</HTML>");

          delay(1);
          //stopping client
          client.stop();
          //controls the Arduino if you press the buttons
          if (readString.indexOf("?button1on") > 0) {
            digitalWrite(led, HIGH);
          }
          if (readString.indexOf("?button1off") > 0) {
            digitalWrite(led, LOW);
          }
          if (readString.indexOf("?button2on") > 0) {
            for (pos = 0; pos < 180; pos += 3) // goes from 0 degrees to 180 degrees
            { // in steps of 1 degree
              microservo.write(pos);              // tell servo to go to position in variable 'pos'
              delay(15);                       // waits 15ms for the servo to reach the position
            }
          }
          if (readString.indexOf("?button2off") > 0) {
            for (pos = 180; pos >= 1; pos -= 3) // goes from 180 degrees to 0 degrees
            {
              microservo.write(pos);              // tell servo to go to position in variable 'pos'
              delay(15);                       // waits 15ms for the servo to reach the position
            }
          }
          //clearing string for next read
          readString = "";

        }
      }
    }
  }
}

Its hard to follow. Please use Auto Format in the IDE and post it again.

I see two ethernet.begin() calls in setup(). Don't know if that is causing a problem.

hi bro thanks

i repost the code with Auto Format tools

i dont know im not good in programming but i think arduino bored can't be worwk like server and client in the same time when i send data from arduino to thingspeak its work like client for ThingSpeak server and when i control ( relay , led, servo moteur ) over web page i think its work like server is that right ??

for ethernet begin() i just do copie past for code parts and made it in one code

:confused:
:confused:

I don't know any reason why Arduino cannot be both client and server, but I am not 100% sure they can because I have never done it. You are the second person to ask me that question recently. I will try to find time to settle the matter.

thanks bro i hope you can find the solution i really need this project for my education