Arduino yun dht11

I tried to join them but once I read one by changing loop some things I read just the other
How can I join this program

#include <Bridge.h>
#include <YunServer.h>
#include <YunClient.h>

// Listen to the default port 5555, the Yún webserver
// will forward there all the HTTP requests you send
YunServer server;

void setup() {
  // Bridge startup
  pinMode(13, OUTPUT);
  digitalWrite(13, LOW);
  Bridge.begin();
  digitalWrite(13, HIGH);

  // Listen for incoming connection only from localhost
  // (no one from the external network could connect)
  server.listenOnLocalhost();
  server.begin();
}

void loop() {
  // Get clients coming from server
  YunClient client = server.accept();

  // There is a new client?
  if (client) {
    // Process request
    process(client);

    // Close connection and free resources.
    client.stop();
  }

  delay(50); // Poll every 50ms
}

void process(YunClient client) {
  // read the command
  String command = client.readStringUntil('/');

  // is "digital" command?
  if (command == "digital") {
    digitalCommand(client);
  }

  // is "analog" command?
  if (command == "analog") {
    analogCommand(client);
  }

  // is "mode" command?
  if (command == "mode") {
    modeCommand(client);
  }
}

void digitalCommand(YunClient client) {
  int pin, value;

  // Read pin number
  pin = client.parseInt();

  // If the next character is a '/' it means we have an URL
  // with a value like: "/digital/13/1"
  if (client.read() == '/') {
    value = client.parseInt();
    digitalWrite(pin, value);
  }
  else {
    value = digitalRead(pin);
  }

  // Send feedback to client
  client.print(F("Pin D"));
  client.print(pin);
  client.print(F(" set to "));
  client.println(value);

  // Update datastore key with the current pin value
  String key = "D";
  key += pin;
  Bridge.put(key, String(value));
}

void analogCommand(YunClient client) {
  int pin, value;

  // Read pin number
  pin = client.parseInt();

  // If the next character is a '/' it means we have an URL
  // with a value like: "/analog/5/120"
  if (client.read() == '/') {
    // Read value and execute command
    value = client.parseInt();
    analogWrite(pin, value);

    // Send feedback to client
    client.print(F("Pin D"));
    client.print(pin);
    client.print(F(" set to analog "));
    client.println(value);

    // Update datastore key with the current pin value
    String key = "D";
    key += pin;
    Bridge.put(key, String(value));
  }
  else {
    // Read analog pin
    value = analogRead(pin);

    // Send feedback to client
    client.print(F("Pin A"));
    client.print(pin);
    client.print(F(" reads analog "));
    client.println(value);

    // Update datastore key with the current pin value
    String key = "A";
    key += pin;
    Bridge.put(key, String(value));
  }
}

void modeCommand(YunClient client) {
  int pin;

  // Read pin number
  pin = client.parseInt();

  // If the next character is not a '/' we have a malformed URL
  if (client.read() != '/') {
    client.println(F("error"));
    return;
  }

  String mode = client.readStringUntil('\r');

  if (mode == "input") {
    pinMode(pin, INPUT);
    // Send feedback to client
    client.print(F("Pin D"));
    client.print(pin);
    client.print(F(" configured as INPUT!"));
    return;
  }

  if (mode == "output") {
    pinMode(pin, OUTPUT);
    // Send feedback to client
    client.print(F("Pin D"));
    client.print(pin);
    client.print(F(" configured as OUTPUT!"));
    return;
  }

  client.print(F("error: invalid mode "));
  client.print(mode);
}

with this so that work simultaneously?

#include "DHT.h"
#include <Bridge.h>
#include <YunServer.h>
#include <YunClient.h>

#define DHTPIN 2
#define DHTTYPE DHT11 

YunServer server;
DHT dht(DHTPIN, DHTTYPE);
int h, t;

void setup() {
  Bridge.begin();
  server.listenOnLocalhost();
  server.begin();
  dht.begin();
}

void loop() {
  h = dht.readHumidity();
  t = dht.readTemperature();

  YunClient client = server.accept();
  if (client) {
    process(client);
    client.stop();
  }
  
  delay(50);
}

void process(YunClient client) {
  
  String command = client.readStringUntil('\r');

  if (command == "termo") {
    temp(client);
  }

  if (command == "idro") {
    idro(client);
  }
}

void temp(YunClient client) {
  if (isnan(t)) {
    client.print(F("Error"));
  } else {
    client.print(t);
  }
}

