将带有JSON数据的ESP8266 + NodeMCUv3的PUT请求发送到NodeJS服务器(本地网络)

时间:2019-12-08 16:31:35

标签: arduino esp8266 arduino-esp8266 arduinojson

我正在尝试使用ESP8266从光传感器发送数据并更新服务器中传感器的“值”。 不幸的是,我花了很多时间,但对我来说却无济于事...

从传感器读取数据并连接到网络已完成并且可以正常工作。

令我们着迷的代码:

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include "ArduinoJsonv6.cpp"
#include <WiFiClient.h>

// Server parameters
const char* Server_ID = "192.168.1.32";
const int port = 8080;
String URL = "/pi/Rooms/LivingRoom/sensors/Light/5dec517737d254014be8f766"; //ID of the sensor

void loop() {

  // Reading data from the sensor
  unsigned long Lux;
  String Lux_string = "";

  tsl.TSL2581_Read_Channel();
  Lux = tsl.calculateLux(2, NOM_INTEG_CYCLE);

  Read_gpio_interrupt(2000, 50000);
  delay(50);

  StaticJsonDocument<200> doc;
  doc["value"] = Lux;
  serializeJson(doc, Serial);

  if(WiFi.status()== WL_CONNECTED){
    WiFiClient client;
    HTTPClient http;
    http.begin(Server_ID + ":" + port + URL);  // (1) How can I pass here my parameters?
    http.addHeader("Content-Type", "application/json");

    int httpResponseCode = http.PUT(HOW CAN I PASS HERE MY JSON - DOC???); // (2) Passing here the json is like black magic...
    String response = http.getString();   

    Serial.println(httpResponseCode);
    Serial.println(response); 
    http.end();

    delay(5000);
  }
}

主要问题:

(1)-如何传递到http.begin我的服务器参数?

(2)-将我的json传递到PUT请求-似乎很简单,但不是。

如果有人可以使用其他库解决该问题,那对我来说也是不错的解决方案。不幸, 每个网站上都有一个使用不同库的解决方案,这非常令人困惑。 谢谢您的帮助

0 个答案:

没有答案