完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
在这里,我试图将数据从 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"); } |
|
相关推荐
|
|
只有小组成员才能发言,加入小组>>
540浏览 6评论
453浏览 5评论
有没有办法在不使用混杂模式的情况下实现Wifi驱动程序接收缓冲区访问中断呢?
434浏览 5评论
436浏览 4评论
409浏览 4评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-11-18 03:47 , Processed in 0.747856 second(s), Total 77, Slave 59 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号