void idro(YunClient client) {
    if (isnan(t)) {
      client.print(F("Error"));
    } else {
      client.print(h);
    }
}

The problem I think is this but these are not know how to solve it:
The reading command for the temperature brings this string
String command = client.readStringUntil ('\ r');
Instead the peo commands for the bridge port this
String command = client.readStringUntil ('/');
Maybe the problem is in the client because if I create two processes and two clients one works well rl other does not need to click Type 3 4 times to start the command.
If it helps put the program I joined I gives me problems

where am I wrong?
THE command http://192.168.1.54/arduino/digital/13/1 only works if I click three times and then the led light up instead the command (http://192.168.1.54/arduino/termo) temperature works well

#include "DHT.h"
#include <Bridge.h>
#include <YunServer.h>
#include <YunClient.h>

#define DHTPIN 2
#define DHTTYPE DHT11 

YunServer server;
DHT dht(DHTPIN, DHTTYPE);
int h, t;

// Listen to the default port 5555, the Yún webserver
// will forward there all the HTTP requests you send
YunServer server2;

void setup() {
  // Bridge startup
  pinMode(13, OUTPUT);
  digitalWrite(13, LOW);
  Bridge.begin();
  digitalWrite(13, HIGH);

  // Listen for incoming connection only from localhost
  // (no one from the external network could connect)
  server.listenOnLocalhost();
  server.begin();
}
void loop() {
  h = dht.readHumidity();
  t = dht.readTemperature();

  YunClient client2 = server2.accept();
  if (client2) {
    process2(client2);
    client2.stop();
  }
  
  delay(50);

 
  
  YunClient client = server.accept();

  if (client) {
    // Process request
    process(client);

    // Close connection and free resources.
    client.stop();
  }

  delay(50); // Poll every 50ms
}
void process2(YunClient client2) {
  
  String command2 = client2.readStringUntil('\r');

  if (command2 == "termo") {
    temp(client2);
  }

  if (command2 == "idro") {
    idro(client2);
  }
}

void temp(YunClient client2) {
  if (isnan(t)) {
    client2.print(F("Error"));
  } else {
    client2.print(t);
  }
}

void idro(YunClient client2) {
    if (isnan(t)) {
      client2.print(F("Error"));
    } else {
      client2.print(h);
    }
}
void process(YunClient client) {
  // read the command
  String command = client.readStringUntil('/');

  // is "digital" command?
  if (command == "digital") {
    digitalCommand(client);
  }

  // is "analog" command?
  if (command == "analog") {
    analogCommand(client);
  }

  // is "mode" command?
  if (command == "mode") {
    modeCommand(client);
  }
}

void digitalCommand(YunClient client) {
  int pin, value;

  // Read pin number
  pin = client.parseInt();

  // If the next character is a '/' it means we have an URL
  // with a value like: "/digital/13/1"
  if (client.read() == '/') {
    value = client.parseInt();
    digitalWrite(pin, value);
  }
  else {
    value = digitalRead(pin);
  }

  // Send feedback to client
  client.print(F("Pin D"));
  client.print(pin);
  client.print(F(" set to "));
  client.println(value);

  // Update datastore key with the current pin value
  String key = "D";
  key += pin;
  Bridge.put(key, String(value));
}

void analogCommand(YunClient client) {
  int pin, value;

  // Read pin number
  pin = client.parseInt();

  // If the next character is a '/' it means we have an URL
  // with a value like: "/analog/5/120"
  if (client.read() == '/') {
    // Read value and execute command
    value = client.parseInt();
    analogWrite(pin, value);

    // Send feedback to client
    client.print(F("Pin D"));
    client.print(pin);
    client.print(F(" set to analog "));
    client.println(value);

    // Update datastore key with the current pin value
    String key = "D";
    key += pin;
    Bridge.put(key, String(value));
  }
  else {
    // Read analog pin
    value = analogRead(pin);

    // Send feedback to client
    client.print(F("Pin A"));
    client.print(pin);
    client.print(F(" reads analog "));
    client.println(value);

    // Update datastore key with the current pin value
    String key = "A";
    key += pin;
    Bridge.put(key, String(value));
  }
}

