【问题标题】:client.connect() additional parameters - queryclient.connect() 附加参数 - 查询
【发布时间】:2018-12-17 15:26:12
【问题描述】:

我使用 ESP32 作为我的 socket.io 服务器应用程序的客户端。我需要 ESP32 在“连接”方法中发送 MAC 地址作为查询。在我的浏览器中,我可以这样做:

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

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

client.connect(host, port)

但我想发送附加信息,例如:

client.connect(host, port, query)

【问题讨论】:

  • WiFiClient 用于 TCP 连接。没有查询参数之类的东西。要么使用 socket.io 库,要么自己实现协议。

标签: arduino iot esp32


【解决方案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

【讨论】:

    猜你喜欢
    • 2019-07-23
    • 2023-03-28
    • 2014-11-30
    • 2019-11-29
    • 1970-01-01
    • 1970-01-01
    • 2014-02-27
    • 1970-01-01
    • 2023-03-31
    相关资源
    最近更新 更多