乐鑫技术交流
直播中

席毖庸

9年用户 1114经验值
擅长:596874
私信 关注
[问答]

如何向Web服务器发送多个POST请求?

在这里,我试图将数据从 nodeMCU 发送到两条路线“/tempData”和“/updateValues”。我尝试了下面的代码,但它不起作用。任何帮助将不胜感激。
代码:全选// For WiFI Connectivity
#include
// for temperature
#include

int pinDHT22 = 2;
SimpleDHT22 dht22(pinDHT22);
// WiFi SSID
const char* ssid     = "ssid";      
// WiFi Password   
const char* password = "password";  

// Enter Server-URL here
const char* host = "server_url";

void setup() {
   
  Serial.begin(74880);
  
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  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());
}


void loop() {

float temperature = 0;
float humidity = 0;
int err = SimpleDHTErrSuccess;
  if ((err = dht22.read2(&temperature, &humidity, NULL)) != SimpleDHTErrSuccess) {   
    //Check for connection error
    Serial.print("Read DHT22 failed, err="); Serial.println(err);delay(2000);
    return;
  }
Serial.print("connecting to ");
Serial.println(host);

// Use WiFiClient class to create TCP connections
WiFiClient client;
const int httpPort = 80;
if (!client.connect(host, httpPort)) {
  Serial.println("connection failed");
  return;
}

// We now create a URI for the request
String url = "/";

Serial.print("Temperature: ");
Serial.print((float)temperature); Serial.println(" *C, ");

Serial.print("Requesting URL: ");
Serial.println(url);
// Location Coordinates (hard coded) when GPS module not in use
String loc = "del";
//  Package-ID of the package being trasported
String id = "H103";
String data = ("Temperature=" + String(((float)temperature)) + "&packageID=" + id + "&Location=" + loc);

   Serial.print("Requesting POST: ");   
    // HTTP FORMAT
   // Send request to the server:
   client.println("POST /tempData HTTP/1.1");
   client.println("Host: server_url");
   client.println("Accept: */*");
   client.println("Content-Type: application/x-www-form-urlencoded");
   client.print("Content-Length: ");
   client.println(data.length());
   client.println();
   client.print(data);
// This will send the request to the server

  Serial.print("Requesting POST: ");   
    // HTTP FORMAT
   // Send request to the server:
   client.println("POST /updateValues HTTP/1.1");
   client.println("Host: server_url");
   client.println("Accept: */*");
   client.println("Content-Type: application/x-www-form-urlencoded");
   client.print("Content-Length: ");
   client.println(data.length());
   client.println();
   client.print(data);
// This will send the request to the server
unsigned long timeout = millis();
while (client.available() == 0) {
if (millis() - timeout > 5000) {
  Serial.println(">>> Client Timeout !");
  client.stop();
  return;
}
}

// Read all the lines of the reply from server and print them to Serial
while(client.available()){
  String line = client.readStringUntil('r');
  Serial.print(line);
}

Serial.println();
Serial.println("closing connection");
}


更多回帖

发帖
×
20
完善资料,
赚取积分