client.connect()其他参数-查询

时间:2018-12-17 15:26:12

标签: arduino iot esp32

我将ESP32用作socket.io服务器应用程序的客户端。我需要ESP32在“ connect”方法中发送MAC地址作为查询。在我的浏览器中,我可以执行以下操作:

io('localhost:2000', {query: { mac: 222222 }})

我需要ESP32来做相同的事情,但是我仅限于WiFiClient库,该库可用于与服务器的基本连接:

client.connect(host, port)

但是我想发送其他信息,例如:

client.connect(host, port, query)

1 个答案:

答案 0 :(得分:-1)

要查询某些内容,您必须准备HTTP请求,并使用println函数对其进行描述。您可以在下面看到一个示例,该示例摘自official site

#include <SPI.h>
#include <WiFi.h>

char ssid[] = "myNetwork";          //  your network SSID (name)
char pass[] = "myPassword";   // your network password

int status = WL_IDLE_STATUS;
IPAddress server(74,125,115,105);  // Google

// Initialize the client library
WiFiClient client;

void setup() {
  Serial.begin(9600);
  Serial.println("Attempting to connect to WPA network...");
  Serial.print("SSID: ");
  Serial.println(ssid);

  status = WiFi.begin(ssid, pass);
  if ( status != WL_CONNECTED) {
    Serial.println("Couldn't get a wifi connection");
    // don't do anything else:
    while(true);
  }
  else {
    Serial.println("Connected to wifi");
    Serial.println("\nStarting connection...");
    // if you get a connection, report back via serial:
    if (client.connect(server, 80)) {
      Serial.println("connected");
      // Make a HTTP request:
      //Here you have to mount your "query" or specify your JSON body (HTTP protocol):
      client.println("GET /search?q=arduino HTTP/1.0"); 
      client.println();
    }
  }
}

您可以找到更多示例herehere (it is a POST example, but it is useful)here