void modeCommand(YunClient client) {
  int pin;

  // Read pin number
  pin = client.parseInt();

  // If the next character is not a '/' we have a malformed URL
  if (client.read() != '/') {
    client.println(F("error"));
    return;
  }

  String mode = client.readStringUntil('\r');

  if (mode == "input") {
    pinMode(pin, INPUT);
    // Send feedback to client
    client.print(F("Pin D"));
    client.print(pin);
    client.print(F(" configured as INPUT!"));
    return;
  }

  if (mode == "output") {
    pinMode(pin, OUTPUT);
    // Send feedback to client
    client.print(F("Pin D"));
    client.print(pin);
    client.print(F(" configured as OUTPUT!"));
    return;
  }

  client.print(F("error: invalid mode "));
  client.print(mode);
}

Why do you have two different YunClient objects with two different server.accept calls? You are clearly missing the point here. Trying to program using copy/paste without understanding what is happening is rarely successful. Sorry to be blunt about it.

The YunServer sets up a server object to listen for incoming connection attempts. By calling server.listenOnLocalHost(), you are telling the YunServer to listen for incoming web requests of the form http://arduino.local/arduino/whatever, where arduino.local is your Yun's name, or IP address, /arduino/ means that the Linux web server should pass this request onto the YunServer/YunClient running in a sketch, and whatever follows it should get passed to the YunClient.

When you call server.accept(), the YunServer object checks to see if there is an incoming connection. If so, it returns a YunClient object that can receive the "whatever" part of the URL and can return a response. If there is no connection, it immediately returns a disconnected client object.

The if (client) statement after the server.accept() is looking to see if it is a valid client object with an established connection. If so, you read the rest of the web request from the client, the "whatever" part, and act on it, in this case by calling a process() function.

So let's start by looking at what happens when there is no request waiting to be processed, which is the case most of the time. The loop() function is called, you read the temperature and humidity, then look to see if there is a connection. Most of the time there won't be, so you wait 50 milliseconds, and look again, and again there probably won't be a connection waiting. Then loop() exits, and is immediately called again. So you are looping over and over again reading temperature/humidity, and then checking for connections twice.

When a request does come in, it will be pure random chance which one of the server.accept() calls will receive the request. There is no way to predict it, it just depends on where it happens to be in the loop when the request comes in. The problem is that only some of the requests are handled by the first call to server.accept() and the code that follows, and some are handled by the other one. If your .../digital/13/1 request comes in while the loop happens to be on the first server.accept(), the process2() function will be called, and it won't know what to do with it. You will only get a valid response to the request if it happens to come in on the second server.accept() call which results in a call to the process() function which does understand the digital request.

It's pretty much dumb luck that the "termo" request works. If you requested it many times, I'm sure you'll find that it's also unreliable. It probably only works because reading the temperature and humidity takes some time. So, there is more time for a request to come in before the client2 accept call. In other words, requests that arrive in the 50 ms after the first server.accept() will be handled by the second one, the one that can handle the digital request. But the window of opportunity to hit the first server.accept() - the one that can handle the termo request, is 50 ms plus the time it takes to read the temperature and humidity. Therefore, since it spends more time in the code before the first server.accept() call, the odds are greater that it is that first server.accept() that will receive the connection. But you can't rely on these odds - you will have a fragile and unstable system if you have two server.accept() calls that are expecting their own individual sets of commands.

The solution is to have only a single server.accept() in the sketch. If there is a connection, it will return a valid client object, which is verified by the "if (client)" statement. If so, you call a single process() function that can handle all of the various possible commands: "digital", "analog", "mode", "termo", "idro", etc. To add more commands, you don't add more server.accept() calls and process() functions, you simply add more if checks to the one process function.

If you have more than one server.accept() you will never know which request is coming in on which client.

You seem to be confused by the '/' vs '\r' in the readStringUntil() calls. What the call is doing is reading data from the client until the specified character is read, or the end of line. By using '\r', it's basically saying that it should always read until the end of the line. The issue is that one sketch is always only expecting a single word, while the other is expecting multiple words separated by slash characters.

The solution is to always use readStringUntil('/') to get the next word:

  • If the request is ".../arduino/digital/13/1", the first word it will return is "digital". If called again inside the function that handles "digital", the next word it returns will be "13".
  • If the request is ".../arduino/termo", the first word it will return is "termo" because it has hit the end of the line before it gets a slash character. The handler for "termo" shouldn't try to read another word, but if it did there wouldn't be anything there.

So, start with the first sketch. Add your DHT11 code to it, and add a couple more if statements to the process() function to look for the words "termo" and "idro", and then add your functions to handle those commands.

an example for pleasure!
Thanks so much

I've solved a single client and a single process
to see the temperature control:http://192.168.1.18/arduino/termo/2/1
2 is the sensor pins 1 is the value for the programam attack the sensor seems to work well