是否可以使用hasArg()函数从两个客户端接收有关请求的参数的信息?

时间:2019-11-21 23:19:31

标签: arduino esp32

我目前正在尝试从两个esp32发送器(客户端)向另一个作为接收器的esp32发送数据(分别来自每个客户端的温度和湿度),对于第一个发送器esp32(客户端),我使用以下代码: / p>

#include <WiFi.h>
#include <DHT.h>
#include <DHT_U.h>
#include "DHT.h"


#define DHTTYPE DHT11

const char *ssid = "loro";
const char *password = "pingu4prez";

const int analogInPin = 0;  
int sensorValue = 0;        
int outputValue = 0;
int outputValue2 = 0; 
const int DHTPin = 22;

DHT dht(DHTPin, DHTTYPE);



void setup() {

  dht.begin();
  Serial.begin(115200);
  delay(10);


  Serial.println();
  Serial.println();
  Serial.print("connecting to ");
  Serial.println(ssid);



  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println(" IP address: ");
  Serial.println(WiFi.localIP());
  Serial.begin(115200);

}

void loop() {


  float temperature = dht.readTemperature();
  float humidity = dht.readHumidity();

  sensorValue = analogRead(A0);
  outputValue = temperature;
  outputValue2 = humidity;



  Serial.print("sensor = ");
  Serial.print(sensorValue);
  Serial.print("\t output temperature = ");
  Serial.println(outputValue);
  Serial.print("\t output humidity = ");
  Serial.println(outputValue2);
  Serial.print("temperature read test =");
  Serial.println(temperature);
  Serial.print("humidity read test =");
  Serial.println(humidity);


  char intToPrint[5];
  itoa(outputValue, intToPrint, 10); 
  char intToPrint2[5];
  itoa(outputValue2, intToPrint2, 10);



  WiFiClient client;
  const char * host = "192.168.4.1";
  const int httpPort = 80;

  if (!client.connect(host, httpPort)) {
    Serial.println("connection failed");
    return;
  }


  String sensor1 = "/data/";
 sensor1 += "?sensor1h=";
 sensor1 += intToPrint2;
 sensor1 += "&sensor1t=";
 sensor1 += intToPrint;

  Serial.print("requesting URL: ");
  Serial.println(sensor1);
   Serial.println();



  client.print(String("GET ") + sensor1 + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" +
               "Connection: close\r\n\r\n");
  unsigned long timeout = millis();
  while (client.available() == 0) {
    if (millis() - timeout > 5000) {
      Serial.println(">>> Client Timeout !");
      client.stop();
      return;
    }
  }

  Serial.println();
  Serial.println("Closing connection");
  Serial.println();
  Serial.println();
  Serial.println();

  delay(500);
}

第二个发件人esp32(client)im使用以下代码:

#include <WiFi.h>
#include <DHT.h>
#include <DHT_U.h>
#include "DHT.h"


#define DHTTYPE DHT11

const char *ssid = "loro";
const char *password = "pingu4prez";

const int analogInPin = 0;  
int sensorValue = 0;        
int outputValue = 0;
int outputValue2 = 0; 
const int DHTPin = 22;

DHT dht(DHTPin, DHTTYPE);



void setup() {

  dht.begin();
  Serial.begin(115200);
  delay(10);


  Serial.println();
  Serial.println();
  Serial.print("connecting to ");
  Serial.println(ssid);



  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println(" IP address: ");
  Serial.println(WiFi.localIP());
  Serial.begin(115200);

}

void loop() {


  float temperature = dht.readTemperature();
  float humidity = dht.readHumidity();

  sensorValue = analogRead(A0);
  outputValue = temperature;
  outputValue2 = humidity;



  Serial.print("sensor = ");
  Serial.print(sensorValue);
  Serial.print("\t output temperature = ");
  Serial.println(outputValue);
  Serial.print("\t output humidity = ");
  Serial.println(outputValue2);
  Serial.print("temperature read test =");
  Serial.println(temperature);
  Serial.print("humidity read test =");
  Serial.println(humidity);


  char intToPrint[5];
  itoa(outputValue, intToPrint, 10); 
  char intToPrint2[5];
  itoa(outputValue2, intToPrint2, 10);



  WiFiClient client;
  const char * host = "192.168.4.1";
  const int httpPort = 80;

  if (!client.connect(host, httpPort)) {
    Serial.println("connection failed");
    return;
  }



String sensor2 = “/data/”;
sensor2 += “?sensor2h=”;//hum
sensor2 += intToPrint2;
sensor2 += “&sensor2t=”;//tem
sensor2 += intToPrint;

Serial.print("requesting URL: ");
Serial.println(sensor1);
Serial.println();



  client.print(String("GET ") + sensor1 + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" +
               "Connection: close\r\n\r\n");
  unsigned long timeout = millis();
  while (client.available() == 0) {
    if (millis() - timeout > 5000) {
      Serial.println(">>> Client Timeout !");
      client.stop();
      return;
    }
  }

  Serial.println();
  Serial.println("Closing connection");
  Serial.println();
  Serial.println();
  Serial.println();

  delay(500);
}

