完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
我有一个连接到 esp8266 的湿度和温度传感器,现在我想将数据发送到云端。我无法上班的是 POST 电话。下面的 POST 请求在 Auth() 函数中返回一条错误消息,告诉我正文没有按预期发送。
我让 api 在 Postman 中正常工作,所以我确定凭据没问题等等。 有人有关于如何执行 POST 请求并正确发送此正文的示例吗?我重新搜索了文档,但只能找到一个 GET 示例…… 在此先感谢! 代码:全选#include #include #include #include \"credentials.h\" #include #include #define DHTPIN 2 #define DHTTYPE DHT21 const char* host = \"login.salesforce.com\"; const int httpsPort = 443; // SHA1 fingerprint of the certificate const char fingerprint[] PROGMEM = \"0B 33 19 AC 6D 9E C1 5F 08 AB 93 17 2A FE F9 E0 90 69 C7 9A\"; //login.salesforce.com DHT_Unified dht(DHTPIN, DHTTYPE); int delayMS = 3000; String requestBody; void setup() { Serial.begin(115200); // Initialize device. dht.begin(); // Initialize sensors Serial.println(); Serial.printf(\"Connecting to %s\", ssid); WiFi.mode(WIFI_STA); WiFi.begin(ssid, password); // Connect to Wifi while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print(\".\"); } Serial.println(); Serial.println(\"WiFi connected\"); Serial.print(\"IP address: \"); Serial.println(WiFi.localIP()); auth(); } void loop() { // Get temperature event and print its value. sensors_event_t event; dht.temperature().getEvent(&event); if (isnan(event.temperature)) { Serial.println(F(\"Error reading temperature!\")); } else { Serial.print(\"Temperature: \"); Serial.print(event.temperature); Serial.println(\"°C\"); } // Get humidity event and print its value. dht.humidity().getEvent(&event); if (isnan(event.relative_humidity)) { Serial.println(F(\"Error reading humidity!\")); } else { Serial.print(F(\"Humidity: \")); Serial.print(event.relative_humidity); Serial.println(F(\"%\")); } // Delay between measurements. delay(delayMS); } void auth() { WiFiClientSecure client; requestBody = \"grant_type=password&client_id=\" + consumerKey + \"&client_secret=\" + consumerSecret + \"&username=\" + userUsername + \"&password=\" + userPassword; Serial.printf(\"connecting to %s using fingerprint %s\\n\", host, fingerprint); client.setFingerprint(fingerprint); if (!client.connect(host, httpsPort)) { Serial.println(\"connection failed\"); return; } else { Serial.println(\"Connected\"); } String url = \"/services/oauth2/token\"; Serial.print(\"requesting URL: \"); Serial.println(url); client.print(String(\"POST \") + url + \" HTTP/1.1\\r\\n\" + \"Host: \" + host + \"\\r\\n\" + \"Content-Type: application/x-www-form-urlencoded\\r\\n\" + requestBody + \"\\r\\n\\r\\n\"); Serial.print(String(\"POST \") + url + \" HTTP/1.1\\r\\n\" + \"Host: \" + host + \"\\r\\n\" + \"Content-Type: application/x-www-form-urlencoded\\r\\n\" + requestBody + \"\\r\\n\\r\\n\"); Serial.println(\"request sent\"); while (client.connected()) { String line = client.readStringUntil(\'\\n\'); if (line == \"\\r\") { Serial.println(\"headers received\"); break; } } String line = client.readString(); if (line.startsWith(\"{\\\"state\\\":\\\"success\\\"\")) { Serial.println(\"Authentication successfull!\"); } else { Serial.println(\"Authentication failed\"); } Serial.println(\"reply was:\"); Serial.println(\"==========\"); Serial.println(line); Serial.println(\"==========\"); Serial.println(\"closing connection\"); } 我真的很感激任何帮助! |
|
相关推荐
|
|
只有小组成员才能发言,加入小组>>
528浏览 6评论
438浏览 5评论
有没有办法在不使用混杂模式的情况下实现Wifi驱动程序接收缓冲区访问中断呢?
425浏览 5评论
422浏览 4评论
396浏览 4评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-11-6 18:10 , Processed in 1.200308 second(s), Total 77, Slave 60 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号