如您所见,除了定义要发送的URL的那一部分之外,两个客户端的代码几乎相同。

我的问题是接收方的函数 handleSentVar(),该函数使用 server.hasArg 来接收有关所请求参数的信息, 如下面的代码所示:

#include <WiFi.h>
#include <WebServer.h>


//U8g2 Constructor List - https://github.com/olikraus/u8g2/wiki/u8g2setupcpp#introduction


const char *ssid = "loro";
const char *password = "pingu4prez";

WebServer server(80);

void handleSentVar() {
  Serial.println("handleSentVar function called...");
  if (server.hasArg("sensor1h")) { 
    Serial.println("reading from sensor received...");

                int readingInt2 = server.arg("sensor1h").toInt();
                int readingInt = server.arg("sensor1t").toInt();
                char readingToPrint[5];
                itoa(readingInt, readingToPrint, 10); //
                char readingToPrint2[5];
                itoa(readingInt2, readingToPrint2, 10); 

                Serial.print("Lectura de temperatura recibida: ");
                Serial.println(readingToPrint);
                Serial.print("Lectura de humedad recibida: ");
                Serial.println(readingToPrint2);
                Serial.println();
                server.send(200, "text/html", "Datos recibidos");
          }
   }

}

void setup() {
  delay(1000);
  Serial.begin(115200);
  Serial.println();
  Serial.print("Configurando AP...");



  WiFi.softAP(ssid, password);

  IPAddress myIP = WiFi.softAPIP();
  Serial.print("Dirección IP del AP: ");
  Serial.println(myIP);
  server.on("/data/", HTTP_GET, handleSentVar); // when the server receives a request with /data/ in the string then run the handleSentVar function
  server.begin();
  Serial.println("servidor HTTP iniciado");

}

void loop() {
  server.handleClient();
}

如何使函数 handleSentVar()从两个客户端接收信息?

1 个答案:

答案 0 :(得分:0)

在接收方客户端上,将两行与server1一起使用,这在发布的程序中尚未定义

Serial.println(sensor1);
...
client.print(String("GET ") + sensor1 + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" +                "Connection: close\r\n\r\n");

因此,服务器1的代码应仅包含服务器1以及用于读取服务器2h的代码,而在其他服务器2上以及用于读取服务器1h的代码。 为了简化维护,除clientId以外,该程序在所有单元上均应相等:

uint8_t clientId = 1 // (range 1 -255) and 0 we should not use
....
String sensor = “/data/”;
sensor += “?clientID=”;
sensor += clientID;
sensor += “&sensorh=”;//hum
sensor += intToPrint2;
sensor += “&sensort=”;//tem
sensor += intToPrint;
....
client.print(String("GET ") + sensor + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" +                "Connection: close\r\n\r\n");

要确保String类具有持久的性能,因为大量使用会像瑞士奶酪一样碎片化堆并导致不可预测的重启。 作为示例,入门者如何:

char clientId ="12"  // saves the conversion

char sensor[256] =  {'\0'}; /* Defined globally - Used for sensor data
char message[512] =  {'\0'}; /* Defined globally - Used for message to send
....
strcpy (sensor, “/data/”);               //Start at the beginning of the array (empties it)
strcat (sensor, “?clientID=”);           // Append to the array content
strcat (sensor, clientID);
strcat (sensor, “&sensorh=”);//hum
strcat (sensor, intToPrint2);
strcat (sensor, “&sensort=”);//tem
strcat (sensor, intToPrint);
....
client.print(message);

消息将以相同的方式构建。 定长数组放在堆栈中,我们正在使用此容器动态地组合所需的数据。 AVR微控制器上的字符串(大写S)是邪恶的根源;-)